]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
5ec1fd3d | 2 | * IPP routines for the CUPS scheduler. |
ef416fc2 | 3 | * |
092db1e2 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 | * |
092db1e2 MS |
10 | * Licensed under Apache License v2.0. See the file "LICENSE" for more |
11 | * information. | |
ef416fc2 | 12 | */ |
13 | ||
14 | /* | |
15 | * Include necessary headers... | |
16 | */ | |
17 | ||
18 | #include "cupsd.h" | |
a603edef | 19 | #include <cups/ppd-private.h> |
ef416fc2 | 20 | |
f899b121 | 21 | #ifdef __APPLE__ |
a29fd7dd | 22 | /*# include <ApplicationServices/ApplicationServices.h> |
229681c1 | 23 | extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id); |
a29fd7dd | 24 | # include <CoreFoundation/CoreFoundation.h>*/ |
f899b121 | 25 | # ifdef HAVE_MEMBERSHIP_H |
26 | # include <membership.h> | |
27 | # endif /* HAVE_MEMBERSHIP_H */ | |
28 | # ifdef HAVE_MEMBERSHIPPRIV_H | |
29 | # include <membershipPriv.h> | |
30 | # else | |
7594b224 | 31 | extern int mbr_user_name_to_uuid(const char* name, uuid_t uu); |
32 | extern int mbr_group_name_to_uuid(const char* name, uuid_t uu); | |
33 | extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember); | |
f899b121 | 34 | # endif /* HAVE_MEMBERSHIPPRIV_H */ |
35 | #endif /* __APPLE__ */ | |
7594b224 | 36 | |
ef416fc2 | 37 | |
ef416fc2 | 38 | /* |
39 | * Local functions... | |
40 | */ | |
41 | ||
42 | static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
43 | static void add_class(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 44 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, |
45 | mime_type_t *filetype, int compression); | |
f7deaa1a | 46 | static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer, |
80ca4592 | 47 | mime_type_t *filetype); |
ef416fc2 | 48 | static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job); |
321d8d57 | 49 | static void add_job_uuid(cupsd_job_t *job); |
ef416fc2 | 50 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
b423cd4c | 51 | static void add_printer_state_reasons(cupsd_client_t *con, |
52 | cupsd_printer_t *p); | |
ef416fc2 | 53 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); |
b423cd4c | 54 | static void apply_printer_defaults(cupsd_printer_t *printer, |
55 | cupsd_job_t *job); | |
ef416fc2 | 56 | static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri); |
57 | static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
58 | static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
59 | static void cancel_subscription(cupsd_client_t *con, int id); | |
3dfe78b3 | 60 | static int check_rss_recipient(const char *recipient); |
ef416fc2 | 61 | static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p); |
aaf19ab0 | 62 | static void close_job(cupsd_client_t *con, ipp_attribute_t *uri); |
fa73b229 | 63 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, |
10d09e33 MS |
64 | ipp_tag_t group, int quickcopy, |
65 | cups_array_t *exclude); | |
b423cd4c | 66 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, |
67 | const char *name); | |
55516fd3 | 68 | static int copy_file(const char *from, const char *to, mode_t mode); |
b423cd4c | 69 | static int copy_model(cupsd_client_t *con, const char *from, |
70 | const char *to); | |
fa73b229 | 71 | static void copy_job_attrs(cupsd_client_t *con, |
72 | cupsd_job_t *job, | |
10d09e33 | 73 | cups_array_t *ra, cups_array_t *exclude); |
fa73b229 | 74 | static void copy_printer_attrs(cupsd_client_t *con, |
75 | cupsd_printer_t *printer, | |
76 | cups_array_t *ra); | |
ef416fc2 | 77 | static void copy_subscription_attrs(cupsd_client_t *con, |
78 | cupsd_subscription_t *sub, | |
10d09e33 MS |
79 | cups_array_t *ra, |
80 | cups_array_t *exclude); | |
ef416fc2 | 81 | static void create_job(cupsd_client_t *con, ipp_attribute_t *uri); |
7ae00c35 | 82 | static void create_local_printer(cupsd_client_t *con); |
ef416fc2 | 83 | static cups_array_t *create_requested_array(ipp_t *request); |
42404685 | 84 | static void create_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 85 | static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
86 | static void get_default(cupsd_client_t *con); | |
87 | static void get_devices(cupsd_client_t *con); | |
2e4ff8af | 88 | static void get_document(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 89 | static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
90 | static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
bd7854cb | 91 | static void get_notifications(cupsd_client_t *con); |
b94498cf | 92 | static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 93 | static void get_ppds(cupsd_client_t *con); |
94 | static void get_printers(cupsd_client_t *con, int type); | |
95 | static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
c168a833 | 96 | static void get_printer_supported(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 97 | static void get_subscription_attrs(cupsd_client_t *con, int sub_id); |
98 | static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); | |
e00b005a | 99 | static const char *get_username(cupsd_client_t *con); |
ef416fc2 | 100 | static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri); |
61cf44e2 | 101 | static void hold_new_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 102 | static void move_job(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 103 | static int ppd_parse_line(const char *line, char *option, int olen, |
104 | char *choice, int clen); | |
105 | static void print_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
c5571a1d | 106 | static void read_job_ticket(cupsd_client_t *con); |
ef416fc2 | 107 | static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
61cf44e2 MS |
108 | static void release_held_new_jobs(cupsd_client_t *con, |
109 | ipp_attribute_t *uri); | |
ef416fc2 | 110 | static void release_job(cupsd_client_t *con, ipp_attribute_t *uri); |
111 | static void renew_subscription(cupsd_client_t *con, int sub_id); | |
112 | static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
f7deaa1a | 113 | static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job, |
114 | ipp_attribute_t *auth_info); | |
ef416fc2 | 115 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); |
f899b121 | 116 | static void send_http_error(cupsd_client_t *con, http_status_t status, |
117 | cupsd_printer_t *printer); | |
a32af27c | 118 | static void send_ipp_status(cupsd_client_t *con, ipp_status_t status, const char *message, ...) _CUPS_FORMAT(3, 4); |
ef416fc2 | 119 | static void set_default(cupsd_client_t *con, ipp_attribute_t *uri); |
120 | static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
c168a833 | 121 | static void set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); |
f0b589f7 | 122 | static int set_printer_defaults(cupsd_client_t *con, cupsd_printer_t *printer); |
ef416fc2 | 123 | static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
124 | static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
07623986 MS |
125 | static void url_encode_attr(ipp_attribute_t *attr, char *buffer, size_t bufsize); |
126 | static char *url_encode_string(const char *s, char *buffer, size_t bufsize); | |
ef416fc2 | 127 | static int user_allowed(cupsd_printer_t *p, const char *username); |
128 | static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
129 | static int validate_name(const char *name); | |
07623986 | 130 | static int validate_user(cupsd_job_t *job, cupsd_client_t *con, const char *owner, char *username, size_t userlen); |
ef416fc2 | 131 | |
132 | ||
133 | /* | |
09a101d6 | 134 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request. |
ef416fc2 | 135 | */ |
136 | ||
137 | int /* O - 1 on success, 0 on failure */ | |
138 | cupsdProcessIPPRequest( | |
139 | cupsd_client_t *con) /* I - Client connection */ | |
140 | { | |
141 | ipp_tag_t group; /* Current group tag */ | |
142 | ipp_attribute_t *attr; /* Current attribute */ | |
143 | ipp_attribute_t *charset; /* Character set attribute */ | |
144 | ipp_attribute_t *language; /* Language attribute */ | |
9f5eb9be | 145 | ipp_attribute_t *uri = NULL; /* Printer or job URI attribute */ |
ef416fc2 | 146 | ipp_attribute_t *username; /* requesting-user-name attr */ |
147 | int sub_id; /* Subscription ID */ | |
092db1e2 | 148 | int valid = 1; /* Valid request? */ |
ef416fc2 | 149 | |
150 | ||
d2123aee MS |
151 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest(%p[%d]): operation_id=%04x(%s)", con, con->number, con->request->request.op.operation_id, ippOpString(con->request->request.op.operation_id)); |
152 | ||
153 | if (LogLevel >= CUPSD_LOG_DEBUG2) | |
154 | { | |
155 | for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(con->request); attr; attr = ippNextAttribute(con->request)) | |
156 | { | |
157 | const char *name; /* Attribute name */ | |
158 | char value[1024]; /* Attribute value */ | |
159 | ||
160 | if (group != ippGetGroupTag(attr)) | |
161 | { | |
162 | group = ippGetGroupTag(attr); | |
163 | if (group != IPP_TAG_ZERO) | |
164 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s", ippTagString(group)); | |
165 | } | |
166 | ||
167 | if ((name = ippGetName(attr)) == NULL) | |
168 | continue; | |
169 | ||
170 | ippAttributeString(attr, value, sizeof(value)); | |
171 | ||
172 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s %s%s '%s'", name, ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)), value); | |
173 | } | |
174 | } | |
ef416fc2 | 175 | |
176 | /* | |
177 | * First build an empty response message for this request... | |
178 | */ | |
179 | ||
180 | con->response = ippNew(); | |
181 | ||
092db1e2 MS |
182 | con->response->request.status.version[0] = con->request->request.op.version[0]; |
183 | con->response->request.status.version[1] = con->request->request.op.version[1]; | |
184 | con->response->request.status.request_id = con->request->request.op.request_id; | |
ef416fc2 | 185 | |
186 | /* | |
187 | * Then validate the request header and required attributes... | |
188 | */ | |
f7deaa1a | 189 | |
092db1e2 | 190 | if (con->request->request.any.version[0] != 1 && con->request->request.any.version[0] != 2) |
ef416fc2 | 191 | { |
192 | /* | |
c168a833 | 193 | * Return an error, since we only support IPP 1.x and 2.x. |
ef416fc2 | 194 | */ |
195 | ||
092db1e2 | 196 | 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 | 197 | |
092db1e2 | 198 | 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 | 199 | } |
f0ab5bff MS |
200 | else if (con->request->request.any.request_id < 1) |
201 | { | |
202 | /* | |
203 | * Return an error, since request IDs must be between 1 and 2^31-1 | |
204 | */ | |
205 | ||
092db1e2 | 206 | 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 | 207 | |
092db1e2 | 208 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Bad request ID %d."), con->request->request.any.request_id); |
f0ab5bff | 209 | } |
fa73b229 | 210 | else if (!con->request->attrs) |
ef416fc2 | 211 | { |
092db1e2 | 212 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s No attributes in request.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname); |
ef416fc2 | 213 | |
092db1e2 | 214 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("No attributes in request.")); |
ef416fc2 | 215 | } |
216 | else | |
217 | { | |
218 | /* | |
219 | * Make sure that the attributes are provided in the correct order and | |
220 | * don't repeat groups... | |
221 | */ | |
222 | ||
223 | for (attr = con->request->attrs, group = attr->group_tag; | |
fa73b229 | 224 | attr; |
ef416fc2 | 225 | attr = attr->next) |
226 | if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO) | |
227 | { | |
228 | /* | |
229 | * Out of order; return an error... | |
230 | */ | |
231 | ||
092db1e2 | 232 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Attribute groups are out of order", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname); |
ef416fc2 | 233 | |
092db1e2 | 234 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute groups are out of order (%x < %x)."), attr->group_tag, group); |
ef416fc2 | 235 | break; |
236 | } | |
237 | else | |
238 | group = attr->group_tag; | |
239 | ||
fa73b229 | 240 | if (!attr) |
ef416fc2 | 241 | { |
242 | /* | |
243 | * Then make sure that the first three attributes are: | |
244 | * | |
245 | * attributes-charset | |
246 | * attributes-natural-language | |
247 | * printer-uri/job-uri | |
248 | */ | |
249 | ||
250 | attr = con->request->attrs; | |
7d4510a0 | 251 | if (attr && attr->name && !strcmp(attr->name, "attributes-charset") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET && attr->group_tag == IPP_TAG_OPERATION) |
ef416fc2 | 252 | charset = attr; |
253 | else | |
254 | charset = NULL; | |
255 | ||
256 | if (attr) | |
257 | attr = attr->next; | |
258 | ||
7d4510a0 | 259 | if (attr && attr->name && !strcmp(attr->name, "attributes-natural-language") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE && attr->group_tag == IPP_TAG_OPERATION) |
1523f10d | 260 | { |
ef416fc2 | 261 | language = attr; |
1523f10d MS |
262 | |
263 | /* | |
264 | * Reset language for this request if different from Accept-Language. | |
265 | */ | |
266 | ||
267 | if (!con->language || | |
268 | strcmp(attr->values[0].string.text, con->language->language)) | |
269 | { | |
270 | cupsLangFree(con->language); | |
271 | con->language = cupsLangGet(attr->values[0].string.text); | |
272 | } | |
273 | } | |
ef416fc2 | 274 | else |
275 | language = NULL; | |
276 | ||
7d4510a0 | 277 | if ((attr = ippFindAttribute(con->request, "printer-uri", IPP_TAG_URI)) != NULL && attr->group_tag == IPP_TAG_OPERATION) |
ef416fc2 | 278 | uri = attr; |
7d4510a0 | 279 | else if ((attr = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI)) != NULL && attr->group_tag == IPP_TAG_OPERATION) |
ef416fc2 | 280 | uri = attr; |
7d4510a0 MS |
281 | else if (con->request->request.op.operation_id == CUPS_GET_PPD && (attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL && attr->group_tag == IPP_TAG_OPERATION) |
282 | uri = attr; | |
ef416fc2 | 283 | else |
284 | uri = NULL; | |
285 | ||
286 | if (charset) | |
092db1e2 | 287 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, charset->values[0].string.text); |
ef416fc2 | 288 | else |
092db1e2 | 289 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, "utf-8"); |
ef416fc2 | 290 | |
291 | if (language) | |
092db1e2 | 292 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, language->values[0].string.text); |
ef416fc2 | 293 | else |
092db1e2 | 294 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, DefaultLanguage); |
ef416fc2 | 295 | |
092db1e2 | 296 | if (charset && _cups_strcasecmp(charset->values[0].string.text, "us-ascii") && _cups_strcasecmp(charset->values[0].string.text, "utf-8")) |
2e4ff8af MS |
297 | { |
298 | /* | |
299 | * Bad character set... | |
300 | */ | |
301 | ||
4d301e69 | 302 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"", |
2e4ff8af | 303 | charset->values[0].string.text); |
092db1e2 MS |
304 | 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); |
305 | send_ipp_status(con, IPP_STATUS_ERROR_CHARSET, _("Unsupported character set \"%s\"."), charset->values[0].string.text); | |
2e4ff8af MS |
306 | } |
307 | else if (!charset || !language || | |
308 | (!uri && | |
309 | con->request->request.op.operation_id != CUPS_GET_DEFAULT && | |
310 | con->request->request.op.operation_id != CUPS_GET_PRINTERS && | |
311 | con->request->request.op.operation_id != CUPS_GET_CLASSES && | |
312 | con->request->request.op.operation_id != CUPS_GET_DEVICES && | |
313 | con->request->request.op.operation_id != CUPS_GET_PPDS)) | |
ef416fc2 | 314 | { |
315 | /* | |
316 | * Return an error, since attributes-charset, | |
317 | * attributes-natural-language, and printer-uri/job-uri are required | |
318 | * for all operations. | |
319 | */ | |
320 | ||
321 | if (!charset) | |
322 | { | |
092db1e2 | 323 | cupsdLogMessage(CUPSD_LOG_ERROR, "Missing attributes-charset attribute."); |
ef416fc2 | 324 | |
092db1e2 | 325 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Missing attributes-charset attribute.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname); |
ef416fc2 | 326 | } |
327 | ||
328 | if (!language) | |
329 | { | |
330 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
092db1e2 | 331 | "Missing attributes-natural-language attribute."); |
ef416fc2 | 332 | |
092db1e2 | 333 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Missing attributes-natural-language attribute.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname); |
ef416fc2 | 334 | } |
335 | ||
336 | if (!uri) | |
337 | { | |
092db1e2 | 338 | cupsdLogMessage(CUPSD_LOG_ERROR, "Missing printer-uri, job-uri, or ppd-name attribute."); |
ef416fc2 | 339 | |
092db1e2 | 340 | 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 | 341 | } |
342 | ||
343 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
344 | ||
fa73b229 | 345 | for (attr = con->request->attrs; attr; attr = attr->next) |
f7deaa1a | 346 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
ef416fc2 | 347 | "attr \"%s\": group_tag = %x, value_tag = %x", |
348 | attr->name ? attr->name : "(null)", attr->group_tag, | |
349 | attr->value_tag); | |
350 | ||
351 | cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes..."); | |
352 | ||
353 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 354 | _("Missing required attributes.")); |
ef416fc2 | 355 | } |
356 | else | |
357 | { | |
358 | /* | |
092db1e2 MS |
359 | * OK, all the checks pass so far; validate "requesting-user-name" |
360 | * attribute value... | |
ef416fc2 | 361 | */ |
362 | ||
092db1e2 MS |
363 | if ((username = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_ZERO)) != NULL) |
364 | { | |
365 | /* | |
366 | * Validate "requesting-user-name"... | |
367 | */ | |
368 | ||
369 | if (username->group_tag != IPP_TAG_OPERATION && StrictConformance) | |
370 | { | |
371 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s \"requesting-user-name\" attribute in wrong group.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname); | |
372 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("\"requesting-user-name\" attribute in wrong group.")); | |
373 | valid = 0; | |
374 | } | |
375 | else if (username->value_tag != IPP_TAG_NAME && username->value_tag != IPP_TAG_NAMELANG) | |
376 | { | |
377 | 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); | |
378 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("\"requesting-user-name\" attribute with wrong syntax.")); | |
379 | if ((attr = ippCopyAttribute(con->response, username, 0)) != NULL) | |
380 | attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
381 | valid = 0; | |
382 | } | |
383 | else if (!ippValidateAttribute(username)) | |
384 | { | |
385 | 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); | |
386 | ||
387 | if (StrictConformance) | |
388 | { | |
389 | /* | |
390 | * Throw an error... | |
391 | */ | |
392 | ||
393 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("\"requesting-user-name\" attribute with wrong syntax.")); | |
394 | if ((attr = ippCopyAttribute(con->response, username, 0)) != NULL) | |
395 | attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
396 | valid = 0; | |
397 | } | |
398 | else | |
399 | { | |
400 | /* | |
401 | * Map bad "requesting-user-name" to 'anonymous'... | |
402 | */ | |
403 | ||
404 | ippSetString(con->request, &username, 0, "anonymous"); | |
405 | } | |
406 | } | |
407 | else if (!strcmp(username->values[0].string.text, "root") && _cups_strcasecmp(con->http->hostname, "localhost") && strcmp(con->username, "root")) | |
ef416fc2 | 408 | { |
409 | /* | |
410 | * Remote unauthenticated user masquerading as local root... | |
411 | */ | |
412 | ||
5e6c3df7 | 413 | ippSetString(con->request, &username, 0, RemoteRoot); |
ef416fc2 | 414 | } |
415 | } | |
416 | ||
092db1e2 | 417 | if ((attr = ippFindAttribute(con->request, "notify-subscription-id", IPP_TAG_INTEGER)) != NULL) |
ef416fc2 | 418 | sub_id = attr->values[0].integer; |
419 | else | |
420 | sub_id = 0; | |
421 | ||
092db1e2 MS |
422 | if (valid) |
423 | { | |
424 | /* | |
425 | * Try processing the operation... | |
426 | */ | |
ef416fc2 | 427 | |
092db1e2 MS |
428 | if (uri) |
429 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", ippOpString(con->request->request.op.operation_id), uri->values[0].string.text); | |
430 | else | |
431 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", ippOpString(con->request->request.op.operation_id)); | |
ef416fc2 | 432 | |
092db1e2 MS |
433 | switch (con->request->request.op.operation_id) |
434 | { | |
435 | case IPP_OP_PRINT_JOB : | |
436 | print_job(con, uri); | |
437 | break; | |
438 | ||
439 | case IPP_OP_VALIDATE_JOB : | |
440 | validate_job(con, uri); | |
441 | break; | |
442 | ||
443 | case IPP_OP_CREATE_JOB : | |
444 | create_job(con, uri); | |
445 | break; | |
446 | ||
447 | case IPP_OP_SEND_DOCUMENT : | |
448 | send_document(con, uri); | |
449 | break; | |
450 | ||
451 | case IPP_OP_CANCEL_JOB : | |
452 | cancel_job(con, uri); | |
453 | break; | |
454 | ||
455 | case IPP_OP_GET_JOB_ATTRIBUTES : | |
456 | get_job_attrs(con, uri); | |
457 | break; | |
458 | ||
459 | case IPP_OP_GET_JOBS : | |
460 | get_jobs(con, uri); | |
461 | break; | |
462 | ||
463 | case IPP_OP_GET_PRINTER_ATTRIBUTES : | |
464 | get_printer_attrs(con, uri); | |
465 | break; | |
466 | ||
467 | case IPP_OP_GET_PRINTER_SUPPORTED_VALUES : | |
468 | get_printer_supported(con, uri); | |
469 | break; | |
470 | ||
471 | case IPP_OP_HOLD_JOB : | |
472 | hold_job(con, uri); | |
473 | break; | |
474 | ||
475 | case IPP_OP_RELEASE_JOB : | |
476 | release_job(con, uri); | |
477 | break; | |
478 | ||
479 | case IPP_OP_RESTART_JOB : | |
480 | restart_job(con, uri); | |
481 | break; | |
482 | ||
483 | case IPP_OP_PAUSE_PRINTER : | |
484 | stop_printer(con, uri); | |
485 | break; | |
486 | ||
487 | case IPP_OP_RESUME_PRINTER : | |
488 | start_printer(con, uri); | |
489 | break; | |
490 | ||
491 | case IPP_OP_PURGE_JOBS : | |
492 | case IPP_OP_CANCEL_JOBS : | |
493 | case IPP_OP_CANCEL_MY_JOBS : | |
494 | cancel_all_jobs(con, uri); | |
495 | break; | |
496 | ||
497 | case IPP_OP_SET_JOB_ATTRIBUTES : | |
498 | set_job_attrs(con, uri); | |
499 | break; | |
500 | ||
501 | case IPP_OP_SET_PRINTER_ATTRIBUTES : | |
502 | set_printer_attrs(con, uri); | |
503 | break; | |
504 | ||
505 | case IPP_OP_HOLD_NEW_JOBS : | |
506 | hold_new_jobs(con, uri); | |
507 | break; | |
508 | ||
509 | case IPP_OP_RELEASE_HELD_NEW_JOBS : | |
510 | release_held_new_jobs(con, uri); | |
511 | break; | |
512 | ||
513 | case IPP_OP_CLOSE_JOB : | |
514 | close_job(con, uri); | |
515 | break; | |
516 | ||
517 | case IPP_OP_CUPS_GET_DEFAULT : | |
518 | get_default(con); | |
519 | break; | |
520 | ||
521 | case IPP_OP_CUPS_GET_PRINTERS : | |
522 | get_printers(con, 0); | |
523 | break; | |
524 | ||
525 | case IPP_OP_CUPS_GET_CLASSES : | |
526 | get_printers(con, CUPS_PRINTER_CLASS); | |
527 | break; | |
528 | ||
529 | case IPP_OP_CUPS_ADD_MODIFY_PRINTER : | |
530 | add_printer(con, uri); | |
531 | break; | |
532 | ||
533 | case IPP_OP_CUPS_DELETE_PRINTER : | |
534 | delete_printer(con, uri); | |
535 | break; | |
536 | ||
537 | case IPP_OP_CUPS_ADD_MODIFY_CLASS : | |
538 | add_class(con, uri); | |
539 | break; | |
540 | ||
541 | case IPP_OP_CUPS_DELETE_CLASS : | |
542 | delete_printer(con, uri); | |
543 | break; | |
544 | ||
545 | case IPP_OP_CUPS_ACCEPT_JOBS : | |
546 | case IPP_OP_ENABLE_PRINTER : | |
547 | accept_jobs(con, uri); | |
548 | break; | |
549 | ||
550 | case IPP_OP_CUPS_REJECT_JOBS : | |
551 | case IPP_OP_DISABLE_PRINTER : | |
552 | reject_jobs(con, uri); | |
553 | break; | |
554 | ||
555 | case IPP_OP_CUPS_SET_DEFAULT : | |
556 | set_default(con, uri); | |
557 | break; | |
558 | ||
559 | case IPP_OP_CUPS_GET_DEVICES : | |
560 | get_devices(con); | |
561 | break; | |
562 | ||
563 | case IPP_OP_CUPS_GET_DOCUMENT : | |
564 | get_document(con, uri); | |
565 | break; | |
566 | ||
567 | case IPP_OP_CUPS_GET_PPD : | |
568 | get_ppd(con, uri); | |
569 | break; | |
570 | ||
571 | case IPP_OP_CUPS_GET_PPDS : | |
572 | get_ppds(con); | |
573 | break; | |
574 | ||
575 | case IPP_OP_CUPS_MOVE_JOB : | |
576 | move_job(con, uri); | |
577 | break; | |
578 | ||
579 | case IPP_OP_CUPS_AUTHENTICATE_JOB : | |
580 | authenticate_job(con, uri); | |
581 | break; | |
582 | ||
583 | case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS : | |
584 | case IPP_OP_CREATE_JOB_SUBSCRIPTIONS : | |
585 | create_subscriptions(con, uri); | |
586 | break; | |
587 | ||
588 | case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES : | |
589 | get_subscription_attrs(con, sub_id); | |
590 | break; | |
591 | ||
592 | case IPP_OP_GET_SUBSCRIPTIONS : | |
593 | get_subscriptions(con, uri); | |
594 | break; | |
595 | ||
596 | case IPP_OP_RENEW_SUBSCRIPTION : | |
597 | renew_subscription(con, sub_id); | |
598 | break; | |
599 | ||
600 | case IPP_OP_CANCEL_SUBSCRIPTION : | |
601 | cancel_subscription(con, sub_id); | |
602 | break; | |
603 | ||
604 | case IPP_OP_GET_NOTIFICATIONS : | |
605 | get_notifications(con); | |
606 | break; | |
607 | ||
608 | case IPP_OP_CUPS_CREATE_LOCAL_PRINTER : | |
609 | create_local_printer(con); | |
610 | break; | |
611 | ||
612 | default : | |
613 | 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)); | |
614 | ||
615 | send_ipp_status(con, IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, _("%s not supported."), ippOpString(con->request->request.op.operation_id)); | |
616 | break; | |
617 | } | |
ef416fc2 | 618 | } |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | if (con->response) | |
624 | { | |
625 | /* | |
626 | * Sending data from the scheduler... | |
627 | */ | |
628 | ||
092db1e2 | 629 | 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 | 630 | |
a73ca01e MS |
631 | httpClearFields(con->http); |
632 | ||
f301802f | 633 | #ifdef CUPSD_USE_CHUNKING |
e200616a MS |
634 | /* |
635 | * Because older versions of CUPS (1.1.17 and older) and some IPP | |
636 | * clients do not implement chunking properly, we cannot use | |
637 | * chunking by default. This may become the default in future | |
638 | * CUPS releases, or we might add a configuration directive for | |
639 | * it. | |
640 | */ | |
07725fee | 641 | |
e200616a MS |
642 | if (con->http->version == HTTP_1_1) |
643 | { | |
092db1e2 | 644 | cupsdLogClient(con, CUPSD_LOG_DEBUG, "Transfer-Encoding: chunked"); |
e200616a MS |
645 | cupsdSetLength(con->http, 0); |
646 | } | |
647 | else | |
f301802f | 648 | #endif /* CUPSD_USE_CHUNKING */ |
e200616a MS |
649 | { |
650 | size_t length; /* Length of response */ | |
b94498cf | 651 | |
07725fee | 652 | |
e200616a | 653 | length = ippLength(con->response); |
d09495fa | 654 | |
e200616a MS |
655 | if (con->file >= 0 && !con->pipe_pid) |
656 | { | |
657 | struct stat fileinfo; /* File information */ | |
b94498cf | 658 | |
e200616a | 659 | if (!fstat(con->file, &fileinfo)) |
7e86f2f6 | 660 | length += (size_t)fileinfo.st_size; |
ef416fc2 | 661 | } |
662 | ||
092db1e2 | 663 | cupsdLogClient(con, CUPSD_LOG_DEBUG, "Content-Length: " CUPS_LLFMT, CUPS_LLCAST length); |
e200616a MS |
664 | httpSetLength(con->http, length); |
665 | } | |
ef416fc2 | 666 | |
e200616a MS |
667 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE)) |
668 | { | |
ef416fc2 | 669 | /* |
670 | * Tell the caller the response header was sent successfully... | |
671 | */ | |
672 | ||
092db1e2 | 673 | cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient, (cupsd_selfunc_t)cupsdWriteClient, con); |
42404685 | 674 | |
ef416fc2 | 675 | return (1); |
676 | } | |
677 | else | |
678 | { | |
679 | /* | |
680 | * Tell the caller the response header could not be sent... | |
681 | */ | |
682 | ||
683 | return (0); | |
684 | } | |
685 | } | |
686 | else | |
687 | { | |
688 | /* | |
689 | * Sending data from a subprocess like cups-deviced; tell the caller | |
690 | * everything is A-OK so far... | |
691 | */ | |
692 | ||
693 | return (1); | |
694 | } | |
695 | } | |
696 | ||
697 | ||
09a101d6 | 698 | /* |
699 | * 'cupsdTimeoutJob()' - Timeout a job waiting on job files. | |
700 | */ | |
701 | ||
91c84a35 | 702 | int /* O - 0 on success, -1 on error */ |
09a101d6 | 703 | cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */ |
704 | { | |
705 | cupsd_printer_t *printer; /* Destination printer or class */ | |
706 | ipp_attribute_t *attr; /* job-sheets attribute */ | |
707 | int kbytes; /* Kilobytes in banner */ | |
708 | ||
709 | ||
710 | job->pending_timeout = 0; | |
711 | ||
712 | /* | |
713 | * See if we need to add the ending sheet... | |
714 | */ | |
715 | ||
6d2f911b MS |
716 | if (!cupsdLoadJob(job)) |
717 | return (-1); | |
718 | ||
09a101d6 | 719 | printer = cupsdFindDest(job->dest); |
720 | attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); | |
721 | ||
a2326b5b | 722 | if (printer && !(printer->type & CUPS_PRINTER_REMOTE) && |
09a101d6 | 723 | attr && attr->num_values > 1) |
724 | { | |
725 | /* | |
726 | * Yes... | |
727 | */ | |
728 | ||
75bd9771 MS |
729 | cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".", |
730 | attr->values[1].string.text); | |
09a101d6 | 731 | |
91c84a35 MS |
732 | if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0) |
733 | return (-1); | |
09a101d6 | 734 | |
735 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
736 | } | |
91c84a35 MS |
737 | |
738 | return (0); | |
09a101d6 | 739 | } |
740 | ||
741 | ||
ef416fc2 | 742 | /* |
743 | * 'accept_jobs()' - Accept print jobs to a printer. | |
744 | */ | |
745 | ||
746 | static void | |
747 | accept_jobs(cupsd_client_t *con, /* I - Client connection */ | |
748 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
749 | { | |
750 | http_status_t status; /* Policy status */ | |
bc44d920 | 751 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 752 | cupsd_printer_t *printer; /* Printer data */ |
753 | ||
754 | ||
755 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con, | |
996acce8 | 756 | con->number, uri->values[0].string.text); |
ef416fc2 | 757 | |
758 | /* | |
759 | * Is the destination valid? | |
760 | */ | |
761 | ||
f7deaa1a | 762 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 763 | { |
764 | /* | |
765 | * Bad URI... | |
766 | */ | |
767 | ||
768 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 769 | _("The printer or class does not exist.")); |
ef416fc2 | 770 | return; |
771 | } | |
772 | ||
773 | /* | |
774 | * Check policy... | |
775 | */ | |
776 | ||
777 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
778 | { | |
f899b121 | 779 | send_http_error(con, status, printer); |
ef416fc2 | 780 | return; |
781 | } | |
782 | ||
783 | /* | |
784 | * Accept jobs sent to the printer... | |
785 | */ | |
786 | ||
787 | printer->accepting = 1; | |
788 | printer->state_message[0] = '\0'; | |
789 | ||
f8b3a85b MS |
790 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, |
791 | "Now accepting jobs."); | |
792 | ||
ef416fc2 | 793 | if (dtype & CUPS_PRINTER_CLASS) |
f7deaa1a | 794 | { |
3dfe78b3 | 795 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
f7deaa1a | 796 | |
797 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").", | |
798 | printer->name, get_username(con)); | |
799 | } | |
ef416fc2 | 800 | else |
f7deaa1a | 801 | { |
3dfe78b3 | 802 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 803 | |
bc44d920 | 804 | cupsdLogMessage(CUPSD_LOG_INFO, |
805 | "Printer \"%s\" now accepting jobs (\"%s\").", | |
f7deaa1a | 806 | printer->name, get_username(con)); |
807 | } | |
ef416fc2 | 808 | |
809 | /* | |
810 | * Everything was ok, so return OK status... | |
811 | */ | |
812 | ||
813 | con->response->request.status.status_code = IPP_OK; | |
814 | } | |
815 | ||
816 | ||
817 | /* | |
818 | * 'add_class()' - Add a class to the system. | |
819 | */ | |
820 | ||
821 | static void | |
822 | add_class(cupsd_client_t *con, /* I - Client connection */ | |
823 | ipp_attribute_t *uri) /* I - URI of class */ | |
824 | { | |
825 | http_status_t status; /* Policy status */ | |
826 | int i; /* Looping var */ | |
61cf44e2 | 827 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
ef416fc2 | 828 | username[HTTP_MAX_URI], /* Username portion of URI */ |
829 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
830 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
831 | int port; /* Port portion of URI */ | |
832 | cupsd_printer_t *pclass, /* Class */ | |
833 | *member; /* Member printer/class */ | |
834 | cups_ptype_t dtype; /* Destination type */ | |
ef416fc2 | 835 | ipp_attribute_t *attr; /* Printer attribute */ |
836 | int modify; /* Non-zero if we just modified */ | |
e00b005a | 837 | int need_restart_job; /* Need to restart job? */ |
ef416fc2 | 838 | |
839 | ||
840 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con, | |
996acce8 | 841 | con->number, uri->values[0].string.text); |
ef416fc2 | 842 | |
843 | /* | |
844 | * Do we have a valid URI? | |
845 | */ | |
846 | ||
61cf44e2 MS |
847 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
848 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 849 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 850 | |
851 | ||
fa73b229 | 852 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) |
ef416fc2 | 853 | { |
854 | /* | |
855 | * No, return an error... | |
856 | */ | |
857 | ||
858 | send_ipp_status(con, IPP_BAD_REQUEST, | |
859 | _("The printer-uri must be of the form " | |
860 | "\"ipp://HOSTNAME/classes/CLASSNAME\".")); | |
861 | return; | |
862 | } | |
863 | ||
864 | /* | |
865 | * Do we have a valid printer name? | |
866 | */ | |
867 | ||
868 | if (!validate_name(resource + 9)) | |
869 | { | |
870 | /* | |
871 | * No, return an error... | |
872 | */ | |
873 | ||
874 | send_ipp_status(con, IPP_BAD_REQUEST, | |
875 | _("The printer-uri \"%s\" contains invalid characters."), | |
876 | uri->values[0].string.text); | |
877 | return; | |
878 | } | |
879 | ||
ef416fc2 | 880 | /* |
881 | * See if the class already exists; if not, create a new class... | |
882 | */ | |
883 | ||
884 | if ((pclass = cupsdFindClass(resource + 9)) == NULL) | |
885 | { | |
886 | /* | |
887 | * Class doesn't exist; see if we have a printer of the same name... | |
888 | */ | |
889 | ||
a2326b5b | 890 | if ((pclass = cupsdFindPrinter(resource + 9)) != NULL) |
ef416fc2 | 891 | { |
892 | /* | |
893 | * Yes, return an error... | |
894 | */ | |
895 | ||
896 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 897 | _("A printer named \"%s\" already exists."), |
ef416fc2 | 898 | resource + 9); |
899 | return; | |
900 | } | |
901 | ||
902 | /* | |
2e4ff8af | 903 | * No, check the default policy and then add the class... |
ef416fc2 | 904 | */ |
905 | ||
2e4ff8af MS |
906 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
907 | { | |
908 | send_http_error(con, status, NULL); | |
909 | return; | |
910 | } | |
911 | ||
ef416fc2 | 912 | pclass = cupsdAddClass(resource + 9); |
913 | modify = 0; | |
409f5497 MS |
914 | |
915 | pclass->printer_id = NextPrinterId ++; | |
ef416fc2 | 916 | } |
2e4ff8af MS |
917 | else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con, |
918 | NULL)) != HTTP_OK) | |
919 | { | |
2fb76298 | 920 | send_http_error(con, status, pclass); |
2e4ff8af MS |
921 | return; |
922 | } | |
ef416fc2 | 923 | else |
924 | modify = 1; | |
925 | ||
926 | /* | |
927 | * Look for attributes and copy them over as needed... | |
928 | */ | |
929 | ||
e00b005a | 930 | need_restart_job = 0; |
931 | ||
9b4bd602 | 932 | if ((attr = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT)) != NULL) |
ef416fc2 | 933 | cupsdSetString(&pclass->location, attr->values[0].string.text); |
934 | ||
9b4bd602 MS |
935 | if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4)) |
936 | cupsdSetString(&pclass->geo_location, attr->values[0].string.text); | |
937 | ||
938 | if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL) | |
939 | cupsdSetString(&pclass->organization, attr->values[0].string.text); | |
940 | ||
941 | if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL) | |
942 | cupsdSetString(&pclass->organizational_unit, attr->values[0].string.text); | |
943 | ||
fa73b229 | 944 | if ((attr = ippFindAttribute(con->request, "printer-info", |
945 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 946 | cupsdSetString(&pclass->info, attr->values[0].string.text); |
947 | ||
fa73b229 | 948 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
f8b3a85b MS |
949 | IPP_TAG_BOOLEAN)) != NULL && |
950 | attr->values[0].boolean != pclass->accepting) | |
ef416fc2 | 951 | { |
bc44d920 | 952 | cupsdLogMessage(CUPSD_LOG_INFO, |
953 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
954 | pclass->name, attr->values[0].boolean, pclass->accepting); | |
ef416fc2 | 955 | |
956 | pclass->accepting = attr->values[0].boolean; | |
f8b3a85b MS |
957 | |
958 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s accepting jobs.", | |
959 | pclass->accepting ? "Now" : "No longer"); | |
ef416fc2 | 960 | } |
961 | ||
7ae00c35 | 962 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL) |
ef416fc2 | 963 | { |
7dc6f91d MS |
964 | if (pclass->type & CUPS_PRINTER_REMOTE) |
965 | { | |
966 | /* | |
967 | * Cannot re-share remote printers. | |
968 | */ | |
969 | ||
970 | send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues.")); | |
f0b589f7 MS |
971 | if (!modify) |
972 | cupsdDeletePrinter(pclass, 0); | |
973 | ||
7dc6f91d MS |
974 | return; |
975 | } | |
976 | ||
7ae00c35 | 977 | if (pclass->shared && !ippGetBoolean(attr, 0)) |
f7deaa1a | 978 | cupsdDeregisterPrinter(pclass, 1); |
ef416fc2 | 979 | |
980 | cupsdLogMessage(CUPSD_LOG_INFO, | |
981 | "Setting %s printer-is-shared to %d (was %d.)", | |
982 | pclass->name, attr->values[0].boolean, pclass->shared); | |
983 | ||
7ae00c35 | 984 | pclass->shared = ippGetBoolean(attr, 0); |
ef416fc2 | 985 | } |
986 | ||
fa73b229 | 987 | if ((attr = ippFindAttribute(con->request, "printer-state", |
988 | IPP_TAG_ENUM)) != NULL) | |
ef416fc2 | 989 | { |
990 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
991 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
992 | { | |
993 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 994 | _("Attempt to set %s printer-state to bad value %d."), |
ef416fc2 | 995 | pclass->name, attr->values[0].integer); |
f0b589f7 MS |
996 | if (!modify) |
997 | cupsdDeletePrinter(pclass, 0); | |
998 | ||
ef416fc2 | 999 | return; |
1000 | } | |
1001 | ||
bc44d920 | 1002 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
1003 | pclass->name, attr->values[0].integer, pclass->state); | |
ef416fc2 | 1004 | |
1005 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
1006 | cupsdStopPrinter(pclass, 0); | |
1007 | else | |
e00b005a | 1008 | { |
ef416fc2 | 1009 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); |
e00b005a | 1010 | need_restart_job = 1; |
1011 | } | |
ef416fc2 | 1012 | } |
fa73b229 | 1013 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
1014 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1015 | { |
1016 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
1017 | sizeof(pclass->state_message)); | |
f8b3a85b MS |
1018 | |
1019 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s", | |
1020 | pclass->state_message); | |
ef416fc2 | 1021 | } |
fa73b229 | 1022 | if ((attr = ippFindAttribute(con->request, "member-uris", |
1023 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 1024 | { |
1025 | /* | |
1026 | * Clear the printer array as needed... | |
1027 | */ | |
1028 | ||
e00b005a | 1029 | need_restart_job = 1; |
1030 | ||
ef416fc2 | 1031 | if (pclass->num_printers > 0) |
1032 | { | |
1033 | free(pclass->printers); | |
1034 | pclass->num_printers = 0; | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * Add each printer or class that is listed... | |
1039 | */ | |
1040 | ||
1041 | for (i = 0; i < attr->num_values; i ++) | |
1042 | { | |
1043 | /* | |
1044 | * Search for the printer or class URI... | |
1045 | */ | |
1046 | ||
f7deaa1a | 1047 | if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member)) |
ef416fc2 | 1048 | { |
1049 | /* | |
1050 | * Bad URI... | |
1051 | */ | |
1052 | ||
1053 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 1054 | _("The printer or class does not exist.")); |
f0b589f7 MS |
1055 | if (!modify) |
1056 | cupsdDeletePrinter(pclass, 0); | |
1057 | ||
ef416fc2 | 1058 | return; |
1059 | } | |
acb056cb MS |
1060 | else if (dtype & CUPS_PRINTER_CLASS) |
1061 | { | |
1062 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 1063 | _("Nested classes are not allowed.")); |
f0b589f7 MS |
1064 | if (!modify) |
1065 | cupsdDeletePrinter(pclass, 0); | |
1066 | ||
acb056cb MS |
1067 | return; |
1068 | } | |
ef416fc2 | 1069 | |
1070 | /* | |
1071 | * Add it to the class... | |
1072 | */ | |
1073 | ||
1074 | cupsdAddPrinterToClass(pclass, member); | |
1075 | } | |
1076 | } | |
1077 | ||
f0b589f7 MS |
1078 | if (!set_printer_defaults(con, pclass)) |
1079 | { | |
1080 | if (!modify) | |
1081 | cupsdDeletePrinter(pclass, 0); | |
1082 | ||
1083 | return; | |
1084 | } | |
b423cd4c | 1085 | |
09a101d6 | 1086 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
1087 | IPP_TAG_KEYWORD)) != NULL) | |
1088 | cupsdSetAuthInfoRequired(pclass, NULL, attr); | |
1089 | ||
9b4bd602 MS |
1090 | pclass->config_time = time(NULL); |
1091 | ||
ef416fc2 | 1092 | /* |
1093 | * Update the printer class attributes and return... | |
1094 | */ | |
1095 | ||
1096 | cupsdSetPrinterAttrs(pclass); | |
3dfe78b3 | 1097 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
e00b005a | 1098 | |
1099 | if (need_restart_job && pclass->job) | |
1100 | { | |
e00b005a | 1101 | /* |
b9faaae1 | 1102 | * Reset the current job to a "pending" status... |
e00b005a | 1103 | */ |
1104 | ||
b9faaae1 MS |
1105 | cupsdSetJobState(pclass->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE, |
1106 | "Job restarted because the class was modified."); | |
e00b005a | 1107 | } |
1108 | ||
3dfe78b3 | 1109 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 1110 | |
1111 | if (modify) | |
1112 | { | |
c7017ecc | 1113 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, |
f8b3a85b MS |
1114 | pclass, NULL, "Class \"%s\" modified by \"%s\".", |
1115 | pclass->name, get_username(con)); | |
ef416fc2 | 1116 | |
1117 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
e00b005a | 1118 | pclass->name, get_username(con)); |
ef416fc2 | 1119 | } |
1120 | else | |
1121 | { | |
c7017ecc | 1122 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, |
f8b3a85b MS |
1123 | pclass, NULL, "New class \"%s\" added by \"%s\".", |
1124 | pclass->name, get_username(con)); | |
ef416fc2 | 1125 | |
1126 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
e00b005a | 1127 | pclass->name, get_username(con)); |
ef416fc2 | 1128 | } |
1129 | ||
1130 | con->response->request.status.status_code = IPP_OK; | |
1131 | } | |
1132 | ||
1133 | ||
1134 | /* | |
1135 | * 'add_file()' - Add a file to a job. | |
1136 | */ | |
1137 | ||
1138 | static int /* O - 0 on success, -1 on error */ | |
1139 | add_file(cupsd_client_t *con, /* I - Connection to client */ | |
1140 | cupsd_job_t *job, /* I - Job to add to */ | |
1141 | mime_type_t *filetype, /* I - Type of file */ | |
1142 | int compression) /* I - Compression */ | |
1143 | { | |
1144 | mime_type_t **filetypes; /* New filetypes array... */ | |
1145 | int *compressions; /* New compressions array... */ | |
1146 | ||
1147 | ||
1148 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
bc44d920 | 1149 | "add_file(con=%p[%d], job=%d, filetype=%s/%s, " |
996acce8 | 1150 | "compression=%d)", con, con ? con->number : -1, job->id, |
bc44d920 | 1151 | filetype->super, filetype->type, compression); |
ef416fc2 | 1152 | |
1153 | /* | |
1154 | * Add the file to the job... | |
1155 | */ | |
1156 | ||
1157 | if (job->num_files == 0) | |
1158 | { | |
1159 | compressions = (int *)malloc(sizeof(int)); | |
1160 | filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *)); | |
1161 | } | |
1162 | else | |
1163 | { | |
1164 | compressions = (int *)realloc(job->compressions, | |
7e86f2f6 | 1165 | (size_t)(job->num_files + 1) * sizeof(int)); |
ef416fc2 | 1166 | filetypes = (mime_type_t **)realloc(job->filetypes, |
7e86f2f6 | 1167 | (size_t)(job->num_files + 1) * |
ef416fc2 | 1168 | sizeof(mime_type_t *)); |
1169 | } | |
1170 | ||
cb7f98ee MS |
1171 | if (compressions) |
1172 | job->compressions = compressions; | |
1173 | ||
1174 | if (filetypes) | |
1175 | job->filetypes = filetypes; | |
1176 | ||
fa73b229 | 1177 | if (!compressions || !filetypes) |
ef416fc2 | 1178 | { |
b9faaae1 MS |
1179 | cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE, |
1180 | "Job aborted because the scheduler ran out of memory."); | |
ef416fc2 | 1181 | |
09a101d6 | 1182 | if (con) |
1183 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
84315f46 | 1184 | _("Unable to allocate memory for file types.")); |
09a101d6 | 1185 | |
ef416fc2 | 1186 | return (-1); |
1187 | } | |
1188 | ||
ef416fc2 | 1189 | job->compressions[job->num_files] = compression; |
ef416fc2 | 1190 | job->filetypes[job->num_files] = filetype; |
1191 | ||
1192 | job->num_files ++; | |
1193 | ||
3dfe78b3 MS |
1194 | job->dirty = 1; |
1195 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
1196 | ||
ef416fc2 | 1197 | return (0); |
1198 | } | |
1199 | ||
1200 | ||
1201 | /* | |
b423cd4c | 1202 | * 'add_job()' - Add a job to a print queue. |
ef416fc2 | 1203 | */ |
1204 | ||
b423cd4c | 1205 | static cupsd_job_t * /* O - Job object */ |
1206 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
f7deaa1a | 1207 | cupsd_printer_t *printer, /* I - Destination printer */ |
80ca4592 | 1208 | mime_type_t *filetype) /* I - First print file type, if any */ |
ef416fc2 | 1209 | { |
b423cd4c | 1210 | http_status_t status; /* Policy status */ |
f7deaa1a | 1211 | ipp_attribute_t *attr, /* Current attribute */ |
1212 | *auth_info; /* auth-info attribute */ | |
5a9febac | 1213 | const char *mandatory; /* Current mandatory job attribute */ |
b423cd4c | 1214 | const char *val; /* Default option value */ |
1215 | int priority; /* Job priority */ | |
b423cd4c | 1216 | cupsd_job_t *job; /* Current job */ |
f7deaa1a | 1217 | char job_uri[HTTP_MAX_URI]; /* Job URI */ |
b423cd4c | 1218 | int kbytes; /* Size of print file */ |
1219 | int i; /* Looping var */ | |
1220 | int lowerpagerange; /* Page range bound */ | |
54afec33 MS |
1221 | int exact; /* Did we have an exact match? */ |
1222 | ipp_attribute_t *media_col, /* media-col attribute */ | |
1223 | *media_margin; /* media-*-margin attribute */ | |
1224 | ipp_t *unsup_col; /* media-col in unsupported response */ | |
5a9febac MS |
1225 | static const char * const readonly[] =/* List of read-only attributes */ |
1226 | { | |
9b4bd602 MS |
1227 | "date-time-at-completed", |
1228 | "date-time-at-creation", | |
1229 | "date-time-at-processing", | |
9514a192 MS |
1230 | "job-detailed-status-messages", |
1231 | "job-document-access-errors", | |
5a9febac | 1232 | "job-id", |
5a9febac | 1233 | "job-impressions-completed", |
9514a192 | 1234 | "job-k-octets-completed", |
5a9febac | 1235 | "job-media-sheets-completed", |
9514a192 MS |
1236 | "job-pages-completed", |
1237 | "job-printer-up-time", | |
1238 | "job-printer-uri", | |
5a9febac MS |
1239 | "job-state", |
1240 | "job-state-message", | |
1241 | "job-state-reasons", | |
9514a192 MS |
1242 | "job-uri", |
1243 | "number-of-documents", | |
1244 | "number-of-intervening-jobs", | |
1245 | "output-device-assigned", | |
5a9febac MS |
1246 | "time-at-completed", |
1247 | "time-at-creation", | |
1248 | "time-at-processing" | |
1249 | }; | |
ef416fc2 | 1250 | |
1251 | ||
f7deaa1a | 1252 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))", |
996acce8 | 1253 | con, con->number, printer, printer->name, |
0a682745 MS |
1254 | filetype, filetype ? filetype->super : "none", |
1255 | filetype ? filetype->type : "none"); | |
ef416fc2 | 1256 | |
b423cd4c | 1257 | /* |
1258 | * Check remote printing to non-shared printer... | |
1259 | */ | |
ef416fc2 | 1260 | |
b423cd4c | 1261 | if (!printer->shared && |
996acce8 MS |
1262 | _cups_strcasecmp(con->http->hostname, "localhost") && |
1263 | _cups_strcasecmp(con->http->hostname, ServerName)) | |
b423cd4c | 1264 | { |
1265 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
84315f46 | 1266 | _("The printer or class is not shared.")); |
b423cd4c | 1267 | return (NULL); |
1268 | } | |
ef416fc2 | 1269 | |
b423cd4c | 1270 | /* |
1271 | * Check policy... | |
1272 | */ | |
ef416fc2 | 1273 | |
f899b121 | 1274 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
7594b224 | 1275 | |
b423cd4c | 1276 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
1277 | { | |
f899b121 | 1278 | send_http_error(con, status, printer); |
b423cd4c | 1279 | return (NULL); |
1280 | } | |
f11a948a MS |
1281 | else if (printer->num_auth_info_required == 1 && |
1282 | !strcmp(printer->auth_info_required[0], "negotiate") && | |
1283 | !con->username[0]) | |
b423cd4c | 1284 | { |
f899b121 | 1285 | send_http_error(con, HTTP_UNAUTHORIZED, printer); |
b423cd4c | 1286 | return (NULL); |
1287 | } | |
7594b224 | 1288 | #ifdef HAVE_SSL |
996acce8 MS |
1289 | else if (auth_info && !con->http->tls && |
1290 | !httpAddrLocalhost(con->http->hostaddr)) | |
7594b224 | 1291 | { |
1292 | /* | |
1293 | * Require encryption of auth-info over non-local connections... | |
1294 | */ | |
1295 | ||
f899b121 | 1296 | send_http_error(con, HTTP_UPGRADE_REQUIRED, printer); |
7594b224 | 1297 | return (NULL); |
1298 | } | |
1299 | #endif /* HAVE_SSL */ | |
ef416fc2 | 1300 | |
b423cd4c | 1301 | /* |
1302 | * See if the printer is accepting jobs... | |
1303 | */ | |
1304 | ||
1305 | if (!printer->accepting) | |
1306 | { | |
1307 | send_ipp_status(con, IPP_NOT_ACCEPTING, | |
1308 | _("Destination \"%s\" is not accepting jobs."), | |
f7deaa1a | 1309 | printer->name); |
b423cd4c | 1310 | return (NULL); |
ef416fc2 | 1311 | } |
ef416fc2 | 1312 | |
b423cd4c | 1313 | /* |
80ca4592 | 1314 | * Validate job template attributes; for now just document-format, |
5a9febac MS |
1315 | * copies, job-sheets, number-up, page-ranges, mandatory attributes, and |
1316 | * media... | |
b423cd4c | 1317 | */ |
ef416fc2 | 1318 | |
5a9febac MS |
1319 | for (i = 0; i < (int)(sizeof(readonly) / sizeof(readonly[0])); i ++) |
1320 | { | |
9514a192 | 1321 | if ((attr = ippFindAttribute(con->request, readonly[i], IPP_TAG_ZERO)) != NULL) |
5a9febac MS |
1322 | { |
1323 | ippDeleteAttribute(con->request, attr); | |
1324 | ||
1325 | if (StrictConformance) | |
1326 | { | |
9514a192 | 1327 | send_ipp_status(con, IPP_BAD_REQUEST, _("The '%s' Job Status attribute cannot be supplied in a job creation request."), readonly[i]); |
5a9febac MS |
1328 | return (NULL); |
1329 | } | |
1330 | ||
9514a192 | 1331 | cupsdLogMessage(CUPSD_LOG_INFO, "Unexpected '%s' Job Status attribute in a job creation request.", readonly[i]); |
5a9febac MS |
1332 | } |
1333 | } | |
1334 | ||
1335 | if (printer->pc) | |
1336 | { | |
1337 | for (mandatory = (char *)cupsArrayFirst(printer->pc->mandatory); | |
1338 | mandatory; | |
1339 | mandatory = (char *)cupsArrayNext(printer->pc->mandatory)) | |
1340 | { | |
1341 | if (!ippFindAttribute(con->request, mandatory, IPP_TAG_ZERO)) | |
1342 | { | |
1343 | /* | |
1344 | * Missing a required attribute... | |
1345 | */ | |
1346 | ||
1347 | send_ipp_status(con, IPP_CONFLICT, | |
1348 | _("The \"%s\" attribute is required for print jobs."), | |
1349 | mandatory); | |
1350 | return (NULL); | |
1351 | } | |
1352 | } | |
1353 | } | |
1354 | ||
80ca4592 | 1355 | if (filetype && printer->filetypes && |
1356 | !cupsArrayFind(printer->filetypes, filetype)) | |
1357 | { | |
1358 | char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
1359 | /* MIME media type string */ | |
1360 | ||
1361 | ||
1362 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
1363 | filetype->type); | |
1364 | ||
1365 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
84315f46 | 1366 | _("Unsupported format \"%s\"."), mimetype); |
80ca4592 | 1367 | |
1368 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
1369 | "document-format", NULL, mimetype); | |
1370 | ||
1371 | return (NULL); | |
1372 | } | |
1373 | ||
b423cd4c | 1374 | if ((attr = ippFindAttribute(con->request, "copies", |
1375 | IPP_TAG_INTEGER)) != NULL) | |
1376 | { | |
1377 | if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies) | |
1378 | { | |
1379 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."), | |
1380 | attr->values[0].integer); | |
1381 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1382 | "copies", attr->values[0].integer); | |
1383 | return (NULL); | |
1384 | } | |
1385 | } | |
ef416fc2 | 1386 | |
3dfe78b3 MS |
1387 | if ((attr = ippFindAttribute(con->request, "job-sheets", |
1388 | IPP_TAG_ZERO)) != NULL) | |
1389 | { | |
1390 | if (attr->value_tag != IPP_TAG_KEYWORD && | |
1391 | attr->value_tag != IPP_TAG_NAME) | |
1392 | { | |
84315f46 | 1393 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type.")); |
3dfe78b3 MS |
1394 | return (NULL); |
1395 | } | |
1396 | ||
1397 | if (attr->num_values > 2) | |
1398 | { | |
1399 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 1400 | _("Too many job-sheets values (%d > 2)."), |
3dfe78b3 MS |
1401 | attr->num_values); |
1402 | return (NULL); | |
1403 | } | |
1404 | ||
1405 | for (i = 0; i < attr->num_values; i ++) | |
1406 | if (strcmp(attr->values[i].string.text, "none") && | |
1407 | !cupsdFindBanner(attr->values[i].string.text)) | |
1408 | { | |
84315f46 | 1409 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"."), |
3dfe78b3 MS |
1410 | attr->values[i].string.text); |
1411 | return (NULL); | |
1412 | } | |
1413 | } | |
1414 | ||
f7deaa1a | 1415 | if ((attr = ippFindAttribute(con->request, "number-up", |
1416 | IPP_TAG_INTEGER)) != NULL) | |
1417 | { | |
1418 | if (attr->values[0].integer != 1 && | |
1419 | attr->values[0].integer != 2 && | |
1420 | attr->values[0].integer != 4 && | |
1421 | attr->values[0].integer != 6 && | |
1422 | attr->values[0].integer != 9 && | |
1423 | attr->values[0].integer != 16) | |
1424 | { | |
1425 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."), | |
1426 | attr->values[0].integer); | |
1427 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1428 | "number-up", attr->values[0].integer); | |
1429 | return (NULL); | |
1430 | } | |
1431 | } | |
1432 | ||
b423cd4c | 1433 | if ((attr = ippFindAttribute(con->request, "page-ranges", |
1434 | IPP_TAG_RANGE)) != NULL) | |
1435 | { | |
1436 | for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++) | |
1437 | { | |
f7deaa1a | 1438 | if (attr->values[i].range.lower < lowerpagerange || |
b423cd4c | 1439 | attr->values[i].range.lower > attr->values[i].range.upper) |
1440 | { | |
1441 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1442 | _("Bad page-ranges values %d-%d."), | |
1443 | attr->values[i].range.lower, | |
1444 | attr->values[i].range.upper); | |
1445 | return (NULL); | |
1446 | } | |
ef416fc2 | 1447 | |
b423cd4c | 1448 | lowerpagerange = attr->values[i].range.upper + 1; |
1449 | } | |
1450 | } | |
ef416fc2 | 1451 | |
54afec33 MS |
1452 | /* |
1453 | * Do media selection as needed... | |
1454 | */ | |
1455 | ||
c7017ecc MS |
1456 | if (!ippFindAttribute(con->request, "PageRegion", IPP_TAG_ZERO) && |
1457 | !ippFindAttribute(con->request, "PageSize", IPP_TAG_ZERO) && | |
f14324a7 | 1458 | _ppdCacheGetPageSize(printer->pc, con->request, NULL, &exact)) |
54afec33 | 1459 | { |
54afec33 MS |
1460 | if (!exact && |
1461 | (media_col = ippFindAttribute(con->request, "media-col", | |
1462 | IPP_TAG_BEGIN_COLLECTION)) != NULL) | |
1463 | { | |
1464 | send_ipp_status(con, IPP_OK_SUBST, _("Unsupported margins.")); | |
1465 | ||
1466 | unsup_col = ippNew(); | |
54afec33 MS |
1467 | if ((media_margin = ippFindAttribute(media_col->values[0].collection, |
1468 | "media-bottom-margin", | |
1469 | IPP_TAG_INTEGER)) != NULL) | |
1470 | ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, | |
1471 | "media-bottom-margin", media_margin->values[0].integer); | |
1472 | ||
1473 | if ((media_margin = ippFindAttribute(media_col->values[0].collection, | |
1474 | "media-left-margin", | |
1475 | IPP_TAG_INTEGER)) != NULL) | |
1476 | ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, | |
1477 | "media-left-margin", media_margin->values[0].integer); | |
1478 | ||
1479 | if ((media_margin = ippFindAttribute(media_col->values[0].collection, | |
1480 | "media-right-margin", | |
1481 | IPP_TAG_INTEGER)) != NULL) | |
1482 | ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, | |
1483 | "media-right-margin", media_margin->values[0].integer); | |
1484 | ||
1485 | if ((media_margin = ippFindAttribute(media_col->values[0].collection, | |
1486 | "media-top-margin", | |
1487 | IPP_TAG_INTEGER)) != NULL) | |
1488 | ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, | |
1489 | "media-top-margin", media_margin->values[0].integer); | |
aaf19ab0 MS |
1490 | |
1491 | ippAddCollection(con->response, IPP_TAG_UNSUPPORTED_GROUP, "media-col", | |
1492 | unsup_col); | |
1493 | ippDelete(unsup_col); | |
54afec33 MS |
1494 | } |
1495 | } | |
1496 | ||
ef416fc2 | 1497 | /* |
b423cd4c | 1498 | * Make sure we aren't over our limit... |
ef416fc2 | 1499 | */ |
1500 | ||
b423cd4c | 1501 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
1502 | cupsdCleanJobs(); | |
ef416fc2 | 1503 | |
f7deaa1a | 1504 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
b423cd4c | 1505 | { |
84315f46 | 1506 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Too many active jobs.")); |
b423cd4c | 1507 | return (NULL); |
1508 | } | |
1509 | ||
bc44d920 | 1510 | if ((i = check_quotas(con, printer)) < 0) |
b423cd4c | 1511 | { |
1512 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1513 | return (NULL); | |
1514 | } | |
bc44d920 | 1515 | else if (i == 0) |
1516 | { | |
1517 | send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print.")); | |
1518 | return (NULL); | |
1519 | } | |
ef416fc2 | 1520 | |
1521 | /* | |
b423cd4c | 1522 | * Create the job and set things up... |
ef416fc2 | 1523 | */ |
1524 | ||
b423cd4c | 1525 | if ((attr = ippFindAttribute(con->request, "job-priority", |
1526 | IPP_TAG_INTEGER)) != NULL) | |
1527 | priority = attr->values[0].integer; | |
1528 | else | |
ef416fc2 | 1529 | { |
b423cd4c | 1530 | if ((val = cupsGetOption("job-priority", printer->num_options, |
1531 | printer->options)) != NULL) | |
1532 | priority = atoi(val); | |
1533 | else | |
1534 | priority = 50; | |
ef416fc2 | 1535 | |
b423cd4c | 1536 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", |
1537 | priority); | |
1538 | } | |
ef416fc2 | 1539 | |
cb7f98ee | 1540 | if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) == NULL) |
f16ea703 | 1541 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled"); |
cb7f98ee MS |
1542 | else if ((attr->value_tag != IPP_TAG_NAME && |
1543 | attr->value_tag != IPP_TAG_NAMELANG) || | |
1544 | attr->num_values != 1) | |
1545 | { | |
1546 | send_ipp_status(con, IPP_ATTRIBUTES, | |
1547 | _("Bad job-name value: Wrong type or count.")); | |
1548 | if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL) | |
1549 | attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
6d14c1bb MS |
1550 | |
1551 | if (StrictConformance) | |
1552 | return (NULL); | |
1553 | ||
1554 | /* Don't use invalid attribute */ | |
1555 | ippDeleteAttribute(con->request, attr); | |
1556 | ||
1557 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled"); | |
cb7f98ee MS |
1558 | } |
1559 | else if (!ippValidateAttribute(attr)) | |
1560 | { | |
1561 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad job-name value: %s"), | |
1562 | cupsLastErrorString()); | |
6d14c1bb | 1563 | |
cb7f98ee MS |
1564 | if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL) |
1565 | attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
6d14c1bb MS |
1566 | |
1567 | if (StrictConformance) | |
1568 | return (NULL); | |
1569 | ||
1570 | /* Don't use invalid attribute */ | |
1571 | ippDeleteAttribute(con->request, attr); | |
1572 | ||
1573 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled"); | |
cb7f98ee | 1574 | } |
ef416fc2 | 1575 | |
49fa4983 MS |
1576 | attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME); |
1577 | ||
b423cd4c | 1578 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) |
1579 | { | |
1580 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
84315f46 | 1581 | _("Unable to add job for destination \"%s\"."), |
f7deaa1a | 1582 | printer->name); |
b423cd4c | 1583 | return (NULL); |
1584 | } | |
ef416fc2 | 1585 | |
a2326b5b | 1586 | job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE); |
b423cd4c | 1587 | job->attrs = con->request; |
3dfe78b3 | 1588 | job->dirty = 1; |
2abf387c | 1589 | con->request = ippNewRequest(job->attrs->request.op.operation_id); |
ef416fc2 | 1590 | |
3dfe78b3 MS |
1591 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); |
1592 | ||
321d8d57 | 1593 | add_job_uuid(job); |
b423cd4c | 1594 | apply_printer_defaults(printer, job); |
ef416fc2 | 1595 | |
b423cd4c | 1596 | if (con->username[0]) |
1597 | { | |
1598 | cupsdSetString(&job->username, con->username); | |
ef416fc2 | 1599 | |
1600 | if (attr) | |
5e6c3df7 | 1601 | ippSetString(job->attrs, &attr, 0, con->username); |
ef416fc2 | 1602 | } |
b423cd4c | 1603 | else if (attr) |
1604 | { | |
1605 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1606 | "add_job: requesting-user-name=\"%s\"", | |
1607 | attr->values[0].string.text); | |
ef416fc2 | 1608 | |
b423cd4c | 1609 | cupsdSetString(&job->username, attr->values[0].string.text); |
1610 | } | |
1611 | else | |
1612 | cupsdSetString(&job->username, "anonymous"); | |
ef416fc2 | 1613 | |
b423cd4c | 1614 | if (!attr) |
1615 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1616 | "job-originating-user-name", NULL, job->username); | |
1617 | else | |
1618 | { | |
5e6c3df7 MS |
1619 | ippSetGroupTag(job->attrs, &attr, IPP_TAG_JOB); |
1620 | ippSetName(job->attrs, &attr, "job-originating-user-name"); | |
b423cd4c | 1621 | } |
ef416fc2 | 1622 | |
f7deaa1a | 1623 | if (con->username[0] || auth_info) |
1624 | { | |
1625 | save_auth_info(con, job, auth_info); | |
1626 | ||
1627 | /* | |
1628 | * Remove the auth-info attribute from the attribute data... | |
1629 | */ | |
1630 | ||
1631 | if (auth_info) | |
a4924f6c | 1632 | ippDeleteAttribute(job->attrs, auth_info); |
f7deaa1a | 1633 | } |
1634 | ||
f16ea703 MS |
1635 | if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_NAME)) != NULL) |
1636 | cupsdSetString(&(job->name), attr->values[0].string.text); | |
1637 | ||
b423cd4c | 1638 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", |
1639 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 1640 | { |
b423cd4c | 1641 | /* |
1642 | * Request contains a job-originating-host-name attribute; validate it... | |
1643 | */ | |
ef416fc2 | 1644 | |
b423cd4c | 1645 | if (attr->value_tag != IPP_TAG_NAME || |
1646 | attr->num_values != 1 || | |
996acce8 | 1647 | strcmp(con->http->hostname, "localhost")) |
ef416fc2 | 1648 | { |
1649 | /* | |
b423cd4c | 1650 | * Can't override the value if we aren't connected via localhost. |
1651 | * Also, we can only have 1 value and it must be a name value. | |
ef416fc2 | 1652 | */ |
1653 | ||
5e6c3df7 MS |
1654 | ippDeleteAttribute(job->attrs, attr); |
1655 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-originating-host-name", NULL, con->http->hostname); | |
b423cd4c | 1656 | } |
5e6c3df7 MS |
1657 | else |
1658 | ippSetGroupTag(job->attrs, &attr, IPP_TAG_JOB); | |
b423cd4c | 1659 | } |
1660 | else | |
1661 | { | |
1662 | /* | |
1663 | * No job-originating-host-name attribute, so use the hostname from | |
1664 | * the connection... | |
1665 | */ | |
bd7854cb | 1666 | |
f7deaa1a | 1667 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, |
996acce8 | 1668 | "job-originating-host-name", NULL, con->http->hostname); |
b423cd4c | 1669 | } |
bd7854cb | 1670 | |
9b4bd602 MS |
1671 | ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-completed"); |
1672 | ippAddDate(job->attrs, IPP_TAG_JOB, "date-time-at-creation", ippTimeToDate(time(NULL))); | |
1673 | ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-processing"); | |
1674 | ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "time-at-completed"); | |
1675 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", time(NULL)); | |
1676 | ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "time-at-processing"); | |
bd7854cb | 1677 | |
1678 | /* | |
b423cd4c | 1679 | * Add remaining job attributes... |
bd7854cb | 1680 | */ |
1681 | ||
b423cd4c | 1682 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
1683 | job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM, | |
1684 | "job-state", IPP_JOB_STOPPED); | |
d09495fa | 1685 | job->state_value = (ipp_jstate_t)job->state->values[0].integer; |
12f89d24 MS |
1686 | job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1687 | "job-state-reasons", NULL, "job-incoming"); | |
9514a192 | 1688 | job->impressions = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-impressions-completed", 0); |
b423cd4c | 1689 | job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, |
1690 | "job-media-sheets-completed", 0); | |
1691 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, | |
1692 | printer->uri); | |
bd7854cb | 1693 | |
f16ea703 | 1694 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL) |
b423cd4c | 1695 | attr->values[0].integer = 0; |
1696 | else | |
1f0275e3 | 1697 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0); |
bd7854cb | 1698 | |
b423cd4c | 1699 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
1700 | IPP_TAG_KEYWORD)) == NULL) | |
1701 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
1702 | if (!attr) | |
ef416fc2 | 1703 | { |
b423cd4c | 1704 | if ((val = cupsGetOption("job-hold-until", printer->num_options, |
1705 | printer->options)) == NULL) | |
1706 | val = "no-hold"; | |
ef416fc2 | 1707 | |
b423cd4c | 1708 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1709 | "job-hold-until", NULL, val); | |
ef416fc2 | 1710 | } |
e0f489cd MS |
1711 | |
1712 | if (printer->holding_new_jobs) | |
1713 | { | |
1714 | /* | |
1715 | * Hold all new jobs on this printer... | |
1716 | */ | |
1717 | ||
1718 | if (attr && strcmp(attr->values[0].string.text, "no-hold")) | |
1719 | cupsdSetJobHoldUntil(job, ippGetString(attr, 0, NULL), 0); | |
1720 | else | |
1721 | cupsdSetJobHoldUntil(job, "indefinite", 0); | |
1722 | ||
1723 | job->state->values[0].integer = IPP_JOB_HELD; | |
1724 | job->state_value = IPP_JOB_HELD; | |
1725 | ||
1726 | ippSetString(job->attrs, &job->reasons, 0, "job-held-on-create"); | |
1727 | } | |
1728 | else if (attr && strcmp(attr->values[0].string.text, "no-hold")) | |
ef416fc2 | 1729 | { |
1730 | /* | |
b423cd4c | 1731 | * Hold job until specified time... |
ef416fc2 | 1732 | */ |
1733 | ||
b9faaae1 | 1734 | cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0); |
e53920b9 | 1735 | |
1736 | job->state->values[0].integer = IPP_JOB_HELD; | |
1737 | job->state_value = IPP_JOB_HELD; | |
12f89d24 MS |
1738 | |
1739 | ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified"); | |
ef416fc2 | 1740 | } |
b423cd4c | 1741 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) |
ef416fc2 | 1742 | { |
dfd5680b | 1743 | job->hold_until = time(NULL) + MultipleOperationTimeout; |
b423cd4c | 1744 | job->state->values[0].integer = IPP_JOB_HELD; |
1745 | job->state_value = IPP_JOB_HELD; | |
1746 | } | |
1747 | else | |
1748 | { | |
1749 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1750 | job->state_value = IPP_JOB_PENDING; | |
12f89d24 MS |
1751 | |
1752 | ippSetString(job->attrs, &job->reasons, 0, "none"); | |
ef416fc2 | 1753 | } |
1754 | ||
a2326b5b | 1755 | if (!(printer->type & CUPS_PRINTER_REMOTE) || Classification) |
ef416fc2 | 1756 | { |
1757 | /* | |
b423cd4c | 1758 | * Add job sheets options... |
ef416fc2 | 1759 | */ |
1760 | ||
b423cd4c | 1761 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1762 | IPP_TAG_ZERO)) == NULL) | |
ef416fc2 | 1763 | { |
b423cd4c | 1764 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1765 | "Adding default job-sheets values \"%s,%s\"...", | |
1766 | printer->job_sheets[0], printer->job_sheets[1]); | |
ef416fc2 | 1767 | |
b423cd4c | 1768 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", |
1769 | 2, NULL, NULL); | |
5e6c3df7 MS |
1770 | ippSetString(job->attrs, &attr, 0, printer->job_sheets[0]); |
1771 | ippSetString(job->attrs, &attr, 1, printer->job_sheets[1]); | |
ef416fc2 | 1772 | } |
1773 | ||
b423cd4c | 1774 | job->job_sheets = attr; |
ef416fc2 | 1775 | |
ef416fc2 | 1776 | /* |
b423cd4c | 1777 | * Enforce classification level if set... |
ef416fc2 | 1778 | */ |
1779 | ||
b423cd4c | 1780 | if (Classification) |
ef416fc2 | 1781 | { |
b423cd4c | 1782 | cupsdLogMessage(CUPSD_LOG_INFO, |
1783 | "Classification=\"%s\", ClassifyOverride=%d", | |
1784 | Classification ? Classification : "(null)", | |
1785 | ClassifyOverride); | |
ef416fc2 | 1786 | |
b423cd4c | 1787 | if (ClassifyOverride) |
1788 | { | |
1789 | if (!strcmp(attr->values[0].string.text, "none") && | |
1790 | (attr->num_values == 1 || | |
1791 | !strcmp(attr->values[1].string.text, "none"))) | |
1792 | { | |
1793 | /* | |
1794 | * Force the leading banner to have the classification on it... | |
1795 | */ | |
ef416fc2 | 1796 | |
5e6c3df7 | 1797 | ippSetString(job->attrs, &attr, 0, Classification); |
e00b005a | 1798 | |
75bd9771 MS |
1799 | cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED " |
1800 | "job-sheets=\"%s,none\", " | |
1801 | "job-originating-user-name=\"%s\"", | |
1802 | Classification, job->username); | |
b423cd4c | 1803 | } |
1804 | else if (attr->num_values == 2 && | |
1805 | strcmp(attr->values[0].string.text, | |
1806 | attr->values[1].string.text) && | |
1807 | strcmp(attr->values[0].string.text, "none") && | |
1808 | strcmp(attr->values[1].string.text, "none")) | |
1809 | { | |
1810 | /* | |
1811 | * Can't put two different security markings on the same document! | |
1812 | */ | |
ef416fc2 | 1813 | |
5e6c3df7 | 1814 | ippSetString(job->attrs, &attr, 1, attr->values[0].string.text); |
ef416fc2 | 1815 | |
75bd9771 MS |
1816 | cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED " |
1817 | "job-sheets=\"%s,%s\", " | |
1818 | "job-originating-user-name=\"%s\"", | |
1819 | attr->values[0].string.text, | |
1820 | attr->values[1].string.text, job->username); | |
b423cd4c | 1821 | } |
1822 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1823 | strcmp(attr->values[0].string.text, "none") && | |
1824 | (attr->num_values == 1 || | |
1825 | (strcmp(attr->values[1].string.text, Classification) && | |
1826 | strcmp(attr->values[1].string.text, "none")))) | |
1827 | { | |
1828 | if (attr->num_values == 1) | |
75bd9771 MS |
1829 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1830 | "CLASSIFICATION OVERRIDDEN " | |
1831 | "job-sheets=\"%s\", " | |
1832 | "job-originating-user-name=\"%s\"", | |
1833 | attr->values[0].string.text, job->username); | |
b423cd4c | 1834 | else |
75bd9771 MS |
1835 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1836 | "CLASSIFICATION OVERRIDDEN " | |
1837 | "job-sheets=\"%s,%s\",fffff " | |
1838 | "job-originating-user-name=\"%s\"", | |
1839 | attr->values[0].string.text, | |
1840 | attr->values[1].string.text, job->username); | |
b423cd4c | 1841 | } |
1842 | } | |
1843 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1844 | (attr->num_values == 1 || | |
1845 | strcmp(attr->values[1].string.text, Classification))) | |
ef416fc2 | 1846 | { |
1847 | /* | |
b423cd4c | 1848 | * Force the banner to have the classification on it... |
ef416fc2 | 1849 | */ |
1850 | ||
b423cd4c | 1851 | if (attr->num_values > 1 && |
1852 | !strcmp(attr->values[0].string.text, attr->values[1].string.text)) | |
1853 | { | |
5e6c3df7 MS |
1854 | ippSetString(job->attrs, &attr, 0, Classification); |
1855 | ippSetString(job->attrs, &attr, 1, Classification); | |
b423cd4c | 1856 | } |
1857 | else | |
1858 | { | |
1859 | if (attr->num_values == 1 || | |
1860 | strcmp(attr->values[0].string.text, "none")) | |
5e6c3df7 | 1861 | ippSetString(job->attrs, &attr, 0, Classification); |
ef416fc2 | 1862 | |
b423cd4c | 1863 | if (attr->num_values > 1 && |
1864 | strcmp(attr->values[1].string.text, "none")) | |
5e6c3df7 | 1865 | ippSetString(job->attrs, &attr, 1, Classification); |
b423cd4c | 1866 | } |
ef416fc2 | 1867 | |
b423cd4c | 1868 | if (attr->num_values > 1) |
75bd9771 MS |
1869 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1870 | "CLASSIFICATION FORCED " | |
1871 | "job-sheets=\"%s,%s\", " | |
1872 | "job-originating-user-name=\"%s\"", | |
1873 | attr->values[0].string.text, | |
1874 | attr->values[1].string.text, job->username); | |
b423cd4c | 1875 | else |
75bd9771 MS |
1876 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1877 | "CLASSIFICATION FORCED " | |
1878 | "job-sheets=\"%s\", " | |
1879 | "job-originating-user-name=\"%s\"", | |
1880 | Classification, job->username); | |
ef416fc2 | 1881 | } |
1882 | } | |
1883 | ||
b423cd4c | 1884 | /* |
1885 | * See if we need to add the starting sheet... | |
1886 | */ | |
ef416fc2 | 1887 | |
a2326b5b | 1888 | if (!(printer->type & CUPS_PRINTER_REMOTE)) |
ef416fc2 | 1889 | { |
75bd9771 MS |
1890 | cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".", |
1891 | attr->values[0].string.text); | |
ef416fc2 | 1892 | |
91c84a35 | 1893 | if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0) |
3dfe78b3 | 1894 | { |
b9faaae1 MS |
1895 | cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE, |
1896 | "Aborting job because the start banner could not be " | |
1897 | "copied."); | |
91c84a35 | 1898 | return (NULL); |
3dfe78b3 | 1899 | } |
ef416fc2 | 1900 | |
b423cd4c | 1901 | cupsdUpdateQuota(printer, job->username, 0, kbytes); |
1902 | } | |
ef416fc2 | 1903 | } |
b423cd4c | 1904 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1905 | IPP_TAG_ZERO)) != NULL) | |
c8fef167 | 1906 | job->job_sheets = attr; |
ef416fc2 | 1907 | |
b423cd4c | 1908 | /* |
1909 | * Fill in the response info... | |
1910 | */ | |
ef416fc2 | 1911 | |
83e08001 | 1912 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
f2534050 | 1913 | con->clientname, con->clientport, "/jobs/%d", job->id); |
b423cd4c | 1914 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
1915 | job_uri); | |
ef416fc2 | 1916 | |
b423cd4c | 1917 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
ef416fc2 | 1918 | |
b423cd4c | 1919 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", |
1920 | job->state_value); | |
9b4bd602 | 1921 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_TEXT, "job-state-message", NULL, ""); |
12f89d24 MS |
1922 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", |
1923 | NULL, job->reasons->values[0].string.text); | |
ef416fc2 | 1924 | |
b423cd4c | 1925 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 1926 | |
b423cd4c | 1927 | /* |
1928 | * Add any job subscriptions... | |
1929 | */ | |
ef416fc2 | 1930 | |
b423cd4c | 1931 | add_job_subscriptions(con, job); |
ef416fc2 | 1932 | |
b423cd4c | 1933 | /* |
1934 | * Set all but the first two attributes to the job attributes group... | |
1935 | */ | |
ef416fc2 | 1936 | |
b423cd4c | 1937 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) |
1938 | attr->group_tag = IPP_TAG_JOB; | |
ef416fc2 | 1939 | |
1940 | /* | |
b423cd4c | 1941 | * Fire the "job created" event... |
ef416fc2 | 1942 | */ |
1943 | ||
b423cd4c | 1944 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); |
ef416fc2 | 1945 | |
1946 | /* | |
b423cd4c | 1947 | * Return the new job... |
ef416fc2 | 1948 | */ |
1949 | ||
b423cd4c | 1950 | return (job); |
1951 | } | |
ef416fc2 | 1952 | |
ef416fc2 | 1953 | |
ef416fc2 | 1954 | /* |
9a4f8274 | 1955 | * 'add_job_subscriptions()' - Add any subscriptions for a job. |
ef416fc2 | 1956 | */ |
1957 | ||
1958 | static void | |
b423cd4c | 1959 | add_job_subscriptions( |
1960 | cupsd_client_t *con, /* I - Client connection */ | |
1961 | cupsd_job_t *job) /* I - Newly created job */ | |
ef416fc2 | 1962 | { |
b423cd4c | 1963 | int i; /* Looping var */ |
1964 | ipp_attribute_t *prev, /* Previous attribute */ | |
1965 | *next, /* Next attribute */ | |
1966 | *attr; /* Current attribute */ | |
1967 | cupsd_subscription_t *sub; /* Subscription object */ | |
1968 | const char *recipient, /* notify-recipient-uri */ | |
1969 | *pullmethod; /* notify-pull-method */ | |
1970 | ipp_attribute_t *user_data; /* notify-user-data */ | |
1971 | int interval; /* notify-time-interval */ | |
1972 | unsigned mask; /* notify-events */ | |
ef416fc2 | 1973 | |
ef416fc2 | 1974 | |
b423cd4c | 1975 | /* |
1976 | * Find the first subscription group attribute; return if we have | |
1977 | * none... | |
1978 | */ | |
ef416fc2 | 1979 | |
1f0275e3 | 1980 | for (attr = job->attrs->attrs; attr; attr = attr->next) |
b423cd4c | 1981 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) |
1982 | break; | |
ef416fc2 | 1983 | |
b423cd4c | 1984 | if (!attr) |
1985 | return; | |
ef416fc2 | 1986 | |
b423cd4c | 1987 | /* |
1988 | * Process the subscription attributes in the request... | |
1989 | */ | |
ef416fc2 | 1990 | |
b423cd4c | 1991 | while (attr) |
1992 | { | |
1993 | recipient = NULL; | |
1994 | pullmethod = NULL; | |
1995 | user_data = NULL; | |
1996 | interval = 0; | |
1997 | mask = CUPSD_EVENT_NONE; | |
ef416fc2 | 1998 | |
b423cd4c | 1999 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
2000 | { | |
f899b121 | 2001 | if (!strcmp(attr->name, "notify-recipient-uri") && |
b423cd4c | 2002 | attr->value_tag == IPP_TAG_URI) |
3dfe78b3 MS |
2003 | { |
2004 | /* | |
2005 | * Validate the recipient scheme against the ServerBin/notifier | |
2006 | * directory... | |
2007 | */ | |
2008 | ||
2009 | char notifier[1024], /* Notifier filename */ | |
2010 | scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
2011 | userpass[HTTP_MAX_URI], /* Username portion of URI */ | |
2012 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2013 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2014 | int port; /* Port portion of URI */ | |
7d4510a0 | 2015 | struct stat info; /* File information */ |
3dfe78b3 | 2016 | |
b423cd4c | 2017 | recipient = attr->values[0].string.text; |
3dfe78b3 MS |
2018 | |
2019 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, | |
2020 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
2021 | host, sizeof(host), &port, | |
2022 | resource, sizeof(resource)) < HTTP_URI_OK) | |
2023 | { | |
2024 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 2025 | _("Bad notify-recipient-uri \"%s\"."), recipient); |
3dfe78b3 MS |
2026 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
2027 | "notify-status-code", IPP_URI_SCHEME); | |
2028 | return; | |
2029 | } | |
2030 | ||
7d4510a0 MS |
2031 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, scheme); |
2032 | if (access(notifier, X_OK) || stat(notifier, &info) || !S_ISREG(info.st_mode)) | |
3dfe78b3 MS |
2033 | { |
2034 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2035 | _("notify-recipient-uri URI \"%s\" uses unknown " | |
84315f46 | 2036 | "scheme."), recipient); |
3dfe78b3 MS |
2037 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
2038 | "notify-status-code", IPP_URI_SCHEME); | |
2039 | return; | |
2040 | } | |
2041 | ||
2042 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
2043 | { | |
2044 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 2045 | _("notify-recipient-uri URI \"%s\" is already used."), |
3dfe78b3 MS |
2046 | recipient); |
2047 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2048 | "notify-status-code", IPP_ATTRIBUTES); | |
2049 | return; | |
2050 | } | |
2051 | } | |
b423cd4c | 2052 | else if (!strcmp(attr->name, "notify-pull-method") && |
2053 | attr->value_tag == IPP_TAG_KEYWORD) | |
3dfe78b3 | 2054 | { |
b423cd4c | 2055 | pullmethod = attr->values[0].string.text; |
3dfe78b3 MS |
2056 | |
2057 | if (strcmp(pullmethod, "ippget")) | |
2058 | { | |
2059 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 2060 | _("Bad notify-pull-method \"%s\"."), pullmethod); |
3dfe78b3 MS |
2061 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
2062 | "notify-status-code", IPP_ATTRIBUTES); | |
2063 | return; | |
2064 | } | |
2065 | } | |
b423cd4c | 2066 | else if (!strcmp(attr->name, "notify-charset") && |
2067 | attr->value_tag == IPP_TAG_CHARSET && | |
2068 | strcmp(attr->values[0].string.text, "us-ascii") && | |
2069 | strcmp(attr->values[0].string.text, "utf-8")) | |
2070 | { | |
2071 | send_ipp_status(con, IPP_CHARSET, | |
84315f46 | 2072 | _("Character set \"%s\" not supported."), |
b423cd4c | 2073 | attr->values[0].string.text); |
2074 | return; | |
2075 | } | |
2076 | else if (!strcmp(attr->name, "notify-natural-language") && | |
2077 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
2078 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
2079 | { | |
2080 | send_ipp_status(con, IPP_CHARSET, | |
84315f46 | 2081 | _("Language \"%s\" not supported."), |
b423cd4c | 2082 | attr->values[0].string.text); |
2083 | return; | |
2084 | } | |
2085 | else if (!strcmp(attr->name, "notify-user-data") && | |
2086 | attr->value_tag == IPP_TAG_STRING) | |
2087 | { | |
2088 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
2089 | { | |
2090 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
2091 | _("The notify-user-data value is too large " | |
84315f46 | 2092 | "(%d > 63 octets)."), |
b423cd4c | 2093 | attr->values[0].unknown.length); |
2094 | return; | |
2095 | } | |
ef416fc2 | 2096 | |
b423cd4c | 2097 | user_data = attr; |
2098 | } | |
2099 | else if (!strcmp(attr->name, "notify-events") && | |
2100 | attr->value_tag == IPP_TAG_KEYWORD) | |
2101 | { | |
2102 | for (i = 0; i < attr->num_values; i ++) | |
2103 | mask |= cupsdEventValue(attr->values[i].string.text); | |
2104 | } | |
2105 | else if (!strcmp(attr->name, "notify-lease-duration")) | |
2106 | { | |
2107 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2108 | _("The notify-lease-duration attribute cannot be " | |
2109 | "used with job subscriptions.")); | |
2110 | return; | |
2111 | } | |
2112 | else if (!strcmp(attr->name, "notify-time-interval") && | |
2113 | attr->value_tag == IPP_TAG_INTEGER) | |
2114 | interval = attr->values[0].integer; | |
ef416fc2 | 2115 | |
b423cd4c | 2116 | attr = attr->next; |
2117 | } | |
ef416fc2 | 2118 | |
b423cd4c | 2119 | if (!recipient && !pullmethod) |
2120 | break; | |
ef416fc2 | 2121 | |
b423cd4c | 2122 | if (mask == CUPSD_EVENT_NONE) |
2123 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
ef416fc2 | 2124 | |
52f6f666 MS |
2125 | if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, |
2126 | recipient, 0)) != NULL) | |
2127 | { | |
2128 | sub->interval = interval; | |
ef416fc2 | 2129 | |
52f6f666 | 2130 | cupsdSetString(&sub->owner, job->username); |
ef416fc2 | 2131 | |
52f6f666 MS |
2132 | if (user_data) |
2133 | { | |
2134 | sub->user_data_len = user_data->values[0].unknown.length; | |
2135 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
07623986 | 2136 | (size_t)sub->user_data_len); |
52f6f666 | 2137 | } |
ef416fc2 | 2138 | |
52f6f666 MS |
2139 | ippAddSeparator(con->response); |
2140 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
2141 | "notify-subscription-id", sub->id); | |
f8b3a85b MS |
2142 | |
2143 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
2144 | sub->id, job->id); | |
ef416fc2 | 2145 | } |
2146 | ||
b423cd4c | 2147 | if (attr) |
2148 | attr = attr->next; | |
ef416fc2 | 2149 | } |
2150 | ||
3dfe78b3 | 2151 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
b423cd4c | 2152 | |
ef416fc2 | 2153 | /* |
b423cd4c | 2154 | * Remove all of the subscription attributes from the job request... |
1f0275e3 MS |
2155 | * |
2156 | * TODO: Optimize this since subscription groups have to come at the | |
2157 | * end of the request... | |
ef416fc2 | 2158 | */ |
2159 | ||
b423cd4c | 2160 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) |
ef416fc2 | 2161 | { |
b423cd4c | 2162 | next = attr->next; |
ef416fc2 | 2163 | |
b423cd4c | 2164 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || |
2165 | attr->group_tag == IPP_TAG_ZERO) | |
2166 | { | |
2167 | /* | |
2168 | * Free and remove this attribute... | |
2169 | */ | |
ef416fc2 | 2170 | |
a2326b5b | 2171 | ippDeleteAttribute(NULL, attr); |
ef416fc2 | 2172 | |
b423cd4c | 2173 | if (prev) |
2174 | prev->next = next; | |
2175 | else | |
2176 | job->attrs->attrs = next; | |
2177 | } | |
2178 | else | |
2179 | prev = attr; | |
ef416fc2 | 2180 | } |
2181 | ||
b423cd4c | 2182 | job->attrs->last = prev; |
2183 | job->attrs->current = prev; | |
2184 | } | |
ef416fc2 | 2185 | |
ef416fc2 | 2186 | |
b423cd4c | 2187 | /* |
2188 | * 'add_job_uuid()' - Add job-uuid attribute to a job. | |
2189 | * | |
2190 | * See RFC 4122 for the definition of UUIDs and the format. | |
2191 | */ | |
2192 | ||
2193 | static void | |
321d8d57 | 2194 | add_job_uuid(cupsd_job_t *job) /* I - Job */ |
b423cd4c | 2195 | { |
82f97232 | 2196 | char uuid[64]; /* job-uuid string */ |
ef416fc2 | 2197 | |
ef416fc2 | 2198 | |
2199 | /* | |
82f97232 | 2200 | * Add a job-uuid attribute if none exists... |
ef416fc2 | 2201 | */ |
2202 | ||
82f97232 MS |
2203 | if (!ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) |
2204 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, | |
7855ab56 MS |
2205 | httpAssembleUUID(ServerName, RemotePort, job->dest, job->id, |
2206 | uuid, sizeof(uuid))); | |
ef416fc2 | 2207 | } |
2208 | ||
2209 | ||
2210 | /* | |
b423cd4c | 2211 | * 'add_printer()' - Add a printer to the system. |
ef416fc2 | 2212 | */ |
2213 | ||
2214 | static void | |
b423cd4c | 2215 | add_printer(cupsd_client_t *con, /* I - Client connection */ |
2216 | ipp_attribute_t *uri) /* I - URI of printer */ | |
ef416fc2 | 2217 | { |
2218 | http_status_t status; /* Policy status */ | |
b423cd4c | 2219 | int i; /* Looping var */ |
09a101d6 | 2220 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2221 | username[HTTP_MAX_URI], /* Username portion of URI */ |
ef416fc2 | 2222 | host[HTTP_MAX_URI], /* Host portion of URI */ |
2223 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2224 | int port; /* Port portion of URI */ | |
b423cd4c | 2225 | cupsd_printer_t *printer; /* Printer/class */ |
2226 | ipp_attribute_t *attr; /* Printer attribute */ | |
2227 | cups_file_t *fp; /* Script/PPD file */ | |
2228 | char line[1024]; /* Line from file... */ | |
2229 | char srcfile[1024], /* Source Script/PPD file */ | |
2230 | dstfile[1024]; /* Destination Script/PPD file */ | |
2231 | int modify; /* Non-zero if we are modifying */ | |
e67e2f9e | 2232 | int changed_driver, /* Changed the PPD? */ |
b226ab99 MS |
2233 | need_restart_job, /* Need to restart job? */ |
2234 | set_device_uri, /* Did we set the device URI? */ | |
09a101d6 | 2235 | set_port_monitor; /* Did we set the port monitor? */ |
ef416fc2 | 2236 | |
2237 | ||
b423cd4c | 2238 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, |
996acce8 | 2239 | con->number, uri->values[0].string.text); |
ef416fc2 | 2240 | |
2241 | /* | |
b423cd4c | 2242 | * Do we have a valid URI? |
ef416fc2 | 2243 | */ |
2244 | ||
09a101d6 | 2245 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
2246 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 2247 | sizeof(host), &port, resource, sizeof(resource)); |
2248 | ||
2249 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
ef416fc2 | 2250 | { |
b423cd4c | 2251 | /* |
2252 | * No, return an error... | |
2253 | */ | |
2254 | ||
ef416fc2 | 2255 | send_ipp_status(con, IPP_BAD_REQUEST, |
b423cd4c | 2256 | _("The printer-uri must be of the form " |
2257 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
ef416fc2 | 2258 | return; |
2259 | } | |
2260 | ||
2261 | /* | |
b423cd4c | 2262 | * Do we have a valid printer name? |
ef416fc2 | 2263 | */ |
2264 | ||
b423cd4c | 2265 | if (!validate_name(resource + 10)) |
ef416fc2 | 2266 | { |
b423cd4c | 2267 | /* |
2268 | * No, return an error... | |
2269 | */ | |
2270 | ||
2271 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2272 | _("The printer-uri \"%s\" contains invalid characters."), | |
2273 | uri->values[0].string.text); | |
2274 | return; | |
ef416fc2 | 2275 | } |
ef416fc2 | 2276 | |
ef416fc2 | 2277 | /* |
b423cd4c | 2278 | * See if the printer already exists; if not, create a new printer... |
2279 | */ | |
ef416fc2 | 2280 | |
b423cd4c | 2281 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) |
ef416fc2 | 2282 | { |
2283 | /* | |
b423cd4c | 2284 | * Printer doesn't exist; see if we have a class of the same name... |
ef416fc2 | 2285 | */ |
2286 | ||
a2326b5b | 2287 | if ((printer = cupsdFindClass(resource + 10)) != NULL) |
ef416fc2 | 2288 | { |
b423cd4c | 2289 | /* |
2290 | * Yes, return an error... | |
2291 | */ | |
ef416fc2 | 2292 | |
b423cd4c | 2293 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
84315f46 | 2294 | _("A class named \"%s\" already exists."), |
b423cd4c | 2295 | resource + 10); |
ef416fc2 | 2296 | return; |
2297 | } | |
2298 | ||
2299 | /* | |
2e4ff8af | 2300 | * No, check the default policy then add the printer... |
ef416fc2 | 2301 | */ |
2302 | ||
2e4ff8af MS |
2303 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2304 | { | |
2305 | send_http_error(con, status, NULL); | |
2306 | return; | |
2307 | } | |
2308 | ||
b423cd4c | 2309 | printer = cupsdAddPrinter(resource + 10); |
2310 | modify = 0; | |
409f5497 MS |
2311 | |
2312 | printer->printer_id = NextPrinterId ++; | |
ef416fc2 | 2313 | } |
2e4ff8af MS |
2314 | else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
2315 | NULL)) != HTTP_OK) | |
2316 | { | |
2fb76298 | 2317 | send_http_error(con, status, printer); |
2e4ff8af MS |
2318 | return; |
2319 | } | |
b423cd4c | 2320 | else |
2321 | modify = 1; | |
ef416fc2 | 2322 | |
b423cd4c | 2323 | /* |
2324 | * Look for attributes and copy them over as needed... | |
2325 | */ | |
ef416fc2 | 2326 | |
b226ab99 | 2327 | changed_driver = 0; |
b423cd4c | 2328 | need_restart_job = 0; |
ef416fc2 | 2329 | |
7ae00c35 MS |
2330 | if ((attr = ippFindAttribute(con->request, "printer-is-temporary", IPP_TAG_BOOLEAN)) != NULL) |
2331 | printer->temporary = ippGetBoolean(attr, 0); | |
2332 | ||
b423cd4c | 2333 | if ((attr = ippFindAttribute(con->request, "printer-location", |
2334 | IPP_TAG_TEXT)) != NULL) | |
2335 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
ef416fc2 | 2336 | |
9b4bd602 MS |
2337 | if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4)) |
2338 | cupsdSetString(&printer->geo_location, attr->values[0].string.text); | |
2339 | ||
2340 | if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL) | |
2341 | cupsdSetString(&printer->organization, attr->values[0].string.text); | |
2342 | ||
2343 | if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL) | |
2344 | cupsdSetString(&printer->organizational_unit, attr->values[0].string.text); | |
2345 | ||
b423cd4c | 2346 | if ((attr = ippFindAttribute(con->request, "printer-info", |
2347 | IPP_TAG_TEXT)) != NULL) | |
2348 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
ef416fc2 | 2349 | |
09a101d6 | 2350 | set_device_uri = 0; |
2351 | ||
b423cd4c | 2352 | if ((attr = ippFindAttribute(con->request, "device-uri", |
2353 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 2354 | { |
2355 | /* | |
b423cd4c | 2356 | * Do we have a valid device URI? |
ef416fc2 | 2357 | */ |
2358 | ||
0af14961 MS |
2359 | http_uri_status_t uri_status; /* URI separation status */ |
2360 | char old_device_uri[1024]; | |
2361 | /* Old device URI */ | |
2fb76298 | 2362 | |
b423cd4c | 2363 | need_restart_job = 1; |
ef416fc2 | 2364 | |
2fb76298 MS |
2365 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, |
2366 | attr->values[0].string.text, | |
2367 | scheme, sizeof(scheme), | |
2368 | username, sizeof(username), | |
2369 | host, sizeof(host), &port, | |
2370 | resource, sizeof(resource)); | |
2371 | ||
f0b589f7 | 2372 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s device-uri: %s", printer->name, httpURIStatusString(uri_status)); |
6961465f | 2373 | |
2fb76298 MS |
2374 | if (uri_status < HTTP_URI_OK) |
2375 | { | |
84315f46 | 2376 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."), |
2fb76298 | 2377 | attr->values[0].string.text); |
f0b589f7 MS |
2378 | if (!modify) |
2379 | cupsdDeletePrinter(printer, 0); | |
2380 | ||
2fb76298 MS |
2381 | return; |
2382 | } | |
b423cd4c | 2383 | |
09a101d6 | 2384 | if (!strcmp(scheme, "file")) |
ef416fc2 | 2385 | { |
2386 | /* | |
b423cd4c | 2387 | * See if the administrator has enabled file devices... |
ef416fc2 | 2388 | */ |
2389 | ||
b423cd4c | 2390 | if (!FileDevice && strcmp(resource, "/dev/null")) |
ef416fc2 | 2391 | { |
2392 | /* | |
b423cd4c | 2393 | * File devices are disabled and the URL is not file:/dev/null... |
ef416fc2 | 2394 | */ |
2395 | ||
b423cd4c | 2396 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
84315f46 | 2397 | _("File device URIs have been disabled. " |
b423cd4c | 2398 | "To enable, see the FileDevice directive in " |
cb7f98ee | 2399 | "\"%s/cups-files.conf\"."), |
b423cd4c | 2400 | ServerRoot); |
f0b589f7 MS |
2401 | if (!modify) |
2402 | cupsdDeletePrinter(printer, 0); | |
2403 | ||
ef416fc2 | 2404 | return; |
2405 | } | |
b423cd4c | 2406 | } |
2407 | else | |
2408 | { | |
ef416fc2 | 2409 | /* |
b423cd4c | 2410 | * See if the backend exists and is executable... |
ef416fc2 | 2411 | */ |
2412 | ||
09a101d6 | 2413 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme); |
b423cd4c | 2414 | if (access(srcfile, X_OK)) |
ef416fc2 | 2415 | { |
2416 | /* | |
b423cd4c | 2417 | * Could not find device in list! |
ef416fc2 | 2418 | */ |
ef416fc2 | 2419 | |
84315f46 MS |
2420 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2421 | _("Bad device-uri scheme \"%s\"."), scheme); | |
f0b589f7 MS |
2422 | if (!modify) |
2423 | cupsdDeletePrinter(printer, 0); | |
2424 | ||
b423cd4c | 2425 | return; |
ef416fc2 | 2426 | } |
2427 | } | |
ef416fc2 | 2428 | |
0af14961 MS |
2429 | if (printer->sanitized_device_uri) |
2430 | strlcpy(old_device_uri, printer->sanitized_device_uri, | |
2431 | sizeof(old_device_uri)); | |
2432 | else | |
2433 | old_device_uri[0] = '\0'; | |
2434 | ||
2435 | cupsdSetDeviceURI(printer, attr->values[0].string.text); | |
2436 | ||
b423cd4c | 2437 | cupsdLogMessage(CUPSD_LOG_INFO, |
2438 | "Setting %s device-uri to \"%s\" (was \"%s\".)", | |
0af14961 MS |
2439 | printer->name, printer->sanitized_device_uri, |
2440 | old_device_uri); | |
ef416fc2 | 2441 | |
09a101d6 | 2442 | set_device_uri = 1; |
ef416fc2 | 2443 | } |
2444 | ||
09a101d6 | 2445 | set_port_monitor = 0; |
2446 | ||
b423cd4c | 2447 | if ((attr = ippFindAttribute(con->request, "port-monitor", |
09a101d6 | 2448 | IPP_TAG_NAME)) != NULL) |
ef416fc2 | 2449 | { |
b423cd4c | 2450 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ |
ef416fc2 | 2451 | |
ef416fc2 | 2452 | |
b423cd4c | 2453 | need_restart_job = 1; |
ef416fc2 | 2454 | |
e4572d57 | 2455 | supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported", |
a41f09e2 | 2456 | IPP_TAG_NAME); |
080811b1 MS |
2457 | if (supported) |
2458 | { | |
2459 | for (i = 0; i < supported->num_values; i ++) | |
2460 | if (!strcmp(supported->values[i].string.text, | |
2461 | attr->values[0].string.text)) | |
2462 | break; | |
2463 | } | |
ef416fc2 | 2464 | |
080811b1 | 2465 | if (!supported || i >= supported->num_values) |
b423cd4c | 2466 | { |
84315f46 | 2467 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."), |
b423cd4c | 2468 | attr->values[0].string.text); |
f0b589f7 MS |
2469 | if (!modify) |
2470 | cupsdDeletePrinter(printer, 0); | |
2471 | ||
b423cd4c | 2472 | return; |
2473 | } | |
ef416fc2 | 2474 | |
b423cd4c | 2475 | cupsdLogMessage(CUPSD_LOG_INFO, |
2476 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2477 | printer->name, attr->values[0].string.text, | |
09a101d6 | 2478 | printer->port_monitor ? printer->port_monitor : "none"); |
ef416fc2 | 2479 | |
b423cd4c | 2480 | if (strcmp(attr->values[0].string.text, "none")) |
2481 | cupsdSetString(&printer->port_monitor, attr->values[0].string.text); | |
2482 | else | |
2483 | cupsdClearString(&printer->port_monitor); | |
09a101d6 | 2484 | |
2485 | set_port_monitor = 1; | |
b423cd4c | 2486 | } |
ef416fc2 | 2487 | |
b423cd4c | 2488 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
f8b3a85b MS |
2489 | IPP_TAG_BOOLEAN)) != NULL && |
2490 | attr->values[0].boolean != printer->accepting) | |
b423cd4c | 2491 | { |
2492 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2493 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
2494 | printer->name, attr->values[0].boolean, printer->accepting); | |
ef416fc2 | 2495 | |
b423cd4c | 2496 | printer->accepting = attr->values[0].boolean; |
f8b3a85b MS |
2497 | |
2498 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, | |
2499 | "%s accepting jobs.", | |
2500 | printer->accepting ? "Now" : "No longer"); | |
b423cd4c | 2501 | } |
bd7854cb | 2502 | |
7ae00c35 | 2503 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL) |
b423cd4c | 2504 | { |
7ae00c35 | 2505 | if (ippGetBoolean(attr, 0) && |
07ed0e9a MS |
2506 | printer->num_auth_info_required == 1 && |
2507 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
2508 | { | |
2509 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2510 | _("Cannot share a remote Kerberized printer.")); | |
f0b589f7 MS |
2511 | if (!modify) |
2512 | cupsdDeletePrinter(printer, 0); | |
2513 | ||
07ed0e9a MS |
2514 | return; |
2515 | } | |
2516 | ||
7dc6f91d MS |
2517 | if (printer->type & CUPS_PRINTER_REMOTE) |
2518 | { | |
2519 | /* | |
2520 | * Cannot re-share remote printers. | |
2521 | */ | |
2522 | ||
2523 | send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues.")); | |
f0b589f7 MS |
2524 | if (!modify) |
2525 | cupsdDeletePrinter(printer, 0); | |
2526 | ||
7dc6f91d MS |
2527 | return; |
2528 | } | |
2529 | ||
7ae00c35 | 2530 | if (printer->shared && !ippGetBoolean(attr, 0)) |
f7deaa1a | 2531 | cupsdDeregisterPrinter(printer, 1); |
bd7854cb | 2532 | |
b423cd4c | 2533 | cupsdLogMessage(CUPSD_LOG_INFO, |
2534 | "Setting %s printer-is-shared to %d (was %d.)", | |
2535 | printer->name, attr->values[0].boolean, printer->shared); | |
bd7854cb | 2536 | |
7ae00c35 MS |
2537 | printer->shared = ippGetBoolean(attr, 0); |
2538 | if (printer->shared && printer->temporary) | |
2539 | printer->temporary = 0; | |
b423cd4c | 2540 | } |
bd7854cb | 2541 | |
b423cd4c | 2542 | if ((attr = ippFindAttribute(con->request, "printer-state", |
2543 | IPP_TAG_ENUM)) != NULL) | |
bd7854cb | 2544 | { |
b423cd4c | 2545 | if (attr->values[0].integer != IPP_PRINTER_IDLE && |
2546 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
2547 | { | |
84315f46 | 2548 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."), |
b423cd4c | 2549 | attr->values[0].integer); |
f0b589f7 MS |
2550 | if (!modify) |
2551 | cupsdDeletePrinter(printer, 0); | |
2552 | ||
b423cd4c | 2553 | return; |
2554 | } | |
bd7854cb | 2555 | |
bc44d920 | 2556 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
2557 | printer->name, attr->values[0].integer, printer->state); | |
b423cd4c | 2558 | |
2559 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
2560 | cupsdStopPrinter(printer, 0); | |
2561 | else | |
2562 | { | |
2563 | need_restart_job = 1; | |
2564 | cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0); | |
2565 | } | |
2566 | } | |
745129be | 2567 | |
b423cd4c | 2568 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
2569 | IPP_TAG_TEXT)) != NULL) | |
2570 | { | |
2571 | strlcpy(printer->state_message, attr->values[0].string.text, | |
2572 | sizeof(printer->state_message)); | |
f8b3a85b MS |
2573 | |
2574 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, "%s", | |
2575 | printer->state_message); | |
bd7854cb | 2576 | } |
2577 | ||
1f6f3dbc MS |
2578 | if ((attr = ippFindAttribute(con->request, "printer-state-reasons", |
2579 | IPP_TAG_KEYWORD)) != NULL) | |
2580 | { | |
2581 | if (attr->num_values > | |
2582 | (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0]))) | |
2583 | { | |
2584 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 2585 | _("Too many printer-state-reasons values (%d > %d)."), |
1f6f3dbc MS |
2586 | attr->num_values, |
2587 | (int)(sizeof(printer->reasons) / | |
2588 | sizeof(printer->reasons[0]))); | |
f0b589f7 MS |
2589 | if (!modify) |
2590 | cupsdDeletePrinter(printer, 0); | |
2591 | ||
1f6f3dbc MS |
2592 | return; |
2593 | } | |
2594 | ||
2595 | for (i = 0; i < printer->num_reasons; i ++) | |
2596 | _cupsStrFree(printer->reasons[i]); | |
2597 | ||
2598 | printer->num_reasons = 0; | |
2599 | for (i = 0; i < attr->num_values; i ++) | |
2600 | { | |
2601 | if (!strcmp(attr->values[i].string.text, "none")) | |
2602 | continue; | |
2603 | ||
2604 | printer->reasons[printer->num_reasons] = | |
426c6a59 | 2605 | _cupsStrRetain(attr->values[i].string.text); |
745129be | 2606 | printer->num_reasons ++; |
1f6f3dbc | 2607 | |
745129be | 2608 | if (!strcmp(attr->values[i].string.text, "paused") && |
1f6f3dbc MS |
2609 | printer->state != IPP_PRINTER_STOPPED) |
2610 | { | |
2611 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2612 | "Setting %s printer-state to %d (was %d.)", | |
2613 | printer->name, IPP_PRINTER_STOPPED, printer->state); | |
2614 | cupsdStopPrinter(printer, 0); | |
2615 | } | |
1f6f3dbc MS |
2616 | } |
2617 | ||
2618 | if (PrintcapFormat == PRINTCAP_PLIST) | |
2619 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); | |
f8b3a85b MS |
2620 | |
2621 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, | |
2622 | "Printer \"%s\" state changed.", printer->name); | |
1f6f3dbc MS |
2623 | } |
2624 | ||
f0b589f7 MS |
2625 | if (!set_printer_defaults(con, printer)) |
2626 | { | |
2627 | if (!modify) | |
2628 | cupsdDeletePrinter(printer, 0); | |
2629 | ||
2630 | return; | |
2631 | } | |
b423cd4c | 2632 | |
09a101d6 | 2633 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
2634 | IPP_TAG_KEYWORD)) != NULL) | |
2635 | cupsdSetAuthInfoRequired(printer, NULL, attr); | |
2636 | ||
bd7854cb | 2637 | /* |
b423cd4c | 2638 | * See if we have all required attributes... |
bd7854cb | 2639 | */ |
2640 | ||
b423cd4c | 2641 | if (!printer->device_uri) |
2642 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
bd7854cb | 2643 | |
2644 | /* | |
e67e2f9e | 2645 | * See if we have a PPD file attached to the request... |
bd7854cb | 2646 | */ |
2647 | ||
b423cd4c | 2648 | if (con->filename) |
2649 | { | |
2650 | need_restart_job = 1; | |
b226ab99 | 2651 | changed_driver = 1; |
bd7854cb | 2652 | |
b423cd4c | 2653 | strlcpy(srcfile, con->filename, sizeof(srcfile)); |
ef416fc2 | 2654 | |
b423cd4c | 2655 | if ((fp = cupsFileOpen(srcfile, "rb"))) |
2656 | { | |
2657 | /* | |
2658 | * Yes; get the first line from it... | |
2659 | */ | |
ef416fc2 | 2660 | |
b423cd4c | 2661 | line[0] = '\0'; |
2662 | cupsFileGets(fp, line, sizeof(line)); | |
2663 | cupsFileClose(fp); | |
ef416fc2 | 2664 | |
b423cd4c | 2665 | /* |
2666 | * Then see what kind of file it is... | |
2667 | */ | |
ef416fc2 | 2668 | |
e67e2f9e | 2669 | if (strncmp(line, "*PPD-Adobe", 10)) |
b423cd4c | 2670 | { |
e67e2f9e | 2671 | send_ipp_status(con, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED, _("Bad PPD file.")); |
f0b589f7 MS |
2672 | if (!modify) |
2673 | cupsdDeletePrinter(printer, 0); | |
2674 | ||
e67e2f9e | 2675 | return; |
b423cd4c | 2676 | } |
ef416fc2 | 2677 | |
b423cd4c | 2678 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2679 | printer->name); | |
ef416fc2 | 2680 | |
e67e2f9e MS |
2681 | /* |
2682 | * The new file is a PPD file, so move the file over to the ppd | |
2683 | * directory... | |
2684 | */ | |
568fa3fa | 2685 | |
e67e2f9e | 2686 | if (copy_file(srcfile, dstfile, ConfigFilePerm)) |
b423cd4c | 2687 | { |
e67e2f9e | 2688 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file - %s"), strerror(errno)); |
f0b589f7 MS |
2689 | if (!modify) |
2690 | cupsdDeletePrinter(printer, 0); | |
2691 | ||
e67e2f9e | 2692 | return; |
b423cd4c | 2693 | } |
e67e2f9e MS |
2694 | |
2695 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully"); | |
b423cd4c | 2696 | } |
2697 | } | |
e67e2f9e | 2698 | else if ((attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL) |
ef416fc2 | 2699 | { |
e67e2f9e MS |
2700 | const char *ppd_name = ippGetString(attr, 0, NULL); |
2701 | /* ppd-name value */ | |
2702 | ||
b423cd4c | 2703 | need_restart_job = 1; |
b226ab99 | 2704 | changed_driver = 1; |
ef416fc2 | 2705 | |
e67e2f9e | 2706 | if (!strcmp(ppd_name, "raw")) |
ef416fc2 | 2707 | { |
b423cd4c | 2708 | /* |
e67e2f9e | 2709 | * Raw driver, remove any existing PPD file. |
b423cd4c | 2710 | */ |
ef416fc2 | 2711 | |
e67e2f9e | 2712 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name); |
b423cd4c | 2713 | unlink(dstfile); |
2714 | } | |
e67e2f9e MS |
2715 | else if (strstr(ppd_name, "../")) |
2716 | { | |
2717 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value.")); | |
f0b589f7 MS |
2718 | if (!modify) |
2719 | cupsdDeletePrinter(printer, 0); | |
2720 | ||
e67e2f9e MS |
2721 | return; |
2722 | } | |
b423cd4c | 2723 | else |
ef416fc2 | 2724 | { |
b423cd4c | 2725 | /* |
2726 | * PPD model file... | |
2727 | */ | |
2728 | ||
e67e2f9e | 2729 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name); |
b423cd4c | 2730 | |
e67e2f9e | 2731 | if (copy_model(con, ppd_name, dstfile)) |
b423cd4c | 2732 | { |
84315f46 | 2733 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file.")); |
f0b589f7 MS |
2734 | if (!modify) |
2735 | cupsdDeletePrinter(printer, 0); | |
2736 | ||
b423cd4c | 2737 | return; |
2738 | } | |
568fa3fa | 2739 | |
e67e2f9e | 2740 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully"); |
b226ab99 MS |
2741 | } |
2742 | } | |
2743 | ||
2744 | if (changed_driver) | |
2745 | { | |
2746 | /* | |
e67e2f9e MS |
2747 | * If we changed the PPD, then remove the printer's cache file and clear the |
2748 | * printer-state-reasons... | |
b226ab99 MS |
2749 | */ |
2750 | ||
2751 | char cache_name[1024]; /* Cache filename for printer attrs */ | |
2752 | ||
f0b589f7 | 2753 | snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, printer->name); |
54afec33 MS |
2754 | unlink(cache_name); |
2755 | ||
c7017ecc MS |
2756 | cupsdSetPrinterReasons(printer, "none"); |
2757 | ||
b226ab99 MS |
2758 | /* |
2759 | * (Re)register color profiles... | |
2760 | */ | |
568fa3fa | 2761 | |
a29fd7dd | 2762 | cupsdRegisterColor(printer); |
ef416fc2 | 2763 | } |
2764 | ||
09a101d6 | 2765 | /* |
2766 | * If we set the device URI but not the port monitor, check which port | |
2767 | * monitor to use by default... | |
2768 | */ | |
2769 | ||
2770 | if (set_device_uri && !set_port_monitor) | |
2771 | { | |
2772 | ppd_file_t *ppd; /* PPD file */ | |
2773 | ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */ | |
2774 | ||
2775 | ||
2776 | httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme, | |
2777 | sizeof(scheme), username, sizeof(username), host, | |
2778 | sizeof(host), &port, resource, sizeof(resource)); | |
2779 | ||
2780 | snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, | |
2781 | printer->name); | |
9c80ffa2 | 2782 | if ((ppd = _ppdOpenFile(srcfile, _PPD_LOCALIZATION_NONE)) != NULL) |
09a101d6 | 2783 | { |
2784 | for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL); | |
2785 | ppdattr; | |
2786 | ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL)) | |
2787 | if (!strcmp(scheme, ppdattr->spec)) | |
2788 | { | |
2789 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2790 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2791 | printer->name, ppdattr->value, | |
bc44d920 | 2792 | printer->port_monitor ? printer->port_monitor |
2793 | : "none"); | |
09a101d6 | 2794 | |
2795 | if (strcmp(ppdattr->value, "none")) | |
2796 | cupsdSetString(&printer->port_monitor, ppdattr->value); | |
2797 | else | |
2798 | cupsdClearString(&printer->port_monitor); | |
2799 | ||
2800 | break; | |
2801 | } | |
2802 | ||
2803 | ppdClose(ppd); | |
2804 | } | |
2805 | } | |
2806 | ||
9b4bd602 MS |
2807 | printer->config_time = time(NULL); |
2808 | ||
ef416fc2 | 2809 | /* |
b423cd4c | 2810 | * Update the printer attributes and return... |
ef416fc2 | 2811 | */ |
2812 | ||
7ae00c35 | 2813 | if (!printer->temporary) |
409f5497 MS |
2814 | { |
2815 | if (!printer->printer_id) | |
2816 | printer->printer_id = NextPrinterId ++; | |
2817 | ||
7ae00c35 | 2818 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
409f5497 MS |
2819 | } |
2820 | ||
2821 | cupsdSetPrinterAttrs(printer); | |
ef416fc2 | 2822 | |
b423cd4c | 2823 | if (need_restart_job && printer->job) |
ef416fc2 | 2824 | { |
b423cd4c | 2825 | /* |
b9faaae1 | 2826 | * Restart the current job... |
b423cd4c | 2827 | */ |
ef416fc2 | 2828 | |
b9faaae1 MS |
2829 | cupsdSetJobState(printer->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE, |
2830 | "Job restarted because the printer was modified."); | |
ef416fc2 | 2831 | } |
2832 | ||
3dfe78b3 | 2833 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
b423cd4c | 2834 | |
2835 | if (modify) | |
ef416fc2 | 2836 | { |
c7017ecc | 2837 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, |
f8b3a85b MS |
2838 | printer, NULL, "Printer \"%s\" modified by \"%s\".", |
2839 | printer->name, get_username(con)); | |
ef416fc2 | 2840 | |
b423cd4c | 2841 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", |
2842 | printer->name, get_username(con)); | |
ef416fc2 | 2843 | } |
b423cd4c | 2844 | else |
2845 | { | |
c7017ecc | 2846 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, |
f8b3a85b MS |
2847 | printer, NULL, "New printer \"%s\" added by \"%s\".", |
2848 | printer->name, get_username(con)); | |
ef416fc2 | 2849 | |
b423cd4c | 2850 | cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".", |
2851 | printer->name, get_username(con)); | |
2852 | } | |
2853 | ||
2854 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 2855 | } |
2856 | ||
2857 | ||
2858 | /* | |
b423cd4c | 2859 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute |
2860 | * based upon the printer state... | |
ef416fc2 | 2861 | */ |
2862 | ||
b423cd4c | 2863 | static void |
2864 | add_printer_state_reasons( | |
2865 | cupsd_client_t *con, /* I - Client connection */ | |
2866 | cupsd_printer_t *p) /* I - Printer info */ | |
ef416fc2 | 2867 | { |
b423cd4c | 2868 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2869 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
996acce8 | 2870 | con, con->number, p, p->name); |
ef416fc2 | 2871 | |
b423cd4c | 2872 | if (p->num_reasons == 0) |
2873 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
745129be | 2874 | "printer-state-reasons", NULL, "none"); |
b423cd4c | 2875 | else |
2876 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2877 | "printer-state-reasons", p->num_reasons, NULL, | |
2878 | (const char * const *)p->reasons); | |
2879 | } | |
ef416fc2 | 2880 | |
ef416fc2 | 2881 | |
b423cd4c | 2882 | /* |
2883 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2884 | * the specified printer or class. | |
2885 | */ | |
ef416fc2 | 2886 | |
b423cd4c | 2887 | static void |
2888 | add_queued_job_count( | |
2889 | cupsd_client_t *con, /* I - Client connection */ | |
2890 | cupsd_printer_t *p) /* I - Printer or class */ | |
2891 | { | |
2892 | int count; /* Number of jobs on destination */ | |
ef416fc2 | 2893 | |
ef416fc2 | 2894 | |
b423cd4c | 2895 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", |
996acce8 | 2896 | con, con->number, p, p->name); |
ef416fc2 | 2897 | |
b423cd4c | 2898 | count = cupsdGetPrinterJobCount(p->name); |
ef416fc2 | 2899 | |
b423cd4c | 2900 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, |
2901 | "queued-job-count", count); | |
2902 | } | |
ef416fc2 | 2903 | |
ef416fc2 | 2904 | |
b423cd4c | 2905 | /* |
2906 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
2907 | */ | |
ef416fc2 | 2908 | |
b423cd4c | 2909 | static void |
2910 | apply_printer_defaults( | |
2911 | cupsd_printer_t *printer, /* I - Printer */ | |
2912 | cupsd_job_t *job) /* I - Job */ | |
2913 | { | |
2914 | int i, /* Looping var */ | |
2915 | num_options; /* Number of default options */ | |
2916 | cups_option_t *options, /* Default options */ | |
f7deaa1a | 2917 | *option; /* Current option */ |
ef416fc2 | 2918 | |
ef416fc2 | 2919 | |
d2123aee MS |
2920 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Applying default options..."); |
2921 | ||
b423cd4c | 2922 | /* |
2923 | * Collect all of the default options and add the missing ones to the | |
2924 | * job object... | |
2925 | */ | |
ef416fc2 | 2926 | |
1f0275e3 MS |
2927 | for (i = printer->num_options, num_options = 0, options = NULL, |
2928 | option = printer->options; | |
b423cd4c | 2929 | i > 0; |
2930 | i --, option ++) | |
2931 | if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO)) | |
2932 | { | |
40cc612a MS |
2933 | if (!strcmp(option->name, "print-quality") && ippFindAttribute(job->attrs, "cupsPrintQuality", IPP_TAG_NAME)) |
2934 | continue; /* Don't override cupsPrintQuality */ | |
2935 | ||
d2123aee MS |
2936 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Adding default %s=%s", option->name, option->value); |
2937 | ||
40cc612a | 2938 | num_options = cupsAddOption(option->name, option->value, num_options, &options); |
b423cd4c | 2939 | } |
ef416fc2 | 2940 | |
b423cd4c | 2941 | /* |
2942 | * Encode these options as attributes in the job object... | |
2943 | */ | |
ef416fc2 | 2944 | |
b423cd4c | 2945 | cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB); |
2946 | cupsFreeOptions(num_options, options); | |
2947 | } | |
2948 | ||
2949 | ||
2950 | /* | |
2951 | * 'authenticate_job()' - Set job authentication info. | |
2952 | */ | |
2953 | ||
2954 | static void | |
2955 | authenticate_job(cupsd_client_t *con, /* I - Client connection */ | |
2956 | ipp_attribute_t *uri) /* I - Job URI */ | |
2957 | { | |
f7deaa1a | 2958 | ipp_attribute_t *attr, /* job-id attribute */ |
2959 | *auth_info; /* auth-info attribute */ | |
b423cd4c | 2960 | int jobid; /* Job ID */ |
2961 | cupsd_job_t *job; /* Current job */ | |
61cf44e2 | 2962 | char scheme[HTTP_MAX_URI], |
b423cd4c | 2963 | /* Method portion of URI */ |
2964 | username[HTTP_MAX_URI], | |
2965 | /* Username portion of URI */ | |
2966 | host[HTTP_MAX_URI], | |
2967 | /* Host portion of URI */ | |
2968 | resource[HTTP_MAX_URI]; | |
2969 | /* Resource portion of URI */ | |
2970 | int port; /* Port portion of URI */ | |
ef416fc2 | 2971 | |
ef416fc2 | 2972 | |
b423cd4c | 2973 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", |
996acce8 | 2974 | con, con->number, uri->values[0].string.text); |
ef416fc2 | 2975 | |
b423cd4c | 2976 | /* |
2977 | * Start with "everything is OK" status... | |
2978 | */ | |
ef416fc2 | 2979 | |
b423cd4c | 2980 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 2981 | |
b423cd4c | 2982 | /* |
2983 | * See if we have a job URI or a printer URI... | |
2984 | */ | |
ef416fc2 | 2985 | |
b423cd4c | 2986 | if (!strcmp(uri->name, "printer-uri")) |
2987 | { | |
2988 | /* | |
2989 | * Got a printer URI; see if we also have a job-id attribute... | |
2990 | */ | |
ef416fc2 | 2991 | |
b423cd4c | 2992 | if ((attr = ippFindAttribute(con->request, "job-id", |
2993 | IPP_TAG_INTEGER)) == NULL) | |
2994 | { | |
2995 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 2996 | _("Got a printer-uri attribute but no job-id.")); |
b423cd4c | 2997 | return; |
2998 | } | |
2999 | ||
3000 | jobid = attr->values[0].integer; | |
3001 | } | |
3002 | else | |
3003 | { | |
3004 | /* | |
3005 | * Got a job URI; parse it to get the job ID... | |
3006 | */ | |
ef416fc2 | 3007 | |
61cf44e2 MS |
3008 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
3009 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 3010 | sizeof(host), &port, resource, sizeof(resource)); |
f7deaa1a | 3011 | |
b423cd4c | 3012 | if (strncmp(resource, "/jobs/", 6)) |
3013 | { | |
3014 | /* | |
3015 | * Not a valid URI! | |
3016 | */ | |
ef416fc2 | 3017 | |
84315f46 | 3018 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
b423cd4c | 3019 | uri->values[0].string.text); |
3020 | return; | |
3021 | } | |
ef416fc2 | 3022 | |
b423cd4c | 3023 | jobid = atoi(resource + 6); |
3024 | } | |
ef416fc2 | 3025 | |
b423cd4c | 3026 | /* |
3027 | * See if the job exists... | |
3028 | */ | |
ef416fc2 | 3029 | |
b423cd4c | 3030 | if ((job = cupsdFindJob(jobid)) == NULL) |
3031 | { | |
3032 | /* | |
3033 | * Nope - return a "not found" error... | |
3034 | */ | |
ef416fc2 | 3035 | |
84315f46 | 3036 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
ef416fc2 | 3037 | return; |
b423cd4c | 3038 | } |
ef416fc2 | 3039 | |
b423cd4c | 3040 | /* |
3041 | * See if the job has been completed... | |
3042 | */ | |
3043 | ||
3044 | if (job->state_value != IPP_JOB_HELD) | |
ef416fc2 | 3045 | { |
3046 | /* | |
b423cd4c | 3047 | * Return a "not-possible" error... |
ef416fc2 | 3048 | */ |
3049 | ||
b423cd4c | 3050 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
84315f46 | 3051 | _("Job #%d is not held for authentication."), |
b423cd4c | 3052 | jobid); |
3053 | return; | |
3054 | } | |
ef416fc2 | 3055 | |
b423cd4c | 3056 | /* |
3057 | * See if we have already authenticated... | |
3058 | */ | |
3059 | ||
f7deaa1a | 3060 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
3061 | ||
3062 | if (!con->username[0] && !auth_info) | |
b423cd4c | 3063 | { |
db1f069b MS |
3064 | cupsd_printer_t *printer; /* Job destination */ |
3065 | ||
db1f069b MS |
3066 | /* |
3067 | * No auth data. If we need to authenticate via Kerberos, send a | |
3068 | * HTTP auth challenge, otherwise just return an IPP error... | |
3069 | */ | |
3070 | ||
3071 | printer = cupsdFindDest(job->dest); | |
3072 | ||
3073 | if (printer && printer->num_auth_info_required > 0 && | |
3074 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
3075 | send_http_error(con, HTTP_UNAUTHORIZED, printer); | |
3076 | else | |
3077 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
84315f46 | 3078 | _("No authentication information provided.")); |
b423cd4c | 3079 | return; |
3080 | } | |
3081 | ||
3082 | /* | |
3083 | * See if the job is owned by the requesting user... | |
3084 | */ | |
3085 | ||
3086 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3087 | { | |
b0f6947b MS |
3088 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
3089 | cupsdFindDest(job->dest)); | |
b423cd4c | 3090 | return; |
3091 | } | |
3092 | ||
3093 | /* | |
3094 | * Save the authentication information for this job... | |
3095 | */ | |
3096 | ||
f7deaa1a | 3097 | save_auth_info(con, job, auth_info); |
b423cd4c | 3098 | |
3099 | /* | |
3100 | * Reset the job-hold-until value to "no-hold"... | |
3101 | */ | |
3102 | ||
3103 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
3104 | IPP_TAG_KEYWORD)) == NULL) | |
3105 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
3106 | ||
3107 | if (attr) | |
3108 | { | |
5e6c3df7 MS |
3109 | ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD); |
3110 | ippSetString(job->attrs, &attr, 0, "no-hold"); | |
ef416fc2 | 3111 | } |
b423cd4c | 3112 | |
3113 | /* | |
3114 | * Release the job and return... | |
3115 | */ | |
3116 | ||
3117 | cupsdReleaseJob(job); | |
3118 | ||
c8fef167 MS |
3119 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, NULL, job, "Job authenticated by user"); |
3120 | ||
75bd9771 | 3121 | cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username); |
c8fef167 MS |
3122 | |
3123 | cupsdCheckJobs(); | |
ef416fc2 | 3124 | } |
3125 | ||
3126 | ||
3127 | /* | |
aaf19ab0 | 3128 | * 'cancel_all_jobs()' - Cancel all or selected print jobs. |
ef416fc2 | 3129 | */ |
3130 | ||
b423cd4c | 3131 | static void |
3132 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
3133 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 3134 | { |
aaf19ab0 | 3135 | int i; /* Looping var */ |
b423cd4c | 3136 | http_status_t status; /* Policy status */ |
b423cd4c | 3137 | cups_ptype_t dtype; /* Destination type */ |
f7deaa1a | 3138 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 3139 | userpass[HTTP_MAX_URI], /* Username portion of URI */ |
f7deaa1a | 3140 | hostname[HTTP_MAX_URI], /* Host portion of URI */ |
b423cd4c | 3141 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
3142 | int port; /* Port portion of URI */ | |
3143 | ipp_attribute_t *attr; /* Attribute in request */ | |
aaf19ab0 MS |
3144 | const char *username = NULL; /* Username */ |
3145 | cupsd_jobaction_t purge = CUPSD_JOB_DEFAULT; | |
3146 | /* Purge? */ | |
b423cd4c | 3147 | cupsd_printer_t *printer; /* Printer */ |
aaf19ab0 MS |
3148 | ipp_attribute_t *job_ids; /* job-ids attribute */ |
3149 | cupsd_job_t *job; /* Job */ | |
ef416fc2 | 3150 | |
3151 | ||
b423cd4c | 3152 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, |
996acce8 | 3153 | con->number, uri->values[0].string.text); |
ef416fc2 | 3154 | |
3155 | /* | |
aaf19ab0 | 3156 | * Get the jobs to cancel/purge... |
ef416fc2 | 3157 | */ |
3158 | ||
aaf19ab0 | 3159 | switch (con->request->request.op.operation_id) |
b423cd4c | 3160 | { |
aaf19ab0 MS |
3161 | case IPP_PURGE_JOBS : |
3162 | /* | |
3163 | * Get the username (if any) for the jobs we want to cancel (only if | |
3164 | * "my-jobs" is specified... | |
3165 | */ | |
ef416fc2 | 3166 | |
aaf19ab0 MS |
3167 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
3168 | IPP_TAG_BOOLEAN)) != NULL && | |
3169 | attr->values[0].boolean) | |
3170 | { | |
3171 | if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
3172 | IPP_TAG_NAME)) != NULL) | |
3173 | username = attr->values[0].string.text; | |
3174 | else | |
3175 | { | |
3176 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 3177 | _("Missing requesting-user-name attribute.")); |
aaf19ab0 MS |
3178 | return; |
3179 | } | |
3180 | } | |
ef416fc2 | 3181 | |
aaf19ab0 MS |
3182 | /* |
3183 | * Look for the "purge-jobs" attribute... | |
3184 | */ | |
3185 | ||
3186 | if ((attr = ippFindAttribute(con->request, "purge-jobs", | |
3187 | IPP_TAG_BOOLEAN)) != NULL) | |
3188 | purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT; | |
3189 | else | |
3190 | purge = CUPSD_JOB_PURGE; | |
3191 | break; | |
3192 | ||
3193 | case IPP_CANCEL_MY_JOBS : | |
3194 | if (con->username[0]) | |
3195 | username = con->username; | |
3196 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
3197 | IPP_TAG_NAME)) != NULL) | |
3198 | username = attr->values[0].string.text; | |
3199 | else | |
3200 | { | |
3201 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 3202 | _("Missing requesting-user-name attribute.")); |
aaf19ab0 MS |
3203 | return; |
3204 | } | |
3205 | ||
3206 | default : | |
3207 | break; | |
ef416fc2 | 3208 | } |
aaf19ab0 MS |
3209 | |
3210 | job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER); | |
ef416fc2 | 3211 | |
b423cd4c | 3212 | /* |
aaf19ab0 | 3213 | * See if we have a printer URI... |
b423cd4c | 3214 | */ |
3215 | ||
aaf19ab0 MS |
3216 | if (strcmp(uri->name, "printer-uri")) |
3217 | { | |
3218 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 3219 | _("The printer-uri attribute is required.")); |
aaf19ab0 MS |
3220 | return; |
3221 | } | |
ef416fc2 | 3222 | |
3223 | /* | |
b423cd4c | 3224 | * And if the destination is valid... |
ef416fc2 | 3225 | */ |
3226 | ||
f7deaa1a | 3227 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3228 | { |
3229 | /* | |
b423cd4c | 3230 | * Bad URI? |
ef416fc2 | 3231 | */ |
3232 | ||
f7deaa1a | 3233 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, |
3234 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
3235 | hostname, sizeof(hostname), &port, | |
3236 | resource, sizeof(resource)); | |
3237 | ||
b423cd4c | 3238 | if ((!strncmp(resource, "/printers/", 10) && resource[10]) || |
3239 | (!strncmp(resource, "/classes/", 9) && resource[9])) | |
3240 | { | |
3241 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 3242 | _("The printer or class does not exist.")); |
b423cd4c | 3243 | return; |
3244 | } | |
ef416fc2 | 3245 | |
b423cd4c | 3246 | /* |
3247 | * Check policy... | |
3248 | */ | |
3249 | ||
3250 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
3251 | { | |
f899b121 | 3252 | send_http_error(con, status, NULL); |
b423cd4c | 3253 | return; |
3254 | } | |
ef416fc2 | 3255 | |
aaf19ab0 MS |
3256 | if (job_ids) |
3257 | { | |
3258 | for (i = 0; i < job_ids->num_values; i ++) | |
3259 | { | |
db8b865d | 3260 | if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL) |
aaf19ab0 | 3261 | break; |
db8b865d MS |
3262 | |
3263 | if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS && | |
3264 | _cups_strcasecmp(job->username, username)) | |
3265 | break; | |
aaf19ab0 MS |
3266 | } |
3267 | ||
3268 | if (i < job_ids->num_values) | |
3269 | { | |
84315f46 | 3270 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
aaf19ab0 MS |
3271 | job_ids->values[i].integer); |
3272 | return; | |
3273 | } | |
ef416fc2 | 3274 | |
aaf19ab0 MS |
3275 | for (i = 0; i < job_ids->num_values; i ++) |
3276 | { | |
3277 | job = cupsdFindJob(job_ids->values[i].integer); | |
ef416fc2 | 3278 | |
aaf19ab0 MS |
3279 | cupsdSetJobState(job, IPP_JOB_CANCELED, purge, |
3280 | purge == CUPSD_JOB_PURGE ? "Job purged by user." : | |
3281 | "Job canceled by user."); | |
3282 | } | |
3283 | ||
3284 | cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".", | |
3285 | purge == CUPSD_JOB_PURGE ? "purged" : "canceled", | |
3286 | get_username(con)); | |
3287 | } | |
3288 | else | |
3289 | { | |
3290 | /* | |
3291 | * Cancel all jobs on all printers... | |
3292 | */ | |
3293 | ||
3294 | cupsdCancelJobs(NULL, username, purge); | |
3295 | ||
3296 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", | |
3297 | purge == CUPSD_JOB_PURGE ? "purged" : "canceled", | |
3298 | get_username(con)); | |
3299 | } | |
ef416fc2 | 3300 | } |
b423cd4c | 3301 | else |
ef416fc2 | 3302 | { |
3303 | /* | |
b423cd4c | 3304 | * Check policy... |
ef416fc2 | 3305 | */ |
3306 | ||
f7deaa1a | 3307 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
3308 | NULL)) != HTTP_OK) | |
b423cd4c | 3309 | { |
f899b121 | 3310 | send_http_error(con, status, printer); |
b423cd4c | 3311 | return; |
3312 | } | |
ef416fc2 | 3313 | |
aaf19ab0 MS |
3314 | if (job_ids) |
3315 | { | |
3316 | for (i = 0; i < job_ids->num_values; i ++) | |
3317 | { | |
3318 | if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL || | |
88f9aafc | 3319 | _cups_strcasecmp(job->dest, printer->name)) |
aaf19ab0 | 3320 | break; |
db8b865d MS |
3321 | |
3322 | if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS && | |
3323 | _cups_strcasecmp(job->username, username)) | |
3324 | break; | |
aaf19ab0 | 3325 | } |
b423cd4c | 3326 | |
aaf19ab0 MS |
3327 | if (i < job_ids->num_values) |
3328 | { | |
84315f46 | 3329 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
aaf19ab0 MS |
3330 | job_ids->values[i].integer); |
3331 | return; | |
3332 | } | |
b423cd4c | 3333 | |
aaf19ab0 MS |
3334 | for (i = 0; i < job_ids->num_values; i ++) |
3335 | { | |
3336 | job = cupsdFindJob(job_ids->values[i].integer); | |
3337 | ||
3338 | cupsdSetJobState(job, IPP_JOB_CANCELED, purge, | |
3339 | purge == CUPSD_JOB_PURGE ? "Job purged by user." : | |
3340 | "Job canceled by user."); | |
3341 | } | |
3342 | ||
3343 | cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".", | |
3344 | purge == CUPSD_JOB_PURGE ? "purged" : "canceled", | |
3345 | get_username(con)); | |
3346 | } | |
3347 | else | |
3348 | { | |
3349 | /* | |
3350 | * Cancel all of the jobs on the named printer... | |
3351 | */ | |
3352 | ||
3353 | cupsdCancelJobs(printer->name, username, purge); | |
3354 | ||
3355 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
3356 | printer->name, | |
3357 | purge == CUPSD_JOB_PURGE ? "purged" : "canceled", | |
3358 | get_username(con)); | |
3359 | } | |
ef416fc2 | 3360 | } |
3361 | ||
b423cd4c | 3362 | con->response->request.status.status_code = IPP_OK; |
8ef06c79 MS |
3363 | |
3364 | cupsdCheckJobs(); | |
b423cd4c | 3365 | } |
3366 | ||
3367 | ||
3368 | /* | |
3369 | * 'cancel_job()' - Cancel a print job. | |
3370 | */ | |
3371 | ||
3372 | static void | |
3373 | cancel_job(cupsd_client_t *con, /* I - Client connection */ | |
3374 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
3375 | { | |
3376 | ipp_attribute_t *attr; /* Current attribute */ | |
3377 | int jobid; /* Job ID */ | |
f7deaa1a | 3378 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 3379 | username[HTTP_MAX_URI], /* Username portion of URI */ |
3380 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
3381 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
3382 | int port; /* Port portion of URI */ | |
3383 | cupsd_job_t *job; /* Job information */ | |
bc44d920 | 3384 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 3385 | cupsd_printer_t *printer; /* Printer data */ |
68b10830 | 3386 | cupsd_jobaction_t purge; /* Purge the job? */ |
b423cd4c | 3387 | |
3388 | ||
3389 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
996acce8 | 3390 | con->number, uri->values[0].string.text); |
b423cd4c | 3391 | |
ef416fc2 | 3392 | /* |
b423cd4c | 3393 | * See if we have a job URI or a printer URI... |
ef416fc2 | 3394 | */ |
3395 | ||
b423cd4c | 3396 | if (!strcmp(uri->name, "printer-uri")) |
3397 | { | |
3398 | /* | |
3399 | * Got a printer URI; see if we also have a job-id attribute... | |
3400 | */ | |
3401 | ||
3402 | if ((attr = ippFindAttribute(con->request, "job-id", | |
3403 | IPP_TAG_INTEGER)) == NULL) | |
3404 | { | |
3405 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 3406 | _("Got a printer-uri attribute but no job-id.")); |
b423cd4c | 3407 | return; |
3408 | } | |
3409 | ||
3410 | if ((jobid = attr->values[0].integer) == 0) | |
ef416fc2 | 3411 | { |
3412 | /* | |
b423cd4c | 3413 | * Find the current job on the specified printer... |
ef416fc2 | 3414 | */ |
3415 | ||
f7deaa1a | 3416 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3417 | { |
3418 | /* | |
b423cd4c | 3419 | * Bad URI... |
ef416fc2 | 3420 | */ |
3421 | ||
b423cd4c | 3422 | send_ipp_status(con, IPP_NOT_FOUND, |
84315f46 | 3423 | _("The printer or class does not exist.")); |
b423cd4c | 3424 | return; |
ef416fc2 | 3425 | } |
3426 | ||
3427 | /* | |
b9faaae1 | 3428 | * See if there are any pending jobs... |
ef416fc2 | 3429 | */ |
3430 | ||
b9faaae1 MS |
3431 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
3432 | job; | |
3433 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
3434 | if (job->state_value <= IPP_JOB_PROCESSING && | |
88f9aafc | 3435 | !_cups_strcasecmp(job->dest, printer->name)) |
b9faaae1 MS |
3436 | break; |
3437 | ||
3438 | if (job) | |
3439 | jobid = job->id; | |
ef416fc2 | 3440 | else |
ef416fc2 | 3441 | { |
3442 | /* | |
b9faaae1 | 3443 | * No, try stopped jobs... |
ef416fc2 | 3444 | */ |
f7deaa1a | 3445 | |
b9faaae1 | 3446 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
b423cd4c | 3447 | job; |
3448 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
b9faaae1 | 3449 | if (job->state_value == IPP_JOB_STOPPED && |
88f9aafc | 3450 | !_cups_strcasecmp(job->dest, printer->name)) |
b423cd4c | 3451 | break; |
ef416fc2 | 3452 | |
b423cd4c | 3453 | if (job) |
3454 | jobid = job->id; | |
3455 | else | |
ef416fc2 | 3456 | { |
84315f46 | 3457 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s."), |
b9faaae1 MS |
3458 | printer->name); |
3459 | return; | |
b423cd4c | 3460 | } |
3461 | } | |
3462 | } | |
3463 | } | |
3464 | else | |
3465 | { | |
3466 | /* | |
3467 | * Got a job URI; parse it to get the job ID... | |
3468 | */ | |
ef416fc2 | 3469 | |
f7deaa1a | 3470 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
3471 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 3472 | sizeof(host), &port, resource, sizeof(resource)); |
f7deaa1a | 3473 | |
b423cd4c | 3474 | if (strncmp(resource, "/jobs/", 6)) |
3475 | { | |
3476 | /* | |
3477 | * Not a valid URI! | |
3478 | */ | |
ef416fc2 | 3479 | |
84315f46 | 3480 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
b423cd4c | 3481 | uri->values[0].string.text); |
3482 | return; | |
3483 | } | |
3484 | ||
3485 | jobid = atoi(resource + 6); | |
3486 | } | |
3487 | ||
0a682745 MS |
3488 | /* |
3489 | * Look for the "purge-job" attribute... | |
3490 | */ | |
3491 | ||
3492 | if ((attr = ippFindAttribute(con->request, "purge-job", | |
3493 | IPP_TAG_BOOLEAN)) != NULL) | |
68b10830 | 3494 | purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT; |
0a682745 | 3495 | else |
68b10830 | 3496 | purge = CUPSD_JOB_DEFAULT; |
0a682745 | 3497 | |
b423cd4c | 3498 | /* |
3499 | * See if the job exists... | |
3500 | */ | |
3501 | ||
3502 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3503 | { | |
3504 | /* | |
3505 | * Nope - return a "not found" error... | |
3506 | */ | |
3507 | ||
84315f46 | 3508 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
b423cd4c | 3509 | return; |
3510 | } | |
3511 | ||
3512 | /* | |
3513 | * See if the job is owned by the requesting user... | |
3514 | */ | |
3515 | ||
3516 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3517 | { | |
b0f6947b MS |
3518 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
3519 | cupsdFindDest(job->dest)); | |
b423cd4c | 3520 | return; |
3521 | } | |
3522 | ||
3523 | /* | |
d09495fa | 3524 | * See if the job is already completed, canceled, or aborted; if so, |
b423cd4c | 3525 | * we can't cancel... |
3526 | */ | |
3527 | ||
68b10830 | 3528 | if (job->state_value >= IPP_JOB_CANCELED && purge != CUPSD_JOB_PURGE) |
b423cd4c | 3529 | { |
e1d6a774 | 3530 | switch (job->state_value) |
3531 | { | |
d09495fa | 3532 | case IPP_JOB_CANCELED : |
e1d6a774 | 3533 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
d09495fa | 3534 | _("Job #%d is already canceled - can\'t cancel."), |
e1d6a774 | 3535 | jobid); |
3536 | break; | |
3537 | ||
3538 | case IPP_JOB_ABORTED : | |
3539 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3540 | _("Job #%d is already aborted - can\'t cancel."), | |
3541 | jobid); | |
3542 | break; | |
3543 | ||
3544 | default : | |
3545 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3546 | _("Job #%d is already completed - can\'t cancel."), | |
3547 | jobid); | |
3548 | break; | |
3549 | } | |
3550 | ||
b423cd4c | 3551 | return; |
3552 | } | |
ef416fc2 | 3553 | |
b423cd4c | 3554 | /* |
3555 | * Cancel the job and return... | |
3556 | */ | |
ef416fc2 | 3557 | |
b9faaae1 | 3558 | cupsdSetJobState(job, IPP_JOB_CANCELED, purge, |
68b10830 MS |
3559 | purge == CUPSD_JOB_PURGE ? "Job purged by \"%s\"" : |
3560 | "Job canceled by \"%s\"", | |
b9faaae1 | 3561 | username); |
b423cd4c | 3562 | cupsdCheckJobs(); |
ef416fc2 | 3563 | |
68b10830 | 3564 | if (purge == CUPSD_JOB_PURGE) |
0a682745 | 3565 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid, |
75bd9771 | 3566 | username); |
0a682745 MS |
3567 | else |
3568 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid, | |
75bd9771 | 3569 | username); |
ef416fc2 | 3570 | |
b423cd4c | 3571 | con->response->request.status.status_code = IPP_OK; |
3572 | } | |
ef416fc2 | 3573 | |
ef416fc2 | 3574 | |
b423cd4c | 3575 | /* |
3576 | * 'cancel_subscription()' - Cancel a subscription. | |
3577 | */ | |
ef416fc2 | 3578 | |
b423cd4c | 3579 | static void |
3580 | cancel_subscription( | |
3581 | cupsd_client_t *con, /* I - Client connection */ | |
3582 | int sub_id) /* I - Subscription ID */ | |
3583 | { | |
3584 | http_status_t status; /* Policy status */ | |
3585 | cupsd_subscription_t *sub; /* Subscription */ | |
ef416fc2 | 3586 | |
ef416fc2 | 3587 | |
b423cd4c | 3588 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3589 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
996acce8 | 3590 | con, con->number, sub_id); |
ef416fc2 | 3591 | |
b423cd4c | 3592 | /* |
3593 | * Is the subscription ID valid? | |
3594 | */ | |
ef416fc2 | 3595 | |
b423cd4c | 3596 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
3597 | { | |
3598 | /* | |
3599 | * Bad subscription ID... | |
3600 | */ | |
ef416fc2 | 3601 | |
b423cd4c | 3602 | send_ipp_status(con, IPP_NOT_FOUND, |
84315f46 | 3603 | _("Subscription #%d does not exist."), sub_id); |
b423cd4c | 3604 | return; |
3605 | } | |
ef416fc2 | 3606 | |
b423cd4c | 3607 | /* |
3608 | * Check policy... | |
3609 | */ | |
ef416fc2 | 3610 | |
b423cd4c | 3611 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
3612 | DefaultPolicyPtr, | |
3613 | con, sub->owner)) != HTTP_OK) | |
3614 | { | |
f899b121 | 3615 | send_http_error(con, status, sub->dest); |
b423cd4c | 3616 | return; |
3617 | } | |
ef416fc2 | 3618 | |
b423cd4c | 3619 | /* |
3620 | * Cancel the subscription... | |
3621 | */ | |
ef416fc2 | 3622 | |
b423cd4c | 3623 | cupsdDeleteSubscription(sub, 1); |
ef416fc2 | 3624 | |
b423cd4c | 3625 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 3626 | } |
3627 | ||
3628 | ||
3dfe78b3 MS |
3629 | /* |
3630 | * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI. | |
3631 | */ | |
3632 | ||
3633 | static int /* O - 1 if OK, 0 if not */ | |
3634 | check_rss_recipient( | |
3635 | const char *recipient) /* I - Recipient URI */ | |
3636 | { | |
3637 | cupsd_subscription_t *sub; /* Current subscription */ | |
3638 | ||
3639 | ||
3640 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
3641 | sub; | |
3642 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
3643 | if (sub->recipient) | |
3644 | { | |
3645 | /* | |
3646 | * Compare the URIs up to the first ?... | |
3647 | */ | |
3648 | ||
3649 | const char *r1, *r2; | |
3650 | ||
3651 | for (r1 = recipient, r2 = sub->recipient; | |
3652 | *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?'; | |
3653 | r1 ++, r2 ++); | |
3654 | ||
3655 | if (*r1 == *r2) | |
3656 | return (0); | |
3657 | } | |
3658 | ||
3659 | return (1); | |
3660 | } | |
3661 | ||
3662 | ||
ef416fc2 | 3663 | /* |
b423cd4c | 3664 | * 'check_quotas()' - Check quotas for a printer and user. |
ef416fc2 | 3665 | */ |
3666 | ||
6c48a6ca MS |
3667 | static int /* O - 1 if OK, 0 if forbidden, |
3668 | -1 if limit reached */ | |
b423cd4c | 3669 | check_quotas(cupsd_client_t *con, /* I - Client connection */ |
3670 | cupsd_printer_t *p) /* I - Printer or class */ | |
ef416fc2 | 3671 | { |
10d09e33 MS |
3672 | char username[33], /* Username */ |
3673 | *name; /* Current user name */ | |
b423cd4c | 3674 | cupsd_quota_t *q; /* Quota data */ |
7594b224 | 3675 | #ifdef HAVE_MBR_UID_TO_UUID |
3676 | /* | |
3677 | * Use Apple membership APIs which require that all names represent | |
3678 | * valid user account or group records accessible by the server. | |
3679 | */ | |
3680 | ||
3681 | uuid_t usr_uuid; /* UUID for job requesting user */ | |
3682 | uuid_t usr2_uuid; /* UUID for ACL user name entry */ | |
3683 | uuid_t grp_uuid; /* UUID for ACL group name entry */ | |
3684 | int mbr_err; /* Error from membership function */ | |
3685 | int is_member; /* Is this user a member? */ | |
3686 | #else | |
3687 | /* | |
3688 | * Use standard POSIX APIs for checking users and groups... | |
3689 | */ | |
3690 | ||
b423cd4c | 3691 | struct passwd *pw; /* User password data */ |
7594b224 | 3692 | #endif /* HAVE_MBR_UID_TO_UUID */ |
ef416fc2 | 3693 | |
3694 | ||
b423cd4c | 3695 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", |
996acce8 | 3696 | con, con->number, p, p->name); |
ef416fc2 | 3697 | |
b423cd4c | 3698 | /* |
3699 | * Figure out who is printing... | |
3700 | */ | |
3701 | ||
3702 | strlcpy(username, get_username(con), sizeof(username)); | |
3703 | ||
a2326b5b MS |
3704 | if ((name = strchr(username, '@')) != NULL) |
3705 | *name = '\0'; /* Strip @REALM */ | |
3706 | ||
b423cd4c | 3707 | /* |
3708 | * Check global active job limits for printers and users... | |
3709 | */ | |
3710 | ||
3711 | if (MaxJobsPerPrinter) | |
ef416fc2 | 3712 | { |
b423cd4c | 3713 | /* |
3714 | * Check if there are too many pending jobs on this printer... | |
3715 | */ | |
3716 | ||
3717 | if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter) | |
3718 | { | |
3719 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...", | |
3720 | p->name); | |
6c48a6ca | 3721 | return (-1); |
b423cd4c | 3722 | } |
3723 | } | |
3724 | ||
3725 | if (MaxJobsPerUser) | |
3726 | { | |
3727 | /* | |
3728 | * Check if there are too many pending jobs for this user... | |
3729 | */ | |
3730 | ||
3731 | if (cupsdGetUserJobCount(username) >= MaxJobsPerUser) | |
3732 | { | |
3733 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...", | |
3734 | username); | |
6c48a6ca | 3735 | return (-1); |
b423cd4c | 3736 | } |
ef416fc2 | 3737 | } |
3738 | ||
3739 | /* | |
b423cd4c | 3740 | * Check against users... |
ef416fc2 | 3741 | */ |
3742 | ||
10d09e33 | 3743 | if (cupsArrayCount(p->users) == 0 && p->k_limit == 0 && p->page_limit == 0) |
b423cd4c | 3744 | return (1); |
3745 | ||
10d09e33 | 3746 | if (cupsArrayCount(p->users)) |
b423cd4c | 3747 | { |
7594b224 | 3748 | #ifdef HAVE_MBR_UID_TO_UUID |
3749 | /* | |
3750 | * Get UUID for job requesting user... | |
3751 | */ | |
3752 | ||
3753 | if (mbr_user_name_to_uuid((char *)username, usr_uuid)) | |
3754 | { | |
3755 | /* | |
3756 | * Unknown user... | |
3757 | */ | |
3758 | ||
3759 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3760 | "check_quotas: UUID lookup failed for user \"%s\"", | |
3761 | username); | |
3762 | cupsdLogMessage(CUPSD_LOG_INFO, | |
3763 | "Denying user \"%s\" access to printer \"%s\" " | |
3764 | "(unknown user)...", | |
3765 | username, p->name); | |
3766 | return (0); | |
3767 | } | |
3768 | #else | |
3769 | /* | |
3770 | * Get UID and GID of requesting user... | |
3771 | */ | |
3772 | ||
b423cd4c | 3773 | pw = getpwnam(username); |
3774 | endpwent(); | |
7594b224 | 3775 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 3776 | |
10d09e33 MS |
3777 | for (name = (char *)cupsArrayFirst(p->users); |
3778 | name; | |
3779 | name = (char *)cupsArrayNext(p->users)) | |
3780 | if (name[0] == '@') | |
b423cd4c | 3781 | { |
3782 | /* | |
3783 | * Check group membership... | |
3784 | */ | |
3785 | ||
7594b224 | 3786 | #ifdef HAVE_MBR_UID_TO_UUID |
10d09e33 | 3787 | if (name[1] == '#') |
c9fc04c6 | 3788 | { |
10d09e33 | 3789 | if (uuid_parse(name + 2, grp_uuid)) |
c9fc04c6 MS |
3790 | uuid_clear(grp_uuid); |
3791 | } | |
10d09e33 | 3792 | else if ((mbr_err = mbr_group_name_to_uuid(name + 1, grp_uuid)) != 0) |
7594b224 | 3793 | { |
3794 | /* | |
3795 | * Invalid ACL entries are ignored for matching; just record a | |
3796 | * warning in the log... | |
3797 | */ | |
3798 | ||
3799 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3800 | "check_quotas: UUID lookup failed for ACL entry " | |
10d09e33 | 3801 | "\"%s\" (err=%d)", name, mbr_err); |
7594b224 | 3802 | cupsdLogMessage(CUPSD_LOG_WARN, |
3803 | "Access control entry \"%s\" not a valid group name; " | |
10d09e33 | 3804 | "entry ignored", name); |
7594b224 | 3805 | } |
7594b224 | 3806 | |
c9fc04c6 MS |
3807 | if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid, |
3808 | &is_member)) != 0) | |
3809 | { | |
3810 | /* | |
3811 | * At this point, there should be no errors, but check anyways... | |
7594b224 | 3812 | */ |
3813 | ||
c9fc04c6 MS |
3814 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
3815 | "check_quotas: group \"%s\" membership check " | |
10d09e33 | 3816 | "failed (err=%d)", name + 1, mbr_err); |
c9fc04c6 | 3817 | is_member = 0; |
7594b224 | 3818 | } |
c9fc04c6 MS |
3819 | |
3820 | /* | |
3821 | * Stop if we found a match... | |
3822 | */ | |
3823 | ||
3824 | if (is_member) | |
3825 | break; | |
3826 | ||
7594b224 | 3827 | #else |
10d09e33 | 3828 | if (cupsdCheckGroup(username, pw, name + 1)) |
b423cd4c | 3829 | break; |
7594b224 | 3830 | #endif /* HAVE_MBR_UID_TO_UUID */ |
3831 | } | |
3832 | #ifdef HAVE_MBR_UID_TO_UUID | |
3833 | else | |
3834 | { | |
10d09e33 | 3835 | if (name[0] == '#') |
c9fc04c6 | 3836 | { |
10d09e33 | 3837 | if (uuid_parse(name + 1, usr2_uuid)) |
c9fc04c6 MS |
3838 | uuid_clear(usr2_uuid); |
3839 | } | |
10d09e33 | 3840 | else if ((mbr_err = mbr_user_name_to_uuid(name, usr2_uuid)) != 0) |
7594b224 | 3841 | { |
3842 | /* | |
3843 | * Invalid ACL entries are ignored for matching; just record a | |
3844 | * warning in the log... | |
3845 | */ | |
3846 | ||
3847 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3848 | "check_quotas: UUID lookup failed for ACL entry " | |
10d09e33 | 3849 | "\"%s\" (err=%d)", name, mbr_err); |
7594b224 | 3850 | cupsdLogMessage(CUPSD_LOG_WARN, |
3851 | "Access control entry \"%s\" not a valid user name; " | |
10d09e33 | 3852 | "entry ignored", name); |
7594b224 | 3853 | } |
7594b224 | 3854 | |
d1c13e16 | 3855 | if (!uuid_compare(usr_uuid, usr2_uuid)) |
c9fc04c6 | 3856 | break; |
b423cd4c | 3857 | } |
7594b224 | 3858 | #else |
88f9aafc | 3859 | else if (!_cups_strcasecmp(username, name)) |
b423cd4c | 3860 | break; |
7594b224 | 3861 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 3862 | |
10d09e33 | 3863 | if ((name != NULL) == p->deny_users) |
ef416fc2 | 3864 | { |
b423cd4c | 3865 | cupsdLogMessage(CUPSD_LOG_INFO, |
3866 | "Denying user \"%s\" access to printer \"%s\"...", | |
3867 | username, p->name); | |
3868 | return (0); | |
ef416fc2 | 3869 | } |
b423cd4c | 3870 | } |
ef416fc2 | 3871 | |
3872 | /* | |
b423cd4c | 3873 | * Check quotas... |
ef416fc2 | 3874 | */ |
3875 | ||
b423cd4c | 3876 | if (p->k_limit || p->page_limit) |
3877 | { | |
3878 | if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL) | |
3879 | { | |
3880 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4d301e69 | 3881 | "Unable to allocate quota data for user \"%s\"", |
b423cd4c | 3882 | username); |
bc44d920 | 3883 | return (-1); |
b423cd4c | 3884 | } |
ef416fc2 | 3885 | |
b423cd4c | 3886 | if ((q->k_count >= p->k_limit && p->k_limit) || |
3887 | (q->page_count >= p->page_limit && p->page_limit)) | |
3888 | { | |
3889 | cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...", | |
3890 | username); | |
bc44d920 | 3891 | return (-1); |
b423cd4c | 3892 | } |
3893 | } | |
3894 | ||
3895 | /* | |
3896 | * If we have gotten this far, we're done! | |
3897 | */ | |
3898 | ||
3899 | return (1); | |
ef416fc2 | 3900 | } |
3901 | ||
3902 | ||
aaf19ab0 MS |
3903 | /* |
3904 | * 'close_job()' - Close a multi-file job. | |
3905 | */ | |
3906 | ||
3907 | static void | |
3908 | close_job(cupsd_client_t *con, /* I - Client connection */ | |
3909 | ipp_attribute_t *uri) /* I - Printer URI */ | |
3910 | { | |
3911 | cupsd_job_t *job; /* Job */ | |
aaf19ab0 MS |
3912 | ipp_attribute_t *attr; /* Attribute */ |
3913 | char job_uri[HTTP_MAX_URI], | |
3914 | /* Job URI */ | |
3915 | username[256]; /* User name */ | |
3916 | ||
3917 | ||
3918 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "close_job(%p[%d], %s)", con, | |
996acce8 | 3919 | con->number, uri->values[0].string.text); |
aaf19ab0 MS |
3920 | |
3921 | /* | |
3922 | * See if we have a job URI or a printer URI... | |
3923 | */ | |
3924 | ||
3925 | if (strcmp(uri->name, "printer-uri")) | |
3926 | { | |
3927 | /* | |
3928 | * job-uri is not supported by Close-Job! | |
3929 | */ | |
3930 | ||
3931 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3932 | _("Close-Job doesn't support the job-uri attribute.")); | |
3933 | return; | |
3934 | } | |
3935 | ||
3936 | /* | |
3937 | * Got a printer URI; see if we also have a job-id attribute... | |
3938 | */ | |
3939 | ||
3940 | if ((attr = ippFindAttribute(con->request, "job-id", | |
3941 | IPP_TAG_INTEGER)) == NULL) | |
3942 | { | |
3943 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 3944 | _("Got a printer-uri attribute but no job-id.")); |
aaf19ab0 MS |
3945 | return; |
3946 | } | |
3947 | ||
3948 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
3949 | { | |
3950 | /* | |
3951 | * Nope - return a "not found" error... | |
3952 | */ | |
3953 | ||
84315f46 | 3954 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
aaf19ab0 MS |
3955 | attr->values[0].integer); |
3956 | return; | |
3957 | } | |
3958 | ||
aaf19ab0 MS |
3959 | /* |
3960 | * See if the job is owned by the requesting user... | |
3961 | */ | |
3962 | ||
3963 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3964 | { | |
3965 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, | |
3966 | cupsdFindDest(job->dest)); | |
3967 | return; | |
3968 | } | |
3969 | ||
3970 | /* | |
3971 | * Add any ending sheet... | |
3972 | */ | |
3973 | ||
3974 | if (cupsdTimeoutJob(job)) | |
3975 | return; | |
3976 | ||
3977 | if (job->state_value == IPP_JOB_STOPPED) | |
3978 | { | |
3979 | job->state->values[0].integer = IPP_JOB_PENDING; | |
3980 | job->state_value = IPP_JOB_PENDING; | |
3981 | } | |
3982 | else if (job->state_value == IPP_JOB_HELD) | |
3983 | { | |
3984 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
3985 | IPP_TAG_KEYWORD)) == NULL) | |
3986 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
3987 | ||
3988 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) | |
3989 | { | |
3990 | job->state->values[0].integer = IPP_JOB_PENDING; | |
3991 | job->state_value = IPP_JOB_PENDING; | |
3992 | } | |
3993 | } | |
3994 | ||
3995 | job->dirty = 1; | |
3996 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
3997 | ||
3998 | /* | |
3999 | * Fill in the response info... | |
4000 | */ | |
4001 | ||
83e08001 | 4002 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
f2534050 | 4003 | con->clientname, con->clientport, "/jobs/%d", job->id); |
aaf19ab0 MS |
4004 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
4005 | job_uri); | |
4006 | ||
4007 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); | |
4008 | ||
4009 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
4010 | job->state_value); | |
4011 | ||
aaf19ab0 MS |
4012 | con->response->request.status.status_code = IPP_OK; |
4013 | ||
4014 | /* | |
4015 | * Start the job if necessary... | |
4016 | */ | |
4017 | ||
4018 | cupsdCheckJobs(); | |
4019 | } | |
4020 | ||
4021 | ||
b423cd4c | 4022 | /* |
4023 | * 'copy_attrs()' - Copy attributes from one request to another. | |
4024 | */ | |
4025 | ||
4026 | static void | |
4027 | copy_attrs(ipp_t *to, /* I - Destination request */ | |
4028 | ipp_t *from, /* I - Source request */ | |
4029 | cups_array_t *ra, /* I - Requested attributes */ | |
4030 | ipp_tag_t group, /* I - Group to copy */ | |
10d09e33 MS |
4031 | int quickcopy, /* I - Do a quick copy? */ |
4032 | cups_array_t *exclude) /* I - Attributes to exclude? */ | |
b423cd4c | 4033 | { |
4034 | ipp_attribute_t *fromattr; /* Source attribute */ | |
ef416fc2 | 4035 | |
ef416fc2 | 4036 | |
b423cd4c | 4037 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4038 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
4039 | to, from, ra, group, quickcopy); | |
ef416fc2 | 4040 | |
b423cd4c | 4041 | if (!to || !from) |
4042 | return; | |
e00b005a | 4043 | |
b423cd4c | 4044 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) |
ef416fc2 | 4045 | { |
4046 | /* | |
b423cd4c | 4047 | * Filter attributes as needed... |
ef416fc2 | 4048 | */ |
4049 | ||
2abf387c | 4050 | if ((group != IPP_TAG_ZERO && fromattr->group_tag != group && |
4051 | fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name) | |
b423cd4c | 4052 | continue; |
4053 | ||
db8b865d MS |
4054 | if (!strcmp(fromattr->name, "document-password") || |
4055 | !strcmp(fromattr->name, "job-authorization-uri") || | |
4056 | !strcmp(fromattr->name, "job-password") || | |
4057 | !strcmp(fromattr->name, "job-password-encryption") || | |
4058 | !strcmp(fromattr->name, "job-printer-uri")) | |
88f9aafc MS |
4059 | continue; |
4060 | ||
c8fef167 | 4061 | if (exclude && |
10d09e33 MS |
4062 | (cupsArrayFind(exclude, fromattr->name) || |
4063 | cupsArrayFind(exclude, "all"))) | |
4064 | { | |
4065 | /* | |
4066 | * We need to exclude this attribute for security reasons; we require the | |
88f9aafc MS |
4067 | * job-id attribute regardless of the security settings for IPP |
4068 | * conformance. | |
4069 | * | |
4070 | * The job-printer-uri attribute is handled by copy_job_attrs(). | |
10d09e33 MS |
4071 | * |
4072 | * Subscription attribute security is handled by copy_subscription_attrs(). | |
4073 | */ | |
4074 | ||
88f9aafc | 4075 | if (strcmp(fromattr->name, "job-id")) |
10d09e33 MS |
4076 | continue; |
4077 | } | |
4078 | ||
b423cd4c | 4079 | if (!ra || cupsArrayFind(ra, fromattr->name)) |
d2354e63 MS |
4080 | { |
4081 | /* | |
4082 | * Don't send collection attributes by default to IPP/1.x clients | |
54afec33 MS |
4083 | * since many do not support collections. Also don't send |
4084 | * media-col-database unless specifically requested by the client. | |
d2354e63 MS |
4085 | */ |
4086 | ||
4087 | if (fromattr->value_tag == IPP_TAG_BEGIN_COLLECTION && | |
54afec33 MS |
4088 | !ra && |
4089 | (to->request.status.version[0] == 1 || | |
4090 | !strcmp(fromattr->name, "media-col-database"))) | |
d2354e63 MS |
4091 | continue; |
4092 | ||
a2326b5b | 4093 | ippCopyAttribute(to, fromattr, quickcopy); |
d2354e63 | 4094 | } |
ef416fc2 | 4095 | } |
b423cd4c | 4096 | } |
ef416fc2 | 4097 | |
ef416fc2 | 4098 | |
b423cd4c | 4099 | /* |
4100 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
4101 | * specified job. | |
4102 | */ | |
ef416fc2 | 4103 | |
b423cd4c | 4104 | static int /* O - Size of banner file in kbytes */ |
4105 | copy_banner(cupsd_client_t *con, /* I - Client connection */ | |
4106 | cupsd_job_t *job, /* I - Job information */ | |
4107 | const char *name) /* I - Name of banner */ | |
4108 | { | |
4109 | int i; /* Looping var */ | |
4110 | int kbytes; /* Size of banner file in kbytes */ | |
4111 | char filename[1024]; /* Job filename */ | |
4112 | cupsd_banner_t *banner; /* Pointer to banner */ | |
4113 | cups_file_t *in; /* Input file */ | |
4114 | cups_file_t *out; /* Output file */ | |
4115 | int ch; /* Character from file */ | |
4116 | char attrname[255], /* Name of attribute */ | |
4117 | *s; /* Pointer into name */ | |
4118 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 4119 | |
ef416fc2 | 4120 | |
3dfe78b3 MS |
4121 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4122 | "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")", | |
996acce8 | 4123 | con, con ? con->number : -1, job, job->id, |
09a101d6 | 4124 | name ? name : "(null)"); |
ef416fc2 | 4125 | |
b423cd4c | 4126 | /* |
4127 | * Find the banner; return if not found or "none"... | |
4128 | */ | |
ef416fc2 | 4129 | |
b423cd4c | 4130 | if (!name || !strcmp(name, "none") || |
4131 | (banner = cupsdFindBanner(name)) == NULL) | |
4132 | return (0); | |
ef416fc2 | 4133 | |
b423cd4c | 4134 | /* |
4135 | * Open the banner and job files... | |
4136 | */ | |
ef416fc2 | 4137 | |
b423cd4c | 4138 | if (add_file(con, job, banner->filetype, 0)) |
91c84a35 | 4139 | return (-1); |
ef416fc2 | 4140 | |
b423cd4c | 4141 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, |
4142 | job->num_files); | |
4143 | if ((out = cupsFileOpen(filename, "w")) == NULL) | |
4144 | { | |
4145 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 | 4146 | "Unable to create banner job file %s - %s", |
b423cd4c | 4147 | filename, strerror(errno)); |
4148 | job->num_files --; | |
4149 | return (0); | |
4150 | } | |
4151 | ||
4152 | fchmod(cupsFileNumber(out), 0640); | |
4153 | fchown(cupsFileNumber(out), RunUser, Group); | |
ef416fc2 | 4154 | |
4155 | /* | |
b423cd4c | 4156 | * Try the localized banner file under the subdirectory... |
ef416fc2 | 4157 | */ |
4158 | ||
80ca4592 | 4159 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, |
b423cd4c | 4160 | sizeof(attrname)); |
4161 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
ef416fc2 | 4162 | { |
4163 | /* | |
b423cd4c | 4164 | * Convert ll-cc to ll_CC... |
ef416fc2 | 4165 | */ |
4166 | ||
b423cd4c | 4167 | attrname[2] = '_'; |
7e86f2f6 MS |
4168 | attrname[3] = (char)toupper(attrname[3] & 255); |
4169 | attrname[4] = (char)toupper(attrname[4] & 255); | |
b423cd4c | 4170 | } |
ef416fc2 | 4171 | |
b423cd4c | 4172 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4173 | attrname, name); | |
ef416fc2 | 4174 | |
b423cd4c | 4175 | if (access(filename, 0) && strlen(attrname) > 2) |
ef416fc2 | 4176 | { |
4177 | /* | |
b423cd4c | 4178 | * Wasn't able to find "ll_CC" locale file; try the non-national |
4179 | * localization banner directory. | |
ef416fc2 | 4180 | */ |
4181 | ||
b423cd4c | 4182 | attrname[2] = '\0'; |
ef416fc2 | 4183 | |
b423cd4c | 4184 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4185 | attrname, name); | |
ef416fc2 | 4186 | } |
b423cd4c | 4187 | |
4188 | if (access(filename, 0)) | |
ef416fc2 | 4189 | { |
4190 | /* | |
b423cd4c | 4191 | * Use the non-localized banner file. |
ef416fc2 | 4192 | */ |
4193 | ||
b423cd4c | 4194 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); |
4195 | } | |
ef416fc2 | 4196 | |
b423cd4c | 4197 | if ((in = cupsFileOpen(filename, "r")) == NULL) |
4198 | { | |
4199 | cupsFileClose(out); | |
4200 | unlink(filename); | |
4201 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 | 4202 | "Unable to open banner template file %s - %s", |
b423cd4c | 4203 | filename, strerror(errno)); |
4204 | job->num_files --; | |
4205 | return (0); | |
ef416fc2 | 4206 | } |
4207 | ||
4208 | /* | |
b423cd4c | 4209 | * Parse the file to the end... |
ef416fc2 | 4210 | */ |
4211 | ||
b423cd4c | 4212 | while ((ch = cupsFileGetChar(in)) != EOF) |
4213 | if (ch == '{') | |
4214 | { | |
4215 | /* | |
4216 | * Get an attribute name... | |
4217 | */ | |
ef416fc2 | 4218 | |
b423cd4c | 4219 | for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;) |
4220 | if (!isalpha(ch & 255) && ch != '-' && ch != '?') | |
4221 | break; | |
4222 | else if (s < (attrname + sizeof(attrname) - 1)) | |
7e86f2f6 | 4223 | *s++ = (char)ch; |
b423cd4c | 4224 | else |
4225 | break; | |
ef416fc2 | 4226 | |
b423cd4c | 4227 | *s = '\0'; |
4228 | ||
4229 | if (ch != '}') | |
4230 | { | |
4231 | /* | |
4232 | * Ignore { followed by stuff that is not an attribute name... | |
4233 | */ | |
4234 | ||
4235 | cupsFilePrintf(out, "{%s%c", attrname, ch); | |
4236 | continue; | |
4237 | } | |
ef416fc2 | 4238 | |
ef416fc2 | 4239 | /* |
b423cd4c | 4240 | * See if it is defined... |
ef416fc2 | 4241 | */ |
4242 | ||
b423cd4c | 4243 | if (attrname[0] == '?') |
4244 | s = attrname + 1; | |
4245 | else | |
4246 | s = attrname; | |
ef416fc2 | 4247 | |
b423cd4c | 4248 | if (!strcmp(s, "printer-name")) |
4249 | { | |
4250 | cupsFilePuts(out, job->dest); | |
4251 | continue; | |
ef416fc2 | 4252 | } |
b423cd4c | 4253 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) |
4254 | { | |
4255 | /* | |
4256 | * See if we have a leading question mark... | |
4257 | */ | |
ef416fc2 | 4258 | |
b423cd4c | 4259 | if (attrname[0] != '?') |
4260 | { | |
4261 | /* | |
4262 | * Nope, write to file as-is; probably a PostScript procedure... | |
4263 | */ | |
ef416fc2 | 4264 | |
b423cd4c | 4265 | cupsFilePrintf(out, "{%s}", attrname); |
4266 | } | |
ef416fc2 | 4267 | |
b423cd4c | 4268 | continue; |
4269 | } | |
ef416fc2 | 4270 | |
b423cd4c | 4271 | /* |
4272 | * Output value(s)... | |
4273 | */ | |
ef416fc2 | 4274 | |
b423cd4c | 4275 | for (i = 0; i < attr->num_values; i ++) |
4276 | { | |
4277 | if (i) | |
4278 | cupsFilePutChar(out, ','); | |
ef416fc2 | 4279 | |
b423cd4c | 4280 | switch (attr->value_tag) |
4281 | { | |
4282 | case IPP_TAG_INTEGER : | |
4283 | case IPP_TAG_ENUM : | |
4284 | if (!strncmp(s, "time-at-", 8)) | |
dfd5680b | 4285 | { |
0268488e MS |
4286 | struct timeval tv; /* Time value */ |
4287 | ||
4288 | tv.tv_sec = attr->values[i].integer; | |
4289 | tv.tv_usec = 0; | |
4290 | ||
dfd5680b MS |
4291 | cupsFilePuts(out, cupsdGetDateTime(&tv, CUPSD_TIME_STANDARD)); |
4292 | } | |
b423cd4c | 4293 | else |
4294 | cupsFilePrintf(out, "%d", attr->values[i].integer); | |
4295 | break; | |
ef416fc2 | 4296 | |
b423cd4c | 4297 | case IPP_TAG_BOOLEAN : |
4298 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
4299 | break; | |
ef416fc2 | 4300 | |
b423cd4c | 4301 | case IPP_TAG_NOVALUE : |
4302 | cupsFilePuts(out, "novalue"); | |
4303 | break; | |
ef416fc2 | 4304 | |
b423cd4c | 4305 | case IPP_TAG_RANGE : |
4306 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
4307 | attr->values[i].range.upper); | |
4308 | break; | |
fa73b229 | 4309 | |
b423cd4c | 4310 | case IPP_TAG_RESOLUTION : |
4311 | cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres, | |
4312 | attr->values[i].resolution.yres, | |
4313 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
3e7fe0ca | 4314 | "dpi" : "dpcm"); |
b423cd4c | 4315 | break; |
fa73b229 | 4316 | |
b423cd4c | 4317 | case IPP_TAG_URI : |
4318 | case IPP_TAG_STRING : | |
4319 | case IPP_TAG_TEXT : | |
4320 | case IPP_TAG_NAME : | |
4321 | case IPP_TAG_KEYWORD : | |
4322 | case IPP_TAG_CHARSET : | |
4323 | case IPP_TAG_LANGUAGE : | |
88f9aafc | 4324 | if (!_cups_strcasecmp(banner->filetype->type, "postscript")) |
b423cd4c | 4325 | { |
4326 | /* | |
4327 | * Need to quote strings for PS banners... | |
4328 | */ | |
fa73b229 | 4329 | |
b423cd4c | 4330 | const char *p; |
fa73b229 | 4331 | |
b423cd4c | 4332 | for (p = attr->values[i].string.text; *p; p ++) |
4333 | { | |
4334 | if (*p == '(' || *p == ')' || *p == '\\') | |
4335 | { | |
4336 | cupsFilePutChar(out, '\\'); | |
4337 | cupsFilePutChar(out, *p); | |
4338 | } | |
4339 | else if (*p < 32 || *p > 126) | |
4340 | cupsFilePrintf(out, "\\%03o", *p & 255); | |
4341 | else | |
4342 | cupsFilePutChar(out, *p); | |
4343 | } | |
4344 | } | |
4345 | else | |
4346 | cupsFilePuts(out, attr->values[i].string.text); | |
4347 | break; | |
fa73b229 | 4348 | |
b423cd4c | 4349 | default : |
4350 | break; /* anti-compiler-warning-code */ | |
4351 | } | |
4352 | } | |
4353 | } | |
4354 | else if (ch == '\\') /* Quoted char */ | |
4355 | { | |
4356 | ch = cupsFileGetChar(in); | |
fa73b229 | 4357 | |
b423cd4c | 4358 | if (ch != '{') /* Only do special handling for \{ */ |
4359 | cupsFilePutChar(out, '\\'); | |
fa73b229 | 4360 | |
b423cd4c | 4361 | cupsFilePutChar(out, ch); |
4362 | } | |
4363 | else | |
4364 | cupsFilePutChar(out, ch); | |
fa73b229 | 4365 | |
b423cd4c | 4366 | cupsFileClose(in); |
fa73b229 | 4367 | |
b423cd4c | 4368 | kbytes = (cupsFileTell(out) + 1023) / 1024; |
fa73b229 | 4369 | |
f16ea703 MS |
4370 | job->koctets += kbytes; |
4371 | ||
4372 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL) | |
b423cd4c | 4373 | attr->values[0].integer += kbytes; |
4374 | ||
4375 | cupsFileClose(out); | |
4376 | ||
4377 | return (kbytes); | |
fa73b229 | 4378 | } |
4379 | ||
4380 | ||
4381 | /* | |
e67e2f9e | 4382 | * 'copy_file()' - Copy a PPD file... |
fa73b229 | 4383 | */ |
4384 | ||
b423cd4c | 4385 | static int /* O - 0 = success, -1 = error */ |
4386 | copy_file(const char *from, /* I - Source file */ | |
55516fd3 MS |
4387 | const char *to, /* I - Destination file */ |
4388 | mode_t mode) /* I - Permissions */ | |
fa73b229 | 4389 | { |
b423cd4c | 4390 | cups_file_t *src, /* Source file */ |
4391 | *dst; /* Destination file */ | |
4392 | int bytes; /* Bytes to read/write */ | |
4393 | char buffer[2048]; /* Copy buffer */ | |
fa73b229 | 4394 | |
4395 | ||
b423cd4c | 4396 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); |
4397 | ||
fa73b229 | 4398 | /* |
b423cd4c | 4399 | * Open the source and destination file for a copy... |
fa73b229 | 4400 | */ |
4401 | ||
b423cd4c | 4402 | if ((src = cupsFileOpen(from, "rb")) == NULL) |
4403 | return (-1); | |
fa73b229 | 4404 | |
55516fd3 | 4405 | if ((dst = cupsdCreateConfFile(to, mode)) == NULL) |
b423cd4c | 4406 | { |
4407 | cupsFileClose(src); | |
4408 | return (-1); | |
4409 | } | |
fa73b229 | 4410 | |
b423cd4c | 4411 | /* |
4412 | * Copy the source file to the destination... | |
4413 | */ | |
fa73b229 | 4414 | |
b423cd4c | 4415 | while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0) |
7e86f2f6 | 4416 | if (cupsFileWrite(dst, buffer, (size_t)bytes) < bytes) |
b423cd4c | 4417 | { |
4418 | cupsFileClose(src); | |
4419 | cupsFileClose(dst); | |
4420 | return (-1); | |
4421 | } | |
fa73b229 | 4422 | |
b423cd4c | 4423 | /* |
4424 | * Close both files and return... | |
4425 | */ | |
fa73b229 | 4426 | |
b423cd4c | 4427 | cupsFileClose(src); |
fa73b229 | 4428 | |
55516fd3 | 4429 | return (cupsdCloseCreatedConfFile(dst, to)); |
b423cd4c | 4430 | } |
fa73b229 | 4431 | |
fa73b229 | 4432 | |
b423cd4c | 4433 | /* |
4434 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
4435 | * as needed... | |
4436 | */ | |
e00b005a | 4437 | |
b423cd4c | 4438 | static int /* O - 0 = success, -1 = error */ |
4439 | copy_model(cupsd_client_t *con, /* I - Client connection */ | |
4440 | const char *from, /* I - Source file */ | |
4441 | const char *to) /* I - Destination file */ | |
4442 | { | |
f7deaa1a | 4443 | fd_set input; /* select() input set */ |
b423cd4c | 4444 | struct timeval timeout; /* select() timeout */ |
bc44d920 | 4445 | int maxfd; /* Max file descriptor for select() */ |
b423cd4c | 4446 | char tempfile[1024]; /* Temporary PPD file */ |
4447 | int tempfd; /* Temporary PPD file descriptor */ | |
4448 | int temppid; /* Process ID of cups-driverd */ | |
4449 | int temppipe[2]; /* Temporary pipes */ | |
4450 | char *argv[4], /* Command-line arguments */ | |
4451 | *envp[MAX_ENV]; /* Environment */ | |
4452 | cups_file_t *src, /* Source file */ | |
4453 | *dst; /* Destination file */ | |
2abf387c | 4454 | ppd_file_t *ppd; /* PPD file */ |
b423cd4c | 4455 | int bytes, /* Bytes from pipe */ |
4456 | total; /* Total bytes from pipe */ | |
2abf387c | 4457 | char buffer[2048]; /* Copy buffer */ |
b423cd4c | 4458 | int i; /* Looping var */ |
4459 | char option[PPD_MAX_NAME], /* Option name */ | |
4460 | choice[PPD_MAX_NAME]; /* Choice name */ | |
54afec33 | 4461 | ppd_size_t *size; /* Default size */ |
b423cd4c | 4462 | int num_defaults; /* Number of default options */ |
2abf387c | 4463 | cups_option_t *defaults; /* Default options */ |
b423cd4c | 4464 | char cups_protocol[PPD_MAX_LINE]; |
4465 | /* cupsProtocol attribute */ | |
e00b005a | 4466 | |
e00b005a | 4467 | |
e67e2f9e | 4468 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con, from, to); |
e00b005a | 4469 | |
b423cd4c | 4470 | /* |
4471 | * Run cups-driverd to get the PPD file... | |
4472 | */ | |
e00b005a | 4473 | |
b423cd4c | 4474 | argv[0] = "cups-driverd"; |
4475 | argv[1] = "cat"; | |
4476 | argv[2] = (char *)from; | |
4477 | argv[3] = NULL; | |
e00b005a | 4478 | |
b423cd4c | 4479 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
e00b005a | 4480 | |
b423cd4c | 4481 | snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin); |
996acce8 | 4482 | snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->number); |
b423cd4c | 4483 | tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
321d8d57 | 4484 | if (tempfd < 0 || cupsdOpenPipe(temppipe)) |
b423cd4c | 4485 | return (-1); |
4486 | ||
b423cd4c | 4487 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
4488 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
fa73b229 | 4489 | |
b423cd4c | 4490 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], |
38e73f87 | 4491 | -1, -1, 0, DefaultProfile, NULL, &temppid)) |
fa73b229 | 4492 | { |
b423cd4c | 4493 | close(tempfd); |
4494 | unlink(tempfile); | |
a4924f6c | 4495 | |
b423cd4c | 4496 | return (-1); |
fa73b229 | 4497 | } |
4498 | ||
b423cd4c | 4499 | close(temppipe[1]); |
fa73b229 | 4500 | |
b423cd4c | 4501 | /* |
4502 | * Wait up to 30 seconds for the PPD file to be copied... | |
4503 | */ | |
fa73b229 | 4504 | |
b423cd4c | 4505 | total = 0; |
fa73b229 | 4506 | |
b423cd4c | 4507 | if (temppipe[0] > CGIPipes[0]) |
4508 | maxfd = temppipe[0] + 1; | |
4509 | else | |
4510 | maxfd = CGIPipes[0] + 1; | |
ef416fc2 | 4511 | |
b423cd4c | 4512 | for (;;) |
4513 | { | |
4514 | /* | |
4515 | * See if we have data ready... | |
4516 | */ | |
ef416fc2 | 4517 | |
f7deaa1a | 4518 | FD_ZERO(&input); |
4519 | FD_SET(temppipe[0], &input); | |
4520 | FD_SET(CGIPipes[0], &input); | |
ef416fc2 | 4521 | |
b423cd4c | 4522 | timeout.tv_sec = 30; |
4523 | timeout.tv_usec = 0; | |
4524 | ||
f7deaa1a | 4525 | if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0) |
b423cd4c | 4526 | { |
4527 | if (errno == EINTR) | |
4528 | continue; | |
4529 | else | |
4530 | break; | |
4531 | } | |
4532 | else if (i == 0) | |
ef416fc2 | 4533 | { |
4534 | /* | |
b423cd4c | 4535 | * We have timed out... |
ef416fc2 | 4536 | */ |
4537 | ||
b423cd4c | 4538 | break; |
ef416fc2 | 4539 | } |
b423cd4c | 4540 | |
f7deaa1a | 4541 | if (FD_ISSET(temppipe[0], &input)) |
ef416fc2 | 4542 | { |
4543 | /* | |
b423cd4c | 4544 | * Read the PPD file from the pipe, and write it to the PPD file. |
ef416fc2 | 4545 | */ |
4546 | ||
b423cd4c | 4547 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) |
4548 | { | |
7e86f2f6 | 4549 | if (write(tempfd, buffer, (size_t)bytes) < bytes) |
b423cd4c | 4550 | break; |
ef416fc2 | 4551 | |
b423cd4c | 4552 | total += bytes; |
4553 | } | |
4554 | else | |
4555 | break; | |
ef416fc2 | 4556 | } |
b423cd4c | 4557 | |
f7deaa1a | 4558 | if (FD_ISSET(CGIPipes[0], &input)) |
b423cd4c | 4559 | cupsdUpdateCGI(); |
ef416fc2 | 4560 | } |
4561 | ||
b423cd4c | 4562 | close(temppipe[0]); |
4563 | close(tempfd); | |
ef416fc2 | 4564 | |
b423cd4c | 4565 | if (!total) |
ef416fc2 | 4566 | { |
b423cd4c | 4567 | /* |
4568 | * No data from cups-deviced... | |
4569 | */ | |
4570 | ||
4d301e69 | 4571 | cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file"); |
b423cd4c | 4572 | unlink(tempfile); |
4573 | return (-1); | |
ef416fc2 | 4574 | } |
4575 | ||
9c80ffa2 MS |
4576 | /* |
4577 | * Open the source file for a copy... | |
4578 | */ | |
4579 | ||
4580 | if ((src = cupsFileOpen(tempfile, "rb")) == NULL) | |
4581 | { | |
4582 | unlink(tempfile); | |
4583 | return (-1); | |
4584 | } | |
4585 | ||
b423cd4c | 4586 | /* |
4587 | * Read the source file and see what page sizes are supported... | |
4588 | */ | |
fa73b229 | 4589 | |
9c80ffa2 | 4590 | if ((ppd = _ppdOpen(src, _PPD_LOCALIZATION_NONE)) == NULL) |
b423cd4c | 4591 | { |
9c80ffa2 | 4592 | cupsFileClose(src); |
b423cd4c | 4593 | unlink(tempfile); |
4594 | return (-1); | |
4595 | } | |
fa73b229 | 4596 | |
ef416fc2 | 4597 | /* |
b423cd4c | 4598 | * Open the destination (if possible) and set the default options... |
ef416fc2 | 4599 | */ |
4600 | ||
b423cd4c | 4601 | num_defaults = 0; |
4602 | defaults = NULL; | |
4603 | cups_protocol[0] = '\0'; | |
ef416fc2 | 4604 | |
b423cd4c | 4605 | if ((dst = cupsFileOpen(to, "rb")) != NULL) |
ef416fc2 | 4606 | { |
4607 | /* | |
b423cd4c | 4608 | * Read all of the default lines from the old PPD... |
ef416fc2 | 4609 | */ |
4610 | ||
b423cd4c | 4611 | while (cupsFileGets(dst, buffer, sizeof(buffer))) |
4612 | if (!strncmp(buffer, "*Default", 8)) | |
4613 | { | |
4614 | /* | |
4615 | * Add the default option... | |
4616 | */ | |
ef416fc2 | 4617 | |
b423cd4c | 4618 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4619 | choice, sizeof(choice))) | |
2abf387c | 4620 | { |
4621 | ppd_option_t *ppdo; /* PPD option */ | |
4622 | ||
4623 | ||
4624 | /* | |
4625 | * Only add the default if the default hasn't already been | |
4626 | * set and the choice exists in the new PPD... | |
4627 | */ | |
4628 | ||
4629 | if (!cupsGetOption(option, num_defaults, defaults) && | |
4630 | (ppdo = ppdFindOption(ppd, option)) != NULL && | |
4631 | ppdFindChoice(ppdo, choice)) | |
4632 | num_defaults = cupsAddOption(option, choice, num_defaults, | |
b423cd4c | 4633 | &defaults); |
2abf387c | 4634 | } |
b423cd4c | 4635 | } |
4636 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
4637 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
ef416fc2 | 4638 | |
b423cd4c | 4639 | cupsFileClose(dst); |
ef416fc2 | 4640 | } |
54afec33 | 4641 | else if ((size = ppdPageSize(ppd, DefaultPaperSize)) != NULL) |
ef416fc2 | 4642 | { |
b423cd4c | 4643 | /* |
4644 | * Add the default media sizes... | |
b423cd4c | 4645 | */ |
4646 | ||
54afec33 | 4647 | num_defaults = cupsAddOption("PageSize", size->name, |
c5571a1d | 4648 | num_defaults, &defaults); |
54afec33 | 4649 | num_defaults = cupsAddOption("PageRegion", size->name, |
c5571a1d | 4650 | num_defaults, &defaults); |
54afec33 | 4651 | num_defaults = cupsAddOption("PaperDimension", size->name, |
c5571a1d | 4652 | num_defaults, &defaults); |
54afec33 | 4653 | num_defaults = cupsAddOption("ImageableArea", size->name, |
c5571a1d | 4654 | num_defaults, &defaults); |
ef416fc2 | 4655 | } |
4656 | ||
2abf387c | 4657 | ppdClose(ppd); |
4658 | ||
ef416fc2 | 4659 | /* |
b423cd4c | 4660 | * Open the destination file for a copy... |
ef416fc2 | 4661 | */ |
4662 | ||
112fc3c0 | 4663 | if ((dst = cupsdCreateConfFile(to, ConfigFilePerm)) == NULL) |
ef416fc2 | 4664 | { |
2abf387c | 4665 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 4666 | cupsFileClose(src); |
4667 | unlink(tempfile); | |
4668 | return (-1); | |
ef416fc2 | 4669 | } |
4670 | ||
4671 | /* | |
b423cd4c | 4672 | * Copy the source file to the destination... |
ef416fc2 | 4673 | */ |
4674 | ||
9c80ffa2 MS |
4675 | cupsFileRewind(src); |
4676 | ||
b423cd4c | 4677 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
ef416fc2 | 4678 | { |
b423cd4c | 4679 | if (!strncmp(buffer, "*Default", 8)) |
ef416fc2 | 4680 | { |
b423cd4c | 4681 | /* |
4682 | * Check for an previous default option choice... | |
4683 | */ | |
ef416fc2 | 4684 | |
b423cd4c | 4685 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4686 | choice, sizeof(choice))) | |
ef416fc2 | 4687 | { |
2abf387c | 4688 | const char *val; /* Default option value */ |
ef416fc2 | 4689 | |
2abf387c | 4690 | |
4691 | if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL) | |
4692 | { | |
4693 | /* | |
4694 | * Substitute the previous choice... | |
4695 | */ | |
4696 | ||
4697 | snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val); | |
4698 | } | |
b423cd4c | 4699 | } |
ef416fc2 | 4700 | } |
b423cd4c | 4701 | |
4702 | cupsFilePrintf(dst, "%s\n", buffer); | |
ef416fc2 | 4703 | } |
4704 | ||
b423cd4c | 4705 | if (cups_protocol[0]) |
4706 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
4707 | ||
2abf387c | 4708 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 4709 | |
ef416fc2 | 4710 | /* |
b423cd4c | 4711 | * Close both files and return... |
ef416fc2 | 4712 | */ |
4713 | ||
b423cd4c | 4714 | cupsFileClose(src); |
4715 | ||
4716 | unlink(tempfile); | |
4717 | ||
112fc3c0 | 4718 | return (cupsdCloseCreatedConfFile(dst, to)); |
b423cd4c | 4719 | } |
4720 | ||
4721 | ||
4722 | /* | |
4723 | * 'copy_job_attrs()' - Copy job attributes. | |
4724 | */ | |
ef416fc2 | 4725 | |
b423cd4c | 4726 | static void |
4727 | copy_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
4728 | cupsd_job_t *job, /* I - Job */ | |
10d09e33 MS |
4729 | cups_array_t *ra, /* I - Requested attributes array */ |
4730 | cups_array_t *exclude) /* I - Private attributes array */ | |
b423cd4c | 4731 | { |
4732 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
ef416fc2 | 4733 | |
ef416fc2 | 4734 | |
4735 | /* | |
b423cd4c | 4736 | * Send the requested attributes for each job... |
ef416fc2 | 4737 | */ |
4738 | ||
10d09e33 MS |
4739 | if (!cupsArrayFind(exclude, "all")) |
4740 | { | |
12f89d24 MS |
4741 | if ((!exclude || !cupsArrayFind(exclude, "number-of-documents")) && |
4742 | (!ra || cupsArrayFind(ra, "number-of-documents"))) | |
10d09e33 | 4743 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, |
12f89d24 | 4744 | "number-of-documents", job->num_files); |
2e4ff8af | 4745 | |
10d09e33 MS |
4746 | if ((!exclude || !cupsArrayFind(exclude, "job-media-progress")) && |
4747 | (!ra || cupsArrayFind(ra, "job-media-progress"))) | |
4748 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
4749 | "job-media-progress", job->progress); | |
9a4f8274 | 4750 | |
10d09e33 MS |
4751 | if ((!exclude || !cupsArrayFind(exclude, "job-more-info")) && |
4752 | (!ra || cupsArrayFind(ra, "job-more-info"))) | |
88f9aafc MS |
4753 | { |
4754 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "http", | |
f2534050 | 4755 | NULL, con->clientname, con->clientport, "/jobs/%d", |
88f9aafc | 4756 | job->id); |
10d09e33 MS |
4757 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, |
4758 | "job-more-info", NULL, job_uri); | |
88f9aafc | 4759 | } |
ef416fc2 | 4760 | |
10d09e33 MS |
4761 | if (job->state_value > IPP_JOB_PROCESSING && |
4762 | (!exclude || !cupsArrayFind(exclude, "job-preserved")) && | |
4763 | (!ra || cupsArrayFind(ra, "job-preserved"))) | |
4764 | ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved", | |
4765 | job->num_files > 0); | |
ef416fc2 | 4766 | |
10d09e33 MS |
4767 | if ((!exclude || !cupsArrayFind(exclude, "job-printer-up-time")) && |
4768 | (!ra || cupsArrayFind(ra, "job-printer-up-time"))) | |
4769 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
4770 | "job-printer-up-time", time(NULL)); | |
4771 | } | |
ef416fc2 | 4772 | |
88f9aafc MS |
4773 | if (!ra || cupsArrayFind(ra, "job-printer-uri")) |
4774 | { | |
4775 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, | |
f2534050 | 4776 | con->clientname, con->clientport, |
a2326b5b MS |
4777 | (job->dtype & CUPS_PRINTER_CLASS) ? "/classes/%s" : |
4778 | "/printers/%s", | |
88f9aafc MS |
4779 | job->dest); |
4780 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
4781 | "job-printer-uri", NULL, job_uri); | |
4782 | } | |
4783 | ||
b423cd4c | 4784 | if (!ra || cupsArrayFind(ra, "job-uri")) |
88f9aafc MS |
4785 | { |
4786 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, | |
f2534050 | 4787 | con->clientname, con->clientport, "/jobs/%d", |
88f9aafc | 4788 | job->id); |
b423cd4c | 4789 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, |
4790 | "job-uri", NULL, job_uri); | |
88f9aafc | 4791 | } |
ef416fc2 | 4792 | |
f16ea703 MS |
4793 | if (job->attrs) |
4794 | { | |
4795 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0, exclude); | |
4796 | } | |
4797 | else | |
4798 | { | |
4799 | /* | |
4800 | * Generate attributes from the job structure... | |
4801 | */ | |
4802 | ||
9b4bd602 MS |
4803 | if (job->completed_time && (!ra || cupsArrayFind(ra, "date-time-at-completed"))) |
4804 | ippAddDate(con->response, IPP_TAG_JOB, "date-time-at-completed", ippTimeToDate(job->completed_time)); | |
4805 | ||
4806 | if (job->creation_time && (!ra || cupsArrayFind(ra, "date-time-at-creation"))) | |
4807 | ippAddDate(con->response, IPP_TAG_JOB, "date-time-at-creation", ippTimeToDate(job->creation_time)); | |
4808 | ||
f16ea703 MS |
4809 | if (!ra || cupsArrayFind(ra, "job-id")) |
4810 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); | |
4811 | ||
4812 | if (!ra || cupsArrayFind(ra, "job-k-octets")) | |
4813 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", job->koctets); | |
4814 | ||
4815 | if (job->name && (!ra || cupsArrayFind(ra, "job-name"))) | |
59a8560a | 4816 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, job->name); |
f16ea703 MS |
4817 | |
4818 | if (job->username && (!ra || cupsArrayFind(ra, "job-originating-user-name"))) | |
59a8560a | 4819 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "job-originating-user-name", NULL, job->username); |
f16ea703 MS |
4820 | |
4821 | if (!ra || cupsArrayFind(ra, "job-state")) | |
4822 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value); | |
4823 | ||
4824 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) | |
4825 | { | |
4826 | switch (job->state_value) | |
4827 | { | |
4828 | default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */ | |
4829 | break; | |
4830 | case IPP_JSTATE_ABORTED : | |
4831 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-aborted-by-system"); | |
4832 | break; | |
4833 | case IPP_JSTATE_CANCELED : | |
4834 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-canceled-by-user"); | |
4835 | break; | |
4836 | case IPP_JSTATE_COMPLETED : | |
4837 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-completed-successfully"); | |
4838 | break; | |
4839 | } | |
4840 | } | |
4841 | ||
4842 | if (job->completed_time && (!ra || cupsArrayFind(ra, "time-at-completed"))) | |
4843 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-completed", (int)job->completed_time); | |
4844 | ||
9b4bd602 | 4845 | if (job->creation_time && (!ra || cupsArrayFind(ra, "time-at-creation"))) |
f16ea703 MS |
4846 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", (int)job->creation_time); |
4847 | } | |
b423cd4c | 4848 | } |
ef416fc2 | 4849 | |
ef416fc2 | 4850 | |
b423cd4c | 4851 | /* |
4852 | * 'copy_printer_attrs()' - Copy printer attributes. | |
4853 | */ | |
ef416fc2 | 4854 | |
b423cd4c | 4855 | static void |
4856 | copy_printer_attrs( | |
4857 | cupsd_client_t *con, /* I - Client connection */ | |
4858 | cupsd_printer_t *printer, /* I - Printer */ | |
4859 | cups_array_t *ra) /* I - Requested attributes array */ | |
4860 | { | |
2fa1ba3c MS |
4861 | char uri[HTTP_MAX_URI]; /* URI value */ |
4862 | time_t curtime; /* Current time */ | |
4863 | int i; /* Looping var */ | |
4864 | int is_encrypted = httpIsEncrypted(con->http); | |
4865 | /* Is the connection encrypted? */ | |
ef416fc2 | 4866 | |
ef416fc2 | 4867 | |
b423cd4c | 4868 | /* |
4869 | * Copy the printer attributes to the response using requested-attributes | |
4870 | * and document-format attributes that may be provided by the client. | |
4871 | */ | |
ef416fc2 | 4872 | |
ca75cc13 MS |
4873 | _cupsRWLockRead(&printer->lock); |
4874 | ||
b423cd4c | 4875 | curtime = time(NULL); |
ef416fc2 | 4876 | |
5a738aea | 4877 | if (!ra || cupsArrayFind(ra, "marker-change-time")) |
2fa1ba3c | 4878 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "marker-change-time", printer->marker_time); |
5a738aea | 4879 | |
2fa1ba3c | 4880 | if (printer->num_printers > 0 && (!ra || cupsArrayFind(ra, "member-uris"))) |
dd1abb6b MS |
4881 | { |
4882 | ipp_attribute_t *member_uris; /* member-uris attribute */ | |
4883 | cupsd_printer_t *p2; /* Printer in class */ | |
4884 | ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */ | |
4885 | ||
4886 | ||
2fa1ba3c | 4887 | if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", printer->num_printers, NULL, NULL)) != NULL) |
dd1abb6b MS |
4888 | { |
4889 | for (i = 0; i < printer->num_printers; i ++) | |
4890 | { | |
4891 | p2 = printer->printers[i]; | |
4892 | ||
2fa1ba3c MS |
4893 | if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported", IPP_TAG_URI)) != NULL) |
4894 | { | |
4895 | member_uris->values[i].string.text = _cupsStrRetain(p2_uri->values[0].string.text); | |
4896 | } | |
dd1abb6b MS |
4897 | else |
4898 | { | |
2fa1ba3c MS |
4899 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "ipps" : "ipp", NULL, con->clientname, con->clientport, (p2->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", p2->name); |
4900 | member_uris->values[i].string.text = _cupsStrAlloc(uri); | |
dd1abb6b MS |
4901 | } |
4902 | } | |
4903 | } | |
4904 | } | |
4905 | ||
323c5de1 | 4906 | if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert"))) |
2fa1ba3c | 4907 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING, "printer-alert", NULL, printer->alert); |
323c5de1 | 4908 | |
2fa1ba3c MS |
4909 | if (printer->alert_description && (!ra || cupsArrayFind(ra, "printer-alert-description"))) |
4910 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-alert-description", NULL, printer->alert_description); | |
323c5de1 | 4911 | |
9b4bd602 MS |
4912 | if (!ra || cupsArrayFind(ra, "printer-config-change-date-time")) |
4913 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-config-change-date-time", ippTimeToDate(printer->config_time)); | |
4914 | ||
4915 | if (!ra || cupsArrayFind(ra, "printer-config-change-time")) | |
2fa1ba3c | 4916 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-config-change-time", printer->config_time); |
9b4bd602 | 4917 | |
b423cd4c | 4918 | if (!ra || cupsArrayFind(ra, "printer-current-time")) |
2fa1ba3c | 4919 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", ippTimeToDate(curtime)); |
ef416fc2 | 4920 | |
37e7e6e0 | 4921 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
d9bca400 MS |
4922 | if (!ra || cupsArrayFind(ra, "printer-dns-sd-name")) |
4923 | { | |
4924 | if (printer->reg_name) | |
2fa1ba3c | 4925 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-dns-sd-name", NULL, printer->reg_name); |
d9bca400 | 4926 | else |
2fa1ba3c | 4927 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE, "printer-dns-sd-name", 0); |
d9bca400 | 4928 | } |
37e7e6e0 | 4929 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
d9bca400 | 4930 | |
b423cd4c | 4931 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) |
2fa1ba3c | 4932 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-error-policy", NULL, printer->error_policy); |
ef416fc2 | 4933 | |
f11a948a MS |
4934 | if (!ra || cupsArrayFind(ra, "printer-error-policy-supported")) |
4935 | { | |
4936 | static const char * const errors[] =/* printer-error-policy-supported values */ | |
2fa1ba3c MS |
4937 | { |
4938 | "abort-job", | |
4939 | "retry-current-job", | |
4940 | "retry-job", | |
4941 | "stop-printer" | |
4942 | }; | |
f11a948a | 4943 | |
a2326b5b | 4944 | if (printer->type & CUPS_PRINTER_CLASS) |
59a8560a | 4945 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_NAME), "printer-error-policy-supported", NULL, "retry-current-job"); |
f11a948a | 4946 | else |
59a8560a | 4947 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_NAME), "printer-error-policy-supported", sizeof(errors) / sizeof(errors[0]), NULL, errors); |
f11a948a MS |
4948 | } |
4949 | ||
7cf5915e MS |
4950 | if (!ra || cupsArrayFind(ra, "printer-icons")) |
4951 | { | |
2fa1ba3c MS |
4952 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, "/icons/%s.png", printer->name); |
4953 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons", NULL, uri); | |
4954 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", uri); | |
7cf5915e MS |
4955 | } |
4956 | ||
b423cd4c | 4957 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) |
7e86f2f6 | 4958 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting); |
ef416fc2 | 4959 | |
b423cd4c | 4960 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) |
7e86f2f6 | 4961 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", (char)printer->shared); |
ef416fc2 | 4962 | |
7ae00c35 MS |
4963 | if (!ra || cupsArrayFind(ra, "printer-is-temporary")) |
4964 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-temporary", (char)printer->temporary); | |
4965 | ||
a2326b5b | 4966 | if (!ra || cupsArrayFind(ra, "printer-more-info")) |
58dc1933 | 4967 | { |
2fa1ba3c MS |
4968 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, (printer->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", printer->name); |
4969 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info", NULL, uri); | |
58dc1933 MS |
4970 | } |
4971 | ||
b423cd4c | 4972 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) |
2fa1ba3c | 4973 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-op-policy", NULL, printer->op_policy); |
b423cd4c | 4974 | |
4975 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
2fa1ba3c | 4976 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state); |
b423cd4c | 4977 | |
9b4bd602 MS |
4978 | if (!ra || cupsArrayFind(ra, "printer-state-change-date-time")) |
4979 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-state-change-date-time", ippTimeToDate(printer->state_time)); | |
4980 | ||
b423cd4c | 4981 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) |
2fa1ba3c | 4982 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-state-change-time", printer->state_time); |
f7deaa1a | 4983 | |
b423cd4c | 4984 | if (!ra || cupsArrayFind(ra, "printer-state-message")) |
2fa1ba3c | 4985 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message", NULL, printer->state_message); |
ef416fc2 | 4986 | |
b423cd4c | 4987 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) |
4988 | add_printer_state_reasons(con, printer); | |
ef416fc2 | 4989 | |
2fa1ba3c MS |
4990 | if (!ra || cupsArrayFind(ra, "printer-strings-uri")) |
4991 | { | |
4992 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, "/strings/%s.strings", printer->name); | |
4993 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-strings-uri", NULL, uri); | |
4994 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-strings-uri=\"%s\"", uri); | |
4995 | } | |
4996 | ||
b423cd4c | 4997 | if (!ra || cupsArrayFind(ra, "printer-type")) |
4998 | { | |
7e86f2f6 | 4999 | cups_ptype_t type; /* printer-type value */ |
ef416fc2 | 5000 | |
ef416fc2 | 5001 | /* |
b423cd4c | 5002 | * Add the CUPS-specific printer-type attribute... |
ef416fc2 | 5003 | */ |
5004 | ||
09a101d6 | 5005 | type = printer->type; |
b423cd4c | 5006 | |
5007 | if (printer == DefaultPrinter) | |
5008 | type |= CUPS_PRINTER_DEFAULT; | |
5009 | ||
5010 | if (!printer->accepting) | |
5011 | type |= CUPS_PRINTER_REJECTING; | |
5012 | ||
5013 | if (!printer->shared) | |
5014 | type |= CUPS_PRINTER_NOT_SHARED; | |
5015 | ||
7e86f2f6 | 5016 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", (int)type); |
ef416fc2 | 5017 | } |
ef416fc2 | 5018 | |
b423cd4c | 5019 | if (!ra || cupsArrayFind(ra, "printer-up-time")) |
2fa1ba3c | 5020 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-up-time", curtime); |
ef416fc2 | 5021 | |
a2326b5b | 5022 | if (!ra || cupsArrayFind(ra, "printer-uri-supported")) |
ef416fc2 | 5023 | { |
2fa1ba3c MS |
5024 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "ipps" : "ipp", NULL, con->clientname, con->clientport, (printer->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", printer->name); |
5025 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri); | |
5026 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", uri); | |
b423cd4c | 5027 | } |
ef416fc2 | 5028 | |
b423cd4c | 5029 | if (!ra || cupsArrayFind(ra, "queued-job-count")) |
5030 | add_queued_job_count(con, printer); | |
ef416fc2 | 5031 | |
10d09e33 | 5032 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0, NULL); |
61cf44e2 | 5033 | if (printer->ppd_attrs) |
10d09e33 MS |
5034 | copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL); |
5035 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL); | |
ca75cc13 MS |
5036 | |
5037 | _cupsRWUnlock(&printer->lock); | |
b423cd4c | 5038 | } |
ef416fc2 | 5039 | |
ef416fc2 | 5040 | |
b423cd4c | 5041 | /* |
5042 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
5043 | */ | |
ef416fc2 | 5044 | |
b423cd4c | 5045 | static void |
5046 | copy_subscription_attrs( | |
5047 | cupsd_client_t *con, /* I - Client connection */ | |
5048 | cupsd_subscription_t *sub, /* I - Subscription */ | |
10d09e33 MS |
5049 | cups_array_t *ra, /* I - Requested attributes array */ |
5050 | cups_array_t *exclude) /* I - Private attributes array */ | |
b423cd4c | 5051 | { |
5052 | ipp_attribute_t *attr; /* Current attribute */ | |
5053 | char printer_uri[HTTP_MAX_URI]; | |
5054 | /* Printer URI */ | |
5055 | int count; /* Number of events */ | |
5056 | unsigned mask; /* Current event mask */ | |
5057 | const char *name; /* Current event name */ | |
ef416fc2 | 5058 | |
ef416fc2 | 5059 | |
10d09e33 MS |
5060 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
5061 | "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)", | |
5062 | con, sub, ra, exclude); | |
5063 | ||
b423cd4c | 5064 | /* |
5065 | * Copy the subscription attributes to the response using the | |
5066 | * requested-attributes attribute that may be provided by the client. | |
5067 | */ | |
ef416fc2 | 5068 | |
10d09e33 | 5069 | if (!exclude || !cupsArrayFind(exclude, "all")) |
b423cd4c | 5070 | { |
10d09e33 MS |
5071 | if ((!exclude || !cupsArrayFind(exclude, "notify-events")) && |
5072 | (!ra || cupsArrayFind(ra, "notify-events"))) | |
b423cd4c | 5073 | { |
10d09e33 | 5074 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_subscription_attrs: notify-events"); |
ef416fc2 | 5075 | |
10d09e33 MS |
5076 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) |
5077 | { | |
5078 | /* | |
5079 | * Simple event list... | |
5080 | */ | |
5081 | ||
59a8560a | 5082 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "notify-events", NULL, name); |
10d09e33 MS |
5083 | } |
5084 | else | |
5085 | { | |
5086 | /* | |
5087 | * Complex event list... | |
5088 | */ | |
ef416fc2 | 5089 | |
10d09e33 MS |
5090 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5091 | if (sub->mask & mask) | |
5092 | count ++; | |
ef416fc2 | 5093 | |
59a8560a | 5094 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "notify-events", count, NULL, NULL); |
ef416fc2 | 5095 | |
10d09e33 MS |
5096 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5097 | if (sub->mask & mask) | |
5098 | { | |
59a8560a | 5099 | attr->values[count].string.text = (char *)cupsdEventName((cupsd_eventmask_t)mask); |
ef416fc2 | 5100 | |
10d09e33 MS |
5101 | count ++; |
5102 | } | |
5103 | } | |
ef416fc2 | 5104 | } |
10d09e33 MS |
5105 | |
5106 | if ((!exclude || !cupsArrayFind(exclude, "notify-lease-duration")) && | |
5107 | (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration")))) | |
5108 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5109 | "notify-lease-duration", sub->lease); | |
5110 | ||
5111 | if ((!exclude || !cupsArrayFind(exclude, "notify-recipient-uri")) && | |
5112 | (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri")))) | |
5113 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5114 | "notify-recipient-uri", NULL, sub->recipient); | |
5115 | else if ((!exclude || !cupsArrayFind(exclude, "notify-pull-method")) && | |
5116 | (!ra || cupsArrayFind(ra, "notify-pull-method"))) | |
5117 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
5118 | "notify-pull-method", NULL, "ippget"); | |
5119 | ||
5120 | if ((!exclude || !cupsArrayFind(exclude, "notify-subscriber-user-name")) && | |
5121 | (!ra || cupsArrayFind(ra, "notify-subscriber-user-name"))) | |
5122 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
5123 | "notify-subscriber-user-name", NULL, sub->owner); | |
5124 | ||
5125 | if ((!exclude || !cupsArrayFind(exclude, "notify-time-interval")) && | |
5126 | (!ra || cupsArrayFind(ra, "notify-time-interval"))) | |
5127 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5128 | "notify-time-interval", sub->interval); | |
5129 | ||
5130 | if (sub->user_data_len > 0 && | |
5131 | (!exclude || !cupsArrayFind(exclude, "notify-user-data")) && | |
5132 | (!ra || cupsArrayFind(ra, "notify-user-data"))) | |
5133 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
5134 | sub->user_data, sub->user_data_len); | |
b423cd4c | 5135 | } |
ef416fc2 | 5136 | |
b423cd4c | 5137 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) |
5138 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5139 | "notify-job-id", sub->job->id); | |
ef416fc2 | 5140 | |
b423cd4c | 5141 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) |
5142 | { | |
5143 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
f2534050 | 5144 | "ipp", NULL, con->clientname, con->clientport, |
b423cd4c | 5145 | "/printers/%s", sub->dest->name); |
5146 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5147 | "notify-printer-uri", NULL, printer_uri); | |
ef416fc2 | 5148 | } |
ef416fc2 | 5149 | |
b423cd4c | 5150 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) |
5151 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5152 | "notify-subscription-id", sub->id); | |
b423cd4c | 5153 | } |
ef416fc2 | 5154 | |
ef416fc2 | 5155 | |
b423cd4c | 5156 | /* |
5157 | * 'create_job()' - Print a file to a printer or class. | |
5158 | */ | |
5159 | ||
5160 | static void | |
5161 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
5162 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5163 | { | |
a29fd7dd | 5164 | int i; /* Looping var */ |
f7deaa1a | 5165 | cupsd_printer_t *printer; /* Printer */ |
5166 | cupsd_job_t *job; /* New job */ | |
a29fd7dd MS |
5167 | static const char * const forbidden_attrs[] = |
5168 | { /* List of forbidden attributes */ | |
5169 | "compression", | |
5170 | "document-format", | |
5171 | "document-name", | |
5172 | "document-natural-language" | |
5173 | }; | |
b423cd4c | 5174 | |
ef416fc2 | 5175 | |
b423cd4c | 5176 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, |
996acce8 | 5177 | con->number, uri->values[0].string.text); |
ef416fc2 | 5178 | |
f7deaa1a | 5179 | /* |
5180 | * Is the destination valid? | |
5181 | */ | |
5182 | ||
5183 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
5184 | { | |
5185 | /* | |
5186 | * Bad URI... | |
5187 | */ | |
5188 | ||
5189 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 5190 | _("The printer or class does not exist.")); |
f7deaa1a | 5191 | return; |
5192 | } | |
5193 | ||
a29fd7dd MS |
5194 | /* |
5195 | * Check for invalid Create-Job attributes and log a warning or error depending | |
5196 | * on whether cupsd is running in "strict conformance" mode... | |
5197 | */ | |
5198 | ||
5199 | for (i = 0; | |
5200 | i < (int)(sizeof(forbidden_attrs) / sizeof(forbidden_attrs[0])); | |
5201 | i ++) | |
5202 | if (ippFindAttribute(con->request, forbidden_attrs[i], IPP_TAG_ZERO)) | |
5203 | { | |
5204 | if (StrictConformance) | |
5205 | { | |
5206 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5207 | _("The '%s' operation attribute cannot be supplied in a " | |
5208 | "Create-Job request."), forbidden_attrs[i]); | |
5209 | return; | |
5210 | } | |
5211 | ||
5212 | cupsdLogMessage(CUPSD_LOG_WARN, | |
5213 | "Unexpected '%s' operation attribute in a Create-Job " | |
5214 | "request.", forbidden_attrs[i]); | |
5215 | } | |
5216 | ||
ef416fc2 | 5217 | /* |
b423cd4c | 5218 | * Create the job object... |
ef416fc2 | 5219 | */ |
5220 | ||
f7deaa1a | 5221 | if ((job = add_job(con, printer, NULL)) == NULL) |
b423cd4c | 5222 | return; |
ef416fc2 | 5223 | |
09a101d6 | 5224 | job->pending_timeout = 1; |
5225 | ||
ef416fc2 | 5226 | /* |
5227 | * Save and log the job... | |
5228 | */ | |
f7deaa1a | 5229 | |
75bd9771 MS |
5230 | cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".", |
5231 | job->dest, job->username); | |
ef416fc2 | 5232 | } |
5233 | ||
5234 | ||
95ece0cb MS |
5235 | /* |
5236 | * 'create_local_bg_thread()' - Background thread for creating a local print queue. | |
5237 | */ | |
5238 | ||
5239 | static void * /* O - Exit status */ | |
5240 | create_local_bg_thread( | |
5241 | cupsd_printer_t *printer) /* I - Printer */ | |
5242 | { | |
5243 | cups_file_t *from, /* Source file */ | |
5244 | *to; /* Destination file */ | |
5245 | char fromppd[1024], /* Source PPD */ | |
5246 | toppd[1024], /* Destination PPD */ | |
5247 | scheme[32], /* URI scheme */ | |
5248 | userpass[256], /* User:pass */ | |
5249 | host[256], /* Hostname */ | |
5250 | resource[1024], /* Resource path */ | |
5251 | line[1024]; /* Line from PPD */ | |
5252 | int port; /* Port number */ | |
5253 | http_encryption_t encryption; /* Type of encryption to use */ | |
5254 | http_t *http; /* Connection to printer */ | |
5255 | ipp_t *request, /* Request to printer */ | |
5256 | *response; /* Response from printer */ | |
5257 | ipp_attribute_t *attr; /* Attribute in response */ | |
7d26bf7d | 5258 | ipp_status_t status; /* Status code */ |
95ece0cb MS |
5259 | |
5260 | ||
5261 | /* | |
5262 | * Try connecting to the printer... | |
5263 | */ | |
5264 | ||
c585706d MS |
5265 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Generating PPD file from \"%s\"...", printer->name, printer->device_uri); |
5266 | ||
95ece0cb | 5267 | 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 |
5268 | { |
5269 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Bad device URI \"%s\".", printer->name, printer->device_uri); | |
95ece0cb | 5270 | return (NULL); |
c585706d | 5271 | } |
95ece0cb MS |
5272 | |
5273 | if (!strcmp(scheme, "ipps") || port == 443) | |
5274 | encryption = HTTP_ENCRYPTION_ALWAYS; | |
5275 | else | |
5276 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; | |
5277 | ||
5278 | if ((http = httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL)) == NULL) | |
c585706d MS |
5279 | { |
5280 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to connect to %s:%d: %s", printer->name, host, port, cupsLastErrorString()); | |
95ece0cb | 5281 | return (NULL); |
c585706d | 5282 | } |
95ece0cb MS |
5283 | |
5284 | /* | |
5285 | * Query the printer for its capabilities... | |
5286 | */ | |
5287 | ||
c585706d MS |
5288 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer->name, host, port); |
5289 | ||
95ece0cb | 5290 | request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); |
7d26bf7d | 5291 | ippSetVersion(request, 2, 0); |
95ece0cb MS |
5292 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri); |
5293 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all"); | |
5294 | ||
5295 | response = cupsDoRequest(http, request, resource); | |
7d26bf7d | 5296 | status = cupsLastError(); |
95ece0cb | 5297 | |
7d26bf7d MS |
5298 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString()); |
5299 | ||
5300 | if (status == IPP_STATUS_ERROR_BAD_REQUEST || status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
5301 | { | |
5302 | /* | |
5303 | * Try request using IPP/1.1, in case we are talking to an old CUPS server or | |
5304 | * printer... | |
5305 | */ | |
5306 | ||
5307 | ippDelete(response); | |
5308 | ||
5309 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Re-sending Get-Printer-Attributes request using IPP/1.1...", printer->name); | |
5310 | ||
5311 | request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); | |
5312 | ippSetVersion(request, 1, 1); | |
5313 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri); | |
5314 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all"); | |
5315 | ||
5316 | response = cupsDoRequest(http, request, resource); | |
5317 | ||
5318 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: IPP/1.1 Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString()); | |
5319 | } | |
c585706d | 5320 | |
95ece0cb MS |
5321 | // TODO: Grab printer icon file... |
5322 | httpClose(http); | |
5323 | ||
5324 | /* | |
5325 | * Write the PPD for the queue... | |
5326 | */ | |
5327 | ||
5328 | if (_ppdCreateFromIPP(fromppd, sizeof(fromppd), response)) | |
5329 | { | |
656c109a MS |
5330 | _cupsRWLockWrite(&printer->lock); |
5331 | ||
95ece0cb MS |
5332 | if ((!printer->info || !*(printer->info)) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL) |
5333 | cupsdSetString(&printer->info, ippGetString(attr, 0, NULL)); | |
5334 | ||
5335 | if ((!printer->location || !*(printer->location)) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL) | |
5336 | cupsdSetString(&printer->location, ippGetString(attr, 0, NULL)); | |
5337 | ||
5338 | if ((!printer->geo_location || !*(printer->geo_location)) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL) | |
5339 | cupsdSetString(&printer->geo_location, ippGetString(attr, 0, NULL)); | |
5340 | ||
656c109a MS |
5341 | _cupsRWUnlock(&printer->lock); |
5342 | ||
95ece0cb | 5343 | if ((from = cupsFileOpen(fromppd, "r")) == NULL) |
c585706d MS |
5344 | { |
5345 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to read generated PPD: %s", printer->name, strerror(errno)); | |
95ece0cb | 5346 | return (NULL); |
c585706d | 5347 | } |
95ece0cb MS |
5348 | |
5349 | snprintf(toppd, sizeof(toppd), "%s/ppd/%s.ppd", ServerRoot, printer->name); | |
5350 | if ((to = cupsdCreateConfFile(toppd, ConfigFilePerm)) == NULL) | |
5351 | { | |
c585706d | 5352 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to create PPD for printer: %s", printer->name, strerror(errno)); |
95ece0cb MS |
5353 | cupsFileClose(from); |
5354 | return (NULL); | |
5355 | } | |
5356 | ||
5357 | while (cupsFileGets(from, line, sizeof(line))) | |
5358 | cupsFilePrintf(to, "%s\n", line); | |
5359 | ||
5360 | cupsFileClose(from); | |
5361 | if (!cupsdCloseCreatedConfFile(to, toppd)) | |
5362 | { | |
c585706d MS |
5363 | printer->config_time = time(NULL); |
5364 | printer->state = IPP_PSTATE_IDLE; | |
5365 | printer->accepting = 1; | |
95ece0cb MS |
5366 | |
5367 | cupsdSetPrinterAttrs(printer); | |
c585706d MS |
5368 | |
5369 | cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, "Printer \"%s\" is now available.", printer->name); | |
5370 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" is now available.", printer->name); | |
95ece0cb MS |
5371 | } |
5372 | } | |
c585706d | 5373 | else |
956bb127 | 5374 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString()); |
95ece0cb MS |
5375 | |
5376 | return (NULL); | |
5377 | } | |
5378 | ||
5379 | ||
7ae00c35 MS |
5380 | /* |
5381 | * 'create_local_printer()' - Create a local (temporary) print queue. | |
5382 | */ | |
5383 | ||
5384 | static void | |
5385 | create_local_printer( | |
5386 | cupsd_client_t *con) /* I - Client connection */ | |
5387 | { | |
95ece0cb MS |
5388 | ipp_attribute_t *device_uri, /* device-uri attribute */ |
5389 | *printer_geo_location, /* printer-geo-location attribute */ | |
5390 | *printer_info, /* printer-info attribute */ | |
5391 | *printer_location, /* printer-location attribute */ | |
5392 | *printer_name; /* printer-name attribute */ | |
5393 | cupsd_printer_t *printer; /* New printer */ | |
5394 | http_status_t status; /* Policy status */ | |
5395 | char name[128], /* Sanitized printer name */ | |
5396 | *nameptr, /* Pointer into name */ | |
5397 | uri[1024]; /* printer-uri-supported value */ | |
5398 | const char *ptr; /* Pointer into attribute value */ | |
5399 | ||
5400 | ||
5401 | /* | |
5402 | * Require local access to create a local printer... | |
5403 | */ | |
5404 | ||
5405 | if (!httpAddrLocalhost(httpGetAddress(con->http))) | |
5406 | { | |
5407 | send_ipp_status(con, IPP_STATUS_ERROR_FORBIDDEN, _("Only local users can create a local printer.")); | |
5408 | return; | |
5409 | } | |
5410 | ||
5411 | /* | |
5412 | * Check any other policy limits... | |
5413 | */ | |
5414 | ||
5415 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5416 | { | |
5417 | send_http_error(con, status, NULL); | |
5418 | return; | |
5419 | } | |
5420 | ||
5421 | /* | |
5422 | * Grab needed attributes... | |
5423 | */ | |
5424 | ||
5425 | if ((printer_name = ippFindAttribute(con->request, "printer-name", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(printer_name) != IPP_TAG_PRINTER || ippGetValueTag(printer_name) != IPP_TAG_NAME) | |
5426 | { | |
5427 | if (!printer_name) | |
5428 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "printer-name"); | |
5429 | else if (ippGetGroupTag(printer_name) != IPP_TAG_PRINTER) | |
5430 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "printer-name"); | |
5431 | else | |
5432 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "printer-name"); | |
5433 | ||
5434 | return; | |
5435 | } | |
5436 | ||
5437 | for (nameptr = name, ptr = ippGetString(printer_name, 0, NULL); *ptr && nameptr < (name + sizeof(name) - 1); ptr ++) | |
5438 | { | |
5439 | /* | |
5440 | * Sanitize the printer name... | |
5441 | */ | |
5442 | ||
5443 | if (_cups_isalnum(*ptr)) | |
5444 | *nameptr++ = *ptr; | |
5445 | else if (nameptr == name || nameptr[-1] != '_') | |
5446 | *nameptr++ = '_'; | |
5447 | } | |
5448 | ||
5449 | *nameptr = '\0'; | |
5450 | ||
5451 | if ((device_uri = ippFindAttribute(con->request, "device-uri", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(device_uri) != IPP_TAG_PRINTER || ippGetValueTag(device_uri) != IPP_TAG_URI) | |
5452 | { | |
5453 | if (!device_uri) | |
5454 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "device-uri"); | |
5455 | else if (ippGetGroupTag(device_uri) != IPP_TAG_PRINTER) | |
5456 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "device-uri"); | |
5457 | else | |
5458 | send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "device-uri"); | |
5459 | ||
5460 | return; | |
5461 | } | |
5462 | ||
5463 | printer_geo_location = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI); | |
5464 | printer_info = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT); | |
5465 | printer_location = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT); | |
5466 | ||
46385a1a MS |
5467 | /* |
5468 | * See if the printer already exists... | |
5469 | */ | |
5470 | ||
5471 | if ((printer = cupsdFindDest(name)) != NULL) | |
5472 | { | |
5473 | send_ipp_status(con, IPP_STATUS_ERROR_NOT_POSSIBLE, _("Printer \"%s\" already exists."), name); | |
5474 | goto add_printer_attributes; | |
5475 | } | |
5476 | ||
95ece0cb MS |
5477 | /* |
5478 | * Create the printer... | |
5479 | */ | |
5480 | ||
5481 | if ((printer = cupsdAddPrinter(name)) == NULL) | |
5482 | { | |
5483 | send_ipp_status(con, IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer.")); | |
5484 | return; | |
5485 | } | |
5486 | ||
167e0655 MS |
5487 | printer->shared = 0; |
5488 | printer->temporary = 1; | |
42deee7e | 5489 | |
95ece0cb MS |
5490 | cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL)); |
5491 | ||
5492 | if (printer_geo_location) | |
5493 | cupsdSetString(&printer->geo_location, ippGetString(printer_geo_location, 0, NULL)); | |
5494 | if (printer_info) | |
5495 | cupsdSetString(&printer->info, ippGetString(printer_info, 0, NULL)); | |
5496 | if (printer_location) | |
5497 | cupsdSetString(&printer->location, ippGetString(printer_location, 0, NULL)); | |
5498 | ||
5499 | cupsdSetPrinterAttrs(printer); | |
5500 | ||
5501 | /* | |
5502 | * Run a background thread to create the PPD... | |
5503 | */ | |
5504 | ||
5505 | _cupsThreadCreate((_cups_thread_func_t)create_local_bg_thread, printer); | |
5506 | ||
5507 | /* | |
5508 | * Return printer attributes... | |
5509 | */ | |
5510 | ||
5511 | send_ipp_status(con, IPP_STATUS_OK, _("Local printer created.")); | |
5512 | ||
46385a1a MS |
5513 | add_printer_attributes: |
5514 | ||
95ece0cb MS |
5515 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting); |
5516 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state); | |
5517 | add_printer_state_reasons(con, printer); | |
5518 | ||
5519 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), httpIsEncrypted(con->http) ? "ipps" : "ipp", NULL, con->clientname, con->clientport, "/printers/%s", printer->name); | |
5520 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri); | |
7ae00c35 MS |
5521 | } |
5522 | ||
5523 | ||
ef416fc2 | 5524 | /* |
5525 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
5526 | */ | |
5527 | ||
5528 | static cups_array_t * /* O - Array of attributes or NULL */ | |
5529 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
5530 | { | |
ef416fc2 | 5531 | cups_array_t *ra; /* Requested attributes array */ |
ef416fc2 | 5532 | |
5533 | ||
5534 | /* | |
db8b865d | 5535 | * Create the array for standard attributes... |
ef416fc2 | 5536 | */ |
5537 | ||
db8b865d | 5538 | ra = ippCreateRequestedArray(request); |
ef416fc2 | 5539 | |
5540 | /* | |
db8b865d | 5541 | * Add CUPS defaults as needed... |
ef416fc2 | 5542 | */ |
5543 | ||
db8b865d | 5544 | if (cupsArrayFind(ra, "printer-defaults")) |
ef416fc2 | 5545 | { |
db8b865d MS |
5546 | /* |
5547 | * Include user-set defaults... | |
5548 | */ | |
ef416fc2 | 5549 | |
db8b865d | 5550 | char *name; /* Option name */ |
f7deaa1a | 5551 | |
db8b865d | 5552 | cupsArrayRemove(ra, "printer-defaults"); |
f7deaa1a | 5553 | |
db8b865d MS |
5554 | for (name = (char *)cupsArrayFirst(CommonDefaults); |
5555 | name; | |
5556 | name = (char *)cupsArrayNext(CommonDefaults)) | |
5557 | if (!cupsArrayFind(ra, name)) | |
f7deaa1a | 5558 | cupsArrayAdd(ra, name); |
ef416fc2 | 5559 | } |
5560 | ||
5561 | return (ra); | |
5562 | } | |
5563 | ||
5564 | ||
5565 | /* | |
42404685 | 5566 | * 'create_subscriptions()' - Create one or more notification subscriptions. |
ef416fc2 | 5567 | */ |
5568 | ||
5569 | static void | |
42404685 | 5570 | create_subscriptions( |
ef416fc2 | 5571 | cupsd_client_t *con, /* I - Client connection */ |
5572 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5573 | { | |
5574 | http_status_t status; /* Policy status */ | |
5575 | int i; /* Looping var */ | |
5576 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 5577 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ed486911 | 5578 | char scheme[HTTP_MAX_URI], |
5579 | /* Scheme portion of URI */ | |
ef416fc2 | 5580 | userpass[HTTP_MAX_URI], |
5581 | /* Username portion of URI */ | |
5582 | host[HTTP_MAX_URI], | |
5583 | /* Host portion of URI */ | |
5584 | resource[HTTP_MAX_URI]; | |
5585 | /* Resource portion of URI */ | |
5586 | int port; /* Port portion of URI */ | |
5587 | cupsd_printer_t *printer; /* Printer/class */ | |
5588 | cupsd_job_t *job; /* Job */ | |
5589 | int jobid; /* Job ID */ | |
5590 | cupsd_subscription_t *sub; /* Subscription object */ | |
bc44d920 | 5591 | const char *username, /* requesting-user-name or |
5592 | authenticated username */ | |
ef416fc2 | 5593 | *recipient, /* notify-recipient-uri */ |
5594 | *pullmethod; /* notify-pull-method */ | |
5595 | ipp_attribute_t *user_data; /* notify-user-data */ | |
5596 | int interval, /* notify-time-interval */ | |
5597 | lease; /* notify-lease-duration */ | |
5598 | unsigned mask; /* notify-events */ | |
f7deaa1a | 5599 | ipp_attribute_t *notify_events,/* notify-events(-default) */ |
5600 | *notify_lease; /* notify-lease-duration(-default) */ | |
ef416fc2 | 5601 | |
5602 | ||
bd7854cb | 5603 | #ifdef DEBUG |
5604 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5605 | { | |
5606 | if (attr->group_tag != IPP_TAG_ZERO) | |
3dfe78b3 | 5607 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag, |
bd7854cb | 5608 | attr->value_tag, attr->name); |
5609 | else | |
3dfe78b3 | 5610 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----"); |
bd7854cb | 5611 | } |
5612 | #endif /* DEBUG */ | |
5613 | ||
ef416fc2 | 5614 | /* |
5615 | * Is the destination valid? | |
5616 | */ | |
5617 | ||
42404685 | 5618 | cupsdLogMessage(CUPSD_LOG_DEBUG, "create_subscriptions(con=%p(%d), uri=\"%s\")", con, con->number, uri->values[0].string.text); |
fa73b229 | 5619 | |
ed486911 | 5620 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
5621 | sizeof(scheme), userpass, sizeof(userpass), host, | |
a4d04587 | 5622 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 5623 | |
5624 | if (!strcmp(resource, "/")) | |
5625 | { | |
ef416fc2 | 5626 | dtype = (cups_ptype_t)0; |
5627 | printer = NULL; | |
5628 | } | |
5629 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
5630 | { | |
ef416fc2 | 5631 | dtype = (cups_ptype_t)0; |
5632 | printer = NULL; | |
5633 | } | |
5634 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
5635 | { | |
ef416fc2 | 5636 | dtype = CUPS_PRINTER_CLASS; |
5637 | printer = NULL; | |
5638 | } | |
f7deaa1a | 5639 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 5640 | { |
5641 | /* | |
5642 | * Bad URI... | |
5643 | */ | |
5644 | ||
5645 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 5646 | _("The printer or class does not exist.")); |
ef416fc2 | 5647 | return; |
5648 | } | |
5649 | ||
5650 | /* | |
5651 | * Check policy... | |
5652 | */ | |
5653 | ||
5654 | if (printer) | |
5655 | { | |
f7deaa1a | 5656 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
5657 | NULL)) != HTTP_OK) | |
ef416fc2 | 5658 | { |
f899b121 | 5659 | send_http_error(con, status, printer); |
ef416fc2 | 5660 | return; |
5661 | } | |
5662 | } | |
5663 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5664 | { | |
f899b121 | 5665 | send_http_error(con, status, NULL); |
ef416fc2 | 5666 | return; |
5667 | } | |
5668 | ||
5669 | /* | |
5670 | * Get the user that is requesting the subscription... | |
5671 | */ | |
5672 | ||
e00b005a | 5673 | username = get_username(con); |
ef416fc2 | 5674 | |
5675 | /* | |
5676 | * Find the first subscription group attribute; return if we have | |
5677 | * none... | |
5678 | */ | |
5679 | ||
5680 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5681 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
5682 | break; | |
5683 | ||
5684 | if (!attr) | |
5685 | { | |
5686 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 5687 | _("No subscription attributes in request.")); |
ef416fc2 | 5688 | return; |
5689 | } | |
5690 | ||
5691 | /* | |
5692 | * Process the subscription attributes in the request... | |
5693 | */ | |
5694 | ||
fa73b229 | 5695 | con->response->request.status.status_code = IPP_BAD_REQUEST; |
5696 | ||
ef416fc2 | 5697 | while (attr) |
5698 | { | |
5699 | recipient = NULL; | |
5700 | pullmethod = NULL; | |
5701 | user_data = NULL; | |
5702 | interval = 0; | |
5703 | lease = DefaultLeaseDuration; | |
5704 | jobid = 0; | |
5705 | mask = CUPSD_EVENT_NONE; | |
5706 | ||
f7deaa1a | 5707 | if (printer) |
5708 | { | |
5709 | notify_events = ippFindAttribute(printer->attrs, "notify-events-default", | |
5710 | IPP_TAG_KEYWORD); | |
5711 | notify_lease = ippFindAttribute(printer->attrs, | |
5712 | "notify-lease-duration-default", | |
5713 | IPP_TAG_INTEGER); | |
5714 | ||
5715 | if (notify_lease) | |
5716 | lease = notify_lease->values[0].integer; | |
5717 | } | |
5718 | else | |
5719 | { | |
5720 | notify_events = NULL; | |
5721 | notify_lease = NULL; | |
5722 | } | |
5723 | ||
ef416fc2 | 5724 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
5725 | { | |
7594b224 | 5726 | if (!strcmp(attr->name, "notify-recipient-uri") && |
ef416fc2 | 5727 | attr->value_tag == IPP_TAG_URI) |
ed486911 | 5728 | { |
5729 | /* | |
5730 | * Validate the recipient scheme against the ServerBin/notifier | |
5731 | * directory... | |
5732 | */ | |
5733 | ||
5734 | char notifier[1024]; /* Notifier filename */ | |
5735 | ||
5736 | ||
ef416fc2 | 5737 | recipient = attr->values[0].string.text; |
ed486911 | 5738 | |
8ca02f3c | 5739 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, |
5740 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
5741 | host, sizeof(host), &port, | |
5742 | resource, sizeof(resource)) < HTTP_URI_OK) | |
ed486911 | 5743 | { |
5744 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 5745 | _("Bad notify-recipient-uri \"%s\"."), recipient); |
ed486911 | 5746 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
5747 | "notify-status-code", IPP_URI_SCHEME); | |
5748 | return; | |
5749 | } | |
5750 | ||
5751 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
5752 | scheme); | |
7d4510a0 | 5753 | if (access(notifier, X_OK) || !strcmp(scheme, ".") || !strcmp(scheme, "..")) |
ed486911 | 5754 | { |
5755 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
bc44d920 | 5756 | _("notify-recipient-uri URI \"%s\" uses unknown " |
84315f46 | 5757 | "scheme."), recipient); |
ed486911 | 5758 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
5759 | "notify-status-code", IPP_URI_SCHEME); | |
5760 | return; | |
5761 | } | |
3dfe78b3 MS |
5762 | |
5763 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
5764 | { | |
5765 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 5766 | _("notify-recipient-uri URI \"%s\" is already used."), |
3dfe78b3 MS |
5767 | recipient); |
5768 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
5769 | "notify-status-code", IPP_ATTRIBUTES); | |
5770 | return; | |
5771 | } | |
ed486911 | 5772 | } |
ef416fc2 | 5773 | else if (!strcmp(attr->name, "notify-pull-method") && |
5774 | attr->value_tag == IPP_TAG_KEYWORD) | |
ed486911 | 5775 | { |
ef416fc2 | 5776 | pullmethod = attr->values[0].string.text; |
ed486911 | 5777 | |
5778 | if (strcmp(pullmethod, "ippget")) | |
5779 | { | |
5780 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 5781 | _("Bad notify-pull-method \"%s\"."), pullmethod); |
ed486911 | 5782 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
5783 | "notify-status-code", IPP_ATTRIBUTES); | |
5784 | return; | |
5785 | } | |
5786 | } | |
ef416fc2 | 5787 | else if (!strcmp(attr->name, "notify-charset") && |
5788 | attr->value_tag == IPP_TAG_CHARSET && | |
5789 | strcmp(attr->values[0].string.text, "us-ascii") && | |
5790 | strcmp(attr->values[0].string.text, "utf-8")) | |
5791 | { | |
5792 | send_ipp_status(con, IPP_CHARSET, | |
84315f46 | 5793 | _("Character set \"%s\" not supported."), |
ef416fc2 | 5794 | attr->values[0].string.text); |
5795 | return; | |
5796 | } | |
5797 | else if (!strcmp(attr->name, "notify-natural-language") && | |
5798 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
5799 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
5800 | { | |
5801 | send_ipp_status(con, IPP_CHARSET, | |
84315f46 | 5802 | _("Language \"%s\" not supported."), |
ef416fc2 | 5803 | attr->values[0].string.text); |
5804 | return; | |
5805 | } | |
5806 | else if (!strcmp(attr->name, "notify-user-data") && | |
5807 | attr->value_tag == IPP_TAG_STRING) | |
5808 | { | |
5809 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
5810 | { | |
5811 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
5812 | _("The notify-user-data value is too large " | |
84315f46 | 5813 | "(%d > 63 octets)."), |
ef416fc2 | 5814 | attr->values[0].unknown.length); |
5815 | return; | |
5816 | } | |
5817 | ||
5818 | user_data = attr; | |
5819 | } | |
5820 | else if (!strcmp(attr->name, "notify-events") && | |
5821 | attr->value_tag == IPP_TAG_KEYWORD) | |
f7deaa1a | 5822 | notify_events = attr; |
ef416fc2 | 5823 | else if (!strcmp(attr->name, "notify-lease-duration") && |
5824 | attr->value_tag == IPP_TAG_INTEGER) | |
5825 | lease = attr->values[0].integer; | |
5826 | else if (!strcmp(attr->name, "notify-time-interval") && | |
5827 | attr->value_tag == IPP_TAG_INTEGER) | |
5828 | interval = attr->values[0].integer; | |
5829 | else if (!strcmp(attr->name, "notify-job-id") && | |
5830 | attr->value_tag == IPP_TAG_INTEGER) | |
5831 | jobid = attr->values[0].integer; | |
5832 | ||
5833 | attr = attr->next; | |
5834 | } | |
5835 | ||
f7deaa1a | 5836 | if (notify_events) |
5837 | { | |
5838 | for (i = 0; i < notify_events->num_values; i ++) | |
5839 | mask |= cupsdEventValue(notify_events->values[i].string.text); | |
5840 | } | |
5841 | ||
bd7854cb | 5842 | if (recipient) |
4cadd620 | 5843 | { |
bd7854cb | 5844 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); |
4cadd620 MS |
5845 | |
5846 | ||
5847 | if (!strncmp(recipient, "mailto:", 7) && user_data) | |
5848 | { | |
5849 | char temp[64]; /* Temporary string */ | |
5850 | ||
5851 | memcpy(temp, user_data->values[0].unknown.data, user_data->values[0].unknown.length); | |
5852 | temp[user_data->values[0].unknown.length] = '\0'; | |
5853 | ||
5854 | if (httpSeparateURI(HTTP_URI_CODING_ALL, temp, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_OK) | |
5855 | { | |
5856 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad notify-user-data \"%s\"."), temp); | |
5857 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, "notify-status-code", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES); | |
5858 | return; | |
5859 | } | |
5860 | } | |
5861 | } | |
5862 | ||
bd7854cb | 5863 | if (pullmethod) |
5864 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
5865 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
5866 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
fa73b229 | 5867 | |
ef416fc2 | 5868 | if (!recipient && !pullmethod) |
5869 | break; | |
5870 | ||
5871 | if (mask == CUPSD_EVENT_NONE) | |
5872 | { | |
5873 | if (jobid) | |
5874 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
5875 | else if (printer) | |
5876 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
5877 | else | |
5878 | { | |
5879 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 5880 | _("notify-events not specified.")); |
ef416fc2 | 5881 | return; |
5882 | } | |
5883 | } | |
5884 | ||
bd7854cb | 5885 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) |
ef416fc2 | 5886 | { |
5887 | cupsdLogMessage(CUPSD_LOG_INFO, | |
42404685 | 5888 | "create_subscriptions: Limiting notify-lease-duration to " |
ef416fc2 | 5889 | "%d seconds.", |
5890 | MaxLeaseDuration); | |
5891 | lease = MaxLeaseDuration; | |
5892 | } | |
5893 | ||
5894 | if (jobid) | |
5895 | { | |
5896 | if ((job = cupsdFindJob(jobid)) == NULL) | |
5897 | { | |
84315f46 MS |
5898 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
5899 | jobid); | |
ef416fc2 | 5900 | return; |
5901 | } | |
5902 | } | |
5903 | else | |
5904 | job = NULL; | |
5905 | ||
52f6f666 MS |
5906 | if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL) |
5907 | { | |
5908 | send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS, | |
5909 | _("There are too many subscriptions.")); | |
5910 | return; | |
5911 | } | |
ef416fc2 | 5912 | |
fa73b229 | 5913 | if (job) |
84315f46 | 5914 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.", |
fa73b229 | 5915 | sub->id, job->id); |
5916 | else if (printer) | |
5917 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
84315f46 | 5918 | "Added subscription #%d for printer \"%s\".", |
fa73b229 | 5919 | sub->id, printer->name); |
5920 | else | |
84315f46 | 5921 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.", |
fa73b229 | 5922 | sub->id); |
5923 | ||
ef416fc2 | 5924 | sub->interval = interval; |
5925 | sub->lease = lease; | |
bd7854cb | 5926 | sub->expire = lease ? time(NULL) + lease : 0; |
ef416fc2 | 5927 | |
5928 | cupsdSetString(&sub->owner, username); | |
5929 | ||
5930 | if (user_data) | |
5931 | { | |
5932 | sub->user_data_len = user_data->values[0].unknown.length; | |
5933 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
07623986 | 5934 | (size_t)sub->user_data_len); |
ef416fc2 | 5935 | } |
5936 | ||
5937 | ippAddSeparator(con->response); | |
5938 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5939 | "notify-subscription-id", sub->id); | |
5940 | ||
fa73b229 | 5941 | con->response->request.status.status_code = IPP_OK; |
5942 | ||
ef416fc2 | 5943 | if (attr) |
5944 | attr = attr->next; | |
5945 | } | |
5946 | ||
3dfe78b3 | 5947 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 5948 | } |
5949 | ||
5950 | ||
5951 | /* | |
5952 | * 'delete_printer()' - Remove a printer or class from the system. | |
5953 | */ | |
5954 | ||
5955 | static void | |
5956 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
5957 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
5958 | { | |
5959 | http_status_t status; /* Policy status */ | |
bc44d920 | 5960 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 5961 | cupsd_printer_t *printer; /* Printer/class */ |
5962 | char filename[1024]; /* Script/PPD filename */ | |
7ae00c35 | 5963 | int temporary; /* Temporary queue? */ |
ef416fc2 | 5964 | |
5965 | ||
5966 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
996acce8 | 5967 | con->number, uri->values[0].string.text); |
ef416fc2 | 5968 | |
5969 | /* | |
5970 | * Do we have a valid URI? | |
5971 | */ | |
5972 | ||
f7deaa1a | 5973 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 5974 | { |
5975 | /* | |
5976 | * Bad URI... | |
5977 | */ | |
5978 | ||
5979 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 5980 | _("The printer or class does not exist.")); |
ef416fc2 | 5981 | return; |
5982 | } | |
5983 | ||
5984 | /* | |
5985 | * Check policy... | |
5986 | */ | |
5987 | ||
5988 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5989 | { | |
f899b121 | 5990 | send_http_error(con, status, NULL); |
ef416fc2 | 5991 | return; |
5992 | } | |
5993 | ||
5994 | /* | |
5995 | * Remove old jobs... | |
5996 | */ | |
5997 | ||
f7deaa1a | 5998 | cupsdCancelJobs(printer->name, NULL, 1); |
ef416fc2 | 5999 | |
6000 | /* | |
6001 | * Remove old subscriptions and send a "deleted printer" event... | |
6002 | */ | |
6003 | ||
6004 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
6005 | "%s \"%s\" deleted by \"%s\".", | |
6006 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
f7deaa1a | 6007 | printer->name, get_username(con)); |
ef416fc2 | 6008 | |
6009 | cupsdExpireSubscriptions(printer, NULL); | |
f7deaa1a | 6010 | |
ef416fc2 | 6011 | /* |
6012 | * Remove any old PPD or script files... | |
6013 | */ | |
6014 | ||
f7deaa1a | 6015 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, |
6016 | printer->name); | |
ef416fc2 | 6017 | unlink(filename); |
928b43f7 MS |
6018 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd.O", ServerRoot, |
6019 | printer->name); | |
6020 | unlink(filename); | |
ef416fc2 | 6021 | |
7cf5915e | 6022 | snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name); |
61cf44e2 MS |
6023 | unlink(filename); |
6024 | ||
f14324a7 | 6025 | snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name); |
54afec33 MS |
6026 | unlink(filename); |
6027 | ||
568fa3fa MS |
6028 | /* |
6029 | * Unregister color profiles... | |
6030 | */ | |
6031 | ||
a29fd7dd | 6032 | cupsdUnregisterColor(printer); |
568fa3fa | 6033 | |
7ae00c35 MS |
6034 | temporary = printer->temporary; |
6035 | ||
ef416fc2 | 6036 | if (dtype & CUPS_PRINTER_CLASS) |
6037 | { | |
f7deaa1a | 6038 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", |
6039 | printer->name, get_username(con)); | |
ef416fc2 | 6040 | |
6041 | cupsdDeletePrinter(printer, 0); | |
7ae00c35 MS |
6042 | if (!temporary) |
6043 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); | |
ef416fc2 | 6044 | } |
6045 | else | |
6046 | { | |
f7deaa1a | 6047 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", |
6048 | printer->name, get_username(con)); | |
ef416fc2 | 6049 | |
7ae00c35 | 6050 | if (cupsdDeletePrinter(printer, 0) && !temporary) |
f8b3a85b MS |
6051 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
6052 | ||
7ae00c35 MS |
6053 | if (!temporary) |
6054 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
ef416fc2 | 6055 | } |
6056 | ||
7ae00c35 MS |
6057 | if (!temporary) |
6058 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); | |
ef416fc2 | 6059 | |
6060 | /* | |
6061 | * Return with no errors... | |
6062 | */ | |
6063 | ||
6064 | con->response->request.status.status_code = IPP_OK; | |
6065 | } | |
6066 | ||
6067 | ||
6068 | /* | |
6069 | * 'get_default()' - Get the default destination. | |
6070 | */ | |
6071 | ||
6072 | static void | |
6073 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
6074 | { | |
fa73b229 | 6075 | http_status_t status; /* Policy status */ |
6076 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 6077 | |
6078 | ||
996acce8 | 6079 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->number); |
ef416fc2 | 6080 | |
6081 | /* | |
6082 | * Check policy... | |
6083 | */ | |
6084 | ||
6085 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6086 | { | |
f899b121 | 6087 | send_http_error(con, status, NULL); |
ef416fc2 | 6088 | return; |
6089 | } | |
6090 | ||
fa73b229 | 6091 | if (DefaultPrinter) |
ef416fc2 | 6092 | { |
fa73b229 | 6093 | ra = create_requested_array(con->request); |
ef416fc2 | 6094 | |
fa73b229 | 6095 | copy_printer_attrs(con, DefaultPrinter, ra); |
ef416fc2 | 6096 | |
fa73b229 | 6097 | cupsArrayDelete(ra); |
ef416fc2 | 6098 | |
fa73b229 | 6099 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6100 | } |
6101 | else | |
84315f46 | 6102 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer.")); |
ef416fc2 | 6103 | } |
6104 | ||
6105 | ||
6106 | /* | |
6107 | * 'get_devices()' - Get the list of available devices on the local system. | |
6108 | */ | |
6109 | ||
6110 | static void | |
6111 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
6112 | { | |
6113 | http_status_t status; /* Policy status */ | |
ae71f5de MS |
6114 | ipp_attribute_t *limit, /* limit attribute */ |
6115 | *timeout, /* timeout attribute */ | |
6116 | *requested, /* requested-attributes attribute */ | |
ed6e7faf MS |
6117 | *exclude, /* exclude-schemes attribute */ |
6118 | *include; /* include-schemes attribute */ | |
ef416fc2 | 6119 | char command[1024], /* cups-deviced command */ |
ed6e7faf | 6120 | options[2048], /* Options to pass to command */ |
ae71f5de | 6121 | requested_str[256], |
89d46774 | 6122 | /* String for requested attributes */ |
ed6e7faf MS |
6123 | exclude_str[512], |
6124 | /* String for excluded schemes */ | |
6125 | include_str[512]; | |
6126 | /* String for included schemes */ | |
ef416fc2 | 6127 | |
6128 | ||
996acce8 | 6129 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->number); |
ef416fc2 | 6130 | |
6131 | /* | |
6132 | * Check policy... | |
6133 | */ | |
6134 | ||
6135 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6136 | { | |
f899b121 | 6137 | send_http_error(con, status, NULL); |
ef416fc2 | 6138 | return; |
6139 | } | |
6140 | ||
6141 | /* | |
6142 | * Run cups-deviced command with the given options... | |
6143 | */ | |
6144 | ||
ae71f5de MS |
6145 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
6146 | timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER); | |
ef416fc2 | 6147 | requested = ippFindAttribute(con->request, "requested-attributes", |
6148 | IPP_TAG_KEYWORD); | |
ae71f5de | 6149 | exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME); |
ed6e7faf | 6150 | include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME); |
ef416fc2 | 6151 | |
6152 | if (requested) | |
89d46774 | 6153 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
ef416fc2 | 6154 | else |
89d46774 | 6155 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); |
ef416fc2 | 6156 | |
ae71f5de MS |
6157 | if (exclude) |
6158 | url_encode_attr(exclude, exclude_str, sizeof(exclude_str)); | |
6159 | else | |
6160 | exclude_str[0] = '\0'; | |
6161 | ||
ed6e7faf MS |
6162 | if (include) |
6163 | url_encode_attr(include, include_str, sizeof(include_str)); | |
6164 | else | |
6165 | include_str[0] = '\0'; | |
6166 | ||
ef416fc2 | 6167 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); |
6168 | snprintf(options, sizeof(options), | |
ed6e7faf | 6169 | "%d+%d+%d+%d+%s%s%s%s%s", |
ef416fc2 | 6170 | con->request->request.op.request_id, |
ae71f5de | 6171 | limit ? limit->values[0].integer : 0, |
7a0cbd5e | 6172 | timeout ? timeout->values[0].integer : 15, |
ae71f5de MS |
6173 | (int)User, |
6174 | requested_str, | |
ed6e7faf MS |
6175 | exclude_str[0] ? "%20" : "", exclude_str, |
6176 | include_str[0] ? "%20" : "", include_str); | |
ef416fc2 | 6177 | |
6178 | if (cupsdSendCommand(con, command, options, 1)) | |
6179 | { | |
6180 | /* | |
6181 | * Command started successfully, don't send an IPP response here... | |
6182 | */ | |
6183 | ||
6184 | ippDelete(con->response); | |
6185 | con->response = NULL; | |
6186 | } | |
6187 | else | |
6188 | { | |
6189 | /* | |
6190 | * Command failed, return "internal error" so the user knows something | |
6191 | * went wrong... | |
6192 | */ | |
6193 | ||
6194 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
6195 | _("cups-deviced failed to execute.")); | |
6196 | } | |
6197 | } | |
6198 | ||
6199 | ||
2e4ff8af MS |
6200 | /* |
6201 | * 'get_document()' - Get a copy of a job file. | |
6202 | */ | |
6203 | ||
6204 | static void | |
6205 | get_document(cupsd_client_t *con, /* I - Client connection */ | |
6206 | ipp_attribute_t *uri) /* I - Job URI */ | |
6207 | { | |
6208 | http_status_t status; /* Policy status */ | |
6209 | ipp_attribute_t *attr; /* Current attribute */ | |
6210 | int jobid; /* Job ID */ | |
6211 | int docnum; /* Document number */ | |
6212 | cupsd_job_t *job; /* Current job */ | |
61cf44e2 | 6213 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
2e4ff8af MS |
6214 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6215 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6216 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6217 | int port; /* Port portion of URI */ | |
6218 | char filename[1024], /* Filename for document */ | |
6219 | format[1024]; /* Format for document */ | |
6220 | ||
6221 | ||
6222 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con, | |
996acce8 | 6223 | con->number, uri->values[0].string.text); |
2e4ff8af MS |
6224 | |
6225 | /* | |
6226 | * See if we have a job URI or a printer URI... | |
6227 | */ | |
6228 | ||
6229 | if (!strcmp(uri->name, "printer-uri")) | |
6230 | { | |
6231 | /* | |
6232 | * Got a printer URI; see if we also have a job-id attribute... | |
6233 | */ | |
6234 | ||
6235 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6236 | IPP_TAG_INTEGER)) == NULL) | |
6237 | { | |
6238 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 6239 | _("Got a printer-uri attribute but no job-id.")); |
2e4ff8af MS |
6240 | return; |
6241 | } | |
6242 | ||
6243 | jobid = attr->values[0].integer; | |
6244 | } | |
6245 | else | |
6246 | { | |
6247 | /* | |
6248 | * Got a job URI; parse it to get the job ID... | |
6249 | */ | |
6250 | ||
61cf44e2 MS |
6251 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
6252 | sizeof(scheme), username, sizeof(username), host, | |
2e4ff8af MS |
6253 | sizeof(host), &port, resource, sizeof(resource)); |
6254 | ||
6255 | if (strncmp(resource, "/jobs/", 6)) | |
6256 | { | |
6257 | /* | |
6258 | * Not a valid URI! | |
6259 | */ | |
6260 | ||
84315f46 | 6261 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
2e4ff8af MS |
6262 | uri->values[0].string.text); |
6263 | return; | |
6264 | } | |
6265 | ||
6266 | jobid = atoi(resource + 6); | |
6267 | } | |
6268 | ||
6269 | /* | |
6270 | * See if the job exists... | |
6271 | */ | |
6272 | ||
6273 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6274 | { | |
6275 | /* | |
6276 | * Nope - return a "not found" error... | |
6277 | */ | |
6278 | ||
84315f46 | 6279 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
2e4ff8af MS |
6280 | return; |
6281 | } | |
6282 | ||
6283 | /* | |
6284 | * Check policy... | |
6285 | */ | |
6286 | ||
e60ec91f MS |
6287 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, |
6288 | job->username)) != HTTP_OK) | |
2e4ff8af MS |
6289 | { |
6290 | send_http_error(con, status, NULL); | |
6291 | return; | |
6292 | } | |
6293 | ||
6294 | /* | |
6295 | * Get the document number... | |
6296 | */ | |
6297 | ||
6298 | if ((attr = ippFindAttribute(con->request, "document-number", | |
6299 | IPP_TAG_INTEGER)) == NULL) | |
6300 | { | |
6301 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 6302 | _("Missing document-number attribute.")); |
2e4ff8af MS |
6303 | return; |
6304 | } | |
6305 | ||
6306 | if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files || | |
6307 | attr->num_values > 1) | |
6308 | { | |
84315f46 MS |
6309 | send_ipp_status(con, IPP_NOT_FOUND, |
6310 | _("Document #%d does not exist in job #%d."), docnum, | |
6311 | jobid); | |
2e4ff8af MS |
6312 | return; |
6313 | } | |
6314 | ||
6315 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid, | |
6316 | docnum); | |
6317 | if ((con->file = open(filename, O_RDONLY)) == -1) | |
6318 | { | |
6319 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6320 | "Unable to open document %d in job %d - %s", docnum, jobid, | |
6321 | strerror(errno)); | |
6322 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 MS |
6323 | _("Unable to open document #%d in job #%d."), docnum, |
6324 | jobid); | |
2e4ff8af MS |
6325 | return; |
6326 | } | |
6327 | ||
6328 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
6329 | ||
6330 | cupsdLoadJob(job); | |
6331 | ||
6332 | snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super, | |
6333 | job->filetypes[docnum - 1]->type); | |
6334 | ||
6335 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
6336 | NULL, format); | |
6337 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number", | |
6338 | docnum); | |
6339 | if ((attr = ippFindAttribute(job->attrs, "document-name", | |
6340 | IPP_TAG_NAME)) != NULL) | |
6341 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name", | |
6342 | NULL, attr->values[0].string.text); | |
6343 | } | |
6344 | ||
6345 | ||
ef416fc2 | 6346 | /* |
fa73b229 | 6347 | * 'get_job_attrs()' - Get job attributes. |
ef416fc2 | 6348 | */ |
6349 | ||
6350 | static void | |
fa73b229 | 6351 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ |
6352 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 6353 | { |
6354 | http_status_t status; /* Policy status */ | |
fa73b229 | 6355 | ipp_attribute_t *attr; /* Current attribute */ |
6356 | int jobid; /* Job ID */ | |
6357 | cupsd_job_t *job; /* Current job */ | |
ef55b745 | 6358 | cupsd_printer_t *printer; /* Current printer */ |
10d09e33 MS |
6359 | cupsd_policy_t *policy; /* Current security policy */ |
6360 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
ef416fc2 | 6361 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6362 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6363 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6364 | int port; /* Port portion of URI */ | |
10d09e33 MS |
6365 | cups_array_t *ra, /* Requested attributes array */ |
6366 | *exclude; /* Private attributes array */ | |
ef416fc2 | 6367 | |
6368 | ||
fa73b229 | 6369 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, |
996acce8 | 6370 | con->number, uri->values[0].string.text); |
ef416fc2 | 6371 | |
6372 | /* | |
fa73b229 | 6373 | * See if we have a job URI or a printer URI... |
6374 | */ | |
6375 | ||
6376 | if (!strcmp(uri->name, "printer-uri")) | |
6377 | { | |
6378 | /* | |
6379 | * Got a printer URI; see if we also have a job-id attribute... | |
6380 | */ | |
6381 | ||
6382 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6383 | IPP_TAG_INTEGER)) == NULL) | |
6384 | { | |
6385 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 6386 | _("Got a printer-uri attribute but no job-id.")); |
fa73b229 | 6387 | return; |
6388 | } | |
6389 | ||
6390 | jobid = attr->values[0].integer; | |
6391 | } | |
6392 | else | |
6393 | { | |
6394 | /* | |
6395 | * Got a job URI; parse it to get the job ID... | |
6396 | */ | |
6397 | ||
61cf44e2 MS |
6398 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
6399 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 6400 | sizeof(host), &port, resource, sizeof(resource)); |
fa73b229 | 6401 | |
6402 | if (strncmp(resource, "/jobs/", 6)) | |
6403 | { | |
6404 | /* | |
6405 | * Not a valid URI! | |
6406 | */ | |
6407 | ||
84315f46 | 6408 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
fa73b229 | 6409 | uri->values[0].string.text); |
6410 | return; | |
6411 | } | |
6412 | ||
6413 | jobid = atoi(resource + 6); | |
6414 | } | |
6415 | ||
6416 | /* | |
6417 | * See if the job exists... | |
6418 | */ | |
6419 | ||
6420 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6421 | { | |
6422 | /* | |
6423 | * Nope - return a "not found" error... | |
6424 | */ | |
6425 | ||
84315f46 | 6426 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
fa73b229 | 6427 | return; |
6428 | } | |
6429 | ||
6430 | /* | |
6431 | * Check policy... | |
6432 | */ | |
6433 | ||
ef55b745 MS |
6434 | if ((printer = job->printer) == NULL) |
6435 | printer = cupsdFindDest(job->dest); | |
6436 | ||
6437 | if (printer) | |
10d09e33 MS |
6438 | policy = printer->op_policy_ptr; |
6439 | else | |
6440 | policy = DefaultPolicyPtr; | |
6441 | ||
6442 | if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK) | |
fa73b229 | 6443 | { |
f899b121 | 6444 | send_http_error(con, status, NULL); |
fa73b229 | 6445 | return; |
6446 | } | |
6447 | ||
10d09e33 MS |
6448 | exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username); |
6449 | ||
fa73b229 | 6450 | /* |
6451 | * Copy attributes... | |
6452 | */ | |
6453 | ||
bd7854cb | 6454 | cupsdLoadJob(job); |
6455 | ||
fa73b229 | 6456 | ra = create_requested_array(con->request); |
10d09e33 | 6457 | copy_job_attrs(con, job, ra, exclude); |
fa73b229 | 6458 | cupsArrayDelete(ra); |
6459 | ||
6460 | con->response->request.status.status_code = IPP_OK; | |
6461 | } | |
6462 | ||
6463 | ||
6464 | /* | |
6465 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
6466 | */ | |
6467 | ||
6468 | static void | |
6469 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
6470 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6471 | { | |
6472 | http_status_t status; /* Policy status */ | |
6473 | ipp_attribute_t *attr; /* Current attribute */ | |
6474 | const char *dest; /* Destination */ | |
bc44d920 | 6475 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
fa73b229 | 6476 | cups_ptype_t dmask; /* Destination type mask */ |
f7deaa1a | 6477 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
fa73b229 | 6478 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6479 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6480 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6481 | int port; /* Port portion of URI */ | |
aaf19ab0 MS |
6482 | int job_comparison; /* Job comparison */ |
6483 | ipp_jstate_t job_state; /* job-state value */ | |
f16ea703 MS |
6484 | int first_job_id = 1, /* First job ID */ |
6485 | first_index = 1, /* First index */ | |
9e6d7a0f MS |
6486 | limit = 0, /* Maximum number of jobs to return */ |
6487 | count, /* Number of jobs that match */ | |
6488 | need_load_job = 0; /* Do we need to load the job? */ | |
f16ea703 | 6489 | const char *job_attr; /* Job attribute requested */ |
aaf19ab0 | 6490 | ipp_attribute_t *job_ids; /* job-ids attribute */ |
fa73b229 | 6491 | cupsd_job_t *job; /* Current job pointer */ |
6492 | cupsd_printer_t *printer; /* Printer */ | |
6493 | cups_array_t *list; /* Which job list... */ | |
eec1fbc3 | 6494 | int delete_list = 0; /* Delete the list afterwards? */ |
10d09e33 MS |
6495 | cups_array_t *ra, /* Requested attributes array */ |
6496 | *exclude; /* Private attributes array */ | |
6497 | cupsd_policy_t *policy; /* Current policy */ | |
fa73b229 | 6498 | |
6499 | ||
996acce8 | 6500 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->number, |
fa73b229 | 6501 | uri->values[0].string.text); |
6502 | ||
6503 | /* | |
6504 | * Is the destination valid? | |
ef416fc2 | 6505 | */ |
6506 | ||
4b3f67ff MS |
6507 | if (strcmp(uri->name, "printer-uri")) |
6508 | { | |
84315f46 | 6509 | send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request.")); |
4b3f67ff MS |
6510 | return; |
6511 | } | |
6512 | ||
f7deaa1a | 6513 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
6514 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 6515 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 6516 | |
b9faaae1 | 6517 | if (!strcmp(resource, "/") || !strcmp(resource, "/jobs")) |
ef416fc2 | 6518 | { |
6519 | dest = NULL; | |
6520 | dtype = (cups_ptype_t)0; | |
6521 | dmask = (cups_ptype_t)0; | |
6522 | printer = NULL; | |
6523 | } | |
fa73b229 | 6524 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) |
ef416fc2 | 6525 | { |
6526 | dest = NULL; | |
6527 | dtype = (cups_ptype_t)0; | |
6528 | dmask = CUPS_PRINTER_CLASS; | |
6529 | printer = NULL; | |
6530 | } | |
fa73b229 | 6531 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) |
ef416fc2 | 6532 | { |
6533 | dest = NULL; | |
6534 | dtype = CUPS_PRINTER_CLASS; | |
6535 | dmask = CUPS_PRINTER_CLASS; | |
6536 | printer = NULL; | |
6537 | } | |
f7deaa1a | 6538 | else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype, |
6539 | &printer)) == NULL) | |
ef416fc2 | 6540 | { |
6541 | /* | |
6542 | * Bad URI... | |
6543 | */ | |
6544 | ||
6545 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 6546 | _("The printer or class does not exist.")); |
ef416fc2 | 6547 | return; |
6548 | } | |
6549 | else | |
b86bc4cf | 6550 | { |
6551 | dtype &= CUPS_PRINTER_CLASS; | |
ef416fc2 | 6552 | dmask = CUPS_PRINTER_CLASS; |
b86bc4cf | 6553 | } |
ef416fc2 | 6554 | |
6555 | /* | |
6556 | * Check policy... | |
6557 | */ | |
6558 | ||
6559 | if (printer) | |
10d09e33 MS |
6560 | policy = printer->op_policy_ptr; |
6561 | else | |
6562 | policy = DefaultPolicyPtr; | |
6563 | ||
6564 | if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK) | |
ef416fc2 | 6565 | { |
f899b121 | 6566 | send_http_error(con, status, NULL); |
ef416fc2 | 6567 | return; |
6568 | } | |
6569 | ||
aaf19ab0 MS |
6570 | job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER); |
6571 | ||
ef416fc2 | 6572 | /* |
6573 | * See if the "which-jobs" attribute have been specified... | |
6574 | */ | |
6575 | ||
fa73b229 | 6576 | if ((attr = ippFindAttribute(con->request, "which-jobs", |
aaf19ab0 MS |
6577 | IPP_TAG_KEYWORD)) != NULL && job_ids) |
6578 | { | |
6579 | send_ipp_status(con, IPP_CONFLICT, | |
6580 | _("The %s attribute cannot be provided with job-ids."), | |
6581 | "which-jobs"); | |
6582 | return; | |
6583 | } | |
6584 | else if (!attr || !strcmp(attr->values[0].string.text, "not-completed")) | |
6585 | { | |
6586 | job_comparison = -1; | |
6587 | job_state = IPP_JOB_STOPPED; | |
e584c068 | 6588 | list = ActiveJobs; |
aaf19ab0 MS |
6589 | } |
6590 | else if (!strcmp(attr->values[0].string.text, "completed")) | |
6591 | { | |
6592 | job_comparison = 1; | |
6593 | job_state = IPP_JOB_CANCELED; | |
eec1fbc3 MS |
6594 | list = cupsdGetCompletedJobs(printer); |
6595 | delete_list = 1; | |
aaf19ab0 MS |
6596 | } |
6597 | else if (!strcmp(attr->values[0].string.text, "aborted")) | |
6598 | { | |
6599 | job_comparison = 0; | |
6600 | job_state = IPP_JOB_ABORTED; | |
eec1fbc3 MS |
6601 | list = cupsdGetCompletedJobs(printer); |
6602 | delete_list = 1; | |
aaf19ab0 MS |
6603 | } |
6604 | else if (!strcmp(attr->values[0].string.text, "all")) | |
6605 | { | |
6606 | job_comparison = 1; | |
6607 | job_state = IPP_JOB_PENDING; | |
6608 | list = Jobs; | |
6609 | } | |
6610 | else if (!strcmp(attr->values[0].string.text, "canceled")) | |
6611 | { | |
6612 | job_comparison = 0; | |
6613 | job_state = IPP_JOB_CANCELED; | |
eec1fbc3 MS |
6614 | list = cupsdGetCompletedJobs(printer); |
6615 | delete_list = 1; | |
aaf19ab0 MS |
6616 | } |
6617 | else if (!strcmp(attr->values[0].string.text, "pending")) | |
ef416fc2 | 6618 | { |
aaf19ab0 MS |
6619 | job_comparison = 0; |
6620 | job_state = IPP_JOB_PENDING; | |
6621 | list = ActiveJobs; | |
ef416fc2 | 6622 | } |
aaf19ab0 | 6623 | else if (!strcmp(attr->values[0].string.text, "pending-held")) |
ef416fc2 | 6624 | { |
aaf19ab0 MS |
6625 | job_comparison = 0; |
6626 | job_state = IPP_JOB_HELD; | |
6627 | list = ActiveJobs; | |
ef416fc2 | 6628 | } |
aaf19ab0 | 6629 | else if (!strcmp(attr->values[0].string.text, "processing")) |
3dfe78b3 | 6630 | { |
aaf19ab0 MS |
6631 | job_comparison = 0; |
6632 | job_state = IPP_JOB_PROCESSING; | |
6633 | list = PrintingJobs; | |
6634 | } | |
6635 | else if (!strcmp(attr->values[0].string.text, "processing-stopped")) | |
6636 | { | |
6637 | job_comparison = 0; | |
6638 | job_state = IPP_JOB_STOPPED; | |
6639 | list = ActiveJobs; | |
3dfe78b3 | 6640 | } |
ef416fc2 | 6641 | else |
6642 | { | |
aaf19ab0 MS |
6643 | send_ipp_status(con, IPP_ATTRIBUTES, |
6644 | _("The which-jobs value \"%s\" is not supported."), | |
6645 | attr->values[0].string.text); | |
6646 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
6647 | "which-jobs", NULL, attr->values[0].string.text); | |
6648 | return; | |
ef416fc2 | 6649 | } |
6650 | ||
6651 | /* | |
6652 | * See if they want to limit the number of jobs reported... | |
6653 | */ | |
6654 | ||
f16ea703 | 6655 | if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL) |
aaf19ab0 MS |
6656 | { |
6657 | if (job_ids) | |
6658 | { | |
6659 | send_ipp_status(con, IPP_CONFLICT, | |
6660 | _("The %s attribute cannot be provided with job-ids."), | |
6661 | "limit"); | |
6662 | return; | |
6663 | } | |
6664 | ||
ef416fc2 | 6665 | limit = attr->values[0].integer; |
aaf19ab0 | 6666 | } |
ef416fc2 | 6667 | |
f16ea703 MS |
6668 | if ((attr = ippFindAttribute(con->request, "first-index", IPP_TAG_INTEGER)) != NULL) |
6669 | { | |
6670 | if (job_ids) | |
6671 | { | |
6672 | send_ipp_status(con, IPP_CONFLICT, | |
6673 | _("The %s attribute cannot be provided with job-ids."), | |
6674 | "first-index"); | |
6675 | return; | |
6676 | } | |
6677 | ||
6678 | first_index = attr->values[0].integer; | |
6679 | } | |
6680 | else if ((attr = ippFindAttribute(con->request, "first-job-id", IPP_TAG_INTEGER)) != NULL) | |
aaf19ab0 MS |
6681 | { |
6682 | if (job_ids) | |
6683 | { | |
6684 | send_ipp_status(con, IPP_CONFLICT, | |
6685 | _("The %s attribute cannot be provided with job-ids."), | |
6686 | "first-job-id"); | |
6687 | return; | |
6688 | } | |
6689 | ||
ef416fc2 | 6690 | first_job_id = attr->values[0].integer; |
aaf19ab0 | 6691 | } |
ef416fc2 | 6692 | |
6693 | /* | |
6694 | * See if we only want to see jobs for a specific user... | |
6695 | */ | |
6696 | ||
f16ea703 | 6697 | if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL && job_ids) |
aaf19ab0 MS |
6698 | { |
6699 | send_ipp_status(con, IPP_CONFLICT, | |
6700 | _("The %s attribute cannot be provided with job-ids."), | |
6701 | "my-jobs"); | |
6702 | return; | |
6703 | } | |
6704 | else if (attr && attr->values[0].boolean) | |
e00b005a | 6705 | strlcpy(username, get_username(con), sizeof(username)); |
ef416fc2 | 6706 | else |
6707 | username[0] = '\0'; | |
6708 | ||
db8b865d | 6709 | ra = create_requested_array(con->request); |
f16ea703 MS |
6710 | for (job_attr = (char *)cupsArrayFirst(ra); job_attr; job_attr = (char *)cupsArrayNext(ra)) |
6711 | if (strcmp(job_attr, "job-id") && | |
6712 | strcmp(job_attr, "job-k-octets") && | |
6713 | strcmp(job_attr, "job-media-progress") && | |
6714 | strcmp(job_attr, "job-more-info") && | |
6715 | strcmp(job_attr, "job-name") && | |
6716 | strcmp(job_attr, "job-originating-user-name") && | |
6717 | strcmp(job_attr, "job-preserved") && | |
6718 | strcmp(job_attr, "job-printer-up-time") && | |
6719 | strcmp(job_attr, "job-printer-uri") && | |
6720 | strcmp(job_attr, "job-state") && | |
6721 | strcmp(job_attr, "job-state-reasons") && | |
6722 | strcmp(job_attr, "job-uri") && | |
6723 | strcmp(job_attr, "time-at-completed") && | |
6724 | strcmp(job_attr, "time-at-creation") && | |
6725 | strcmp(job_attr, "number-of-documents")) | |
6726 | { | |
6727 | need_load_job = 1; | |
6728 | break; | |
6729 | } | |
6730 | ||
6731 | if (need_load_job && (limit == 0 || limit > 500) && (list == Jobs || delete_list)) | |
6732 | { | |
6733 | /* | |
6734 | * Limit expensive Get-Jobs for job history to 500 jobs... | |
6735 | */ | |
6736 | ||
6737 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "limit", 500); | |
6738 | ||
6739 | if (limit) | |
6740 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, "limit", limit); | |
6741 | ||
6742 | limit = 500; | |
6743 | ||
6744 | cupsdLogClient(con, CUPSD_LOG_INFO, "Limiting Get-Jobs response to %d jobs.", limit); | |
6745 | } | |
ef416fc2 | 6746 | |
6747 | /* | |
6748 | * OK, build a list of jobs for this printer... | |
6749 | */ | |
6750 | ||
aaf19ab0 | 6751 | if (job_ids) |
ef416fc2 | 6752 | { |
aaf19ab0 | 6753 | int i; /* Looping var */ |
ef416fc2 | 6754 | |
aaf19ab0 MS |
6755 | for (i = 0; i < job_ids->num_values; i ++) |
6756 | { | |
4220952d | 6757 | if (!cupsdFindJob(job_ids->values[i].integer)) |
aaf19ab0 MS |
6758 | break; |
6759 | } | |
ef416fc2 | 6760 | |
aaf19ab0 MS |
6761 | if (i < job_ids->num_values) |
6762 | { | |
84315f46 | 6763 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
aaf19ab0 MS |
6764 | job_ids->values[i].integer); |
6765 | return; | |
6766 | } | |
0a682745 | 6767 | |
aaf19ab0 MS |
6768 | for (i = 0; i < job_ids->num_values; i ++) |
6769 | { | |
6770 | job = cupsdFindJob(job_ids->values[i].integer); | |
0a682745 | 6771 | |
f16ea703 | 6772 | if (need_load_job && !job->attrs) |
aaf19ab0 | 6773 | { |
f16ea703 MS |
6774 | cupsdLoadJob(job); |
6775 | ||
6776 | if (!job->attrs) | |
6777 | { | |
6778 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id); | |
6779 | continue; | |
6780 | } | |
aaf19ab0 | 6781 | } |
ef416fc2 | 6782 | |
aaf19ab0 MS |
6783 | if (i > 0) |
6784 | ippAddSeparator(con->response); | |
bd7854cb | 6785 | |
10d09e33 MS |
6786 | exclude = cupsdGetPrivateAttrs(job->printer ? |
6787 | job->printer->op_policy_ptr : | |
6788 | policy, con, job->printer, | |
6789 | job->username); | |
6790 | ||
6791 | copy_job_attrs(con, job, ra, exclude); | |
d1c13e16 | 6792 | } |
aaf19ab0 MS |
6793 | } |
6794 | else | |
6795 | { | |
9e6d7a0f MS |
6796 | if (first_index > 1) |
6797 | job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1); | |
6798 | else | |
6799 | job = (cupsd_job_t *)cupsArrayFirst(list); | |
6800 | ||
6801 | for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list)) | |
aaf19ab0 MS |
6802 | { |
6803 | /* | |
6804 | * Filter out jobs that don't match... | |
6805 | */ | |
bd7854cb | 6806 | |
aaf19ab0 MS |
6807 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
6808 | "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", " | |
6809 | "state_value=%d, attrs=%p", job->id, job->dest, | |
6810 | job->username, job->state_value, job->attrs); | |
7594b224 | 6811 | |
aaf19ab0 MS |
6812 | if (!job->dest || !job->username) |
6813 | cupsdLoadJob(job); | |
fa73b229 | 6814 | |
aaf19ab0 MS |
6815 | if (!job->dest || !job->username) |
6816 | continue; | |
ef416fc2 | 6817 | |
aaf19ab0 MS |
6818 | if ((dest && strcmp(job->dest, dest)) && |
6819 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
6820 | continue; | |
6821 | if ((job->dtype & dmask) != dtype && | |
6822 | (!job->printer || (job->printer->type & dmask) != dtype)) | |
6823 | continue; | |
ef416fc2 | 6824 | |
aaf19ab0 MS |
6825 | if ((job_comparison < 0 && job->state_value > job_state) || |
6826 | (job_comparison == 0 && job->state_value != job_state) || | |
6827 | (job_comparison > 0 && job->state_value < job_state)) | |
6828 | continue; | |
6829 | ||
6830 | if (job->id < first_job_id) | |
6831 | continue; | |
6832 | ||
f16ea703 | 6833 | if (need_load_job && !job->attrs) |
aaf19ab0 | 6834 | { |
f16ea703 MS |
6835 | cupsdLoadJob(job); |
6836 | ||
6837 | if (!job->attrs) | |
6838 | { | |
6839 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id); | |
6840 | continue; | |
6841 | } | |
aaf19ab0 MS |
6842 | } |
6843 | ||
88f9aafc | 6844 | if (username[0] && _cups_strcasecmp(username, job->username)) |
aaf19ab0 MS |
6845 | continue; |
6846 | ||
6847 | if (count > 0) | |
6848 | ippAddSeparator(con->response); | |
6849 | ||
6850 | count ++; | |
6851 | ||
10d09e33 MS |
6852 | exclude = cupsdGetPrivateAttrs(job->printer ? |
6853 | job->printer->op_policy_ptr : | |
6854 | policy, con, job->printer, | |
6855 | job->username); | |
6856 | ||
6857 | copy_job_attrs(con, job, ra, exclude); | |
aaf19ab0 MS |
6858 | } |
6859 | ||
6860 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count); | |
6861 | } | |
d1c13e16 | 6862 | |
fa73b229 | 6863 | cupsArrayDelete(ra); |
ef416fc2 | 6864 | |
eec1fbc3 MS |
6865 | if (delete_list) |
6866 | cupsArrayDelete(list); | |
6867 | ||
fa73b229 | 6868 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6869 | } |
6870 | ||
6871 | ||
6872 | /* | |
6873 | * 'get_notifications()' - Get events for a subscription. | |
6874 | */ | |
6875 | ||
6876 | static void | |
bd7854cb | 6877 | get_notifications(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 6878 | { |
bd7854cb | 6879 | int i, j; /* Looping vars */ |
6880 | http_status_t status; /* Policy status */ | |
6881 | cupsd_subscription_t *sub; /* Subscription */ | |
6882 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
6883 | *sequences; /* notify-sequence-numbers */ | |
6884 | int min_seq; /* Minimum sequence number */ | |
6885 | int interval; /* Poll interval */ | |
6886 | ||
6887 | ||
f7deaa1a | 6888 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])", |
996acce8 | 6889 | con, con->number); |
bd7854cb | 6890 | |
6891 | /* | |
6892 | * Get subscription attributes... | |
6893 | */ | |
6894 | ||
6895 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
6896 | IPP_TAG_INTEGER); | |
6897 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
6898 | IPP_TAG_INTEGER); | |
6899 | ||
6900 | if (!ids) | |
6901 | { | |
6902 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 6903 | _("Missing notify-subscription-ids attribute.")); |
bd7854cb | 6904 | return; |
6905 | } | |
6906 | ||
6907 | /* | |
6908 | * Are the subscription IDs valid? | |
6909 | */ | |
6910 | ||
6911 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
6912 | { | |
6913 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
6914 | { | |
6915 | /* | |
6916 | * Bad subscription ID... | |
6917 | */ | |
6918 | ||
84315f46 | 6919 | send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."), |
bd7854cb | 6920 | ids->values[i].integer); |
6921 | return; | |
6922 | } | |
6923 | ||
6924 | /* | |
6925 | * Check policy... | |
6926 | */ | |
6927 | ||
6928 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
6929 | DefaultPolicyPtr, | |
6930 | con, sub->owner)) != HTTP_OK) | |
6931 | { | |
f899b121 | 6932 | send_http_error(con, status, sub->dest); |
bd7854cb | 6933 | return; |
6934 | } | |
6935 | ||
6936 | /* | |
6937 | * Check the subscription type and update the interval accordingly. | |
6938 | */ | |
6939 | ||
6940 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
6941 | interval > 10) | |
6942 | interval = 10; | |
6943 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
6944 | interval = 0; | |
6945 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
6946 | interval > 30) | |
6947 | interval = 30; | |
6948 | } | |
6949 | ||
6950 | /* | |
6951 | * Tell the client to poll again in N seconds... | |
6952 | */ | |
6953 | ||
6954 | if (interval > 0) | |
6955 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6956 | "notify-get-interval", interval); | |
6957 | ||
6958 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6959 | "printer-up-time", time(NULL)); | |
6960 | ||
6961 | /* | |
6962 | * Copy the subscription event attributes to the response. | |
6963 | */ | |
6964 | ||
6965 | con->response->request.status.status_code = | |
6966 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
6967 | ||
6968 | for (i = 0; i < ids->num_values; i ++) | |
6969 | { | |
6970 | /* | |
6971 | * Get the subscription and sequence number... | |
6972 | */ | |
6973 | ||
6974 | sub = cupsdFindSubscription(ids->values[i].integer); | |
6975 | ||
6976 | if (sequences && i < sequences->num_values) | |
6977 | min_seq = sequences->values[i].integer; | |
6978 | else | |
6979 | min_seq = 1; | |
6980 | ||
6981 | /* | |
6982 | * If we don't have any new events, nothing to do here... | |
6983 | */ | |
6984 | ||
10d09e33 | 6985 | if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events))) |
bd7854cb | 6986 | continue; |
6987 | ||
6988 | /* | |
6989 | * Otherwise copy all of the new events... | |
6990 | */ | |
6991 | ||
6992 | if (sub->first_event_id > min_seq) | |
6993 | j = 0; | |
6994 | else | |
6995 | j = min_seq - sub->first_event_id; | |
6996 | ||
10d09e33 | 6997 | for (; j < cupsArrayCount(sub->events); j ++) |
b423cd4c | 6998 | { |
6999 | ippAddSeparator(con->response); | |
ef416fc2 | 7000 | |
10d09e33 MS |
7001 | copy_attrs(con->response, |
7002 | ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL, | |
7003 | IPP_TAG_EVENT_NOTIFICATION, 0, NULL); | |
b423cd4c | 7004 | } |
7005 | } | |
7006 | } | |
ef416fc2 | 7007 | |
ef416fc2 | 7008 | |
b94498cf | 7009 | /* |
7010 | * 'get_ppd()' - Get a named PPD from the local system. | |
7011 | */ | |
7012 | ||
7013 | static void | |
7014 | get_ppd(cupsd_client_t *con, /* I - Client connection */ | |
7015 | ipp_attribute_t *uri) /* I - Printer URI or PPD name */ | |
7016 | { | |
7017 | http_status_t status; /* Policy status */ | |
7018 | cupsd_printer_t *dest; /* Destination */ | |
7019 | cups_ptype_t dtype; /* Destination type */ | |
7020 | ||
7021 | ||
7022 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con, | |
996acce8 | 7023 | con->number, uri, uri->name, uri->values[0].string.text); |
b94498cf | 7024 | |
e67e2f9e | 7025 | if (!strcmp(ippGetName(uri), "ppd-name")) |
b94498cf | 7026 | { |
7027 | /* | |
7028 | * Return a PPD file from cups-driverd... | |
7029 | */ | |
7030 | ||
e67e2f9e MS |
7031 | const char *ppd_name = ippGetString(uri, 0, NULL); |
7032 | /* ppd-name value */ | |
7033 | char command[1024], /* cups-driverd command */ | |
7034 | options[1024], /* Options to pass to command */ | |
7035 | oppd_name[1024]; /* Escaped ppd-name */ | |
b94498cf | 7036 | |
7037 | /* | |
7038 | * Check policy... | |
7039 | */ | |
7040 | ||
7041 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7042 | { | |
7043 | send_http_error(con, status, NULL); | |
7044 | return; | |
7045 | } | |
7046 | ||
e67e2f9e MS |
7047 | /* |
7048 | * Check ppd-name value... | |
7049 | */ | |
7050 | ||
7051 | if (strstr(ppd_name, "../")) | |
7052 | { | |
7053 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value.")); | |
7054 | return; | |
7055 | } | |
7056 | ||
b94498cf | 7057 | /* |
7058 | * Run cups-driverd command with the given options... | |
7059 | */ | |
7060 | ||
7061 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); | |
e67e2f9e MS |
7062 | url_encode_string(ppd_name, oppd_name, sizeof(oppd_name)); |
7063 | snprintf(options, sizeof(options), "get+%d+%s", ippGetRequestId(con->request), oppd_name); | |
b94498cf | 7064 | |
7065 | if (cupsdSendCommand(con, command, options, 0)) | |
7066 | { | |
7067 | /* | |
7068 | * Command started successfully, don't send an IPP response here... | |
7069 | */ | |
7070 | ||
7071 | ippDelete(con->response); | |
7072 | con->response = NULL; | |
7073 | } | |
7074 | else | |
7075 | { | |
7076 | /* | |
7077 | * Command failed, return "internal error" so the user knows something | |
7078 | * went wrong... | |
7079 | */ | |
7080 | ||
e67e2f9e | 7081 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("cups-driverd failed to execute.")); |
b94498cf | 7082 | } |
7083 | } | |
e67e2f9e | 7084 | else if (!strcmp(ippGetName(uri), "printer-uri") && cupsdValidateDest(ippGetString(uri, 0, NULL), &dtype, &dest)) |
b94498cf | 7085 | { |
7086 | int i; /* Looping var */ | |
7087 | char filename[1024]; /* PPD filename */ | |
7088 | ||
b94498cf | 7089 | /* |
7090 | * Check policy... | |
7091 | */ | |
7092 | ||
7093 | if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7094 | { | |
2fb76298 | 7095 | send_http_error(con, status, dest); |
b94498cf | 7096 | return; |
7097 | } | |
7098 | ||
7099 | /* | |
7100 | * See if we need the PPD for a class or remote printer... | |
7101 | */ | |
7102 | ||
e67e2f9e | 7103 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->name); |
bc44d920 | 7104 | |
7105 | if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0)) | |
b94498cf | 7106 | { |
e67e2f9e MS |
7107 | send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer.")); |
7108 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->uri); | |
b94498cf | 7109 | return; |
7110 | } | |
a2326b5b | 7111 | else if (dtype & CUPS_PRINTER_CLASS) |
b94498cf | 7112 | { |
7113 | for (i = 0; i < dest->num_printers; i ++) | |
a2326b5b | 7114 | if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS)) |
bc44d920 | 7115 | { |
e67e2f9e | 7116 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->printers[i]->name); |
bc44d920 | 7117 | |
7118 | if (!access(filename, 0)) | |
7119 | break; | |
7120 | } | |
b94498cf | 7121 | |
7122 | if (i < dest->num_printers) | |
7123 | dest = dest->printers[i]; | |
7124 | else | |
7125 | { | |
e67e2f9e MS |
7126 | send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer.")); |
7127 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->printers[0]->uri); | |
b94498cf | 7128 | return; |
7129 | } | |
7130 | } | |
7131 | ||
7132 | /* | |
7133 | * Found the printer with the PPD file, now see if there is one... | |
7134 | */ | |
7135 | ||
b94498cf | 7136 | if ((con->file = open(filename, O_RDONLY)) < 0) |
7137 | { | |
e67e2f9e | 7138 | 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 | 7139 | return; |
7140 | } | |
7141 | ||
7142 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
7143 | ||
7144 | con->pipe_pid = 0; | |
7145 | ||
e67e2f9e | 7146 | ippSetStatusCode(con->response, IPP_STATUS_OK); |
b94498cf | 7147 | } |
7148 | else | |
e67e2f9e | 7149 | send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be found."), ippGetString(uri, 0, NULL)); |
b94498cf | 7150 | } |
7151 | ||
7152 | ||
b423cd4c | 7153 | /* |
7154 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
7155 | */ | |
ef416fc2 | 7156 | |
b423cd4c | 7157 | static void |
7158 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
7159 | { | |
7160 | http_status_t status; /* Policy status */ | |
b423cd4c | 7161 | ipp_attribute_t *limit, /* Limit attribute */ |
b94498cf | 7162 | *device, /* ppd-device-id attribute */ |
7163 | *language, /* ppd-natural-language attribute */ | |
b423cd4c | 7164 | *make, /* ppd-make attribute */ |
b94498cf | 7165 | *model, /* ppd-make-and-model attribute */ |
3d8365b8 | 7166 | *model_number, /* ppd-model-number attribute */ |
b94498cf | 7167 | *product, /* ppd-product attribute */ |
7168 | *psversion, /* ppd-psverion attribute */ | |
3d8365b8 | 7169 | *type, /* ppd-type attribute */ |
ed6e7faf MS |
7170 | *requested, /* requested-attributes attribute */ |
7171 | *exclude, /* exclude-schemes attribute */ | |
7172 | *include; /* include-schemes attribute */ | |
b94498cf | 7173 | char command[1024], /* cups-driverd command */ |
ed6e7faf | 7174 | options[4096], /* Options to pass to command */ |
b94498cf | 7175 | device_str[256],/* Escaped ppd-device-id string */ |
7176 | language_str[256], | |
bc44d920 | 7177 | /* Escaped ppd-natural-language */ |
b94498cf | 7178 | make_str[256], /* Escaped ppd-make string */ |
7179 | model_str[256], /* Escaped ppd-make-and-model string */ | |
3d8365b8 | 7180 | model_number_str[256], |
7181 | /* ppd-model-number string */ | |
b94498cf | 7182 | product_str[256], |
7183 | /* Escaped ppd-product string */ | |
7184 | psversion_str[256], | |
7185 | /* Escaped ppd-psversion string */ | |
3d8365b8 | 7186 | type_str[256], /* Escaped ppd-type string */ |
ed6e7faf | 7187 | requested_str[256], |
89d46774 | 7188 | /* String for requested attributes */ |
ed6e7faf MS |
7189 | exclude_str[512], |
7190 | /* String for excluded schemes */ | |
7191 | include_str[512]; | |
7192 | /* String for included schemes */ | |
ef416fc2 | 7193 | |
ef416fc2 | 7194 | |
996acce8 | 7195 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->number); |
ef416fc2 | 7196 | |
7197 | /* | |
b423cd4c | 7198 | * Check policy... |
ef416fc2 | 7199 | */ |
7200 | ||
b423cd4c | 7201 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7202 | { |
f899b121 | 7203 | send_http_error(con, status, NULL); |
b423cd4c | 7204 | return; |
ef416fc2 | 7205 | } |
ef416fc2 | 7206 | |
b423cd4c | 7207 | /* |
7208 | * Run cups-driverd command with the given options... | |
7209 | */ | |
7210 | ||
3d8365b8 | 7211 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
7212 | device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT); | |
7213 | language = ippFindAttribute(con->request, "ppd-natural-language", | |
7214 | IPP_TAG_LANGUAGE); | |
7215 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
7216 | model = ippFindAttribute(con->request, "ppd-make-and-model", | |
7217 | IPP_TAG_TEXT); | |
7218 | model_number = ippFindAttribute(con->request, "ppd-model-number", | |
7219 | IPP_TAG_INTEGER); | |
7220 | product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT); | |
7221 | psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT); | |
7222 | type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD); | |
7223 | requested = ippFindAttribute(con->request, "requested-attributes", | |
7224 | IPP_TAG_KEYWORD); | |
8b450588 MS |
7225 | exclude = ippFindAttribute(con->request, "exclude-schemes", |
7226 | IPP_TAG_NAME); | |
7227 | include = ippFindAttribute(con->request, "include-schemes", | |
7228 | IPP_TAG_NAME); | |
b423cd4c | 7229 | |
7230 | if (requested) | |
89d46774 | 7231 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
7232 | else | |
7233 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); | |
ef416fc2 | 7234 | |
b94498cf | 7235 | if (device) |
7236 | url_encode_attr(device, device_str, sizeof(device_str)); | |
7237 | else | |
7238 | device_str[0] = '\0'; | |
7239 | ||
7240 | if (language) | |
7241 | url_encode_attr(language, language_str, sizeof(language_str)); | |
7242 | else | |
7243 | language_str[0] = '\0'; | |
7244 | ||
89d46774 | 7245 | if (make) |
7246 | url_encode_attr(make, make_str, sizeof(make_str)); | |
b423cd4c | 7247 | else |
89d46774 | 7248 | make_str[0] = '\0'; |
fa73b229 | 7249 | |
b94498cf | 7250 | if (model) |
7251 | url_encode_attr(model, model_str, sizeof(model_str)); | |
7252 | else | |
7253 | model_str[0] = '\0'; | |
7254 | ||
3d8365b8 | 7255 | if (model_number) |
7256 | snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d", | |
7257 | model_number->values[0].integer); | |
7258 | else | |
7259 | model_number_str[0] = '\0'; | |
7260 | ||
b94498cf | 7261 | if (product) |
7262 | url_encode_attr(product, product_str, sizeof(product_str)); | |
7263 | else | |
7264 | product_str[0] = '\0'; | |
7265 | ||
7266 | if (psversion) | |
7267 | url_encode_attr(psversion, psversion_str, sizeof(psversion_str)); | |
7268 | else | |
7269 | psversion_str[0] = '\0'; | |
7270 | ||
3d8365b8 | 7271 | if (type) |
7272 | url_encode_attr(type, type_str, sizeof(type_str)); | |
7273 | else | |
7274 | type_str[0] = '\0'; | |
7275 | ||
ed6e7faf MS |
7276 | if (exclude) |
7277 | url_encode_attr(exclude, exclude_str, sizeof(exclude_str)); | |
7278 | else | |
7279 | exclude_str[0] = '\0'; | |
7280 | ||
7281 | if (include) | |
7282 | url_encode_attr(include, include_str, sizeof(include_str)); | |
7283 | else | |
7284 | include_str[0] = '\0'; | |
7285 | ||
b423cd4c | 7286 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); |
3d8365b8 | 7287 | snprintf(options, sizeof(options), |
ed6e7faf | 7288 | "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 | 7289 | con->request->request.op.request_id, |
7290 | limit ? limit->values[0].integer : 0, | |
b94498cf | 7291 | requested_str, |
7292 | device ? "%20" : "", device_str, | |
7293 | language ? "%20" : "", language_str, | |
7294 | make ? "%20" : "", make_str, | |
7295 | model ? "%20" : "", model_str, | |
3d8365b8 | 7296 | model_number ? "%20" : "", model_number_str, |
b94498cf | 7297 | product ? "%20" : "", product_str, |
3d8365b8 | 7298 | psversion ? "%20" : "", psversion_str, |
ed6e7faf MS |
7299 | type ? "%20" : "", type_str, |
7300 | exclude_str[0] ? "%20" : "", exclude_str, | |
7301 | include_str[0] ? "%20" : "", include_str); | |
ef416fc2 | 7302 | |
b423cd4c | 7303 | if (cupsdSendCommand(con, command, options, 0)) |
7304 | { | |
7305 | /* | |
7306 | * Command started successfully, don't send an IPP response here... | |
7307 | */ | |
7308 | ||
7309 | ippDelete(con->response); | |
7310 | con->response = NULL; | |
7311 | } | |
7312 | else | |
7313 | { | |
7314 | /* | |
7315 | * Command failed, return "internal error" so the user knows something | |
7316 | * went wrong... | |
7317 | */ | |
7318 | ||
7319 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7320 | _("cups-driverd failed to execute.")); | |
7321 | } | |
ef416fc2 | 7322 | } |
7323 | ||
7324 | ||
7325 | /* | |
b423cd4c | 7326 | * 'get_printer_attrs()' - Get printer attributes. |
ef416fc2 | 7327 | */ |
7328 | ||
7329 | static void | |
b423cd4c | 7330 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ |
7331 | ipp_attribute_t *uri) /* I - Printer URI */ | |
ef416fc2 | 7332 | { |
7333 | http_status_t status; /* Policy status */ | |
bc44d920 | 7334 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 7335 | cupsd_printer_t *printer; /* Printer/class */ |
ef416fc2 | 7336 | cups_array_t *ra; /* Requested attributes array */ |
7337 | ||
7338 | ||
b423cd4c | 7339 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, |
996acce8 | 7340 | con->number, uri->values[0].string.text); |
ef416fc2 | 7341 | |
7342 | /* | |
b423cd4c | 7343 | * Is the destination valid? |
ef416fc2 | 7344 | */ |
7345 | ||
f7deaa1a | 7346 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 7347 | { |
7348 | /* | |
b423cd4c | 7349 | * Bad URI... |
ef416fc2 | 7350 | */ |
7351 | ||
7352 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 7353 | _("The printer or class does not exist.")); |
ef416fc2 | 7354 | return; |
7355 | } | |
7356 | ||
7357 | /* | |
7358 | * Check policy... | |
7359 | */ | |
7360 | ||
b423cd4c | 7361 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7362 | { |
f899b121 | 7363 | send_http_error(con, status, printer); |
ef416fc2 | 7364 | return; |
7365 | } | |
7366 | ||
7367 | /* | |
b423cd4c | 7368 | * Send the attributes... |
ef416fc2 | 7369 | */ |
7370 | ||
7371 | ra = create_requested_array(con->request); | |
7372 | ||
b423cd4c | 7373 | copy_printer_attrs(con, printer, ra); |
ef416fc2 | 7374 | |
7375 | cupsArrayDelete(ra); | |
7376 | ||
7377 | con->response->request.status.status_code = IPP_OK; | |
7378 | } | |
7379 | ||
7380 | ||
c168a833 MS |
7381 | /* |
7382 | * 'get_printer_supported()' - Get printer supported values. | |
7383 | */ | |
7384 | ||
7385 | static void | |
7386 | get_printer_supported( | |
7387 | cupsd_client_t *con, /* I - Client connection */ | |
7388 | ipp_attribute_t *uri) /* I - Printer URI */ | |
7389 | { | |
7390 | http_status_t status; /* Policy status */ | |
7391 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
7392 | cupsd_printer_t *printer; /* Printer/class */ | |
7393 | ||
7394 | ||
7395 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con, | |
996acce8 | 7396 | con->number, uri->values[0].string.text); |
c168a833 MS |
7397 | |
7398 | /* | |
7399 | * Is the destination valid? | |
7400 | */ | |
7401 | ||
7402 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
7403 | { | |
7404 | /* | |
7405 | * Bad URI... | |
7406 | */ | |
7407 | ||
7408 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 7409 | _("The printer or class does not exist.")); |
c168a833 MS |
7410 | return; |
7411 | } | |
7412 | ||
7413 | /* | |
7414 | * Check policy... | |
7415 | */ | |
7416 | ||
7417 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7418 | { | |
7419 | send_http_error(con, status, printer); | |
7420 | return; | |
7421 | } | |
7422 | ||
7423 | /* | |
7424 | * Return a list of attributes that can be set via Set-Printer-Attributes. | |
7425 | */ | |
7426 | ||
9b4bd602 MS |
7427 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE, |
7428 | "printer-geo-location", 0); | |
c168a833 MS |
7429 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE, |
7430 | "printer-info", 0); | |
7431 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE, | |
7432 | "printer-location", 0); | |
9b4bd602 MS |
7433 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE, |
7434 | "printer-organization", 0); | |
7435 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE, | |
7436 | "printer-organizational-unit", 0); | |
c168a833 MS |
7437 | |
7438 | con->response->request.status.status_code = IPP_OK; | |
7439 | } | |
7440 | ||
7441 | ||
ef416fc2 | 7442 | /* |
b423cd4c | 7443 | * 'get_printers()' - Get a list of printers or classes. |
ef416fc2 | 7444 | */ |
7445 | ||
7446 | static void | |
b423cd4c | 7447 | get_printers(cupsd_client_t *con, /* I - Client connection */ |
7448 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
ef416fc2 | 7449 | { |
b423cd4c | 7450 | http_status_t status; /* Policy status */ |
7451 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 7452 | int limit; /* Max number of printers to return */ |
b423cd4c | 7453 | int count; /* Number of printers that match */ |
409f5497 | 7454 | int printer_id; /* Printer we are interested in */ |
b423cd4c | 7455 | cupsd_printer_t *printer; /* Current printer pointer */ |
7e86f2f6 | 7456 | cups_ptype_t printer_type, /* printer-type attribute */ |
b423cd4c | 7457 | printer_mask; /* printer-type-mask attribute */ |
7458 | char *location; /* Location string */ | |
7459 | const char *username; /* Current user */ | |
7460 | char *first_printer_name; /* first-printer-name attribute */ | |
7461 | cups_array_t *ra; /* Requested attributes array */ | |
e07d4801 | 7462 | int local; /* Local connection? */ |
ef416fc2 | 7463 | |
ef416fc2 | 7464 | |
b423cd4c | 7465 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, |
996acce8 | 7466 | con->number, type); |
ef416fc2 | 7467 | |
7468 | /* | |
7469 | * Check policy... | |
7470 | */ | |
7471 | ||
b423cd4c | 7472 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7473 | { |
f899b121 | 7474 | send_http_error(con, status, NULL); |
ef416fc2 | 7475 | return; |
7476 | } | |
7477 | ||
7478 | /* | |
b423cd4c | 7479 | * Check for printers... |
ef416fc2 | 7480 | */ |
7481 | ||
b423cd4c | 7482 | if (!Printers || !cupsArrayCount(Printers)) |
7483 | { | |
7484 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
7485 | return; | |
7486 | } | |
7487 | ||
7488 | /* | |
7489 | * See if they want to limit the number of printers reported... | |
7490 | */ | |
ef416fc2 | 7491 | |
fa73b229 | 7492 | if ((attr = ippFindAttribute(con->request, "limit", |
7493 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 7494 | limit = attr->values[0].integer; |
7495 | else | |
b423cd4c | 7496 | limit = 10000000; |
7497 | ||
7498 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
7499 | IPP_TAG_NAME)) != NULL) | |
7500 | first_printer_name = attr->values[0].string.text; | |
7501 | else | |
7502 | first_printer_name = NULL; | |
ef416fc2 | 7503 | |
7504 | /* | |
b423cd4c | 7505 | * Support filtering... |
ef416fc2 | 7506 | */ |
7507 | ||
409f5497 MS |
7508 | if ((attr = ippFindAttribute(con->request, "printer-id", IPP_TAG_INTEGER)) != NULL) |
7509 | { | |
7510 | if ((printer_id = ippGetInteger(attr, 0)) <= 0) | |
7511 | { | |
7512 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad \"printer-id\" value %d."), printer_id); | |
7513 | return; | |
7514 | } | |
7515 | } | |
7516 | else | |
7517 | printer_id = 0; | |
7518 | ||
b423cd4c | 7519 | if ((attr = ippFindAttribute(con->request, "printer-type", |
7520 | IPP_TAG_ENUM)) != NULL) | |
7e86f2f6 | 7521 | printer_type = (cups_ptype_t)attr->values[0].integer; |
ef416fc2 | 7522 | else |
7e86f2f6 | 7523 | printer_type = (cups_ptype_t)0; |
ef416fc2 | 7524 | |
b423cd4c | 7525 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", |
7526 | IPP_TAG_ENUM)) != NULL) | |
7e86f2f6 | 7527 | printer_mask = (cups_ptype_t)attr->values[0].integer; |
ef416fc2 | 7528 | else |
7e86f2f6 | 7529 | printer_mask = (cups_ptype_t)0; |
e00b005a | 7530 | |
e07d4801 MS |
7531 | local = httpAddrLocalhost(&(con->clientaddr)); |
7532 | ||
b423cd4c | 7533 | if ((attr = ippFindAttribute(con->request, "printer-location", |
7534 | IPP_TAG_TEXT)) != NULL) | |
7535 | location = attr->values[0].string.text; | |
7536 | else | |
7537 | location = NULL; | |
e00b005a | 7538 | |
7539 | if (con->username[0]) | |
b423cd4c | 7540 | username = con->username; |
e00b005a | 7541 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
7542 | IPP_TAG_NAME)) != NULL) | |
b423cd4c | 7543 | username = attr->values[0].string.text; |
e00b005a | 7544 | else |
b423cd4c | 7545 | username = NULL; |
ef416fc2 | 7546 | |
b423cd4c | 7547 | ra = create_requested_array(con->request); |
ef416fc2 | 7548 | |
7549 | /* | |
b423cd4c | 7550 | * OK, build a list of printers for this printer... |
ef416fc2 | 7551 | */ |
7552 | ||
b423cd4c | 7553 | if (first_printer_name) |
ef416fc2 | 7554 | { |
b423cd4c | 7555 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) |
7556 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 7557 | } |
7558 | else | |
b423cd4c | 7559 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
ef416fc2 | 7560 | |
b423cd4c | 7561 | for (count = 0; |
7562 | count < limit && printer; | |
7563 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
7564 | { | |
e07d4801 MS |
7565 | if (!local && !printer->shared) |
7566 | continue; | |
7567 | ||
409f5497 MS |
7568 | if (printer_id && printer->printer_id != printer_id) |
7569 | continue; | |
7570 | ||
b423cd4c | 7571 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && |
7572 | (printer->type & printer_mask) == printer_type && | |
b19ccc9e | 7573 | (!location || |
88f9aafc | 7574 | (printer->location && !_cups_strcasecmp(printer->location, location)))) |
ef416fc2 | 7575 | { |
b423cd4c | 7576 | /* |
7577 | * If a username is specified, see if it is allowed or denied | |
7578 | * access... | |
7579 | */ | |
ef416fc2 | 7580 | |
10d09e33 MS |
7581 | if (cupsArrayCount(printer->users) && username && |
7582 | !user_allowed(printer, username)) | |
b423cd4c | 7583 | continue; |
ef416fc2 | 7584 | |
b423cd4c | 7585 | /* |
7586 | * Add the group separator as needed... | |
7587 | */ | |
ef416fc2 | 7588 | |
b423cd4c | 7589 | if (count > 0) |
7590 | ippAddSeparator(con->response); | |
ef416fc2 | 7591 | |
b423cd4c | 7592 | count ++; |
ef416fc2 | 7593 | |
b423cd4c | 7594 | /* |
7595 | * Send the attributes... | |
7596 | */ | |
ef416fc2 | 7597 | |
b423cd4c | 7598 | copy_printer_attrs(con, printer, ra); |
7599 | } | |
ef416fc2 | 7600 | } |
7601 | ||
b423cd4c | 7602 | cupsArrayDelete(ra); |
ef416fc2 | 7603 | |
7604 | con->response->request.status.status_code = IPP_OK; | |
7605 | } | |
7606 | ||
7607 | ||
7608 | /* | |
b423cd4c | 7609 | * 'get_subscription_attrs()' - Get subscription attributes. |
ef416fc2 | 7610 | */ |
7611 | ||
7612 | static void | |
b423cd4c | 7613 | get_subscription_attrs( |
7614 | cupsd_client_t *con, /* I - Client connection */ | |
7615 | int sub_id) /* I - Subscription ID */ | |
ef416fc2 | 7616 | { |
b423cd4c | 7617 | http_status_t status; /* Policy status */ |
7618 | cupsd_subscription_t *sub; /* Subscription */ | |
10d09e33 MS |
7619 | cupsd_policy_t *policy; /* Current security policy */ |
7620 | cups_array_t *ra, /* Requested attributes array */ | |
7621 | *exclude; /* Private attributes array */ | |
ef416fc2 | 7622 | |
7623 | ||
b423cd4c | 7624 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7625 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
996acce8 | 7626 | con, con->number, sub_id); |
ef416fc2 | 7627 | |
79d3cd17 MS |
7628 | /* |
7629 | * Expire subscriptions as needed... | |
7630 | */ | |
7631 | ||
7632 | cupsdExpireSubscriptions(NULL, NULL); | |
7633 | ||
fa73b229 | 7634 | /* |
b423cd4c | 7635 | * Is the subscription ID valid? |
fa73b229 | 7636 | */ |
7637 | ||
b423cd4c | 7638 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
fa73b229 | 7639 | { |
7640 | /* | |
b423cd4c | 7641 | * Bad subscription ID... |
fa73b229 | 7642 | */ |
7643 | ||
84315f46 MS |
7644 | send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."), |
7645 | sub_id); | |
fa73b229 | 7646 | return; |
7647 | } | |
7648 | ||
7649 | /* | |
7650 | * Check policy... | |
7651 | */ | |
7652 | ||
10d09e33 MS |
7653 | if (sub->dest) |
7654 | policy = sub->dest->op_policy_ptr; | |
7655 | else | |
7656 | policy = DefaultPolicyPtr; | |
7657 | ||
7658 | if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK) | |
fa73b229 | 7659 | { |
f899b121 | 7660 | send_http_error(con, status, sub->dest); |
fa73b229 | 7661 | return; |
7662 | } | |
7663 | ||
10d09e33 MS |
7664 | exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner); |
7665 | ||
ef416fc2 | 7666 | /* |
b423cd4c | 7667 | * Copy the subscription attributes to the response using the |
7668 | * requested-attributes attribute that may be provided by the client. | |
ef416fc2 | 7669 | */ |
7670 | ||
b423cd4c | 7671 | ra = create_requested_array(con->request); |
fa73b229 | 7672 | |
10d09e33 | 7673 | copy_subscription_attrs(con, sub, ra, exclude); |
ef416fc2 | 7674 | |
b423cd4c | 7675 | cupsArrayDelete(ra); |
fa73b229 | 7676 | |
b423cd4c | 7677 | con->response->request.status.status_code = IPP_OK; |
7678 | } | |
fa73b229 | 7679 | |
fa73b229 | 7680 | |
b423cd4c | 7681 | /* |
7682 | * 'get_subscriptions()' - Get subscriptions. | |
7683 | */ | |
ef416fc2 | 7684 | |
b423cd4c | 7685 | static void |
7686 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
7687 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
7688 | { | |
7689 | http_status_t status; /* Policy status */ | |
7690 | int count; /* Number of subscriptions */ | |
7691 | int limit; /* Limit */ | |
7692 | cupsd_subscription_t *sub; /* Subscription */ | |
7693 | cups_array_t *ra; /* Requested attributes array */ | |
7694 | ipp_attribute_t *attr; /* Attribute */ | |
bc44d920 | 7695 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
f7deaa1a | 7696 | char scheme[HTTP_MAX_URI], |
7697 | /* Scheme portion of URI */ | |
b423cd4c | 7698 | username[HTTP_MAX_URI], |
7699 | /* Username portion of URI */ | |
7700 | host[HTTP_MAX_URI], | |
7701 | /* Host portion of URI */ | |
7702 | resource[HTTP_MAX_URI]; | |
7703 | /* Resource portion of URI */ | |
7704 | int port; /* Port portion of URI */ | |
7705 | cupsd_job_t *job; /* Job pointer */ | |
7706 | cupsd_printer_t *printer; /* Printer */ | |
10d09e33 MS |
7707 | cupsd_policy_t *policy; /* Policy */ |
7708 | cups_array_t *exclude; /* Private attributes array */ | |
fa73b229 | 7709 | |
fa73b229 | 7710 | |
b423cd4c | 7711 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7712 | "get_subscriptions(con=%p[%d], uri=%s)", | |
996acce8 | 7713 | con, con->number, uri->values[0].string.text); |
b423cd4c | 7714 | |
7715 | /* | |
7716 | * Is the destination valid? | |
7717 | */ | |
7718 | ||
f7deaa1a | 7719 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7720 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7721 | sizeof(host), &port, resource, sizeof(resource)); |
7722 | ||
7723 | if (!strcmp(resource, "/") || | |
7724 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
7725 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
7726 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
7727 | { | |
7728 | printer = NULL; | |
7729 | job = NULL; | |
ef416fc2 | 7730 | } |
b423cd4c | 7731 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) |
ef416fc2 | 7732 | { |
b423cd4c | 7733 | printer = NULL; |
7734 | job = cupsdFindJob(atoi(resource + 6)); | |
ef416fc2 | 7735 | |
b423cd4c | 7736 | if (!job) |
ef416fc2 | 7737 | { |
84315f46 MS |
7738 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
7739 | atoi(resource + 6)); | |
ef416fc2 | 7740 | return; |
7741 | } | |
b423cd4c | 7742 | } |
f7deaa1a | 7743 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
b423cd4c | 7744 | { |
fa73b229 | 7745 | /* |
b423cd4c | 7746 | * Bad URI... |
fa73b229 | 7747 | */ |
7748 | ||
b423cd4c | 7749 | send_ipp_status(con, IPP_NOT_FOUND, |
84315f46 | 7750 | _("The printer or class does not exist.")); |
b423cd4c | 7751 | return; |
7752 | } | |
7753 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
7754 | IPP_TAG_INTEGER)) != NULL) | |
7755 | { | |
7756 | job = cupsdFindJob(attr->values[0].integer); | |
ef416fc2 | 7757 | |
b423cd4c | 7758 | if (!job) |
fa73b229 | 7759 | { |
84315f46 | 7760 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), |
b423cd4c | 7761 | attr->values[0].integer); |
fa73b229 | 7762 | return; |
7763 | } | |
ef416fc2 | 7764 | } |
b423cd4c | 7765 | else |
7766 | job = NULL; | |
ef416fc2 | 7767 | |
7768 | /* | |
b423cd4c | 7769 | * Check policy... |
ef416fc2 | 7770 | */ |
7771 | ||
10d09e33 MS |
7772 | if (printer) |
7773 | policy = printer->op_policy_ptr; | |
7774 | else | |
7775 | policy = DefaultPolicyPtr; | |
7776 | ||
7777 | if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK) | |
ef416fc2 | 7778 | { |
f899b121 | 7779 | send_http_error(con, status, printer); |
b423cd4c | 7780 | return; |
7781 | } | |
ef416fc2 | 7782 | |
79d3cd17 MS |
7783 | /* |
7784 | * Expire subscriptions as needed... | |
7785 | */ | |
7786 | ||
7787 | cupsdExpireSubscriptions(NULL, NULL); | |
7788 | ||
b423cd4c | 7789 | /* |
7790 | * Copy the subscription attributes to the response using the | |
7791 | * requested-attributes attribute that may be provided by the client. | |
7792 | */ | |
ef416fc2 | 7793 | |
b423cd4c | 7794 | ra = create_requested_array(con->request); |
ef416fc2 | 7795 | |
b423cd4c | 7796 | if ((attr = ippFindAttribute(con->request, "limit", |
7797 | IPP_TAG_INTEGER)) != NULL) | |
7798 | limit = attr->values[0].integer; | |
7799 | else | |
7800 | limit = 0; | |
ef416fc2 | 7801 | |
b423cd4c | 7802 | /* |
7803 | * See if we only want to see subscriptions for a specific user... | |
7804 | */ | |
ef416fc2 | 7805 | |
b423cd4c | 7806 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", |
7807 | IPP_TAG_BOOLEAN)) != NULL && | |
7808 | attr->values[0].boolean) | |
7809 | strlcpy(username, get_username(con), sizeof(username)); | |
fa73b229 | 7810 | else |
b423cd4c | 7811 | username[0] = '\0'; |
ef416fc2 | 7812 | |
b423cd4c | 7813 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; |
7814 | sub; | |
7815 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
7816 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
88f9aafc | 7817 | (!username[0] || !_cups_strcasecmp(username, sub->owner))) |
fa73b229 | 7818 | { |
b423cd4c | 7819 | ippAddSeparator(con->response); |
10d09e33 MS |
7820 | |
7821 | exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr : | |
7822 | policy, con, sub->dest, | |
7823 | sub->owner); | |
7824 | ||
7825 | copy_subscription_attrs(con, sub, ra, exclude); | |
ef416fc2 | 7826 | |
b423cd4c | 7827 | count ++; |
7828 | if (limit && count >= limit) | |
7829 | break; | |
7830 | } | |
ef416fc2 | 7831 | |
b423cd4c | 7832 | cupsArrayDelete(ra); |
fa73b229 | 7833 | |
b423cd4c | 7834 | if (count) |
7835 | con->response->request.status.status_code = IPP_OK; | |
7836 | else | |
7837 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
7838 | } | |
ef416fc2 | 7839 | |
ef416fc2 | 7840 | |
b423cd4c | 7841 | /* |
7842 | * 'get_username()' - Get the username associated with a request. | |
7843 | */ | |
ef416fc2 | 7844 | |
b423cd4c | 7845 | static const char * /* O - Username */ |
7846 | get_username(cupsd_client_t *con) /* I - Connection */ | |
7847 | { | |
7848 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 7849 | |
ef416fc2 | 7850 | |
b423cd4c | 7851 | if (con->username[0]) |
7852 | return (con->username); | |
7853 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
7854 | IPP_TAG_NAME)) != NULL) | |
7855 | return (attr->values[0].string.text); | |
7856 | else | |
7857 | return ("anonymous"); | |
ef416fc2 | 7858 | } |
7859 | ||
7860 | ||
7861 | /* | |
b423cd4c | 7862 | * 'hold_job()' - Hold a print job. |
ef416fc2 | 7863 | */ |
7864 | ||
b423cd4c | 7865 | static void |
7866 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
7867 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 7868 | { |
b9faaae1 MS |
7869 | ipp_attribute_t *attr; /* Current job-hold-until */ |
7870 | const char *when; /* New value */ | |
b423cd4c | 7871 | int jobid; /* Job ID */ |
61cf44e2 | 7872 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 7873 | username[HTTP_MAX_URI], /* Username portion of URI */ |
7874 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7875 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7876 | int port; /* Port portion of URI */ | |
7877 | cupsd_job_t *job; /* Job information */ | |
7878 | ||
ef416fc2 | 7879 | |
996acce8 | 7880 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->number, |
b423cd4c | 7881 | uri->values[0].string.text); |
ef416fc2 | 7882 | |
7883 | /* | |
b423cd4c | 7884 | * See if we have a job URI or a printer URI... |
ef416fc2 | 7885 | */ |
7886 | ||
b423cd4c | 7887 | if (!strcmp(uri->name, "printer-uri")) |
7888 | { | |
7889 | /* | |
7890 | * Got a printer URI; see if we also have a job-id attribute... | |
7891 | */ | |
ef416fc2 | 7892 | |
b423cd4c | 7893 | if ((attr = ippFindAttribute(con->request, "job-id", |
7894 | IPP_TAG_INTEGER)) == NULL) | |
7895 | { | |
7896 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 7897 | _("Got a printer-uri attribute but no job-id.")); |
b423cd4c | 7898 | return; |
7899 | } | |
ef416fc2 | 7900 | |
b423cd4c | 7901 | jobid = attr->values[0].integer; |
7902 | } | |
ef416fc2 | 7903 | else |
ef416fc2 | 7904 | { |
b423cd4c | 7905 | /* |
7906 | * Got a job URI; parse it to get the job ID... | |
7907 | */ | |
ef416fc2 | 7908 | |
61cf44e2 MS |
7909 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7910 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7911 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 7912 | |
b423cd4c | 7913 | if (strncmp(resource, "/jobs/", 6)) |
7914 | { | |
7915 | /* | |
7916 | * Not a valid URI! | |
7917 | */ | |
ef416fc2 | 7918 | |
b423cd4c | 7919 | send_ipp_status(con, IPP_BAD_REQUEST, |
84315f46 | 7920 | _("Bad job-uri \"%s\"."), |
b423cd4c | 7921 | uri->values[0].string.text); |
7922 | return; | |
7923 | } | |
ef416fc2 | 7924 | |
b423cd4c | 7925 | jobid = atoi(resource + 6); |
7926 | } | |
ef416fc2 | 7927 | |
ef416fc2 | 7928 | /* |
b423cd4c | 7929 | * See if the job exists... |
ef416fc2 | 7930 | */ |
7931 | ||
b423cd4c | 7932 | if ((job = cupsdFindJob(jobid)) == NULL) |
7933 | { | |
7934 | /* | |
7935 | * Nope - return a "not found" error... | |
7936 | */ | |
7937 | ||
84315f46 | 7938 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
b423cd4c | 7939 | return; |
7940 | } | |
ef416fc2 | 7941 | |
7942 | /* | |
b423cd4c | 7943 | * See if the job is owned by the requesting user... |
ef416fc2 | 7944 | */ |
7945 | ||
b423cd4c | 7946 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
7947 | { | |
b0f6947b MS |
7948 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
7949 | cupsdFindDest(job->dest)); | |
b423cd4c | 7950 | return; |
7951 | } | |
ef416fc2 | 7952 | |
f99f3698 MS |
7953 | /* |
7954 | * See if the job is in a state that allows holding... | |
7955 | */ | |
7956 | ||
7957 | if (job->state_value > IPP_JOB_STOPPED) | |
7958 | { | |
7959 | /* | |
7960 | * Return a "not-possible" error... | |
7961 | */ | |
7962 | ||
7963 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7964 | _("Job #%d is finished and cannot be altered."), | |
7965 | job->id); | |
7966 | return; | |
7967 | } | |
7968 | ||
ef416fc2 | 7969 | /* |
b423cd4c | 7970 | * Hold the job and return... |
ef416fc2 | 7971 | */ |
7972 | ||
4cadd620 | 7973 | if ((attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_ZERO)) != NULL) |
b423cd4c | 7974 | { |
4cadd620 MS |
7975 | if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1 || !ippValidateAttribute(attr)) |
7976 | { | |
7977 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value.")); | |
7978 | ippCopyAttribute(con->response, attr, 0); | |
7979 | return; | |
7980 | } | |
7981 | ||
7982 | when = ippGetString(attr, 0, NULL); | |
d09495fa | 7983 | |
01ce6322 | 7984 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
b9faaae1 | 7985 | "Job job-hold-until value changed by user."); |
b423cd4c | 7986 | } |
b9faaae1 MS |
7987 | else |
7988 | when = "indefinite"; | |
ef416fc2 | 7989 | |
b9faaae1 MS |
7990 | cupsdSetJobHoldUntil(job, when, 1); |
7991 | cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".", | |
7992 | username); | |
b423cd4c | 7993 | |
7994 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 7995 | } |
7996 | ||
7997 | ||
61cf44e2 MS |
7998 | /* |
7999 | * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class. | |
8000 | */ | |
8001 | ||
8002 | static void | |
8003 | hold_new_jobs(cupsd_client_t *con, /* I - Connection */ | |
8004 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8005 | { | |
8006 | http_status_t status; /* Policy status */ | |
8007 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
8008 | cupsd_printer_t *printer; /* Printer data */ | |
8009 | ||
8010 | ||
8011 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con, | |
996acce8 | 8012 | con->number, uri->values[0].string.text); |
61cf44e2 MS |
8013 | |
8014 | /* | |
8015 | * Is the destination valid? | |
8016 | */ | |
8017 | ||
8018 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
8019 | { | |
8020 | /* | |
8021 | * Bad URI... | |
8022 | */ | |
8023 | ||
8024 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8025 | _("The printer or class does not exist.")); |
61cf44e2 MS |
8026 | return; |
8027 | } | |
8028 | ||
8029 | /* | |
8030 | * Check policy... | |
8031 | */ | |
8032 | ||
8033 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8034 | { | |
8035 | send_http_error(con, status, printer); | |
8036 | return; | |
8037 | } | |
8038 | ||
8039 | /* | |
8040 | * Hold pending/new jobs sent to the printer... | |
8041 | */ | |
8042 | ||
8043 | printer->holding_new_jobs = 1; | |
8044 | ||
8045 | cupsdSetPrinterReasons(printer, "+hold-new-jobs"); | |
61cf44e2 MS |
8046 | |
8047 | if (dtype & CUPS_PRINTER_CLASS) | |
8048 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8049 | "Class \"%s\" now holding pending/new jobs (\"%s\").", | |
8050 | printer->name, get_username(con)); | |
8051 | else | |
8052 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8053 | "Printer \"%s\" now holding pending/new jobs (\"%s\").", | |
8054 | printer->name, get_username(con)); | |
8055 | ||
8056 | /* | |
8057 | * Everything was ok, so return OK status... | |
8058 | */ | |
8059 | ||
8060 | con->response->request.status.status_code = IPP_OK; | |
8061 | } | |
8062 | ||
8063 | ||
ef416fc2 | 8064 | /* |
b423cd4c | 8065 | * 'move_job()' - Move a job to a new destination. |
ef416fc2 | 8066 | */ |
8067 | ||
8068 | static void | |
b423cd4c | 8069 | move_job(cupsd_client_t *con, /* I - Client connection */ |
8070 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 8071 | { |
8072 | http_status_t status; /* Policy status */ | |
8073 | ipp_attribute_t *attr; /* Current attribute */ | |
b423cd4c | 8074 | int jobid; /* Job ID */ |
ef416fc2 | 8075 | cupsd_job_t *job; /* Current job */ |
d09495fa | 8076 | const char *src; /* Source printer/class */ |
b423cd4c | 8077 | cups_ptype_t stype, /* Source type (printer or class) */ |
bc44d920 | 8078 | dtype; /* Destination type (printer/class) */ |
f7deaa1a | 8079 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
ef416fc2 | 8080 | username[HTTP_MAX_URI], /* Username portion of URI */ |
8081 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
b423cd4c | 8082 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
ef416fc2 | 8083 | int port; /* Port portion of URI */ |
b423cd4c | 8084 | cupsd_printer_t *sprinter, /* Source printer */ |
8085 | *dprinter; /* Destination printer */ | |
ef416fc2 | 8086 | |
8087 | ||
996acce8 | 8088 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->number, |
ef416fc2 | 8089 | uri->values[0].string.text); |
8090 | ||
8091 | /* | |
b423cd4c | 8092 | * Get the new printer or class... |
ef416fc2 | 8093 | */ |
8094 | ||
b423cd4c | 8095 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", |
8096 | IPP_TAG_URI)) == NULL) | |
ef416fc2 | 8097 | { |
b423cd4c | 8098 | /* |
8099 | * Need job-printer-uri... | |
8100 | */ | |
ef416fc2 | 8101 | |
b423cd4c | 8102 | send_ipp_status(con, IPP_BAD_REQUEST, |
84315f46 | 8103 | _("job-printer-uri attribute missing.")); |
b423cd4c | 8104 | return; |
ef416fc2 | 8105 | } |
f7faf1f5 | 8106 | |
f7deaa1a | 8107 | if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter)) |
ef416fc2 | 8108 | { |
b423cd4c | 8109 | /* |
8110 | * Bad URI... | |
8111 | */ | |
ef416fc2 | 8112 | |
b423cd4c | 8113 | send_ipp_status(con, IPP_NOT_FOUND, |
84315f46 | 8114 | _("The printer or class does not exist.")); |
b423cd4c | 8115 | return; |
ef416fc2 | 8116 | } |
8117 | ||
ef416fc2 | 8118 | /* |
b423cd4c | 8119 | * See if we have a job URI or a printer URI... |
ef416fc2 | 8120 | */ |
8121 | ||
f7deaa1a | 8122 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
8123 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 8124 | sizeof(host), &port, resource, sizeof(resource)); |
8125 | ||
8126 | if (!strcmp(uri->name, "printer-uri")) | |
ef416fc2 | 8127 | { |
8128 | /* | |
b423cd4c | 8129 | * Got a printer URI; see if we also have a job-id attribute... |
ef416fc2 | 8130 | */ |
8131 | ||
b423cd4c | 8132 | if ((attr = ippFindAttribute(con->request, "job-id", |
8133 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8134 | { |
b423cd4c | 8135 | /* |
8136 | * Move all jobs... | |
8137 | */ | |
8138 | ||
f7deaa1a | 8139 | if ((src = cupsdValidateDest(uri->values[0].string.text, &stype, |
8140 | &sprinter)) == NULL) | |
b423cd4c | 8141 | { |
8142 | /* | |
8143 | * Bad URI... | |
8144 | */ | |
8145 | ||
8146 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8147 | _("The printer or class does not exist.")); |
b423cd4c | 8148 | return; |
8149 | } | |
8150 | ||
8151 | job = NULL; | |
8152 | } | |
8153 | else | |
8154 | { | |
8155 | /* | |
8156 | * Otherwise, just move a single job... | |
8157 | */ | |
8158 | ||
8159 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
8160 | { | |
8161 | /* | |
8162 | * Nope - return a "not found" error... | |
8163 | */ | |
8164 | ||
8165 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8166 | _("Job #%d does not exist."), attr->values[0].integer); |
b423cd4c | 8167 | return; |
8168 | } | |
8169 | else | |
8170 | { | |
8171 | /* | |
8172 | * Job found, initialize source pointers... | |
8173 | */ | |
8174 | ||
8175 | src = NULL; | |
8176 | sprinter = NULL; | |
8177 | } | |
ef416fc2 | 8178 | } |
8179 | } | |
8180 | else | |
8181 | { | |
8182 | /* | |
b423cd4c | 8183 | * Got a job URI; parse it to get the job ID... |
ef416fc2 | 8184 | */ |
8185 | ||
b423cd4c | 8186 | if (strncmp(resource, "/jobs/", 6)) |
8187 | { | |
8188 | /* | |
8189 | * Not a valid URI! | |
8190 | */ | |
8191 | ||
84315f46 | 8192 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
b423cd4c | 8193 | uri->values[0].string.text); |
8194 | return; | |
8195 | } | |
ef416fc2 | 8196 | |
ef416fc2 | 8197 | /* |
b423cd4c | 8198 | * See if the job exists... |
ef416fc2 | 8199 | */ |
8200 | ||
b423cd4c | 8201 | jobid = atoi(resource + 6); |
ef416fc2 | 8202 | |
b423cd4c | 8203 | if ((job = cupsdFindJob(jobid)) == NULL) |
ef416fc2 | 8204 | { |
8205 | /* | |
b423cd4c | 8206 | * Nope - return a "not found" error... |
ef416fc2 | 8207 | */ |
8208 | ||
84315f46 | 8209 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
b423cd4c | 8210 | return; |
ef416fc2 | 8211 | } |
8212 | else | |
b423cd4c | 8213 | { |
8214 | /* | |
8215 | * Job found, initialize source pointers... | |
8216 | */ | |
ef416fc2 | 8217 | |
b423cd4c | 8218 | src = NULL; |
8219 | sprinter = NULL; | |
8220 | } | |
ef416fc2 | 8221 | } |
8222 | ||
ac884b6a MS |
8223 | /* |
8224 | * Check the policy of the destination printer... | |
8225 | */ | |
8226 | ||
8227 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, | |
8228 | job ? job->username : NULL)) != HTTP_OK) | |
8229 | { | |
8230 | send_http_error(con, status, dprinter); | |
8231 | return; | |
8232 | } | |
8233 | ||
ef416fc2 | 8234 | /* |
b423cd4c | 8235 | * Now move the job or jobs... |
ef416fc2 | 8236 | */ |
8237 | ||
b423cd4c | 8238 | if (job) |
8239 | { | |
8240 | /* | |
8241 | * See if the job has been completed... | |
8242 | */ | |
8243 | ||
8244 | if (job->state_value > IPP_JOB_STOPPED) | |
8245 | { | |
8246 | /* | |
8247 | * Return a "not-possible" error... | |
8248 | */ | |
8249 | ||
8250 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 8251 | _("Job #%d is finished and cannot be altered."), |
b423cd4c | 8252 | job->id); |
8253 | return; | |
8254 | } | |
ef416fc2 | 8255 | |
b423cd4c | 8256 | /* |
8257 | * See if the job is owned by the requesting user... | |
8258 | */ | |
ef416fc2 | 8259 | |
b423cd4c | 8260 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8261 | { | |
b0f6947b MS |
8262 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
8263 | cupsdFindDest(job->dest)); | |
b423cd4c | 8264 | return; |
8265 | } | |
ef416fc2 | 8266 | |
ef416fc2 | 8267 | /* |
b423cd4c | 8268 | * Move the job to a different printer or class... |
ef416fc2 | 8269 | */ |
8270 | ||
e53920b9 | 8271 | cupsdMoveJob(job, dprinter); |
ef416fc2 | 8272 | } |
b423cd4c | 8273 | else |
8274 | { | |
8275 | /* | |
8276 | * Got the source printer, now look through the jobs... | |
8277 | */ | |
ef416fc2 | 8278 | |
b423cd4c | 8279 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
8280 | job; | |
8281 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
8282 | { | |
8283 | /* | |
8284 | * See if the job is pointing at the source printer or has not been | |
8285 | * completed... | |
8286 | */ | |
ef416fc2 | 8287 | |
88f9aafc | 8288 | if (_cups_strcasecmp(job->dest, src) || |
b423cd4c | 8289 | job->state_value > IPP_JOB_STOPPED) |
8290 | continue; | |
ef416fc2 | 8291 | |
b423cd4c | 8292 | /* |
8293 | * See if the job can be moved by the requesting user... | |
8294 | */ | |
ef416fc2 | 8295 | |
b423cd4c | 8296 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8297 | continue; | |
ef416fc2 | 8298 | |
b423cd4c | 8299 | /* |
8300 | * Move the job to a different printer or class... | |
8301 | */ | |
ef416fc2 | 8302 | |
e53920b9 | 8303 | cupsdMoveJob(job, dprinter); |
b423cd4c | 8304 | } |
ef416fc2 | 8305 | } |
8306 | ||
8307 | /* | |
b423cd4c | 8308 | * Start jobs if possible... |
ef416fc2 | 8309 | */ |
8310 | ||
b423cd4c | 8311 | cupsdCheckJobs(); |
ef416fc2 | 8312 | |
8313 | /* | |
b423cd4c | 8314 | * Return with "everything is OK" status... |
ef416fc2 | 8315 | */ |
8316 | ||
b423cd4c | 8317 | con->response->request.status.status_code = IPP_OK; |
8318 | } | |
ef416fc2 | 8319 | |
ef416fc2 | 8320 | |
b423cd4c | 8321 | /* |
8322 | * 'ppd_parse_line()' - Parse a PPD default line. | |
8323 | */ | |
ef416fc2 | 8324 | |
b423cd4c | 8325 | static int /* O - 0 on success, -1 on failure */ |
8326 | ppd_parse_line(const char *line, /* I - Line */ | |
8327 | char *option, /* O - Option name */ | |
8328 | int olen, /* I - Size of option name */ | |
8329 | char *choice, /* O - Choice name */ | |
8330 | int clen) /* I - Size of choice name */ | |
8331 | { | |
8332 | /* | |
8333 | * Verify this is a default option line... | |
8334 | */ | |
ef416fc2 | 8335 | |
b423cd4c | 8336 | if (strncmp(line, "*Default", 8)) |
8337 | return (-1); | |
ef416fc2 | 8338 | |
b423cd4c | 8339 | /* |
8340 | * Read the option name... | |
8341 | */ | |
8342 | ||
18ecb428 MS |
8343 | for (line += 8, olen --; |
8344 | *line > ' ' && *line < 0x7f && *line != ':' && *line != '/'; | |
8345 | line ++) | |
b423cd4c | 8346 | if (olen > 0) |
8347 | { | |
8348 | *option++ = *line; | |
8349 | olen --; | |
ef416fc2 | 8350 | } |
8351 | ||
b423cd4c | 8352 | *option = '\0'; |
ef416fc2 | 8353 | |
b423cd4c | 8354 | /* |
8355 | * Skip everything else up to the colon (:)... | |
8356 | */ | |
ef416fc2 | 8357 | |
b423cd4c | 8358 | while (*line && *line != ':') |
8359 | line ++; | |
ef416fc2 | 8360 | |
b423cd4c | 8361 | if (!*line) |
8362 | return (-1); | |
ef416fc2 | 8363 | |
b423cd4c | 8364 | line ++; |
ef416fc2 | 8365 | |
b423cd4c | 8366 | /* |
8367 | * Now grab the option choice, skipping leading whitespace... | |
8368 | */ | |
ef416fc2 | 8369 | |
b423cd4c | 8370 | while (isspace(*line & 255)) |
8371 | line ++; | |
ef416fc2 | 8372 | |
18ecb428 MS |
8373 | for (clen --; |
8374 | *line > ' ' && *line < 0x7f && *line != ':' && *line != '/'; | |
8375 | line ++) | |
b423cd4c | 8376 | if (clen > 0) |
8377 | { | |
8378 | *choice++ = *line; | |
8379 | clen --; | |
8380 | } | |
ef416fc2 | 8381 | |
b423cd4c | 8382 | *choice = '\0'; |
ef416fc2 | 8383 | |
b423cd4c | 8384 | /* |
8385 | * Return with no errors... | |
8386 | */ | |
ef416fc2 | 8387 | |
b423cd4c | 8388 | return (0); |
8389 | } | |
ef416fc2 | 8390 | |
ef416fc2 | 8391 | |
b423cd4c | 8392 | /* |
8393 | * 'print_job()' - Print a file to a printer or class. | |
8394 | */ | |
ef416fc2 | 8395 | |
b423cd4c | 8396 | static void |
8397 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
8398 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8399 | { | |
8400 | ipp_attribute_t *attr; /* Current attribute */ | |
9514a192 | 8401 | ipp_attribute_t *doc_name; /* document-name attribute */ |
b423cd4c | 8402 | ipp_attribute_t *format; /* Document-format attribute */ |
f7deaa1a | 8403 | const char *default_format; /* document-format-default value */ |
b423cd4c | 8404 | cupsd_job_t *job; /* New job */ |
8405 | char filename[1024]; /* Job filename */ | |
8406 | mime_type_t *filetype; /* Type of file */ | |
8407 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
8408 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
8409 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
8410 | /* Textual name of mime type */ | |
8411 | cupsd_printer_t *printer; /* Printer data */ | |
8412 | struct stat fileinfo; /* File information */ | |
8413 | int kbytes; /* Size of file */ | |
8414 | int compression; /* Document compression */ | |
ef416fc2 | 8415 | |
ef416fc2 | 8416 | |
996acce8 | 8417 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->number, |
b423cd4c | 8418 | uri->values[0].string.text); |
ef416fc2 | 8419 | |
b423cd4c | 8420 | /* |
8421 | * Validate print file attributes, for now just document-format and | |
8422 | * compression (CUPS only supports "none" and "gzip")... | |
8423 | */ | |
ef416fc2 | 8424 | |
b423cd4c | 8425 | compression = CUPS_FILE_NONE; |
ef416fc2 | 8426 | |
b423cd4c | 8427 | if ((attr = ippFindAttribute(con->request, "compression", |
8428 | IPP_TAG_KEYWORD)) != NULL) | |
8429 | { | |
8430 | if (strcmp(attr->values[0].string.text, "none") | |
8431 | #ifdef HAVE_LIBZ | |
8432 | && strcmp(attr->values[0].string.text, "gzip") | |
8433 | #endif /* HAVE_LIBZ */ | |
8434 | ) | |
8435 | { | |
8436 | send_ipp_status(con, IPP_ATTRIBUTES, | |
84315f46 | 8437 | _("Unsupported compression \"%s\"."), |
b423cd4c | 8438 | attr->values[0].string.text); |
8439 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
8440 | "compression", NULL, attr->values[0].string.text); | |
8441 | return; | |
8442 | } | |
ef416fc2 | 8443 | |
b423cd4c | 8444 | #ifdef HAVE_LIBZ |
8445 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
8446 | compression = CUPS_FILE_GZIP; | |
8447 | #endif /* HAVE_LIBZ */ | |
8448 | } | |
ef416fc2 | 8449 | |
b423cd4c | 8450 | /* |
8451 | * Do we have a file to print? | |
8452 | */ | |
ef416fc2 | 8453 | |
b423cd4c | 8454 | if (!con->filename) |
8455 | { | |
84315f46 | 8456 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request.")); |
b423cd4c | 8457 | return; |
8458 | } | |
ef416fc2 | 8459 | |
f7deaa1a | 8460 | /* |
8461 | * Is the destination valid? | |
8462 | */ | |
8463 | ||
8464 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
8465 | { | |
8466 | /* | |
8467 | * Bad URI... | |
8468 | */ | |
8469 | ||
8470 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8471 | _("The printer or class does not exist.")); |
f7deaa1a | 8472 | return; |
8473 | } | |
8474 | ||
b423cd4c | 8475 | /* |
8476 | * Is it a format we support? | |
8477 | */ | |
ef416fc2 | 8478 | |
9514a192 MS |
8479 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
8480 | if (doc_name) | |
8481 | ippSetName(con->request, &doc_name, "document-name-supplied"); | |
8482 | ||
b423cd4c | 8483 | if ((format = ippFindAttribute(con->request, "document-format", |
8484 | IPP_TAG_MIMETYPE)) != NULL) | |
8485 | { | |
8486 | /* | |
8487 | * Grab format from client... | |
8488 | */ | |
8489 | ||
c5b24bfa | 8490 | if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", super, |
f7deaa1a | 8491 | type) != 2) |
b423cd4c | 8492 | { |
8493 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 8494 | _("Bad document-format \"%s\"."), |
b423cd4c | 8495 | format->values[0].string.text); |
8496 | return; | |
ef416fc2 | 8497 | } |
9514a192 MS |
8498 | |
8499 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL)); | |
b423cd4c | 8500 | } |
f7deaa1a | 8501 | else if ((default_format = cupsGetOption("document-format", |
8502 | printer->num_options, | |
8503 | printer->options)) != NULL) | |
8504 | { | |
8505 | /* | |
8506 | * Use default document format... | |
8507 | */ | |
8508 | ||
c5b24bfa | 8509 | if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2) |
f7deaa1a | 8510 | { |
8511 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 8512 | _("Bad document-format \"%s\"."), |
f7deaa1a | 8513 | default_format); |
8514 | return; | |
8515 | } | |
8516 | } | |
b423cd4c | 8517 | else |
8518 | { | |
8519 | /* | |
f7deaa1a | 8520 | * Auto-type it! |
b423cd4c | 8521 | */ |
8522 | ||
5a9febac MS |
8523 | strlcpy(super, "application", sizeof(super)); |
8524 | strlcpy(type, "octet-stream", sizeof(type)); | |
b423cd4c | 8525 | } |
ef416fc2 | 8526 | |
b423cd4c | 8527 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
8528 | { | |
ef416fc2 | 8529 | /* |
b423cd4c | 8530 | * Auto-type the file... |
ef416fc2 | 8531 | */ |
8532 | ||
a0f6818e | 8533 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file..."); |
b423cd4c | 8534 | |
9514a192 | 8535 | |
b423cd4c | 8536 | filetype = mimeFileType(MimeDatabase, con->filename, |
8537 | doc_name ? doc_name->values[0].string.text : NULL, | |
8538 | &compression); | |
8539 | ||
f7deaa1a | 8540 | if (!filetype) |
8541 | filetype = mimeType(MimeDatabase, super, type); | |
a0f6818e MS |
8542 | |
8543 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.", | |
8544 | filetype->super, filetype->type); | |
9514a192 MS |
8545 | |
8546 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type); | |
8547 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype); | |
f7deaa1a | 8548 | } |
8549 | else | |
8550 | filetype = mimeType(MimeDatabase, super, type); | |
ef416fc2 | 8551 | |
f7deaa1a | 8552 | if (filetype && |
8553 | (!format || | |
8554 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) | |
8555 | { | |
8556 | /* | |
8557 | * Replace the document-format attribute value with the auto-typed or | |
8558 | * default one. | |
8559 | */ | |
b423cd4c | 8560 | |
f7deaa1a | 8561 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
8562 | filetype->type); | |
ed486911 | 8563 | |
f7deaa1a | 8564 | if (format) |
5e6c3df7 | 8565 | ippSetString(con->request, &format, 0, mimetype); |
b423cd4c | 8566 | else |
f7deaa1a | 8567 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
8568 | "document-format", NULL, mimetype); | |
ef416fc2 | 8569 | } |
f7deaa1a | 8570 | else if (!filetype) |
b423cd4c | 8571 | { |
8572 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
84315f46 | 8573 | _("Unsupported document-format \"%s\"."), |
c7017ecc MS |
8574 | format ? format->values[0].string.text : |
8575 | "application/octet-stream"); | |
b423cd4c | 8576 | cupsdLogMessage(CUPSD_LOG_INFO, |
8577 | "Hint: Do you have the raw file printing rules enabled?"); | |
8578 | ||
8579 | if (format) | |
8580 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
8581 | "document-format", NULL, format->values[0].string.text); | |
8582 | ||
8583 | return; | |
8584 | } | |
8585 | ||
b423cd4c | 8586 | /* |
8587 | * Read any embedded job ticket info from PS files... | |
8588 | */ | |
8589 | ||
88f9aafc MS |
8590 | if (!_cups_strcasecmp(filetype->super, "application") && |
8591 | (!_cups_strcasecmp(filetype->type, "postscript") || | |
8592 | !_cups_strcasecmp(filetype->type, "pdf"))) | |
c5571a1d | 8593 | read_job_ticket(con); |
b423cd4c | 8594 | |
8595 | /* | |
8596 | * Create the job object... | |
8597 | */ | |
8598 | ||
f7deaa1a | 8599 | if ((job = add_job(con, printer, filetype)) == NULL) |
b423cd4c | 8600 | return; |
8601 | ||
8602 | /* | |
8603 | * Update quota data... | |
8604 | */ | |
8605 | ||
8606 | if (stat(con->filename, &fileinfo)) | |
8607 | kbytes = 0; | |
8608 | else | |
8609 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
8610 | ||
8611 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
8612 | ||
f16ea703 MS |
8613 | job->koctets += kbytes; |
8614 | ||
8615 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL) | |
b423cd4c | 8616 | attr->values[0].integer += kbytes; |
8617 | ||
ef416fc2 | 8618 | /* |
8619 | * Add the job file... | |
8620 | */ | |
8621 | ||
8622 | if (add_file(con, job, filetype, compression)) | |
8623 | return; | |
8624 | ||
6d8021f4 MS |
8625 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files); |
8626 | if (rename(con->filename, filename)) | |
8627 | { | |
8628 | cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno)); | |
8629 | ||
8630 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file.")); | |
8631 | return; | |
8632 | } | |
8633 | ||
ef416fc2 | 8634 | cupsdClearString(&con->filename); |
8635 | ||
8636 | /* | |
8637 | * See if we need to add the ending sheet... | |
8638 | */ | |
8639 | ||
91c84a35 MS |
8640 | if (cupsdTimeoutJob(job)) |
8641 | return; | |
ef416fc2 | 8642 | |
ef416fc2 | 8643 | /* |
8644 | * Log and save the job... | |
8645 | */ | |
8646 | ||
75bd9771 MS |
8647 | cupsdLogJob(job, CUPSD_LOG_INFO, |
8648 | "File of type %s/%s queued by \"%s\".", | |
8649 | filetype->super, filetype->type, job->username); | |
8650 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until); | |
b19ccc9e MS |
8651 | cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".", |
8652 | job->dest, job->username); | |
ef416fc2 | 8653 | |
ef416fc2 | 8654 | /* |
8655 | * Start the job if possible... | |
8656 | */ | |
8657 | ||
8658 | cupsdCheckJobs(); | |
8659 | } | |
8660 | ||
8661 | ||
8662 | /* | |
c5571a1d | 8663 | * 'read_job_ticket()' - Read a job ticket embedded in a print file. |
ef416fc2 | 8664 | * |
c5571a1d | 8665 | * This function only gets called when printing a single PDF or PostScript |
ef416fc2 | 8666 | * file using the Print-Job operation. It doesn't work for Create-Job + |
8667 | * Send-File, since the job attributes need to be set at job creation | |
c5571a1d MS |
8668 | * time for banners to work. The embedded job ticket stuff is here |
8669 | * primarily to allow the Windows printer driver for CUPS to pass in JCL | |
ef416fc2 | 8670 | * options and IPP attributes which otherwise would be lost. |
8671 | * | |
c5571a1d | 8672 | * The format of a job ticket is simple: |
ef416fc2 | 8673 | * |
8674 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
8675 | * | |
8676 | * %cupsJobTicket: attr1=value1 | |
8677 | * %cupsJobTicket: attr2=value2 | |
8678 | * ... | |
8679 | * %cupsJobTicket: attrN=valueN | |
8680 | * | |
8681 | * Job ticket lines must appear immediately after the first line that | |
c5571a1d MS |
8682 | * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS |
8683 | * stops looking for job ticket info when it finds a line that does not begin | |
ef416fc2 | 8684 | * with "%cupsJobTicket:". |
8685 | * | |
8686 | * The maximum length of a job ticket line, including the prefix, is | |
8687 | * 255 characters to conform with the Adobe DSC. | |
8688 | * | |
8689 | * Read-only attributes are rejected with a notice to the error log in | |
8690 | * case a malicious user tries anything. Since the job ticket is read | |
8691 | * prior to attribute validation in print_job(), job ticket attributes | |
8692 | * will go through the same validation as IPP attributes... | |
8693 | */ | |
8694 | ||
8695 | static void | |
c5571a1d | 8696 | read_job_ticket(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 8697 | { |
8698 | cups_file_t *fp; /* File to read from */ | |
8699 | char line[256]; /* Line data */ | |
8700 | int num_options; /* Number of options */ | |
8701 | cups_option_t *options; /* Options */ | |
8702 | ipp_t *ticket; /* New attributes */ | |
8703 | ipp_attribute_t *attr, /* Current attribute */ | |
8704 | *attr2, /* Job attribute */ | |
8705 | *prev2; /* Previous job attribute */ | |
8706 | ||
8707 | ||
8708 | /* | |
8709 | * First open the print file... | |
8710 | */ | |
8711 | ||
8712 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
8713 | { | |
8714 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
c5571a1d | 8715 | "Unable to open print file for job ticket - %s", |
ef416fc2 | 8716 | strerror(errno)); |
8717 | return; | |
8718 | } | |
8719 | ||
8720 | /* | |
8721 | * Skip the first line... | |
8722 | */ | |
8723 | ||
8724 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
8725 | { | |
8726 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
c5571a1d | 8727 | "Unable to read from print file for job ticket - %s", |
ef416fc2 | 8728 | strerror(errno)); |
8729 | cupsFileClose(fp); | |
8730 | return; | |
8731 | } | |
8732 | ||
c5571a1d | 8733 | if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5)) |
ef416fc2 | 8734 | { |
8735 | /* | |
8736 | * Not a DSC-compliant file, so no job ticket info will be available... | |
8737 | */ | |
8738 | ||
8739 | cupsFileClose(fp); | |
8740 | return; | |
8741 | } | |
8742 | ||
8743 | /* | |
8744 | * Read job ticket info from the file... | |
8745 | */ | |
8746 | ||
8747 | num_options = 0; | |
8748 | options = NULL; | |
8749 | ||
fa73b229 | 8750 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 8751 | { |
8752 | /* | |
8753 | * Stop at the first non-ticket line... | |
8754 | */ | |
8755 | ||
fa73b229 | 8756 | if (strncmp(line, "%cupsJobTicket:", 15)) |
ef416fc2 | 8757 | break; |
8758 | ||
8759 | /* | |
8760 | * Add the options to the option array... | |
8761 | */ | |
8762 | ||
8763 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
8764 | } | |
8765 | ||
8766 | /* | |
8767 | * Done with the file; see if we have any options... | |
8768 | */ | |
8769 | ||
8770 | cupsFileClose(fp); | |
8771 | ||
8772 | if (num_options == 0) | |
8773 | return; | |
8774 | ||
8775 | /* | |
8776 | * OK, convert the options to an attribute list, and apply them to | |
8777 | * the request... | |
8778 | */ | |
8779 | ||
8780 | ticket = ippNew(); | |
8781 | cupsEncodeOptions(ticket, num_options, options); | |
8782 | ||
8783 | /* | |
8784 | * See what the user wants to change. | |
8785 | */ | |
8786 | ||
fa73b229 | 8787 | for (attr = ticket->attrs; attr; attr = attr->next) |
ef416fc2 | 8788 | { |
8789 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8790 | continue; | |
8791 | ||
9514a192 MS |
8792 | if (!strncmp(attr->name, "date-time-at-", 13) || |
8793 | !strcmp(attr->name, "job-impressions-completed") || | |
ef416fc2 | 8794 | !strcmp(attr->name, "job-media-sheets-completed") || |
9514a192 | 8795 | !strncmp(attr->name, "job-k-octets", 12) || |
ef416fc2 | 8796 | !strcmp(attr->name, "job-id") || |
9514a192 MS |
8797 | !strcmp(attr->name, "job-originating-host-name") || |
8798 | !strcmp(attr->name, "job-originating-user-name") || | |
8799 | !strcmp(attr->name, "job-pages-completed") || | |
8800 | !strcmp(attr->name, "job-printer-uri") || | |
ef416fc2 | 8801 | !strncmp(attr->name, "job-state", 9) || |
9514a192 | 8802 | !strcmp(attr->name, "job-uri") || |
ef416fc2 | 8803 | !strncmp(attr->name, "time-at-", 8)) |
8804 | continue; /* Read-only attrs */ | |
8805 | ||
fa73b229 | 8806 | if ((attr2 = ippFindAttribute(con->request, attr->name, |
8807 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 8808 | { |
8809 | /* | |
8810 | * Some other value; first free the old value... | |
8811 | */ | |
8812 | ||
8813 | if (con->request->attrs == attr2) | |
8814 | { | |
8815 | con->request->attrs = attr2->next; | |
8816 | prev2 = NULL; | |
8817 | } | |
8818 | else | |
8819 | { | |
fa73b229 | 8820 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) |
ef416fc2 | 8821 | if (prev2->next == attr2) |
8822 | { | |
8823 | prev2->next = attr2->next; | |
8824 | break; | |
8825 | } | |
8826 | } | |
8827 | ||
8828 | if (con->request->last == attr2) | |
8829 | con->request->last = prev2; | |
8830 | ||
a2326b5b | 8831 | ippDeleteAttribute(NULL, attr2); |
ef416fc2 | 8832 | } |
8833 | ||
8834 | /* | |
8835 | * Add new option by copying it... | |
8836 | */ | |
8837 | ||
a2326b5b | 8838 | ippCopyAttribute(con->request, attr, 0); |
ef416fc2 | 8839 | } |
8840 | ||
8841 | /* | |
8842 | * Then free the attribute list and option array... | |
8843 | */ | |
8844 | ||
8845 | ippDelete(ticket); | |
8846 | cupsFreeOptions(num_options, options); | |
8847 | } | |
8848 | ||
8849 | ||
8850 | /* | |
8851 | * 'reject_jobs()' - Reject print jobs to a printer. | |
8852 | */ | |
8853 | ||
8854 | static void | |
8855 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
8856 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
8857 | { | |
8858 | http_status_t status; /* Policy status */ | |
bc44d920 | 8859 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 8860 | cupsd_printer_t *printer; /* Printer data */ |
8861 | ipp_attribute_t *attr; /* printer-state-message text */ | |
8862 | ||
8863 | ||
8864 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
996acce8 | 8865 | con->number, uri->values[0].string.text); |
ef416fc2 | 8866 | |
8867 | /* | |
8868 | * Is the destination valid? | |
8869 | */ | |
8870 | ||
f7deaa1a | 8871 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 8872 | { |
8873 | /* | |
8874 | * Bad URI... | |
8875 | */ | |
8876 | ||
8877 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8878 | _("The printer or class does not exist.")); |
ef416fc2 | 8879 | return; |
8880 | } | |
8881 | ||
8882 | /* | |
8883 | * Check policy... | |
8884 | */ | |
8885 | ||
8886 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8887 | { | |
f899b121 | 8888 | send_http_error(con, status, printer); |
ef416fc2 | 8889 | return; |
8890 | } | |
8891 | ||
8892 | /* | |
8893 | * Reject jobs sent to the printer... | |
8894 | */ | |
8895 | ||
8896 | printer->accepting = 0; | |
8897 | ||
8898 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8899 | IPP_TAG_TEXT)) == NULL) | |
5a9febac MS |
8900 | strlcpy(printer->state_message, "Rejecting Jobs", |
8901 | sizeof(printer->state_message)); | |
ef416fc2 | 8902 | else |
8903 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8904 | sizeof(printer->state_message)); | |
8905 | ||
f8b3a85b MS |
8906 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, |
8907 | "No longer accepting jobs."); | |
8908 | ||
ef416fc2 | 8909 | if (dtype & CUPS_PRINTER_CLASS) |
8910 | { | |
3dfe78b3 | 8911 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
ef416fc2 | 8912 | |
8913 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8914 | printer->name, get_username(con)); |
ef416fc2 | 8915 | } |
8916 | else | |
8917 | { | |
3dfe78b3 | 8918 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 8919 | |
8920 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8921 | printer->name, get_username(con)); |
ef416fc2 | 8922 | } |
8923 | ||
8924 | /* | |
8925 | * Everything was ok, so return OK status... | |
8926 | */ | |
8927 | ||
8928 | con->response->request.status.status_code = IPP_OK; | |
8929 | } | |
8930 | ||
8931 | ||
61cf44e2 MS |
8932 | /* |
8933 | * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class. | |
8934 | */ | |
8935 | ||
8936 | static void | |
8937 | release_held_new_jobs( | |
8938 | cupsd_client_t *con, /* I - Connection */ | |
8939 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8940 | { | |
8941 | http_status_t status; /* Policy status */ | |
8942 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
8943 | cupsd_printer_t *printer; /* Printer data */ | |
8944 | ||
8945 | ||
8946 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con, | |
996acce8 | 8947 | con->number, uri->values[0].string.text); |
61cf44e2 MS |
8948 | |
8949 | /* | |
8950 | * Is the destination valid? | |
8951 | */ | |
8952 | ||
8953 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
8954 | { | |
8955 | /* | |
8956 | * Bad URI... | |
8957 | */ | |
8958 | ||
8959 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 8960 | _("The printer or class does not exist.")); |
61cf44e2 MS |
8961 | return; |
8962 | } | |
8963 | ||
8964 | /* | |
8965 | * Check policy... | |
8966 | */ | |
8967 | ||
8968 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8969 | { | |
8970 | send_http_error(con, status, printer); | |
8971 | return; | |
8972 | } | |
8973 | ||
8974 | /* | |
8975 | * Hold pending/new jobs sent to the printer... | |
8976 | */ | |
8977 | ||
8978 | printer->holding_new_jobs = 0; | |
8979 | ||
8980 | cupsdSetPrinterReasons(printer, "-hold-new-jobs"); | |
61cf44e2 MS |
8981 | |
8982 | if (dtype & CUPS_PRINTER_CLASS) | |
8983 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8984 | "Class \"%s\" now printing pending/new jobs (\"%s\").", | |
8985 | printer->name, get_username(con)); | |
8986 | else | |
8987 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8988 | "Printer \"%s\" now printing pending/new jobs (\"%s\").", | |
8989 | printer->name, get_username(con)); | |
8990 | ||
e0f489cd MS |
8991 | cupsdCheckJobs(); |
8992 | ||
61cf44e2 MS |
8993 | /* |
8994 | * Everything was ok, so return OK status... | |
8995 | */ | |
8996 | ||
8997 | con->response->request.status.status_code = IPP_OK; | |
8998 | } | |
8999 | ||
9000 | ||
ef416fc2 | 9001 | /* |
9002 | * 'release_job()' - Release a held print job. | |
9003 | */ | |
9004 | ||
9005 | static void | |
9006 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
9007 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
9008 | { | |
9009 | ipp_attribute_t *attr; /* Current attribute */ | |
9010 | int jobid; /* Job ID */ | |
61cf44e2 | 9011 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
ef416fc2 | 9012 | username[HTTP_MAX_URI], /* Username portion of URI */ |
9013 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
9014 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9015 | int port; /* Port portion of URI */ | |
9016 | cupsd_job_t *job; /* Job information */ | |
9017 | ||
9018 | ||
9019 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
996acce8 | 9020 | con->number, uri->values[0].string.text); |
ef416fc2 | 9021 | |
9022 | /* | |
9023 | * See if we have a job URI or a printer URI... | |
9024 | */ | |
9025 | ||
9026 | if (!strcmp(uri->name, "printer-uri")) | |
9027 | { | |
9028 | /* | |
9029 | * Got a printer URI; see if we also have a job-id attribute... | |
9030 | */ | |
9031 | ||
fa73b229 | 9032 | if ((attr = ippFindAttribute(con->request, "job-id", |
9033 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9034 | { |
9035 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 9036 | _("Got a printer-uri attribute but no job-id.")); |
ef416fc2 | 9037 | return; |
9038 | } | |
9039 | ||
9040 | jobid = attr->values[0].integer; | |
9041 | } | |
9042 | else | |
9043 | { | |
9044 | /* | |
9045 | * Got a job URI; parse it to get the job ID... | |
9046 | */ | |
9047 | ||
61cf44e2 MS |
9048 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
9049 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 9050 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 9051 | |
9052 | if (strncmp(resource, "/jobs/", 6)) | |
9053 | { | |
9054 | /* | |
9055 | * Not a valid URI! | |
9056 | */ | |
9057 | ||
84315f46 | 9058 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
ef416fc2 | 9059 | uri->values[0].string.text); |
9060 | return; | |
9061 | } | |
9062 | ||
9063 | jobid = atoi(resource + 6); | |
9064 | } | |
9065 | ||
9066 | /* | |
9067 | * See if the job exists... | |
9068 | */ | |
9069 | ||
9070 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9071 | { | |
9072 | /* | |
9073 | * Nope - return a "not found" error... | |
9074 | */ | |
9075 | ||
84315f46 | 9076 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
ef416fc2 | 9077 | return; |
9078 | } | |
9079 | ||
9080 | /* | |
9081 | * See if job is "held"... | |
9082 | */ | |
9083 | ||
bd7854cb | 9084 | if (job->state_value != IPP_JOB_HELD) |
ef416fc2 | 9085 | { |
9086 | /* | |
9087 | * Nope - return a "not possible" error... | |
9088 | */ | |
9089 | ||
84315f46 | 9090 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid); |
ef416fc2 | 9091 | return; |
9092 | } | |
9093 | ||
9094 | /* | |
9095 | * See if the job is owned by the requesting user... | |
9096 | */ | |
9097 | ||
9098 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9099 | { | |
b0f6947b MS |
9100 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
9101 | cupsdFindDest(job->dest)); | |
ef416fc2 | 9102 | return; |
9103 | } | |
9104 | ||
9105 | /* | |
9106 | * Reset the job-hold-until value to "no-hold"... | |
9107 | */ | |
9108 | ||
fa73b229 | 9109 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9110 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9111 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9112 | ||
fa73b229 | 9113 | if (attr) |
ef416fc2 | 9114 | { |
5e6c3df7 MS |
9115 | ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD); |
9116 | ippSetString(job->attrs, &attr, 0, "no-hold"); | |
d09495fa | 9117 | |
01ce6322 | 9118 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 9119 | "Job job-hold-until value changed by user."); |
3e7fe0ca | 9120 | ippSetString(job->attrs, &job->reasons, 0, "none"); |
ef416fc2 | 9121 | } |
9122 | ||
9123 | /* | |
9124 | * Release the job and return... | |
9125 | */ | |
9126 | ||
9127 | cupsdReleaseJob(job); | |
9128 | ||
01ce6322 | 9129 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 9130 | "Job released by user."); |
9131 | ||
75bd9771 | 9132 | cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username); |
ef416fc2 | 9133 | |
9134 | con->response->request.status.status_code = IPP_OK; | |
005dd1eb MS |
9135 | |
9136 | cupsdCheckJobs(); | |
ef416fc2 | 9137 | } |
9138 | ||
9139 | ||
9140 | /* | |
9141 | * 'renew_subscription()' - Renew an existing subscription... | |
9142 | */ | |
9143 | ||
9144 | static void | |
9145 | renew_subscription( | |
9146 | cupsd_client_t *con, /* I - Client connection */ | |
9147 | int sub_id) /* I - Subscription ID */ | |
9148 | { | |
bd7854cb | 9149 | http_status_t status; /* Policy status */ |
9150 | cupsd_subscription_t *sub; /* Subscription */ | |
9151 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
9152 | ||
9153 | ||
9154 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
9155 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
996acce8 | 9156 | con, con->number, sub_id); |
bd7854cb | 9157 | |
9158 | /* | |
9159 | * Is the subscription ID valid? | |
9160 | */ | |
9161 | ||
9162 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
9163 | { | |
9164 | /* | |
9165 | * Bad subscription ID... | |
9166 | */ | |
9167 | ||
84315f46 MS |
9168 | send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."), |
9169 | sub_id); | |
bd7854cb | 9170 | return; |
9171 | } | |
9172 | ||
9173 | if (sub->job) | |
9174 | { | |
9175 | /* | |
9176 | * Job subscriptions cannot be renewed... | |
9177 | */ | |
9178 | ||
9179 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 9180 | _("Job subscriptions cannot be renewed.")); |
bd7854cb | 9181 | return; |
9182 | } | |
9183 | ||
9184 | /* | |
9185 | * Check policy... | |
9186 | */ | |
9187 | ||
9188 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
9189 | DefaultPolicyPtr, | |
9190 | con, sub->owner)) != HTTP_OK) | |
9191 | { | |
f899b121 | 9192 | send_http_error(con, status, sub->dest); |
bd7854cb | 9193 | return; |
9194 | } | |
9195 | ||
9196 | /* | |
9197 | * Renew the subscription... | |
9198 | */ | |
9199 | ||
9200 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
9201 | IPP_TAG_INTEGER); | |
9202 | ||
9203 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
9204 | ||
9205 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
9206 | { | |
9207 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9208 | "renew_subscription: Limiting notify-lease-duration to " | |
9209 | "%d seconds.", | |
9210 | MaxLeaseDuration); | |
9211 | sub->lease = MaxLeaseDuration; | |
9212 | } | |
9213 | ||
9214 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
9215 | ||
3dfe78b3 | 9216 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
bd7854cb | 9217 | |
9218 | con->response->request.status.status_code = IPP_OK; | |
b94498cf | 9219 | |
9220 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
9221 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 9222 | } |
9223 | ||
9224 | ||
9225 | /* | |
9226 | * 'restart_job()' - Restart an old print job. | |
9227 | */ | |
9228 | ||
9229 | static void | |
9230 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
9231 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
9232 | { | |
9233 | ipp_attribute_t *attr; /* Current attribute */ | |
9234 | int jobid; /* Job ID */ | |
238c3832 | 9235 | cupsd_job_t *job; /* Job information */ |
61cf44e2 | 9236 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
ef416fc2 | 9237 | username[HTTP_MAX_URI], /* Username portion of URI */ |
9238 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
9239 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9240 | int port; /* Port portion of URI */ | |
ef416fc2 | 9241 | |
9242 | ||
9243 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
996acce8 | 9244 | con->number, uri->values[0].string.text); |
ef416fc2 | 9245 | |
9246 | /* | |
9247 | * See if we have a job URI or a printer URI... | |
9248 | */ | |
9249 | ||
9250 | if (!strcmp(uri->name, "printer-uri")) | |
9251 | { | |
9252 | /* | |
9253 | * Got a printer URI; see if we also have a job-id attribute... | |
9254 | */ | |
9255 | ||
fa73b229 | 9256 | if ((attr = ippFindAttribute(con->request, "job-id", |
9257 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9258 | { |
9259 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 9260 | _("Got a printer-uri attribute but no job-id.")); |
ef416fc2 | 9261 | return; |
9262 | } | |
9263 | ||
9264 | jobid = attr->values[0].integer; | |
9265 | } | |
9266 | else | |
9267 | { | |
9268 | /* | |
9269 | * Got a job URI; parse it to get the job ID... | |
9270 | */ | |
9271 | ||
61cf44e2 MS |
9272 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
9273 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 9274 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 9275 | |
fa73b229 | 9276 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 9277 | { |
9278 | /* | |
9279 | * Not a valid URI! | |
9280 | */ | |
9281 | ||
84315f46 | 9282 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
ef416fc2 | 9283 | uri->values[0].string.text); |
9284 | return; | |
9285 | } | |
9286 | ||
9287 | jobid = atoi(resource + 6); | |
9288 | } | |
9289 | ||
9290 | /* | |
9291 | * See if the job exists... | |
9292 | */ | |
9293 | ||
9294 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9295 | { | |
9296 | /* | |
9297 | * Nope - return a "not found" error... | |
9298 | */ | |
9299 | ||
84315f46 | 9300 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
ef416fc2 | 9301 | return; |
9302 | } | |
9303 | ||
9304 | /* | |
9305 | * See if job is in any of the "completed" states... | |
9306 | */ | |
9307 | ||
bd7854cb | 9308 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 9309 | { |
9310 | /* | |
9311 | * Nope - return a "not possible" error... | |
9312 | */ | |
9313 | ||
84315f46 | 9314 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."), |
ef416fc2 | 9315 | jobid); |
9316 | return; | |
9317 | } | |
9318 | ||
9319 | /* | |
9320 | * See if we have retained the job files... | |
9321 | */ | |
9322 | ||
bd7854cb | 9323 | cupsdLoadJob(job); |
9324 | ||
07725fee | 9325 | if (!job->attrs || job->num_files == 0) |
ef416fc2 | 9326 | { |
9327 | /* | |
9328 | * Nope - return a "not possible" error... | |
9329 | */ | |
9330 | ||
9331 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 9332 | _("Job #%d cannot be restarted - no files."), jobid); |
ef416fc2 | 9333 | return; |
9334 | } | |
9335 | ||
9336 | /* | |
9337 | * See if the job is owned by the requesting user... | |
9338 | */ | |
9339 | ||
9340 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9341 | { | |
b0f6947b MS |
9342 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
9343 | cupsdFindDest(job->dest)); | |
ef416fc2 | 9344 | return; |
9345 | } | |
9346 | ||
9347 | /* | |
238c3832 | 9348 | * See if the job-hold-until attribute is specified... |
ef416fc2 | 9349 | */ |
9350 | ||
238c3832 MS |
9351 | if ((attr = ippFindAttribute(con->request, "job-hold-until", |
9352 | IPP_TAG_KEYWORD)) == NULL) | |
9353 | attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
9354 | ||
9355 | if (attr && strcmp(attr->values[0].string.text, "no-hold")) | |
9356 | { | |
9357 | /* | |
9358 | * Return the job to a held state... | |
9359 | */ | |
9360 | ||
9361 | cupsdLogJob(job, CUPSD_LOG_DEBUG, | |
9362 | "Restarted by \"%s\" with job-hold-until=%s.", | |
9363 | username, attr->values[0].string.text); | |
9364 | cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0); | |
9365 | ||
9366 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE, | |
9367 | NULL, job, "Job restarted by user with job-hold-until=%s", | |
9368 | attr->values[0].string.text); | |
9369 | } | |
9370 | else | |
9371 | { | |
9372 | /* | |
9373 | * Restart the job... | |
9374 | */ | |
9375 | ||
9376 | cupsdRestartJob(job); | |
f11a948a | 9377 | cupsdCheckJobs(); |
238c3832 | 9378 | } |
ef416fc2 | 9379 | |
75bd9771 | 9380 | cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username); |
ef416fc2 | 9381 | |
9382 | con->response->request.status.status_code = IPP_OK; | |
9383 | } | |
9384 | ||
9385 | ||
9386 | /* | |
9387 | * 'save_auth_info()' - Save authentication information for a job. | |
9388 | */ | |
9389 | ||
9390 | static void | |
f7deaa1a | 9391 | save_auth_info( |
9392 | cupsd_client_t *con, /* I - Client connection */ | |
9393 | cupsd_job_t *job, /* I - Job */ | |
9394 | ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */ | |
ef416fc2 | 9395 | { |
09a101d6 | 9396 | int i; /* Looping var */ |
9397 | char filename[1024]; /* Job authentication filename */ | |
9398 | cups_file_t *fp; /* Job authentication file */ | |
85dda01c | 9399 | char line[65536]; /* Line for file */ |
09a101d6 | 9400 | cupsd_printer_t *dest; /* Destination printer/class */ |
ef416fc2 | 9401 | |
9402 | ||
9403 | /* | |
9404 | * This function saves the in-memory authentication information for | |
9405 | * a job so that it can be used to authenticate with a remote host. | |
9406 | * The information is stored in a file that is readable only by the | |
f7deaa1a | 9407 | * root user. The fields are Base-64 encoded, each on a separate line, |
9408 | * followed by random number (up to 1024) of newlines to limit the | |
9409 | * amount of information that is exposed. | |
ef416fc2 | 9410 | * |
9411 | * Because of the potential for exposing of authentication information, | |
9412 | * this functionality is only enabled when running cupsd as root. | |
9413 | * | |
9414 | * This caching only works for the Basic and BasicDigest authentication | |
9415 | * types. Digest authentication cannot be cached this way, and in | |
9416 | * the future Kerberos authentication may make all of this obsolete. | |
9417 | * | |
9418 | * Authentication information is saved whenever an authenticated | |
9419 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
9420 | * performed. | |
9421 | * | |
9422 | * This information is deleted after a job is completed or canceled, | |
9423 | * so reprints may require subsequent re-authentication. | |
9424 | */ | |
9425 | ||
9426 | if (RunUser) | |
9427 | return; | |
9428 | ||
09a101d6 | 9429 | if ((dest = cupsdFindDest(job->dest)) == NULL) |
9430 | return; | |
9431 | ||
ef416fc2 | 9432 | /* |
9433 | * Create the authentication file and change permissions... | |
9434 | */ | |
9435 | ||
9436 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
9437 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
9438 | { | |
9439 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9440 | "Unable to save authentication info to \"%s\" - %s", | |
9441 | filename, strerror(errno)); | |
9442 | return; | |
9443 | } | |
9444 | ||
9445 | fchown(cupsFileNumber(fp), 0, 0); | |
9446 | fchmod(cupsFileNumber(fp), 0400); | |
9447 | ||
94436c5a | 9448 | cupsFilePuts(fp, "CUPSD-AUTH-V3\n"); |
dcb445bc | 9449 | |
88f9aafc MS |
9450 | for (i = 0; |
9451 | i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0])); | |
9452 | i ++) | |
9453 | cupsdClearString(job->auth_env + i); | |
9454 | ||
09a101d6 | 9455 | if (auth_info && auth_info->num_values == dest->num_auth_info_required) |
f7deaa1a | 9456 | { |
9457 | /* | |
09a101d6 | 9458 | * Write 1 to 3 auth values... |
f7deaa1a | 9459 | */ |
ef416fc2 | 9460 | |
88f9aafc MS |
9461 | for (i = 0; |
9462 | i < auth_info->num_values && | |
9463 | i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0])); | |
9464 | i ++) | |
f7deaa1a | 9465 | { |
94436c5a MS |
9466 | if (strcmp(dest->auth_info_required[i], "negotiate")) |
9467 | { | |
7e86f2f6 | 9468 | httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, (int)strlen(auth_info->values[i].string.text)); |
94436c5a MS |
9469 | cupsFilePutConf(fp, dest->auth_info_required[i], line); |
9470 | } | |
9471 | else | |
9472 | cupsFilePutConf(fp, dest->auth_info_required[i], | |
9473 | auth_info->values[i].string.text); | |
09a101d6 | 9474 | |
9475 | if (!strcmp(dest->auth_info_required[i], "username")) | |
88f9aafc | 9476 | cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s", |
09a101d6 | 9477 | auth_info->values[i].string.text); |
9478 | else if (!strcmp(dest->auth_info_required[i], "domain")) | |
88f9aafc | 9479 | cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s", |
09a101d6 | 9480 | auth_info->values[i].string.text); |
9481 | else if (!strcmp(dest->auth_info_required[i], "password")) | |
88f9aafc MS |
9482 | cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s", |
9483 | auth_info->values[i].string.text); | |
9484 | else if (!strcmp(dest->auth_info_required[i], "negotiate")) | |
9485 | cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s", | |
9486 | auth_info->values[i].string.text); | |
9487 | else | |
dcb445bc | 9488 | i --; |
f7deaa1a | 9489 | } |
9490 | } | |
dcb445bc MS |
9491 | else if (auth_info && auth_info->num_values == 2 && |
9492 | dest->num_auth_info_required == 1 && | |
9493 | !strcmp(dest->auth_info_required[0], "negotiate")) | |
9494 | { | |
9495 | /* | |
9496 | * Allow fallback to username+password for Kerberized queues... | |
9497 | */ | |
9498 | ||
7e86f2f6 | 9499 | httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text, (int)strlen(auth_info->values[0].string.text)); |
dcb445bc MS |
9500 | cupsFilePutConf(fp, "username", line); |
9501 | ||
9502 | cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", | |
9503 | auth_info->values[0].string.text); | |
9504 | ||
7e86f2f6 | 9505 | httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text, (int)strlen(auth_info->values[1].string.text)); |
dcb445bc MS |
9506 | cupsFilePutConf(fp, "password", line); |
9507 | ||
9508 | cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", | |
9509 | auth_info->values[1].string.text); | |
9510 | } | |
09a101d6 | 9511 | else if (con->username[0]) |
f7deaa1a | 9512 | { |
9513 | /* | |
9514 | * Write the authenticated username... | |
9515 | */ | |
ef416fc2 | 9516 | |
7e86f2f6 | 9517 | httpEncode64_2(line, sizeof(line), con->username, (int)strlen(con->username)); |
dcb445bc | 9518 | cupsFilePutConf(fp, "username", line); |
ef416fc2 | 9519 | |
88f9aafc | 9520 | cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username); |
09a101d6 | 9521 | |
f7deaa1a | 9522 | /* |
9523 | * Write the authenticated password... | |
9524 | */ | |
9525 | ||
7e86f2f6 | 9526 | httpEncode64_2(line, sizeof(line), con->password, (int)strlen(con->password)); |
dcb445bc | 9527 | cupsFilePutConf(fp, "password", line); |
09a101d6 | 9528 | |
88f9aafc | 9529 | cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password); |
f7deaa1a | 9530 | } |
ef416fc2 | 9531 | |
07ed0e9a | 9532 | #ifdef HAVE_GSSAPI |
eac3a0a0 | 9533 | if (con->gss_uid > 0) |
07ed0e9a | 9534 | { |
dcb445bc | 9535 | cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid); |
07ed0e9a MS |
9536 | cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid); |
9537 | } | |
9538 | #endif /* HAVE_GSSAPI */ | |
9539 | ||
ef416fc2 | 9540 | /* |
9541 | * Write a random number of newlines to the end of the file... | |
9542 | */ | |
9543 | ||
41681883 | 9544 | for (i = (CUPS_RAND() % 1024); i >= 0; i --) |
ef416fc2 | 9545 | cupsFilePutChar(fp, '\n'); |
9546 | ||
9547 | /* | |
9548 | * Close the file and return... | |
9549 | */ | |
9550 | ||
9551 | cupsFileClose(fp); | |
9552 | } | |
9553 | ||
9554 | ||
9555 | /* | |
9556 | * 'send_document()' - Send a file to a printer or class. | |
9557 | */ | |
9558 | ||
9559 | static void | |
9560 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
9561 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9562 | { | |
9563 | ipp_attribute_t *attr; /* Current attribute */ | |
a0f6818e MS |
9564 | ipp_attribute_t *format; /* Request's document-format attribute */ |
9565 | ipp_attribute_t *jformat; /* Job's document-format attribute */ | |
f7deaa1a | 9566 | const char *default_format;/* document-format-default value */ |
ef416fc2 | 9567 | int jobid; /* Job ID number */ |
9568 | cupsd_job_t *job; /* Current job */ | |
9569 | char job_uri[HTTP_MAX_URI], | |
9570 | /* Job URI */ | |
61cf44e2 | 9571 | scheme[HTTP_MAX_URI], |
ef416fc2 | 9572 | /* Method portion of URI */ |
9573 | username[HTTP_MAX_URI], | |
9574 | /* Username portion of URI */ | |
9575 | host[HTTP_MAX_URI], | |
9576 | /* Host portion of URI */ | |
9577 | resource[HTTP_MAX_URI]; | |
9578 | /* Resource portion of URI */ | |
9579 | int port; /* Port portion of URI */ | |
9580 | mime_type_t *filetype; /* Type of file */ | |
9581 | char super[MIME_MAX_SUPER], | |
9582 | /* Supertype of file */ | |
9583 | type[MIME_MAX_TYPE], | |
9584 | /* Subtype of file */ | |
9585 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
9586 | /* Textual name of mime type */ | |
9587 | char filename[1024]; /* Job filename */ | |
9588 | cupsd_printer_t *printer; /* Current printer */ | |
9589 | struct stat fileinfo; /* File information */ | |
9590 | int kbytes; /* Size of file */ | |
9591 | int compression; /* Type of compression */ | |
75bd9771 | 9592 | int start_job; /* Start the job? */ |
ef416fc2 | 9593 | |
9594 | ||
9595 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
996acce8 | 9596 | con->number, uri->values[0].string.text); |
ef416fc2 | 9597 | |
9598 | /* | |
9599 | * See if we have a job URI or a printer URI... | |
9600 | */ | |
9601 | ||
9602 | if (!strcmp(uri->name, "printer-uri")) | |
9603 | { | |
9604 | /* | |
9605 | * Got a printer URI; see if we also have a job-id attribute... | |
9606 | */ | |
9607 | ||
fa73b229 | 9608 | if ((attr = ippFindAttribute(con->request, "job-id", |
9609 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9610 | { |
9611 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 9612 | _("Got a printer-uri attribute but no job-id.")); |
ef416fc2 | 9613 | return; |
9614 | } | |
9615 | ||
9616 | jobid = attr->values[0].integer; | |
9617 | } | |
9618 | else | |
9619 | { | |
9620 | /* | |
9621 | * Got a job URI; parse it to get the job ID... | |
9622 | */ | |
9623 | ||
61cf44e2 MS |
9624 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
9625 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 9626 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 9627 | |
9628 | if (strncmp(resource, "/jobs/", 6)) | |
9629 | { | |
9630 | /* | |
9631 | * Not a valid URI! | |
9632 | */ | |
9633 | ||
84315f46 | 9634 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
ef416fc2 | 9635 | uri->values[0].string.text); |
9636 | return; | |
9637 | } | |
9638 | ||
9639 | jobid = atoi(resource + 6); | |
9640 | } | |
9641 | ||
9642 | /* | |
9643 | * See if the job exists... | |
9644 | */ | |
9645 | ||
9646 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9647 | { | |
9648 | /* | |
9649 | * Nope - return a "not found" error... | |
9650 | */ | |
9651 | ||
84315f46 | 9652 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
ef416fc2 | 9653 | return; |
9654 | } | |
9655 | ||
f301802f | 9656 | printer = cupsdFindDest(job->dest); |
9657 | ||
ef416fc2 | 9658 | /* |
9659 | * See if the job is owned by the requesting user... | |
9660 | */ | |
9661 | ||
9662 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9663 | { | |
b0f6947b MS |
9664 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
9665 | cupsdFindDest(job->dest)); | |
ef416fc2 | 9666 | return; |
9667 | } | |
9668 | ||
9669 | /* | |
9670 | * OK, see if the client is sending the document compressed - CUPS | |
9671 | * only supports "none" and "gzip". | |
9672 | */ | |
9673 | ||
9674 | compression = CUPS_FILE_NONE; | |
9675 | ||
fa73b229 | 9676 | if ((attr = ippFindAttribute(con->request, "compression", |
9677 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 9678 | { |
9679 | if (strcmp(attr->values[0].string.text, "none") | |
9680 | #ifdef HAVE_LIBZ | |
9681 | && strcmp(attr->values[0].string.text, "gzip") | |
9682 | #endif /* HAVE_LIBZ */ | |
9683 | ) | |
9684 | { | |
84315f46 | 9685 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."), |
ef416fc2 | 9686 | attr->values[0].string.text); |
9687 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9688 | "compression", NULL, attr->values[0].string.text); | |
9689 | return; | |
9690 | } | |
9691 | ||
9692 | #ifdef HAVE_LIBZ | |
9693 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
9694 | compression = CUPS_FILE_GZIP; | |
9695 | #endif /* HAVE_LIBZ */ | |
9696 | } | |
9697 | ||
9698 | /* | |
9699 | * Do we have a file to print? | |
9700 | */ | |
9701 | ||
83e08001 MS |
9702 | if ((attr = ippFindAttribute(con->request, "last-document", |
9703 | IPP_TAG_BOOLEAN)) == NULL) | |
9704 | { | |
9705 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9706 | _("Missing last-document attribute in request.")); | |
9707 | return; | |
9708 | } | |
9709 | ||
ef416fc2 | 9710 | if (!con->filename) |
9711 | { | |
6d2f911b MS |
9712 | /* |
9713 | * Check for an empty request with "last-document" set to true, which is | |
9714 | * used to close an "open" job by RFC 2911, section 3.3.2. | |
9715 | */ | |
9716 | ||
83e08001 | 9717 | if (job->num_files > 0 && attr->values[0].boolean) |
6d2f911b MS |
9718 | goto last_document; |
9719 | ||
84315f46 | 9720 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request.")); |
ef416fc2 | 9721 | return; |
9722 | } | |
9723 | ||
9724 | /* | |
9725 | * Is it a format we support? | |
9726 | */ | |
9727 | ||
9514a192 MS |
9728 | cupsdLoadJob(job); |
9729 | ||
ef416fc2 | 9730 | if ((format = ippFindAttribute(con->request, "document-format", |
9731 | IPP_TAG_MIMETYPE)) != NULL) | |
9732 | { | |
9733 | /* | |
9734 | * Grab format from client... | |
9735 | */ | |
9736 | ||
c5b24bfa | 9737 | if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", |
bc44d920 | 9738 | super, type) != 2) |
ef416fc2 | 9739 | { |
84315f46 | 9740 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."), |
ef416fc2 | 9741 | format->values[0].string.text); |
9742 | return; | |
9743 | } | |
9514a192 MS |
9744 | |
9745 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL)); | |
ef416fc2 | 9746 | } |
f7deaa1a | 9747 | else if ((default_format = cupsGetOption("document-format", |
9748 | printer->num_options, | |
9749 | printer->options)) != NULL) | |
9750 | { | |
9751 | /* | |
9752 | * Use default document format... | |
9753 | */ | |
9754 | ||
c5b24bfa | 9755 | if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2) |
f7deaa1a | 9756 | { |
9757 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 9758 | _("Bad document-format-default \"%s\"."), default_format); |
f7deaa1a | 9759 | return; |
9760 | } | |
9761 | } | |
ef416fc2 | 9762 | else |
9763 | { | |
9764 | /* | |
9765 | * No document format attribute? Auto-type it! | |
9766 | */ | |
9767 | ||
5a9febac MS |
9768 | strlcpy(super, "application", sizeof(super)); |
9769 | strlcpy(type, "octet-stream", sizeof(type)); | |
ef416fc2 | 9770 | } |
9771 | ||
fa73b229 | 9772 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
ef416fc2 | 9773 | { |
9774 | /* | |
9775 | * Auto-type the file... | |
9776 | */ | |
9777 | ||
bd7854cb | 9778 | ipp_attribute_t *doc_name; /* document-name attribute */ |
9779 | ||
9780 | ||
75bd9771 | 9781 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file..."); |
ef416fc2 | 9782 | |
bd7854cb | 9783 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
9784 | filetype = mimeFileType(MimeDatabase, con->filename, | |
9785 | doc_name ? doc_name->values[0].string.text : NULL, | |
9786 | &compression); | |
ef416fc2 | 9787 | |
f7deaa1a | 9788 | if (!filetype) |
f7faf1f5 | 9789 | filetype = mimeType(MimeDatabase, super, type); |
a0f6818e | 9790 | |
0268488e MS |
9791 | if (filetype) |
9792 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.", | |
9793 | filetype->super, filetype->type); | |
9514a192 MS |
9794 | |
9795 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type); | |
9796 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype); | |
ed486911 | 9797 | } |
f7faf1f5 | 9798 | else |
9799 | filetype = mimeType(MimeDatabase, super, type); | |
9800 | ||
49d87452 | 9801 | if (filetype) |
f7deaa1a | 9802 | { |
9803 | /* | |
9804 | * Replace the document-format attribute value with the auto-typed or | |
9805 | * default one. | |
9806 | */ | |
9807 | ||
9808 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9809 | filetype->type); | |
9810 | ||
49d87452 MS |
9811 | if ((jformat = ippFindAttribute(job->attrs, "document-format", |
9812 | IPP_TAG_MIMETYPE)) != NULL) | |
5e6c3df7 | 9813 | ippSetString(job->attrs, &jformat, 0, mimetype); |
f7deaa1a | 9814 | else |
a0f6818e | 9815 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
f7deaa1a | 9816 | "document-format", NULL, mimetype); |
9817 | } | |
9818 | else if (!filetype) | |
ef416fc2 | 9819 | { |
9820 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
84315f46 | 9821 | _("Unsupported document-format \"%s/%s\"."), super, type); |
ef416fc2 | 9822 | cupsdLogMessage(CUPSD_LOG_INFO, |
9823 | "Hint: Do you have the raw file printing rules enabled?"); | |
9824 | ||
9825 | if (format) | |
9826 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9827 | "document-format", NULL, format->values[0].string.text); | |
9828 | ||
9829 | return; | |
9830 | } | |
9831 | ||
80ca4592 | 9832 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) |
9833 | { | |
9834 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9835 | filetype->type); | |
9836 | ||
9837 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
84315f46 | 9838 | _("Unsupported document-format \"%s\"."), mimetype); |
80ca4592 | 9839 | |
9840 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9841 | "document-format", NULL, mimetype); | |
9842 | ||
9843 | return; | |
9844 | } | |
9845 | ||
ef416fc2 | 9846 | /* |
9847 | * Add the file to the job... | |
9848 | */ | |
9849 | ||
9850 | if (add_file(con, job, filetype, compression)) | |
9851 | return; | |
9852 | ||
9514a192 MS |
9853 | if ((attr = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME)) != NULL) |
9854 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "document-name-supplied", NULL, ippGetString(attr, 0, NULL)); | |
9855 | ||
ef416fc2 | 9856 | if (stat(con->filename, &fileinfo)) |
9857 | kbytes = 0; | |
9858 | else | |
9859 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
9860 | ||
9861 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
9862 | ||
f16ea703 MS |
9863 | job->koctets += kbytes; |
9864 | ||
9865 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 9866 | attr->values[0].integer += kbytes; |
9867 | ||
6d8021f4 MS |
9868 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files); |
9869 | if (rename(con->filename, filename)) | |
9870 | { | |
9871 | cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno)); | |
9872 | ||
9873 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file.")); | |
9874 | return; | |
9875 | } | |
ef416fc2 | 9876 | |
9877 | cupsdClearString(&con->filename); | |
9878 | ||
75bd9771 MS |
9879 | cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".", |
9880 | filetype->super, filetype->type, job->username); | |
ef416fc2 | 9881 | |
9882 | /* | |
9883 | * Start the job if this is the last document... | |
9884 | */ | |
9885 | ||
6d2f911b MS |
9886 | last_document: |
9887 | ||
fa73b229 | 9888 | if ((attr = ippFindAttribute(con->request, "last-document", |
9889 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 9890 | attr->values[0].boolean) |
9891 | { | |
9892 | /* | |
9893 | * See if we need to add the ending sheet... | |
9894 | */ | |
9895 | ||
91c84a35 MS |
9896 | if (cupsdTimeoutJob(job)) |
9897 | return; | |
ef416fc2 | 9898 | |
bd7854cb | 9899 | if (job->state_value == IPP_JOB_STOPPED) |
9900 | { | |
ef416fc2 | 9901 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9902 | job->state_value = IPP_JOB_PENDING; |
12f89d24 MS |
9903 | |
9904 | ippSetString(job->attrs, &job->reasons, 0, "none"); | |
bd7854cb | 9905 | } |
9906 | else if (job->state_value == IPP_JOB_HELD) | |
ef416fc2 | 9907 | { |
fa73b229 | 9908 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9909 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9910 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9911 | ||
fa73b229 | 9912 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
bd7854cb | 9913 | { |
ef416fc2 | 9914 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9915 | job->state_value = IPP_JOB_PENDING; |
12f89d24 MS |
9916 | |
9917 | ippSetString(job->attrs, &job->reasons, 0, "none"); | |
bd7854cb | 9918 | } |
12f89d24 MS |
9919 | else |
9920 | ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified"); | |
ef416fc2 | 9921 | } |
9922 | ||
3dfe78b3 MS |
9923 | job->dirty = 1; |
9924 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 9925 | |
75bd9771 | 9926 | start_job = 1; |
ef416fc2 | 9927 | } |
9928 | else | |
9929 | { | |
fa73b229 | 9930 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9931 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9932 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9933 | ||
fa73b229 | 9934 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
ef416fc2 | 9935 | { |
9936 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 9937 | job->state_value = IPP_JOB_HELD; |
dfd5680b | 9938 | job->hold_until = time(NULL) + MultipleOperationTimeout; |
3dfe78b3 | 9939 | |
12f89d24 MS |
9940 | ippSetString(job->attrs, &job->reasons, 0, "job-incoming"); |
9941 | ||
9942 | job->dirty = 1; | |
3dfe78b3 | 9943 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); |
ef416fc2 | 9944 | } |
75bd9771 MS |
9945 | |
9946 | start_job = 0; | |
ef416fc2 | 9947 | } |
9948 | ||
9949 | /* | |
9950 | * Fill in the response info... | |
9951 | */ | |
9952 | ||
83e08001 | 9953 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
f2534050 | 9954 | con->clientname, con->clientport, "/jobs/%d", jobid); |
ef416fc2 | 9955 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
9956 | job_uri); | |
9957 | ||
9958 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
9959 | ||
9960 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
75bd9771 | 9961 | job->state_value); |
12f89d24 MS |
9962 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", |
9963 | NULL, job->reasons->values[0].string.text); | |
ef416fc2 | 9964 | |
9965 | con->response->request.status.status_code = IPP_OK; | |
75bd9771 MS |
9966 | |
9967 | /* | |
9968 | * Start the job if necessary... | |
9969 | */ | |
9970 | ||
9971 | if (start_job) | |
9972 | cupsdCheckJobs(); | |
ef416fc2 | 9973 | } |
9974 | ||
9975 | ||
9976 | /* | |
9977 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
9978 | */ | |
9979 | ||
9980 | static void | |
f899b121 | 9981 | send_http_error( |
9982 | cupsd_client_t *con, /* I - Client connection */ | |
9983 | http_status_t status, /* I - HTTP status code */ | |
9984 | cupsd_printer_t *printer) /* I - Printer, if any */ | |
ef416fc2 | 9985 | { |
9f5eb9be MS |
9986 | ipp_attribute_t *uri; /* Request URI, if any */ |
9987 | ||
9988 | ||
9989 | if ((uri = ippFindAttribute(con->request, "printer-uri", | |
9990 | IPP_TAG_URI)) == NULL) | |
9991 | uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI); | |
9992 | ||
9993 | cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG, | |
21f36711 | 9994 | "[Client %d] Returning HTTP %s for %s (%s) from %s", |
996acce8 | 9995 | con->number, httpStatus(status), |
321d8d57 MS |
9996 | con->request ? |
9997 | ippOpString(con->request->request.op.operation_id) : | |
9998 | "no operation-id", | |
9f5eb9be | 9999 | uri ? uri->values[0].string.text : "no URI", |
996acce8 | 10000 | con->http->hostname); |
ef416fc2 | 10001 | |
5f64df29 MS |
10002 | if (printer) |
10003 | { | |
db0bd74a | 10004 | int auth_type; /* Type of authentication required */ |
2fb76298 MS |
10005 | |
10006 | ||
5f64df29 MS |
10007 | auth_type = CUPSD_AUTH_NONE; |
10008 | ||
10009 | if (status == HTTP_UNAUTHORIZED && | |
10010 | printer->num_auth_info_required > 0 && | |
10011 | !strcmp(printer->auth_info_required[0], "negotiate") && | |
10012 | con->request && | |
10013 | (con->request->request.op.operation_id == IPP_PRINT_JOB || | |
10014 | con->request->request.op.operation_id == IPP_CREATE_JOB || | |
10015 | con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB)) | |
10016 | { | |
10017 | /* | |
10018 | * Creating and authenticating jobs requires Kerberos... | |
10019 | */ | |
10020 | ||
10021 | auth_type = CUPSD_AUTH_NEGOTIATE; | |
10022 | } | |
db0bd74a | 10023 | else |
5f64df29 MS |
10024 | { |
10025 | /* | |
10026 | * Use policy/location-defined authentication requirements... | |
10027 | */ | |
10028 | ||
10029 | char resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
10030 | cupsd_location_t *auth; /* Pointer to authentication element */ | |
10031 | ||
10032 | ||
10033 | if (printer->type & CUPS_PRINTER_CLASS) | |
10034 | snprintf(resource, sizeof(resource), "/classes/%s", printer->name); | |
10035 | else | |
10036 | snprintf(resource, sizeof(resource), "/printers/%s", printer->name); | |
10037 | ||
10038 | if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL || | |
10039 | auth->type == CUPSD_AUTH_NONE) | |
10040 | auth = cupsdFindPolicyOp(printer->op_policy_ptr, | |
10041 | con->request ? | |
10042 | con->request->request.op.operation_id : | |
10043 | IPP_PRINT_JOB); | |
10044 | ||
10045 | if (auth) | |
10046 | { | |
10047 | if (auth->type == CUPSD_AUTH_DEFAULT) | |
dcb445bc | 10048 | auth_type = cupsdDefaultAuthType(); |
5f64df29 MS |
10049 | else |
10050 | auth_type = auth->type; | |
10051 | } | |
10052 | } | |
2fb76298 | 10053 | |
db0bd74a | 10054 | cupsdSendError(con, status, auth_type); |
2fb76298 | 10055 | } |
f899b121 | 10056 | else |
5bd77a73 | 10057 | cupsdSendError(con, status, CUPSD_AUTH_NONE); |
ef416fc2 | 10058 | |
10059 | ippDelete(con->response); | |
10060 | con->response = NULL; | |
10061 | ||
10062 | return; | |
10063 | } | |
10064 | ||
10065 | ||
10066 | /* | |
10067 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
10068 | */ | |
10069 | ||
10070 | static void | |
10071 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
1f0275e3 MS |
10072 | ipp_status_t status, /* I - IPP status code */ |
10073 | const char *message,/* I - Status message */ | |
10074 | ...) /* I - Additional args as needed */ | |
ef416fc2 | 10075 | { |
10076 | va_list ap; /* Pointer to additional args */ | |
10077 | char formatted[1024]; /* Formatted errror message */ | |
10078 | ||
10079 | ||
3d8365b8 | 10080 | va_start(ap, message); |
10081 | vsnprintf(formatted, sizeof(formatted), | |
10082 | _cupsLangString(con->language, message), ap); | |
10083 | va_end(ap); | |
ef416fc2 | 10084 | |
3d8365b8 | 10085 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s", |
10086 | ippOpString(con->request->request.op.operation_id), | |
10087 | ippErrorString(status), formatted); | |
ef416fc2 | 10088 | |
10089 | con->response->request.status.status_code = status; | |
10090 | ||
fa73b229 | 10091 | if (ippFindAttribute(con->response, "attributes-charset", |
10092 | IPP_TAG_ZERO) == NULL) | |
ef416fc2 | 10093 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
f8b3a85b | 10094 | "attributes-charset", NULL, "utf-8"); |
ef416fc2 | 10095 | |
10096 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
10097 | IPP_TAG_ZERO) == NULL) | |
10098 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
10099 | "attributes-natural-language", NULL, DefaultLanguage); | |
10100 | ||
3d8365b8 | 10101 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
10102 | "status-message", NULL, formatted); | |
ef416fc2 | 10103 | } |
10104 | ||
10105 | ||
10106 | /* | |
10107 | * 'set_default()' - Set the default destination... | |
10108 | */ | |
10109 | ||
10110 | static void | |
10111 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
10112 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10113 | { | |
10114 | http_status_t status; /* Policy status */ | |
bc44d920 | 10115 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
dd1abb6b MS |
10116 | cupsd_printer_t *printer, /* Printer */ |
10117 | *oldprinter; /* Old default printer */ | |
ef416fc2 | 10118 | |
10119 | ||
10120 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
996acce8 | 10121 | con->number, uri->values[0].string.text); |
ef416fc2 | 10122 | |
10123 | /* | |
10124 | * Is the destination valid? | |
10125 | */ | |
10126 | ||
f7deaa1a | 10127 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10128 | { |
10129 | /* | |
10130 | * Bad URI... | |
10131 | */ | |
10132 | ||
10133 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 10134 | _("The printer or class does not exist.")); |
ef416fc2 | 10135 | return; |
10136 | } | |
10137 | ||
10138 | /* | |
10139 | * Check policy... | |
10140 | */ | |
10141 | ||
10142 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
10143 | { | |
f899b121 | 10144 | send_http_error(con, status, NULL); |
ef416fc2 | 10145 | return; |
10146 | } | |
10147 | ||
10148 | /* | |
10149 | * Set it as the default... | |
10150 | */ | |
10151 | ||
dd1abb6b | 10152 | oldprinter = DefaultPrinter; |
ef416fc2 | 10153 | DefaultPrinter = printer; |
10154 | ||
dd1abb6b MS |
10155 | if (oldprinter) |
10156 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL, | |
10157 | "%s is no longer the default printer.", oldprinter->name); | |
10158 | ||
10159 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, | |
10160 | "%s is now the default printer.", printer->name); | |
10161 | ||
3dfe78b3 | 10162 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES | |
a2326b5b | 10163 | CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 10164 | |
10165 | cupsdLogMessage(CUPSD_LOG_INFO, | |
f7deaa1a | 10166 | "Default destination set to \"%s\" by \"%s\".", |
10167 | printer->name, get_username(con)); | |
ef416fc2 | 10168 | |
10169 | /* | |
10170 | * Everything was ok, so return OK status... | |
10171 | */ | |
10172 | ||
10173 | con->response->request.status.status_code = IPP_OK; | |
10174 | } | |
10175 | ||
10176 | ||
10177 | /* | |
10178 | * 'set_job_attrs()' - Set job attributes. | |
10179 | */ | |
10180 | ||
10181 | static void | |
10182 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
10183 | ipp_attribute_t *uri) /* I - Job URI */ | |
10184 | { | |
10185 | ipp_attribute_t *attr, /* Current attribute */ | |
10186 | *attr2; /* Job attribute */ | |
10187 | int jobid; /* Job ID */ | |
10188 | cupsd_job_t *job; /* Current job */ | |
61cf44e2 | 10189 | char scheme[HTTP_MAX_URI], |
ef416fc2 | 10190 | /* Method portion of URI */ |
10191 | username[HTTP_MAX_URI], | |
10192 | /* Username portion of URI */ | |
10193 | host[HTTP_MAX_URI], | |
10194 | /* Host portion of URI */ | |
10195 | resource[HTTP_MAX_URI]; | |
10196 | /* Resource portion of URI */ | |
10197 | int port; /* Port portion of URI */ | |
d09495fa | 10198 | int event; /* Events? */ |
005dd1eb | 10199 | int check_jobs; /* Check jobs? */ |
ef416fc2 | 10200 | |
10201 | ||
10202 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
996acce8 | 10203 | con->number, uri->values[0].string.text); |
ef416fc2 | 10204 | |
10205 | /* | |
10206 | * Start with "everything is OK" status... | |
10207 | */ | |
10208 | ||
10209 | con->response->request.status.status_code = IPP_OK; | |
10210 | ||
10211 | /* | |
10212 | * See if we have a job URI or a printer URI... | |
10213 | */ | |
10214 | ||
fa73b229 | 10215 | if (!strcmp(uri->name, "printer-uri")) |
ef416fc2 | 10216 | { |
10217 | /* | |
10218 | * Got a printer URI; see if we also have a job-id attribute... | |
10219 | */ | |
10220 | ||
fa73b229 | 10221 | if ((attr = ippFindAttribute(con->request, "job-id", |
10222 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 10223 | { |
10224 | send_ipp_status(con, IPP_BAD_REQUEST, | |
84315f46 | 10225 | _("Got a printer-uri attribute but no job-id.")); |
ef416fc2 | 10226 | return; |
10227 | } | |
10228 | ||
10229 | jobid = attr->values[0].integer; | |
10230 | } | |
10231 | else | |
10232 | { | |
10233 | /* | |
10234 | * Got a job URI; parse it to get the job ID... | |
10235 | */ | |
10236 | ||
61cf44e2 MS |
10237 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
10238 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 10239 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 10240 | |
fa73b229 | 10241 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 10242 | { |
10243 | /* | |
10244 | * Not a valid URI! | |
10245 | */ | |
10246 | ||
84315f46 | 10247 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."), |
ef416fc2 | 10248 | uri->values[0].string.text); |
10249 | return; | |
10250 | } | |
10251 | ||
10252 | jobid = atoi(resource + 6); | |
10253 | } | |
10254 | ||
10255 | /* | |
10256 | * See if the job exists... | |
10257 | */ | |
10258 | ||
10259 | if ((job = cupsdFindJob(jobid)) == NULL) | |
10260 | { | |
10261 | /* | |
10262 | * Nope - return a "not found" error... | |
10263 | */ | |
10264 | ||
84315f46 | 10265 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid); |
ef416fc2 | 10266 | return; |
10267 | } | |
10268 | ||
10269 | /* | |
10270 | * See if the job has been completed... | |
10271 | */ | |
10272 | ||
bd7854cb | 10273 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 10274 | { |
10275 | /* | |
10276 | * Return a "not-possible" error... | |
10277 | */ | |
10278 | ||
10279 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
84315f46 | 10280 | _("Job #%d is finished and cannot be altered."), jobid); |
ef416fc2 | 10281 | return; |
10282 | } | |
10283 | ||
10284 | /* | |
10285 | * See if the job is owned by the requesting user... | |
10286 | */ | |
10287 | ||
10288 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
10289 | { | |
b0f6947b MS |
10290 | send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED, |
10291 | cupsdFindDest(job->dest)); | |
ef416fc2 | 10292 | return; |
10293 | } | |
10294 | ||
10295 | /* | |
10296 | * See what the user wants to change. | |
10297 | */ | |
10298 | ||
bd7854cb | 10299 | cupsdLoadJob(job); |
10300 | ||
005dd1eb MS |
10301 | check_jobs = 0; |
10302 | event = 0; | |
d09495fa | 10303 | |
fa73b229 | 10304 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 10305 | { |
10306 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
10307 | continue; | |
10308 | ||
10309 | if (!strcmp(attr->name, "attributes-charset") || | |
10310 | !strcmp(attr->name, "attributes-natural-language") || | |
9514a192 MS |
10311 | !strncmp(attr->name, "date-time-at-", 13) || |
10312 | !strncmp(attr->name, "document-compression", 20) || | |
10313 | !strncmp(attr->name, "document-format", 15) || | |
ef416fc2 | 10314 | !strcmp(attr->name, "job-detailed-status-messages") || |
10315 | !strcmp(attr->name, "job-document-access-errors") || | |
10316 | !strcmp(attr->name, "job-id") || | |
5d6412a9 | 10317 | !strcmp(attr->name, "job-impressions-completed") || |
9514a192 MS |
10318 | !strcmp(attr->name, "job-k-octets-completed") || |
10319 | !strcmp(attr->name, "job-media-sheets-completed") || | |
ef416fc2 | 10320 | !strcmp(attr->name, "job-originating-host-name") || |
10321 | !strcmp(attr->name, "job-originating-user-name") || | |
9514a192 | 10322 | !strcmp(attr->name, "job-pages-completed") || |
ef416fc2 | 10323 | !strcmp(attr->name, "job-printer-up-time") || |
10324 | !strcmp(attr->name, "job-printer-uri") || | |
10325 | !strcmp(attr->name, "job-sheets") || | |
10326 | !strcmp(attr->name, "job-state-message") || | |
10327 | !strcmp(attr->name, "job-state-reasons") || | |
10328 | !strcmp(attr->name, "job-uri") || | |
10329 | !strcmp(attr->name, "number-of-documents") || | |
10330 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
10331 | !strcmp(attr->name, "output-device-assigned") || | |
ef416fc2 | 10332 | !strncmp(attr->name, "time-at-", 8)) |
10333 | { | |
10334 | /* | |
10335 | * Read-only attrs! | |
10336 | */ | |
10337 | ||
10338 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
10339 | _("%s cannot be changed."), attr->name); | |
10340 | ||
a2326b5b MS |
10341 | attr2 = ippCopyAttribute(con->response, attr, 0); |
10342 | ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP); | |
ef416fc2 | 10343 | continue; |
10344 | } | |
10345 | ||
4cadd620 MS |
10346 | if (!ippValidateAttribute(attr)) |
10347 | { | |
10348 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad '%s' value."), attr->name); | |
10349 | ippCopyAttribute(con->response, attr, 0); | |
10350 | return; | |
10351 | } | |
10352 | ||
10353 | if (!strcmp(attr->name, "job-hold-until")) | |
10354 | { | |
10355 | const char *when = ippGetString(attr, 0, NULL); | |
10356 | /* job-hold-until value */ | |
10357 | ||
10358 | if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1) | |
10359 | { | |
10360 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value.")); | |
10361 | ippCopyAttribute(con->response, attr, 0); | |
10362 | return; | |
10363 | } | |
10364 | ||
10365 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s", when); | |
10366 | cupsdSetJobHoldUntil(job, when, 0); | |
10367 | ||
10368 | if (!strcmp(when, "no-hold")) | |
10369 | { | |
10370 | cupsdReleaseJob(job); | |
10371 | check_jobs = 1; | |
10372 | } | |
10373 | else | |
10374 | cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".", username); | |
10375 | ||
10376 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE; | |
10377 | } | |
10378 | else if (!strcmp(attr->name, "job-priority")) | |
ef416fc2 | 10379 | { |
10380 | /* | |
10381 | * Change the job priority... | |
10382 | */ | |
10383 | ||
10384 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10385 | { | |
84315f46 | 10386 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value.")); |
ef416fc2 | 10387 | |
a2326b5b MS |
10388 | attr2 = ippCopyAttribute(con->response, attr, 0); |
10389 | ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP); | |
ef416fc2 | 10390 | } |
bd7854cb | 10391 | else if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 10392 | { |
10393 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10394 | _("Job is completed and cannot be changed.")); | |
10395 | return; | |
10396 | } | |
10397 | else if (con->response->request.status.status_code == IPP_OK) | |
d09495fa | 10398 | { |
005dd1eb MS |
10399 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d", |
10400 | attr->values[0].integer); | |
ef416fc2 | 10401 | cupsdSetJobPriority(job, attr->values[0].integer); |
005dd1eb MS |
10402 | |
10403 | check_jobs = 1; | |
10404 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
10405 | CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED; | |
d09495fa | 10406 | } |
ef416fc2 | 10407 | } |
10408 | else if (!strcmp(attr->name, "job-state")) | |
10409 | { | |
10410 | /* | |
10411 | * Change the job state... | |
10412 | */ | |
10413 | ||
10414 | if (attr->value_tag != IPP_TAG_ENUM) | |
10415 | { | |
84315f46 | 10416 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value.")); |
ef416fc2 | 10417 | |
a2326b5b MS |
10418 | attr2 = ippCopyAttribute(con->response, attr, 0); |
10419 | ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP); | |
ef416fc2 | 10420 | } |
10421 | else | |
10422 | { | |
10423 | switch (attr->values[0].integer) | |
10424 | { | |
10425 | case IPP_JOB_PENDING : | |
10426 | case IPP_JOB_HELD : | |
bd7854cb | 10427 | if (job->state_value > IPP_JOB_HELD) |
ef416fc2 | 10428 | { |
10429 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10430 | _("Job state cannot be changed.")); | |
10431 | return; | |
10432 | } | |
10433 | else if (con->response->request.status.status_code == IPP_OK) | |
bd7854cb | 10434 | { |
005dd1eb MS |
10435 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d", |
10436 | attr->values[0].integer); | |
7e86f2f6 | 10437 | cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer, CUPSD_JOB_DEFAULT, "Job state changed by \"%s\"", username); |
005dd1eb | 10438 | check_jobs = 1; |
bd7854cb | 10439 | } |
ef416fc2 | 10440 | break; |
10441 | ||
10442 | case IPP_JOB_PROCESSING : | |
10443 | case IPP_JOB_STOPPED : | |
bd7854cb | 10444 | if (job->state_value != attr->values[0].integer) |
ef416fc2 | 10445 | { |
10446 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10447 | _("Job state cannot be changed.")); | |
10448 | return; | |
10449 | } | |
10450 | break; | |
10451 | ||
d09495fa | 10452 | case IPP_JOB_CANCELED : |
ef416fc2 | 10453 | case IPP_JOB_ABORTED : |
10454 | case IPP_JOB_COMPLETED : | |
bd7854cb | 10455 | if (job->state_value > IPP_JOB_PROCESSING) |
ef416fc2 | 10456 | { |
10457 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10458 | _("Job state cannot be changed.")); | |
10459 | return; | |
10460 | } | |
10461 | else if (con->response->request.status.status_code == IPP_OK) | |
005dd1eb MS |
10462 | { |
10463 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d", | |
10464 | attr->values[0].integer); | |
b9faaae1 MS |
10465 | cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer, |
10466 | CUPSD_JOB_DEFAULT, | |
10467 | "Job state changed by \"%s\"", username); | |
005dd1eb MS |
10468 | check_jobs = 1; |
10469 | } | |
ef416fc2 | 10470 | break; |
10471 | } | |
10472 | } | |
10473 | } | |
10474 | else if (con->response->request.status.status_code != IPP_OK) | |
10475 | continue; | |
fa73b229 | 10476 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, |
10477 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 10478 | { |
10479 | /* | |
10480 | * Some other value; first free the old value... | |
10481 | */ | |
10482 | ||
10483 | if (job->attrs->prev) | |
10484 | job->attrs->prev->next = attr2->next; | |
10485 | else | |
10486 | job->attrs->attrs = attr2->next; | |
10487 | ||
10488 | if (job->attrs->last == attr2) | |
10489 | job->attrs->last = job->attrs->prev; | |
10490 | ||
a2326b5b | 10491 | ippDeleteAttribute(NULL, attr2); |
ef416fc2 | 10492 | |
10493 | /* | |
10494 | * Then copy the attribute... | |
10495 | */ | |
10496 | ||
a2326b5b | 10497 | ippCopyAttribute(job->attrs, attr, 0); |
ef416fc2 | 10498 | } |
10499 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
10500 | { | |
10501 | /* | |
10502 | * Delete the attribute... | |
10503 | */ | |
10504 | ||
10505 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
10506 | IPP_TAG_ZERO)) != NULL) | |
10507 | { | |
10508 | if (job->attrs->prev) | |
10509 | job->attrs->prev->next = attr2->next; | |
10510 | else | |
10511 | job->attrs->attrs = attr2->next; | |
10512 | ||
10513 | if (attr2 == job->attrs->last) | |
10514 | job->attrs->last = job->attrs->prev; | |
10515 | ||
a2326b5b | 10516 | ippDeleteAttribute(NULL, attr2); |
d09495fa | 10517 | |
10518 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10519 | } |
10520 | } | |
10521 | else | |
10522 | { | |
10523 | /* | |
10524 | * Add new option by copying it... | |
10525 | */ | |
10526 | ||
a2326b5b | 10527 | ippCopyAttribute(job->attrs, attr, 0); |
d09495fa | 10528 | |
10529 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10530 | } |
10531 | } | |
10532 | ||
10533 | /* | |
10534 | * Save the job... | |
10535 | */ | |
10536 | ||
3dfe78b3 MS |
10537 | job->dirty = 1; |
10538 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 10539 | |
d09495fa | 10540 | /* |
10541 | * Send events as needed... | |
10542 | */ | |
10543 | ||
d9bca400 | 10544 | if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED) |
01ce6322 MS |
10545 | cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED, |
10546 | cupsdFindDest(job->dest), job, | |
d9bca400 MS |
10547 | "Job priority changed by user."); |
10548 | ||
d09495fa | 10549 | if (event & CUPSD_EVENT_JOB_STATE) |
01ce6322 | 10550 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 10551 | job->state_value == IPP_JOB_HELD ? |
10552 | "Job held by user." : "Job restarted by user."); | |
10553 | ||
10554 | if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED) | |
01ce6322 | 10555 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 10556 | "Job options changed by user."); |
10557 | ||
ef416fc2 | 10558 | /* |
10559 | * Start jobs if possible... | |
10560 | */ | |
10561 | ||
005dd1eb MS |
10562 | if (check_jobs) |
10563 | cupsdCheckJobs(); | |
ef416fc2 | 10564 | } |
10565 | ||
10566 | ||
c168a833 MS |
10567 | /* |
10568 | * 'set_printer_attrs()' - Set printer attributes. | |
10569 | */ | |
10570 | ||
10571 | static void | |
10572 | set_printer_attrs(cupsd_client_t *con, /* I - Client connection */ | |
10573 | ipp_attribute_t *uri) /* I - Printer */ | |
10574 | { | |
10575 | http_status_t status; /* Policy status */ | |
10576 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
10577 | cupsd_printer_t *printer; /* Printer/class */ | |
10578 | ipp_attribute_t *attr; /* Printer attribute */ | |
bf3816c7 | 10579 | int changed = 0; /* Was anything changed? */ |
c168a833 MS |
10580 | |
10581 | ||
10582 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con, | |
996acce8 | 10583 | con->number, uri->values[0].string.text); |
c168a833 MS |
10584 | |
10585 | /* | |
10586 | * Is the destination valid? | |
10587 | */ | |
10588 | ||
10589 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
10590 | { | |
10591 | /* | |
10592 | * Bad URI... | |
10593 | */ | |
10594 | ||
10595 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 10596 | _("The printer or class does not exist.")); |
c168a833 MS |
10597 | return; |
10598 | } | |
10599 | ||
10600 | /* | |
10601 | * Check policy... | |
10602 | */ | |
10603 | ||
10604 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10605 | { | |
10606 | send_http_error(con, status, printer); | |
10607 | return; | |
10608 | } | |
10609 | ||
10610 | /* | |
10611 | * Return a list of attributes that can be set via Set-Printer-Attributes. | |
10612 | */ | |
10613 | ||
10614 | if ((attr = ippFindAttribute(con->request, "printer-location", | |
10615 | IPP_TAG_TEXT)) != NULL) | |
10616 | { | |
10617 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
10618 | changed = 1; | |
10619 | } | |
10620 | ||
9b4bd602 MS |
10621 | if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4)) |
10622 | { | |
10623 | cupsdSetString(&printer->geo_location, attr->values[0].string.text); | |
10624 | changed = 1; | |
10625 | } | |
10626 | ||
10627 | if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL) | |
10628 | { | |
10629 | cupsdSetString(&printer->organization, attr->values[0].string.text); | |
10630 | changed = 1; | |
10631 | } | |
10632 | ||
10633 | if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL) | |
10634 | { | |
10635 | cupsdSetString(&printer->organizational_unit, attr->values[0].string.text); | |
10636 | changed = 1; | |
10637 | } | |
10638 | ||
c168a833 MS |
10639 | if ((attr = ippFindAttribute(con->request, "printer-info", |
10640 | IPP_TAG_TEXT)) != NULL) | |
10641 | { | |
10642 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
10643 | changed = 1; | |
10644 | } | |
10645 | ||
10646 | /* | |
10647 | * Update the printer attributes and return... | |
10648 | */ | |
10649 | ||
10650 | if (changed) | |
10651 | { | |
9b4bd602 MS |
10652 | printer->config_time = time(NULL); |
10653 | ||
c168a833 MS |
10654 | cupsdSetPrinterAttrs(printer); |
10655 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
10656 | ||
10657 | cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, | |
10658 | "Printer \"%s\" description or location changed by \"%s\".", | |
10659 | printer->name, get_username(con)); | |
10660 | ||
10661 | cupsdLogMessage(CUPSD_LOG_INFO, | |
10662 | "Printer \"%s\" description or location changed by \"%s\".", | |
10663 | printer->name, get_username(con)); | |
10664 | } | |
10665 | ||
10666 | con->response->request.status.status_code = IPP_OK; | |
10667 | } | |
10668 | ||
10669 | ||
b423cd4c | 10670 | /* |
10671 | * 'set_printer_defaults()' - Set printer default options from a request. | |
10672 | */ | |
10673 | ||
f0b589f7 | 10674 | static int /* O - 1 on success, 0 on failure */ |
b423cd4c | 10675 | set_printer_defaults( |
10676 | cupsd_client_t *con, /* I - Client connection */ | |
10677 | cupsd_printer_t *printer) /* I - Printer */ | |
10678 | { | |
10679 | int i; /* Looping var */ | |
10680 | ipp_attribute_t *attr; /* Current attribute */ | |
7e86f2f6 | 10681 | size_t namelen; /* Length of attribute name */ |
b423cd4c | 10682 | char name[256], /* New attribute name */ |
10683 | value[256]; /* String version of integer attrs */ | |
10684 | ||
10685 | ||
10686 | for (attr = con->request->attrs; attr; attr = attr->next) | |
10687 | { | |
10688 | /* | |
10689 | * Skip non-printer attributes... | |
10690 | */ | |
10691 | ||
10692 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
10693 | continue; | |
10694 | ||
10695 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
10696 | ||
10697 | if (!strcmp(attr->name, "job-sheets-default")) | |
10698 | { | |
10699 | /* | |
10700 | * Only allow keywords and names... | |
10701 | */ | |
10702 | ||
10703 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10704 | continue; | |
10705 | ||
10706 | /* | |
10707 | * Only allow job-sheets-default to be set when running without a | |
10708 | * system high classification level... | |
10709 | */ | |
10710 | ||
10711 | if (Classification) | |
10712 | continue; | |
10713 | ||
10714 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
10715 | ||
10716 | if (attr->num_values > 1) | |
10717 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
10718 | else | |
10719 | cupsdSetString(&printer->job_sheets[1], "none"); | |
10720 | } | |
10721 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
10722 | { | |
10d09e33 | 10723 | cupsdFreeStrings(&(printer->users)); |
b423cd4c | 10724 | |
10725 | printer->deny_users = 0; | |
10726 | ||
10727 | if (attr->value_tag == IPP_TAG_NAME && | |
10728 | (attr->num_values > 1 || | |
10729 | strcmp(attr->values[0].string.text, "all"))) | |
10730 | { | |
10731 | for (i = 0; i < attr->num_values; i ++) | |
10d09e33 | 10732 | cupsdAddString(&(printer->users), attr->values[i].string.text); |
b423cd4c | 10733 | } |
10734 | } | |
10735 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
10736 | { | |
10d09e33 | 10737 | cupsdFreeStrings(&(printer->users)); |
b423cd4c | 10738 | |
10739 | printer->deny_users = 1; | |
10740 | ||
10741 | if (attr->value_tag == IPP_TAG_NAME && | |
10742 | (attr->num_values > 1 || | |
10743 | strcmp(attr->values[0].string.text, "none"))) | |
10744 | { | |
10745 | for (i = 0; i < attr->num_values; i ++) | |
10d09e33 | 10746 | cupsdAddString(&(printer->users), attr->values[i].string.text); |
b423cd4c | 10747 | } |
10748 | } | |
10749 | else if (!strcmp(attr->name, "job-quota-period")) | |
10750 | { | |
10751 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10752 | continue; | |
10753 | ||
10754 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
10755 | attr->values[0].integer); | |
10756 | cupsdFreeQuotas(printer); | |
10757 | ||
10758 | printer->quota_period = attr->values[0].integer; | |
10759 | } | |
10760 | else if (!strcmp(attr->name, "job-k-limit")) | |
10761 | { | |
10762 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10763 | continue; | |
10764 | ||
10765 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
10766 | attr->values[0].integer); | |
10767 | cupsdFreeQuotas(printer); | |
10768 | ||
10769 | printer->k_limit = attr->values[0].integer; | |
10770 | } | |
10771 | else if (!strcmp(attr->name, "job-page-limit")) | |
10772 | { | |
10773 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10774 | continue; | |
10775 | ||
10776 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
10777 | attr->values[0].integer); | |
10778 | cupsdFreeQuotas(printer); | |
10779 | ||
10780 | printer->page_limit = attr->values[0].integer; | |
10781 | } | |
10782 | else if (!strcmp(attr->name, "printer-op-policy")) | |
10783 | { | |
10784 | cupsd_policy_t *p; /* Policy */ | |
10785 | ||
10786 | ||
10787 | if (attr->value_tag != IPP_TAG_NAME) | |
10788 | continue; | |
10789 | ||
10790 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
10791 | { | |
10792 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10793 | "Setting printer-op-policy to \"%s\"...", | |
10794 | attr->values[0].string.text); | |
10795 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
10796 | printer->op_policy_ptr = p; | |
10797 | } | |
10798 | else | |
10799 | { | |
10800 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10801 | _("Unknown printer-op-policy \"%s\"."), | |
10802 | attr->values[0].string.text); | |
f0b589f7 | 10803 | return (0); |
b423cd4c | 10804 | } |
10805 | } | |
10806 | else if (!strcmp(attr->name, "printer-error-policy")) | |
10807 | { | |
10808 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10809 | continue; | |
10810 | ||
f11a948a | 10811 | if (strcmp(attr->values[0].string.text, "retry-current-job") && |
a2326b5b | 10812 | ((printer->type & CUPS_PRINTER_CLASS) || |
f11a948a MS |
10813 | (strcmp(attr->values[0].string.text, "abort-job") && |
10814 | strcmp(attr->values[0].string.text, "retry-job") && | |
10815 | strcmp(attr->values[0].string.text, "stop-printer")))) | |
b423cd4c | 10816 | { |
10817 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10818 | _("Unknown printer-error-policy \"%s\"."), | |
10819 | attr->values[0].string.text); | |
f0b589f7 | 10820 | return (0); |
b423cd4c | 10821 | } |
10822 | ||
10823 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10824 | "Setting printer-error-policy to \"%s\"...", | |
10825 | attr->values[0].string.text); | |
10826 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
10827 | } | |
b423cd4c | 10828 | |
10829 | /* | |
10830 | * Skip any other non-default attributes... | |
10831 | */ | |
10832 | ||
10833 | namelen = strlen(attr->name); | |
10834 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
10835 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
10836 | continue; | |
10837 | ||
10838 | /* | |
10839 | * OK, anything else must be a user-defined default... | |
10840 | */ | |
10841 | ||
10842 | strlcpy(name, attr->name, sizeof(name)); | |
10843 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
10844 | ||
10845 | switch (attr->value_tag) | |
10846 | { | |
10847 | case IPP_TAG_DELETEATTR : | |
10848 | printer->num_options = cupsRemoveOption(name, | |
10849 | printer->num_options, | |
10850 | &(printer->options)); | |
10851 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10852 | "Deleting %s", attr->name); | |
10853 | break; | |
10854 | ||
10855 | case IPP_TAG_NAME : | |
5a9febac | 10856 | case IPP_TAG_TEXT : |
b423cd4c | 10857 | case IPP_TAG_KEYWORD : |
10858 | case IPP_TAG_URI : | |
10859 | printer->num_options = cupsAddOption(name, | |
10860 | attr->values[0].string.text, | |
10861 | printer->num_options, | |
10862 | &(printer->options)); | |
10863 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10864 | "Setting %s to \"%s\"...", attr->name, | |
10865 | attr->values[0].string.text); | |
10866 | break; | |
10867 | ||
10868 | case IPP_TAG_BOOLEAN : | |
10869 | printer->num_options = cupsAddOption(name, | |
10870 | attr->values[0].boolean ? | |
10871 | "true" : "false", | |
10872 | printer->num_options, | |
10873 | &(printer->options)); | |
10874 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10875 | "Setting %s to %s...", attr->name, | |
10876 | attr->values[0].boolean ? "true" : "false"); | |
10877 | break; | |
10878 | ||
10879 | case IPP_TAG_INTEGER : | |
10880 | case IPP_TAG_ENUM : | |
10881 | sprintf(value, "%d", attr->values[0].integer); | |
10882 | printer->num_options = cupsAddOption(name, value, | |
10883 | printer->num_options, | |
10884 | &(printer->options)); | |
10885 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10886 | "Setting %s to %s...", attr->name, value); | |
10887 | break; | |
10888 | ||
10889 | case IPP_TAG_RANGE : | |
10890 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
10891 | attr->values[0].range.upper); | |
10892 | printer->num_options = cupsAddOption(name, value, | |
10893 | printer->num_options, | |
10894 | &(printer->options)); | |
10895 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10896 | "Setting %s to %s...", attr->name, value); | |
10897 | break; | |
10898 | ||
10899 | case IPP_TAG_RESOLUTION : | |
10900 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
10901 | attr->values[0].resolution.yres, | |
10902 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
3e7fe0ca | 10903 | "dpi" : "dpcm"); |
b423cd4c | 10904 | printer->num_options = cupsAddOption(name, value, |
10905 | printer->num_options, | |
10906 | &(printer->options)); | |
10907 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10908 | "Setting %s to %s...", attr->name, value); | |
10909 | break; | |
10910 | ||
10911 | default : | |
10912 | /* Do nothing for other values */ | |
10913 | break; | |
10914 | } | |
10915 | } | |
f0b589f7 MS |
10916 | |
10917 | return (1); | |
b423cd4c | 10918 | } |
10919 | ||
10920 | ||
ef416fc2 | 10921 | /* |
10922 | * 'start_printer()' - Start a printer. | |
10923 | */ | |
10924 | ||
10925 | static void | |
10926 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
10927 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10928 | { | |
10d09e33 | 10929 | int i; /* Temporary variable */ |
ef416fc2 | 10930 | http_status_t status; /* Policy status */ |
bc44d920 | 10931 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10932 | cupsd_printer_t *printer; /* Printer data */ |
10933 | ||
10934 | ||
10935 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
996acce8 | 10936 | con->number, uri->values[0].string.text); |
ef416fc2 | 10937 | |
10938 | /* | |
10939 | * Is the destination valid? | |
10940 | */ | |
10941 | ||
f7deaa1a | 10942 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10943 | { |
10944 | /* | |
10945 | * Bad URI... | |
10946 | */ | |
10947 | ||
10948 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 10949 | _("The printer or class does not exist.")); |
ef416fc2 | 10950 | return; |
10951 | } | |
10952 | ||
10953 | /* | |
10954 | * Check policy... | |
10955 | */ | |
10956 | ||
10957 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10958 | { | |
f899b121 | 10959 | send_http_error(con, status, printer); |
ef416fc2 | 10960 | return; |
10961 | } | |
10962 | ||
10963 | /* | |
10964 | * Start the printer... | |
10965 | */ | |
10966 | ||
10967 | printer->state_message[0] = '\0'; | |
10968 | ||
10969 | cupsdStartPrinter(printer, 1); | |
10970 | ||
10971 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 10972 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", |
10973 | printer->name, get_username(con)); | |
e00b005a | 10974 | else |
f7deaa1a | 10975 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", |
10976 | printer->name, get_username(con)); | |
ef416fc2 | 10977 | |
10978 | cupsdCheckJobs(); | |
10979 | ||
10d09e33 MS |
10980 | /* |
10981 | * Check quotas... | |
10982 | */ | |
10983 | ||
10984 | if ((i = check_quotas(con, printer)) < 0) | |
10985 | { | |
10986 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
10987 | return; | |
10988 | } | |
10989 | else if (i == 0) | |
10990 | { | |
10991 | send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print.")); | |
10992 | return; | |
10993 | } | |
10994 | ||
ef416fc2 | 10995 | /* |
10996 | * Everything was ok, so return OK status... | |
10997 | */ | |
10998 | ||
10999 | con->response->request.status.status_code = IPP_OK; | |
11000 | } | |
11001 | ||
11002 | ||
11003 | /* | |
11004 | * 'stop_printer()' - Stop a printer. | |
11005 | */ | |
11006 | ||
11007 | static void | |
11008 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
11009 | ipp_attribute_t *uri) /* I - Printer URI */ | |
11010 | { | |
11011 | http_status_t status; /* Policy status */ | |
bc44d920 | 11012 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 11013 | cupsd_printer_t *printer; /* Printer data */ |
11014 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
11015 | ||
11016 | ||
11017 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
996acce8 | 11018 | con->number, uri->values[0].string.text); |
ef416fc2 | 11019 | |
11020 | /* | |
11021 | * Is the destination valid? | |
11022 | */ | |
11023 | ||
f7deaa1a | 11024 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 11025 | { |
11026 | /* | |
11027 | * Bad URI... | |
11028 | */ | |
11029 | ||
11030 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 11031 | _("The printer or class does not exist.")); |
ef416fc2 | 11032 | return; |
11033 | } | |
11034 | ||
11035 | /* | |
11036 | * Check policy... | |
11037 | */ | |
11038 | ||
11039 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
11040 | { | |
f899b121 | 11041 | send_http_error(con, status, printer); |
ef416fc2 | 11042 | return; |
11043 | } | |
11044 | ||
11045 | /* | |
11046 | * Stop the printer... | |
11047 | */ | |
11048 | ||
11049 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
11050 | IPP_TAG_TEXT)) == NULL) | |
5a9febac | 11051 | strlcpy(printer->state_message, "Paused", sizeof(printer->state_message)); |
ef416fc2 | 11052 | else |
11053 | { | |
11054 | strlcpy(printer->state_message, attr->values[0].string.text, | |
11055 | sizeof(printer->state_message)); | |
11056 | } | |
11057 | ||
11058 | cupsdStopPrinter(printer, 1); | |
11059 | ||
11060 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 11061 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", |
11062 | printer->name, get_username(con)); | |
ef416fc2 | 11063 | else |
f7deaa1a | 11064 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", |
11065 | printer->name, get_username(con)); | |
ef416fc2 | 11066 | |
11067 | /* | |
11068 | * Everything was ok, so return OK status... | |
11069 | */ | |
11070 | ||
11071 | con->response->request.status.status_code = IPP_OK; | |
11072 | } | |
11073 | ||
11074 | ||
89d46774 | 11075 | /* |
11076 | * 'url_encode_attr()' - URL-encode a string attribute. | |
11077 | */ | |
11078 | ||
11079 | static void | |
11080 | url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */ | |
11081 | char *buffer,/* I - String buffer */ | |
07623986 | 11082 | size_t bufsize)/* I - Size of buffer */ |
89d46774 | 11083 | { |
11084 | int i; /* Looping var */ | |
11085 | char *bufptr, /* Pointer into buffer */ | |
b94498cf | 11086 | *bufend; /* End of buffer */ |
89d46774 | 11087 | |
11088 | ||
11089 | strlcpy(buffer, attr->name, bufsize); | |
11090 | bufptr = buffer + strlen(buffer); | |
11091 | bufend = buffer + bufsize - 1; | |
11092 | ||
11093 | for (i = 0; i < attr->num_values; i ++) | |
11094 | { | |
11095 | if (bufptr >= bufend) | |
11096 | break; | |
11097 | ||
11098 | if (i) | |
11099 | *bufptr++ = ','; | |
11100 | else | |
11101 | *bufptr++ = '='; | |
11102 | ||
11103 | if (bufptr >= bufend) | |
11104 | break; | |
11105 | ||
11106 | *bufptr++ = '\''; | |
11107 | ||
07623986 | 11108 | bufptr = url_encode_string(attr->values[i].string.text, bufptr, (size_t)(bufend - bufptr + 1)); |
89d46774 | 11109 | |
11110 | if (bufptr >= bufend) | |
11111 | break; | |
11112 | ||
11113 | *bufptr++ = '\''; | |
11114 | } | |
11115 | ||
11116 | *bufptr = '\0'; | |
11117 | } | |
11118 | ||
11119 | ||
b94498cf | 11120 | /* |
11121 | * 'url_encode_string()' - URL-encode a string. | |
11122 | */ | |
11123 | ||
11124 | static char * /* O - End of string */ | |
11125 | url_encode_string(const char *s, /* I - String */ | |
11126 | char *buffer, /* I - String buffer */ | |
07623986 | 11127 | size_t bufsize) /* I - Size of buffer */ |
b94498cf | 11128 | { |
11129 | char *bufptr, /* Pointer into buffer */ | |
11130 | *bufend; /* End of buffer */ | |
11131 | static const char *hex = "0123456789ABCDEF"; | |
11132 | /* Hex digits */ | |
11133 | ||
11134 | ||
11135 | bufptr = buffer; | |
11136 | bufend = buffer + bufsize - 1; | |
11137 | ||
11138 | while (*s && bufptr < bufend) | |
11139 | { | |
f701418f | 11140 | if (*s == ' ' || *s == '%' || *s == '+') |
b94498cf | 11141 | { |
11142 | if (bufptr >= (bufend - 2)) | |
11143 | break; | |
11144 | ||
11145 | *bufptr++ = '%'; | |
11146 | *bufptr++ = hex[(*s >> 4) & 15]; | |
11147 | *bufptr++ = hex[*s & 15]; | |
11148 | ||
11149 | s ++; | |
11150 | } | |
11151 | else if (*s == '\'' || *s == '\\') | |
11152 | { | |
11153 | if (bufptr >= (bufend - 1)) | |
11154 | break; | |
11155 | ||
11156 | *bufptr++ = '\\'; | |
11157 | *bufptr++ = *s++; | |
11158 | } | |
11159 | else | |
11160 | *bufptr++ = *s++; | |
11161 | } | |
11162 | ||
11163 | *bufptr = '\0'; | |
11164 | ||
11165 | return (bufptr); | |
11166 | } | |
11167 | ||
11168 | ||
ef416fc2 | 11169 | /* |
11170 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
11171 | */ | |
11172 | ||
11173 | static int /* O - 0 if not allowed, 1 if allowed */ | |
11174 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
11175 | const char *username) /* I - Username */ | |
11176 | { | |
ef416fc2 | 11177 | struct passwd *pw; /* User password data */ |
5bd77a73 | 11178 | char baseuser[256], /* Base username */ |
10d09e33 MS |
11179 | *baseptr, /* Pointer to "@" in base username */ |
11180 | *name; /* Current user name */ | |
ef416fc2 | 11181 | |
11182 | ||
10d09e33 | 11183 | if (cupsArrayCount(p->users) == 0) |
ef416fc2 | 11184 | return (1); |
11185 | ||
11186 | if (!strcmp(username, "root")) | |
11187 | return (1); | |
11188 | ||
5bd77a73 MS |
11189 | if (strchr(username, '@')) |
11190 | { | |
11191 | /* | |
11192 | * Strip @REALM for username check... | |
11193 | */ | |
11194 | ||
11195 | strlcpy(baseuser, username, sizeof(baseuser)); | |
11196 | ||
11197 | if ((baseptr = strchr(baseuser, '@')) != NULL) | |
11198 | *baseptr = '\0'; | |
11199 | ||
11200 | username = baseuser; | |
11201 | } | |
11202 | ||
ef416fc2 | 11203 | pw = getpwnam(username); |
11204 | endpwent(); | |
11205 | ||
10d09e33 MS |
11206 | for (name = (char *)cupsArrayFirst(p->users); |
11207 | name; | |
11208 | name = (char *)cupsArrayNext(p->users)) | |
ef416fc2 | 11209 | { |
10d09e33 | 11210 | if (name[0] == '@') |
ef416fc2 | 11211 | { |
11212 | /* | |
11213 | * Check group membership... | |
11214 | */ | |
11215 | ||
10d09e33 | 11216 | if (cupsdCheckGroup(username, pw, name + 1)) |
ef416fc2 | 11217 | break; |
11218 | } | |
10d09e33 | 11219 | else if (name[0] == '#') |
8922323b MS |
11220 | { |
11221 | /* | |
11222 | * Check UUID... | |
11223 | */ | |
11224 | ||
10d09e33 | 11225 | if (cupsdCheckGroup(username, pw, name)) |
8922323b MS |
11226 | break; |
11227 | } | |
88f9aafc | 11228 | else if (!_cups_strcasecmp(username, name)) |
ef416fc2 | 11229 | break; |
11230 | } | |
11231 | ||
10d09e33 | 11232 | return ((name != NULL) != p->deny_users); |
ef416fc2 | 11233 | } |
11234 | ||
11235 | ||
11236 | /* | |
11237 | * 'validate_job()' - Validate printer options and destination. | |
11238 | */ | |
11239 | ||
11240 | static void | |
11241 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
11242 | ipp_attribute_t *uri) /* I - Printer URI */ | |
11243 | { | |
11244 | http_status_t status; /* Policy status */ | |
7d5824d6 MS |
11245 | ipp_attribute_t *attr; /* Current attribute */ |
11246 | #ifdef HAVE_SSL | |
11247 | ipp_attribute_t *auth_info; /* auth-info attribute */ | |
11248 | #endif /* HAVE_SSL */ | |
5a9febac MS |
11249 | ipp_attribute_t *format, /* Document-format attribute */ |
11250 | *name; /* Job-name attribute */ | |
bc44d920 | 11251 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 11252 | char super[MIME_MAX_SUPER], |
11253 | /* Supertype of file */ | |
11254 | type[MIME_MAX_TYPE]; | |
11255 | /* Subtype of file */ | |
11256 | cupsd_printer_t *printer; /* Printer */ | |
11257 | ||
11258 | ||
11259 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
996acce8 | 11260 | con->number, uri->values[0].string.text); |
ef416fc2 | 11261 | |
11262 | /* | |
11263 | * OK, see if the client is sending the document compressed - CUPS | |
11264 | * doesn't support compression yet... | |
11265 | */ | |
11266 | ||
fa73b229 | 11267 | if ((attr = ippFindAttribute(con->request, "compression", |
c7017ecc | 11268 | IPP_TAG_KEYWORD)) != NULL) |
ef416fc2 | 11269 | { |
c7017ecc MS |
11270 | if (strcmp(attr->values[0].string.text, "none") |
11271 | #ifdef HAVE_LIBZ | |
11272 | && strcmp(attr->values[0].string.text, "gzip") | |
11273 | #endif /* HAVE_LIBZ */ | |
11274 | ) | |
11275 | { | |
11276 | send_ipp_status(con, IPP_ATTRIBUTES, | |
5a9febac | 11277 | _("Unsupported 'compression' value \"%s\"."), |
c7017ecc MS |
11278 | attr->values[0].string.text); |
11279 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
11280 | "compression", NULL, attr->values[0].string.text); | |
11281 | return; | |
11282 | } | |
ef416fc2 | 11283 | } |
11284 | ||
11285 | /* | |
11286 | * Is it a format we support? | |
11287 | */ | |
11288 | ||
11289 | if ((format = ippFindAttribute(con->request, "document-format", | |
11290 | IPP_TAG_MIMETYPE)) != NULL) | |
11291 | { | |
c5b24bfa | 11292 | if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", |
bc44d920 | 11293 | super, type) != 2) |
ef416fc2 | 11294 | { |
5a9febac MS |
11295 | send_ipp_status(con, IPP_BAD_REQUEST, |
11296 | _("Bad 'document-format' value \"%s\"."), | |
ef416fc2 | 11297 | format->values[0].string.text); |
11298 | return; | |
11299 | } | |
11300 | ||
fa73b229 | 11301 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && |
11302 | !mimeType(MimeDatabase, super, type)) | |
ef416fc2 | 11303 | { |
11304 | cupsdLogMessage(CUPSD_LOG_INFO, | |
11305 | "Hint: Do you have the raw file printing rules enabled?"); | |
11306 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
5a9febac | 11307 | _("Unsupported 'document-format' value \"%s\"."), |
ef416fc2 | 11308 | format->values[0].string.text); |
11309 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
11310 | "document-format", NULL, format->values[0].string.text); | |
11311 | return; | |
11312 | } | |
11313 | } | |
11314 | ||
4cadd620 MS |
11315 | /* |
11316 | * Is the job-hold-until value valid? | |
11317 | */ | |
11318 | ||
11319 | 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))) | |
11320 | { | |
11321 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value.")); | |
11322 | ippCopyAttribute(con->response, attr, 0); | |
11323 | return; | |
11324 | } | |
11325 | ||
5a9febac MS |
11326 | /* |
11327 | * Is the job-name valid? | |
11328 | */ | |
11329 | ||
11330 | if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL) | |
11331 | { | |
5a9febac | 11332 | if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) || |
4cadd620 | 11333 | name->num_values != 1 || !ippValidateAttribute(name)) |
5a9febac MS |
11334 | { |
11335 | if (StrictConformance) | |
11336 | { | |
4cadd620 | 11337 | send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-name' value.")); |
5a9febac MS |
11338 | ippCopyAttribute(con->response, name, 0); |
11339 | return; | |
11340 | } | |
11341 | else | |
11342 | { | |
4cadd620 | 11343 | cupsdLogMessage(CUPSD_LOG_WARN, "Unsupported 'job-name' value, deleting from request."); |
5a9febac MS |
11344 | ippDeleteAttribute(con->request, name); |
11345 | } | |
11346 | } | |
11347 | } | |
11348 | ||
ef416fc2 | 11349 | /* |
11350 | * Is the destination valid? | |
11351 | */ | |
11352 | ||
f7deaa1a | 11353 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 11354 | { |
11355 | /* | |
11356 | * Bad URI... | |
11357 | */ | |
11358 | ||
11359 | send_ipp_status(con, IPP_NOT_FOUND, | |
84315f46 | 11360 | _("The printer or class does not exist.")); |
ef416fc2 | 11361 | return; |
11362 | } | |
11363 | ||
11364 | /* | |
11365 | * Check policy... | |
11366 | */ | |
11367 | ||
7d5824d6 | 11368 | #ifdef HAVE_SSL |
c7017ecc | 11369 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
7d5824d6 | 11370 | #endif /* HAVE_SSL */ |
c7017ecc | 11371 | |
ef416fc2 | 11372 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
11373 | { | |
f899b121 | 11374 | send_http_error(con, status, printer); |
ef416fc2 | 11375 | return; |
11376 | } | |
c7017ecc MS |
11377 | else if (printer->num_auth_info_required == 1 && |
11378 | !strcmp(printer->auth_info_required[0], "negotiate") && | |
11379 | !con->username[0]) | |
11380 | { | |
11381 | send_http_error(con, HTTP_UNAUTHORIZED, printer); | |
11382 | return; | |
11383 | } | |
11384 | #ifdef HAVE_SSL | |
996acce8 MS |
11385 | else if (auth_info && !con->http->tls && |
11386 | !httpAddrLocalhost(con->http->hostaddr)) | |
c7017ecc MS |
11387 | { |
11388 | /* | |
11389 | * Require encryption of auth-info over non-local connections... | |
11390 | */ | |
11391 | ||
11392 | send_http_error(con, HTTP_UPGRADE_REQUIRED, printer); | |
11393 | return; | |
11394 | } | |
11395 | #endif /* HAVE_SSL */ | |
ef416fc2 | 11396 | |
11397 | /* | |
11398 | * Everything was ok, so return OK status... | |
11399 | */ | |
11400 | ||
11401 | con->response->request.status.status_code = IPP_OK; | |
11402 | } | |
11403 | ||
11404 | ||
11405 | /* | |
11406 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
11407 | */ | |
11408 | ||
bc44d920 | 11409 | static int /* O - 0 if name is no good, 1 if good */ |
ef416fc2 | 11410 | validate_name(const char *name) /* I - Name to check */ |
11411 | { | |
11412 | const char *ptr; /* Pointer into name */ | |
11413 | ||
11414 | ||
11415 | /* | |
11416 | * Scan the whole name... | |
11417 | */ | |
11418 | ||
11419 | for (ptr = name; *ptr; ptr ++) | |
f7deaa1a | 11420 | if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') |
ef416fc2 | 11421 | return (0); |
11422 | ||
11423 | /* | |
11424 | * All the characters are good; validate the length, too... | |
11425 | */ | |
11426 | ||
11427 | return ((ptr - name) < 128); | |
11428 | } | |
11429 | ||
11430 | ||
11431 | /* | |
11432 | * 'validate_user()' - Validate the user for the request. | |
11433 | */ | |
11434 | ||
11435 | static int /* O - 1 if permitted, 0 otherwise */ | |
11436 | validate_user(cupsd_job_t *job, /* I - Job */ | |
11437 | cupsd_client_t *con, /* I - Client connection */ | |
11438 | const char *owner, /* I - Owner of job/resource */ | |
11439 | char *username, /* O - Authenticated username */ | |
07623986 | 11440 | size_t userlen) /* I - Length of username */ |
ef416fc2 | 11441 | { |
ef416fc2 | 11442 | cupsd_printer_t *printer; /* Printer for job */ |
11443 | ||
11444 | ||
07623986 | 11445 | 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 | 11446 | |
11447 | /* | |
11448 | * Validate input... | |
11449 | */ | |
11450 | ||
11451 | if (!con || !owner || !username || userlen <= 0) | |
11452 | return (0); | |
11453 | ||
11454 | /* | |
11455 | * Get the best authenticated username that is available. | |
11456 | */ | |
11457 | ||
e00b005a | 11458 | strlcpy(username, get_username(con), userlen); |
ef416fc2 | 11459 | |
11460 | /* | |
11461 | * Check the username against the owner... | |
11462 | */ | |
11463 | ||
80ca4592 | 11464 | printer = cupsdFindDest(job->dest); |
ef416fc2 | 11465 | |
11466 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
11467 | con, owner) == HTTP_OK); | |
11468 | } |