]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: ipp.c 7682 2008-06-21 00:06:02Z mike $" |
ef416fc2 | 3 | * |
4 | * IPP routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
8 | * | |
9 | * This file contains Kerberos support code, copyright 2006 by | |
10 | * Jelmer Vernooij. | |
ef416fc2 | 11 | * |
12 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 13 | * property of Apple Inc. and are protected by Federal copyright |
14 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
15 | * which should have been included with this file. If this file is | |
16 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 17 | * |
18 | * Contents: | |
19 | * | |
a603edef MS |
20 | * cupsdProcessIPPRequest() - Process an incoming IPP request. |
21 | * cupsdTimeoutJob() - Timeout a job waiting on job files. | |
22 | * accept_jobs() - Accept print jobs to a printer. | |
23 | * add_class() - Add a class to the system. | |
24 | * add_file() - Add a file to a job. | |
25 | * add_job() - Add a job to a print queue. | |
26 | * add_job_state_reasons() - Add the "job-state-reasons" attribute based | |
27 | * upon the job and printer state... | |
9a4f8274 | 28 | * add_job_subscriptions() - Add any subscriptions for a job. |
a603edef MS |
29 | * add_job_uuid() - Add job-uuid attribute to a job. |
30 | * add_printer() - Add a printer to the system. | |
31 | * add_printer_state_reasons() - Add the "printer-state-reasons" attribute | |
32 | * based upon the printer state... | |
33 | * add_queued_job_count() - Add the "queued-job-count" attribute for the | |
34 | * specified printer or class. | |
35 | * apple_init_profile() - Initialize a color profile. | |
36 | * apple_register_profiles() - Register color profiles for a printer. | |
37 | * apple_unregister_profiles() - Remove color profiles for the specified | |
38 | * printer. | |
39 | * apply_printer_defaults() - Apply printer default options to a job. | |
40 | * authenticate_job() - Set job authentication info. | |
41 | * cancel_all_jobs() - Cancel all print jobs. | |
42 | * cancel_job() - Cancel a print job. | |
43 | * cancel_subscription() - Cancel a subscription. | |
44 | * check_quotas() - Check quotas for a printer and user. | |
3dfe78b3 MS |
45 | * check_rss_recipient() - Check that we do not have a duplicate RSS |
46 | * feed URI. | |
a603edef MS |
47 | * copy_attribute() - Copy a single attribute. |
48 | * copy_attrs() - Copy attributes from one request to another. | |
49 | * copy_banner() - Copy a banner file to the requests directory | |
50 | * for the specified job. | |
51 | * copy_file() - Copy a PPD file or interface script... | |
52 | * copy_model() - Copy a PPD model file, substituting default | |
53 | * values as needed... | |
54 | * copy_job_attrs() - Copy job attributes. | |
55 | * copy_printer_attrs() - Copy printer attributes. | |
56 | * copy_subscription_attrs() - Copy subscription attributes. | |
57 | * create_job() - Print a file to a printer or class. | |
58 | * create_requested_array() - Create an array for the requested-attributes. | |
59 | * create_subscription() - Create a notification subscription. | |
60 | * delete_printer() - Remove a printer or class from the system. | |
61 | * get_default() - Get the default destination. | |
62 | * get_devices() - Get the list of available devices on the | |
63 | * local system. | |
64 | * get_document() - Get a copy of a job file. | |
65 | * get_job_attrs() - Get job attributes. | |
66 | * get_jobs() - Get a list of jobs for the specified printer. | |
67 | * get_notifications() - Get events for a subscription. | |
68 | * get_ppd() - Get a named PPD from the local system. | |
69 | * get_ppds() - Get the list of PPD files on the local | |
70 | * system. | |
71 | * get_printer_attrs() - Get printer attributes. | |
72 | * get_printers() - Get a list of printers or classes. | |
73 | * get_subscription_attrs() - Get subscription attributes. | |
74 | * get_subscriptions() - Get subscriptions. | |
75 | * get_username() - Get the username associated with a request. | |
76 | * hold_job() - Hold a print job. | |
77 | * move_job() - Move a job to a new destination. | |
78 | * ppd_parse_line() - Parse a PPD default line. | |
79 | * print_job() - Print a file to a printer or class. | |
c5571a1d | 80 | * read_job_ticket() - Read a job ticket embedded in a print file. |
a603edef MS |
81 | * reject_jobs() - Reject print jobs to a printer. |
82 | * release_job() - Release a held print job. | |
83 | * renew_subscription() - Renew an existing subscription... | |
84 | * restart_job() - Restart an old print job. | |
85 | * save_auth_info() - Save authentication information for a job. | |
86 | * save_krb5_creds() - Save Kerberos credentials for the job. | |
87 | * send_document() - Send a file to a printer or class. | |
88 | * send_http_error() - Send a HTTP error back to the IPP client. | |
89 | * send_ipp_status() - Send a status back to the IPP client. | |
90 | * set_default() - Set the default destination... | |
91 | * set_job_attrs() - Set job attributes. | |
92 | * set_printer_defaults() - Set printer default options from a request. | |
93 | * start_printer() - Start a printer. | |
94 | * stop_printer() - Stop a printer. | |
95 | * url_encode_attr() - URL-encode a string attribute. | |
96 | * url_encode_string() - URL-encode a string. | |
97 | * user_allowed() - See if a user is allowed to print to a queue. | |
98 | * validate_job() - Validate printer options and destination. | |
99 | * validate_name() - Make sure the printer name only contains | |
100 | * valid chars. | |
101 | * validate_user() - Validate the user for the request. | |
ef416fc2 | 102 | */ |
103 | ||
104 | /* | |
105 | * Include necessary headers... | |
106 | */ | |
107 | ||
108 | #include "cupsd.h" | |
a603edef | 109 | #include <cups/ppd-private.h> |
ef416fc2 | 110 | |
111 | #ifdef HAVE_LIBPAPER | |
112 | # include <paper.h> | |
113 | #endif /* HAVE_LIBPAPER */ | |
114 | ||
f899b121 | 115 | #ifdef __APPLE__ |
568fa3fa MS |
116 | # include <ApplicationServices/ApplicationServices.h> |
117 | # include <CoreFoundation/CoreFoundation.h> | |
f899b121 | 118 | # ifdef HAVE_MEMBERSHIP_H |
119 | # include <membership.h> | |
120 | # endif /* HAVE_MEMBERSHIP_H */ | |
121 | # ifdef HAVE_MEMBERSHIPPRIV_H | |
122 | # include <membershipPriv.h> | |
123 | # else | |
7594b224 | 124 | extern int mbr_user_name_to_uuid(const char* name, uuid_t uu); |
125 | extern int mbr_group_name_to_uuid(const char* name, uuid_t uu); | |
126 | extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember); | |
f899b121 | 127 | # endif /* HAVE_MEMBERSHIPPRIV_H */ |
128 | #endif /* __APPLE__ */ | |
7594b224 | 129 | |
ef416fc2 | 130 | |
ef416fc2 | 131 | /* |
132 | * Local functions... | |
133 | */ | |
134 | ||
135 | static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
136 | static void add_class(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 137 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, |
138 | mime_type_t *filetype, int compression); | |
f7deaa1a | 139 | static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer, |
80ca4592 | 140 | mime_type_t *filetype); |
ef416fc2 | 141 | static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job); |
142 | static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job); | |
bd7854cb | 143 | static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job); |
ef416fc2 | 144 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
b423cd4c | 145 | static void add_printer_state_reasons(cupsd_client_t *con, |
146 | cupsd_printer_t *p); | |
ef416fc2 | 147 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); |
568fa3fa | 148 | #ifdef __APPLE__ |
a603edef MS |
149 | static void apple_init_profile(ppd_file_t *ppd, cups_array_t *languages, |
150 | CMDeviceProfileInfo *profile, unsigned id, | |
151 | const char *name, const char *text, | |
152 | const char *iccfile); | |
568fa3fa MS |
153 | static void apple_register_profiles(cupsd_printer_t *p); |
154 | static void apple_unregister_profiles(cupsd_printer_t *p); | |
155 | #endif /* __APPLE__ */ | |
b423cd4c | 156 | static void apply_printer_defaults(cupsd_printer_t *printer, |
157 | cupsd_job_t *job); | |
ef416fc2 | 158 | static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri); |
159 | static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
160 | static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
161 | static void cancel_subscription(cupsd_client_t *con, int id); | |
3dfe78b3 | 162 | static int check_rss_recipient(const char *recipient); |
ef416fc2 | 163 | static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p); |
164 | static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr, | |
165 | int quickcopy); | |
fa73b229 | 166 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, |
ef416fc2 | 167 | ipp_tag_t group, int quickcopy); |
b423cd4c | 168 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, |
169 | const char *name); | |
ef416fc2 | 170 | static int copy_file(const char *from, const char *to); |
b423cd4c | 171 | static int copy_model(cupsd_client_t *con, const char *from, |
172 | const char *to); | |
fa73b229 | 173 | static void copy_job_attrs(cupsd_client_t *con, |
174 | cupsd_job_t *job, | |
175 | cups_array_t *ra); | |
176 | static void copy_printer_attrs(cupsd_client_t *con, | |
177 | cupsd_printer_t *printer, | |
178 | cups_array_t *ra); | |
ef416fc2 | 179 | static void copy_subscription_attrs(cupsd_client_t *con, |
180 | cupsd_subscription_t *sub, | |
181 | cups_array_t *ra); | |
182 | static void create_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
183 | static cups_array_t *create_requested_array(ipp_t *request); | |
184 | static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri); | |
185 | static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
186 | static void get_default(cupsd_client_t *con); | |
187 | static void get_devices(cupsd_client_t *con); | |
2e4ff8af | 188 | static void get_document(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 189 | static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
190 | static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
bd7854cb | 191 | static void get_notifications(cupsd_client_t *con); |
b94498cf | 192 | static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 193 | static void get_ppds(cupsd_client_t *con); |
194 | static void get_printers(cupsd_client_t *con, int type); | |
195 | static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
196 | static void get_subscription_attrs(cupsd_client_t *con, int sub_id); | |
197 | static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); | |
e00b005a | 198 | static const char *get_username(cupsd_client_t *con); |
ef416fc2 | 199 | static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri); |
200 | static void move_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
ef416fc2 | 201 | static int ppd_parse_line(const char *line, char *option, int olen, |
202 | char *choice, int clen); | |
203 | static void print_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
c5571a1d | 204 | static void read_job_ticket(cupsd_client_t *con); |
ef416fc2 | 205 | static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
206 | static void release_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
207 | static void renew_subscription(cupsd_client_t *con, int sub_id); | |
208 | static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
f7deaa1a | 209 | static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job, |
210 | ipp_attribute_t *auth_info); | |
211 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
212 | static void save_krb5_creds(cupsd_client_t *con, cupsd_job_t *job); | |
213 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ | |
ef416fc2 | 214 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); |
f899b121 | 215 | static void send_http_error(cupsd_client_t *con, http_status_t status, |
216 | cupsd_printer_t *printer); | |
ef416fc2 | 217 | static void send_ipp_status(cupsd_client_t *con, ipp_status_t status, |
218 | const char *message, ...) | |
219 | # ifdef __GNUC__ | |
220 | __attribute__ ((__format__ (__printf__, 3, 4))) | |
221 | # endif /* __GNUC__ */ | |
222 | ; | |
223 | static void set_default(cupsd_client_t *con, ipp_attribute_t *uri); | |
224 | static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 225 | static void set_printer_defaults(cupsd_client_t *con, |
226 | cupsd_printer_t *printer); | |
ef416fc2 | 227 | static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
228 | static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
89d46774 | 229 | static void url_encode_attr(ipp_attribute_t *attr, char *buffer, |
230 | int bufsize); | |
b94498cf | 231 | static char *url_encode_string(const char *s, char *buffer, int bufsize); |
ef416fc2 | 232 | static int user_allowed(cupsd_printer_t *p, const char *username); |
233 | static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
234 | static int validate_name(const char *name); | |
235 | static int validate_user(cupsd_job_t *job, cupsd_client_t *con, | |
236 | const char *owner, char *username, | |
237 | int userlen); | |
238 | ||
239 | ||
240 | /* | |
09a101d6 | 241 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request. |
ef416fc2 | 242 | */ |
243 | ||
244 | int /* O - 1 on success, 0 on failure */ | |
245 | cupsdProcessIPPRequest( | |
246 | cupsd_client_t *con) /* I - Client connection */ | |
247 | { | |
248 | ipp_tag_t group; /* Current group tag */ | |
249 | ipp_attribute_t *attr; /* Current attribute */ | |
250 | ipp_attribute_t *charset; /* Character set attribute */ | |
251 | ipp_attribute_t *language; /* Language attribute */ | |
9f5eb9be | 252 | ipp_attribute_t *uri = NULL; /* Printer or job URI attribute */ |
ef416fc2 | 253 | ipp_attribute_t *username; /* requesting-user-name attr */ |
254 | int sub_id; /* Subscription ID */ | |
255 | ||
256 | ||
257 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
258 | "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x", | |
259 | con, con->http.fd, con->request->request.op.operation_id); | |
260 | ||
261 | /* | |
262 | * First build an empty response message for this request... | |
263 | */ | |
264 | ||
265 | con->response = ippNew(); | |
266 | ||
bc44d920 | 267 | con->response->request.status.version[0] = |
268 | con->request->request.op.version[0]; | |
269 | con->response->request.status.version[1] = | |
270 | con->request->request.op.version[1]; | |
271 | con->response->request.status.request_id = | |
272 | con->request->request.op.request_id; | |
ef416fc2 | 273 | |
274 | /* | |
275 | * Then validate the request header and required attributes... | |
276 | */ | |
f7deaa1a | 277 | |
ef416fc2 | 278 | if (con->request->request.any.version[0] != 1) |
279 | { | |
280 | /* | |
281 | * Return an error, since we only support IPP 1.x. | |
282 | */ | |
283 | ||
284 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
285 | "%04X %s Bad request version number %d.%d", | |
286 | IPP_VERSION_NOT_SUPPORTED, con->http.hostname, | |
287 | con->request->request.any.version[0], | |
288 | con->request->request.any.version[1]); | |
289 | ||
290 | send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED, | |
291 | _("Bad request version number %d.%d!"), | |
292 | con->request->request.any.version[0], | |
293 | con->request->request.any.version[1]); | |
f7deaa1a | 294 | } |
fa73b229 | 295 | else if (!con->request->attrs) |
ef416fc2 | 296 | { |
297 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
298 | "%04X %s No attributes in request", | |
299 | IPP_BAD_REQUEST, con->http.hostname); | |
300 | ||
301 | send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!")); | |
302 | } | |
303 | else | |
304 | { | |
305 | /* | |
306 | * Make sure that the attributes are provided in the correct order and | |
307 | * don't repeat groups... | |
308 | */ | |
309 | ||
310 | for (attr = con->request->attrs, group = attr->group_tag; | |
fa73b229 | 311 | attr; |
ef416fc2 | 312 | attr = attr->next) |
313 | if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO) | |
314 | { | |
315 | /* | |
316 | * Out of order; return an error... | |
317 | */ | |
318 | ||
319 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
320 | "%04X %s Attribute groups are out of order", | |
321 | IPP_BAD_REQUEST, con->http.hostname); | |
322 | ||
323 | send_ipp_status(con, IPP_BAD_REQUEST, | |
324 | _("Attribute groups are out of order (%x < %x)!"), | |
325 | attr->group_tag, group); | |
326 | break; | |
327 | } | |
328 | else | |
329 | group = attr->group_tag; | |
330 | ||
fa73b229 | 331 | if (!attr) |
ef416fc2 | 332 | { |
333 | /* | |
334 | * Then make sure that the first three attributes are: | |
335 | * | |
336 | * attributes-charset | |
337 | * attributes-natural-language | |
338 | * printer-uri/job-uri | |
339 | */ | |
340 | ||
341 | attr = con->request->attrs; | |
342 | if (attr && !strcmp(attr->name, "attributes-charset") && | |
fa73b229 | 343 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET) |
ef416fc2 | 344 | charset = attr; |
345 | else | |
346 | charset = NULL; | |
347 | ||
348 | if (attr) | |
349 | attr = attr->next; | |
350 | ||
351 | if (attr && !strcmp(attr->name, "attributes-natural-language") && | |
fa73b229 | 352 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE) |
ef416fc2 | 353 | language = attr; |
354 | else | |
355 | language = NULL; | |
356 | ||
fa73b229 | 357 | if ((attr = ippFindAttribute(con->request, "printer-uri", |
358 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 359 | uri = attr; |
fa73b229 | 360 | else if ((attr = ippFindAttribute(con->request, "job-uri", |
361 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 362 | uri = attr; |
b94498cf | 363 | else if (con->request->request.op.operation_id == CUPS_GET_PPD) |
364 | uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME); | |
ef416fc2 | 365 | else |
366 | uri = NULL; | |
367 | ||
368 | if (charset) | |
369 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
bc44d920 | 370 | "attributes-charset", NULL, |
371 | charset->values[0].string.text); | |
ef416fc2 | 372 | else |
373 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
374 | "attributes-charset", NULL, DefaultCharset); | |
375 | ||
376 | if (language) | |
377 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
378 | "attributes-natural-language", NULL, | |
379 | language->values[0].string.text); | |
380 | else | |
381 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
382 | "attributes-natural-language", NULL, DefaultLanguage); | |
383 | ||
2e4ff8af MS |
384 | if (charset && |
385 | strcasecmp(charset->values[0].string.text, "us-ascii") && | |
386 | strcasecmp(charset->values[0].string.text, "utf-8")) | |
387 | { | |
388 | /* | |
389 | * Bad character set... | |
390 | */ | |
391 | ||
392 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"!", | |
393 | charset->values[0].string.text); | |
394 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
395 | "%04X %s Unsupported attributes-charset value \"%s\"", | |
396 | IPP_CHARSET, con->http.hostname, | |
397 | charset->values[0].string.text); | |
398 | send_ipp_status(con, IPP_BAD_REQUEST, | |
399 | _("Unsupported character set \"%s\"!"), | |
400 | charset->values[0].string.text); | |
401 | } | |
402 | else if (!charset || !language || | |
403 | (!uri && | |
404 | con->request->request.op.operation_id != CUPS_GET_DEFAULT && | |
405 | con->request->request.op.operation_id != CUPS_GET_PRINTERS && | |
406 | con->request->request.op.operation_id != CUPS_GET_CLASSES && | |
407 | con->request->request.op.operation_id != CUPS_GET_DEVICES && | |
408 | con->request->request.op.operation_id != CUPS_GET_PPDS)) | |
ef416fc2 | 409 | { |
410 | /* | |
411 | * Return an error, since attributes-charset, | |
412 | * attributes-natural-language, and printer-uri/job-uri are required | |
413 | * for all operations. | |
414 | */ | |
415 | ||
416 | if (!charset) | |
417 | { | |
418 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
419 | "Missing attributes-charset attribute!"); | |
420 | ||
421 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
422 | "%04X %s Missing attributes-charset attribute", | |
423 | IPP_BAD_REQUEST, con->http.hostname); | |
424 | } | |
425 | ||
426 | if (!language) | |
427 | { | |
428 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
429 | "Missing attributes-natural-language attribute!"); | |
430 | ||
431 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
432 | "%04X %s Missing attributes-natural-language attribute", | |
433 | IPP_BAD_REQUEST, con->http.hostname); | |
434 | } | |
435 | ||
436 | if (!uri) | |
437 | { | |
438 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b94498cf | 439 | "Missing printer-uri, job-uri, or ppd-name " |
440 | "attribute!"); | |
ef416fc2 | 441 | |
442 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
b94498cf | 443 | "%04X %s Missing printer-uri, job-uri, or ppd-name " |
444 | "attribute", IPP_BAD_REQUEST, con->http.hostname); | |
ef416fc2 | 445 | } |
446 | ||
447 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
448 | ||
fa73b229 | 449 | for (attr = con->request->attrs; attr; attr = attr->next) |
f7deaa1a | 450 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
ef416fc2 | 451 | "attr \"%s\": group_tag = %x, value_tag = %x", |
452 | attr->name ? attr->name : "(null)", attr->group_tag, | |
453 | attr->value_tag); | |
454 | ||
455 | cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes..."); | |
456 | ||
457 | send_ipp_status(con, IPP_BAD_REQUEST, | |
458 | _("Missing required attributes!")); | |
459 | } | |
460 | else | |
461 | { | |
462 | /* | |
463 | * OK, all the checks pass so far; make sure requesting-user-name is | |
464 | * not "root" from a remote host... | |
465 | */ | |
466 | ||
467 | if ((username = ippFindAttribute(con->request, "requesting-user-name", | |
468 | IPP_TAG_NAME)) != NULL) | |
469 | { | |
470 | /* | |
471 | * Check for root user... | |
472 | */ | |
473 | ||
474 | if (!strcmp(username->values[0].string.text, "root") && | |
475 | strcasecmp(con->http.hostname, "localhost") && | |
476 | strcmp(con->username, "root")) | |
477 | { | |
478 | /* | |
479 | * Remote unauthenticated user masquerading as local root... | |
480 | */ | |
481 | ||
757d2cad | 482 | _cupsStrFree(username->values[0].string.text); |
483 | username->values[0].string.text = _cupsStrAlloc(RemoteRoot); | |
ef416fc2 | 484 | } |
485 | } | |
486 | ||
487 | if ((attr = ippFindAttribute(con->request, "notify-subscription-id", | |
488 | IPP_TAG_INTEGER)) != NULL) | |
489 | sub_id = attr->values[0].integer; | |
490 | else | |
491 | sub_id = 0; | |
492 | ||
493 | /* | |
494 | * Then try processing the operation... | |
495 | */ | |
496 | ||
497 | if (uri) | |
bd7854cb | 498 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", |
499 | ippOpString(con->request->request.op.operation_id), | |
500 | uri->values[0].string.text); | |
501 | else | |
502 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", | |
503 | ippOpString(con->request->request.op.operation_id)); | |
ef416fc2 | 504 | |
505 | switch (con->request->request.op.operation_id) | |
506 | { | |
507 | case IPP_PRINT_JOB : | |
508 | print_job(con, uri); | |
509 | break; | |
510 | ||
511 | case IPP_VALIDATE_JOB : | |
512 | validate_job(con, uri); | |
513 | break; | |
514 | ||
515 | case IPP_CREATE_JOB : | |
516 | create_job(con, uri); | |
517 | break; | |
518 | ||
519 | case IPP_SEND_DOCUMENT : | |
520 | send_document(con, uri); | |
521 | break; | |
522 | ||
523 | case IPP_CANCEL_JOB : | |
524 | cancel_job(con, uri); | |
525 | break; | |
526 | ||
527 | case IPP_GET_JOB_ATTRIBUTES : | |
528 | get_job_attrs(con, uri); | |
529 | break; | |
530 | ||
531 | case IPP_GET_JOBS : | |
532 | get_jobs(con, uri); | |
533 | break; | |
534 | ||
535 | case IPP_GET_PRINTER_ATTRIBUTES : | |
536 | get_printer_attrs(con, uri); | |
537 | break; | |
538 | ||
539 | case IPP_HOLD_JOB : | |
540 | hold_job(con, uri); | |
541 | break; | |
542 | ||
543 | case IPP_RELEASE_JOB : | |
544 | release_job(con, uri); | |
545 | break; | |
546 | ||
547 | case IPP_RESTART_JOB : | |
548 | restart_job(con, uri); | |
549 | break; | |
550 | ||
551 | case IPP_PAUSE_PRINTER : | |
552 | stop_printer(con, uri); | |
553 | break; | |
554 | ||
555 | case IPP_RESUME_PRINTER : | |
556 | start_printer(con, uri); | |
557 | break; | |
558 | ||
559 | case IPP_PURGE_JOBS : | |
560 | cancel_all_jobs(con, uri); | |
561 | break; | |
562 | ||
563 | case IPP_SET_JOB_ATTRIBUTES : | |
564 | set_job_attrs(con, uri); | |
565 | break; | |
566 | ||
567 | case CUPS_GET_DEFAULT : | |
568 | get_default(con); | |
569 | break; | |
570 | ||
571 | case CUPS_GET_PRINTERS : | |
572 | get_printers(con, 0); | |
573 | break; | |
574 | ||
575 | case CUPS_GET_CLASSES : | |
576 | get_printers(con, CUPS_PRINTER_CLASS); | |
577 | break; | |
578 | ||
579 | case CUPS_ADD_PRINTER : | |
580 | add_printer(con, uri); | |
581 | break; | |
582 | ||
583 | case CUPS_DELETE_PRINTER : | |
584 | delete_printer(con, uri); | |
585 | break; | |
586 | ||
587 | case CUPS_ADD_CLASS : | |
588 | add_class(con, uri); | |
589 | break; | |
590 | ||
591 | case CUPS_DELETE_CLASS : | |
592 | delete_printer(con, uri); | |
593 | break; | |
594 | ||
595 | case CUPS_ACCEPT_JOBS : | |
596 | case IPP_ENABLE_PRINTER : | |
597 | accept_jobs(con, uri); | |
598 | break; | |
599 | ||
600 | case CUPS_REJECT_JOBS : | |
601 | case IPP_DISABLE_PRINTER : | |
602 | reject_jobs(con, uri); | |
603 | break; | |
604 | ||
605 | case CUPS_SET_DEFAULT : | |
606 | set_default(con, uri); | |
607 | break; | |
608 | ||
609 | case CUPS_GET_DEVICES : | |
610 | get_devices(con); | |
611 | break; | |
612 | ||
2e4ff8af MS |
613 | case CUPS_GET_DOCUMENT : |
614 | get_document(con, uri); | |
615 | break; | |
616 | ||
b94498cf | 617 | case CUPS_GET_PPD : |
618 | get_ppd(con, uri); | |
619 | break; | |
620 | ||
ef416fc2 | 621 | case CUPS_GET_PPDS : |
622 | get_ppds(con); | |
623 | break; | |
624 | ||
625 | case CUPS_MOVE_JOB : | |
626 | move_job(con, uri); | |
627 | break; | |
628 | ||
629 | case CUPS_AUTHENTICATE_JOB : | |
630 | authenticate_job(con, uri); | |
631 | break; | |
632 | ||
633 | case IPP_CREATE_PRINTER_SUBSCRIPTION : | |
634 | case IPP_CREATE_JOB_SUBSCRIPTION : | |
635 | create_subscription(con, uri); | |
636 | break; | |
637 | ||
638 | case IPP_GET_SUBSCRIPTION_ATTRIBUTES : | |
639 | get_subscription_attrs(con, sub_id); | |
640 | break; | |
641 | ||
642 | case IPP_GET_SUBSCRIPTIONS : | |
643 | get_subscriptions(con, uri); | |
644 | break; | |
645 | ||
646 | case IPP_RENEW_SUBSCRIPTION : | |
647 | renew_subscription(con, sub_id); | |
648 | break; | |
649 | ||
650 | case IPP_CANCEL_SUBSCRIPTION : | |
651 | cancel_subscription(con, sub_id); | |
652 | break; | |
653 | ||
654 | case IPP_GET_NOTIFICATIONS : | |
bd7854cb | 655 | get_notifications(con); |
ef416fc2 | 656 | break; |
657 | ||
658 | default : | |
659 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
660 | "%04X %s Operation %04X (%s) not supported", | |
661 | IPP_OPERATION_NOT_SUPPORTED, con->http.hostname, | |
662 | con->request->request.op.operation_id, | |
663 | ippOpString(con->request->request.op.operation_id)); | |
664 | ||
665 | send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED, | |
666 | _("%s not supported!"), | |
bc44d920 | 667 | ippOpString( |
668 | con->request->request.op.operation_id)); | |
ef416fc2 | 669 | break; |
670 | } | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | if (con->response) | |
676 | { | |
677 | /* | |
678 | * Sending data from the scheduler... | |
679 | */ | |
680 | ||
ee571f26 | 681 | cupsdLogMessage(con->response->request.status.status_code |
c5571a1d MS |
682 | >= IPP_BAD_REQUEST && |
683 | con->response->request.status.status_code | |
684 | != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG, | |
9f5eb9be | 685 | "Returning IPP %s for %s (%s) from %s", |
ee571f26 MS |
686 | ippErrorString(con->response->request.status.status_code), |
687 | ippOpString(con->request->request.op.operation_id), | |
9f5eb9be | 688 | uri ? uri->values[0].string.text : "no URI", |
ee571f26 | 689 | con->http.hostname); |
ef416fc2 | 690 | |
5bd77a73 | 691 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE)) |
ef416fc2 | 692 | { |
f301802f | 693 | #ifdef CUPSD_USE_CHUNKING |
694 | /* | |
695 | * Because older versions of CUPS (1.1.17 and older) and some IPP | |
d09495fa | 696 | * clients do not implement chunking properly, we cannot use |
f301802f | 697 | * chunking by default. This may become the default in future |
698 | * CUPS releases, or we might add a configuration directive for | |
699 | * it. | |
700 | */ | |
701 | ||
ef416fc2 | 702 | if (con->http.version == HTTP_1_1) |
703 | { | |
07725fee | 704 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n") < 0) |
705 | return (0); | |
706 | ||
707 | if (cupsdFlushHeader(con) < 0) | |
708 | return (0); | |
d09495fa | 709 | |
710 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
ef416fc2 | 711 | } |
712 | else | |
f301802f | 713 | #endif /* CUPSD_USE_CHUNKING */ |
ef416fc2 | 714 | { |
d09495fa | 715 | size_t length; /* Length of response */ |
ef416fc2 | 716 | |
d09495fa | 717 | |
718 | length = ippLength(con->response); | |
ef416fc2 | 719 | |
b94498cf | 720 | if (con->file >= 0 && !con->pipe_pid) |
721 | { | |
722 | struct stat fileinfo; /* File information */ | |
723 | ||
724 | ||
725 | if (!fstat(con->file, &fileinfo)) | |
726 | length += fileinfo.st_size; | |
727 | } | |
728 | ||
07725fee | 729 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n", |
730 | CUPS_LLCAST length) < 0) | |
731 | return (0); | |
732 | ||
733 | if (cupsdFlushHeader(con) < 0) | |
734 | return (0); | |
d09495fa | 735 | |
736 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
737 | con->http.data_remaining = length; | |
b94498cf | 738 | |
739 | if (con->http.data_remaining <= INT_MAX) | |
740 | con->http._data_remaining = con->http.data_remaining; | |
741 | else | |
742 | con->http._data_remaining = INT_MAX; | |
ef416fc2 | 743 | } |
744 | ||
f7deaa1a | 745 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, |
746 | (cupsd_selfunc_t)cupsdWriteClient, con); | |
ef416fc2 | 747 | |
748 | /* | |
749 | * Tell the caller the response header was sent successfully... | |
750 | */ | |
751 | ||
752 | return (1); | |
753 | } | |
754 | else | |
755 | { | |
756 | /* | |
757 | * Tell the caller the response header could not be sent... | |
758 | */ | |
759 | ||
760 | return (0); | |
761 | } | |
762 | } | |
763 | else | |
764 | { | |
765 | /* | |
766 | * Sending data from a subprocess like cups-deviced; tell the caller | |
767 | * everything is A-OK so far... | |
768 | */ | |
769 | ||
770 | return (1); | |
771 | } | |
772 | } | |
773 | ||
774 | ||
09a101d6 | 775 | /* |
776 | * 'cupsdTimeoutJob()' - Timeout a job waiting on job files. | |
777 | */ | |
778 | ||
91c84a35 | 779 | int /* O - 0 on success, -1 on error */ |
09a101d6 | 780 | cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */ |
781 | { | |
782 | cupsd_printer_t *printer; /* Destination printer or class */ | |
783 | ipp_attribute_t *attr; /* job-sheets attribute */ | |
784 | int kbytes; /* Kilobytes in banner */ | |
785 | ||
786 | ||
787 | job->pending_timeout = 0; | |
788 | ||
789 | /* | |
790 | * See if we need to add the ending sheet... | |
791 | */ | |
792 | ||
793 | printer = cupsdFindDest(job->dest); | |
794 | attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); | |
795 | ||
796 | if (printer && | |
797 | !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && | |
798 | attr && attr->num_values > 1) | |
799 | { | |
800 | /* | |
801 | * Yes... | |
802 | */ | |
803 | ||
75bd9771 MS |
804 | cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".", |
805 | attr->values[1].string.text); | |
09a101d6 | 806 | |
91c84a35 MS |
807 | if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0) |
808 | return (-1); | |
09a101d6 | 809 | |
810 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
811 | } | |
91c84a35 MS |
812 | |
813 | return (0); | |
09a101d6 | 814 | } |
815 | ||
816 | ||
ef416fc2 | 817 | /* |
818 | * 'accept_jobs()' - Accept print jobs to a printer. | |
819 | */ | |
820 | ||
821 | static void | |
822 | accept_jobs(cupsd_client_t *con, /* I - Client connection */ | |
823 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
824 | { | |
825 | http_status_t status; /* Policy status */ | |
bc44d920 | 826 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 827 | cupsd_printer_t *printer; /* Printer data */ |
828 | ||
829 | ||
830 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con, | |
831 | con->http.fd, uri->values[0].string.text); | |
832 | ||
833 | /* | |
834 | * Is the destination valid? | |
835 | */ | |
836 | ||
f7deaa1a | 837 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 838 | { |
839 | /* | |
840 | * Bad URI... | |
841 | */ | |
842 | ||
843 | send_ipp_status(con, IPP_NOT_FOUND, | |
844 | _("The printer or class was not found.")); | |
845 | return; | |
846 | } | |
847 | ||
848 | /* | |
849 | * Check policy... | |
850 | */ | |
851 | ||
852 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
853 | { | |
f899b121 | 854 | send_http_error(con, status, printer); |
ef416fc2 | 855 | return; |
856 | } | |
857 | ||
858 | /* | |
859 | * Accept jobs sent to the printer... | |
860 | */ | |
861 | ||
862 | printer->accepting = 1; | |
863 | printer->state_message[0] = '\0'; | |
864 | ||
865 | cupsdAddPrinterHistory(printer); | |
866 | ||
867 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 868 | { |
3dfe78b3 | 869 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
f7deaa1a | 870 | |
871 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").", | |
872 | printer->name, get_username(con)); | |
873 | } | |
ef416fc2 | 874 | else |
f7deaa1a | 875 | { |
3dfe78b3 | 876 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 877 | |
bc44d920 | 878 | cupsdLogMessage(CUPSD_LOG_INFO, |
879 | "Printer \"%s\" now accepting jobs (\"%s\").", | |
f7deaa1a | 880 | printer->name, get_username(con)); |
881 | } | |
ef416fc2 | 882 | |
883 | /* | |
884 | * Everything was ok, so return OK status... | |
885 | */ | |
886 | ||
887 | con->response->request.status.status_code = IPP_OK; | |
888 | } | |
889 | ||
890 | ||
891 | /* | |
892 | * 'add_class()' - Add a class to the system. | |
893 | */ | |
894 | ||
895 | static void | |
896 | add_class(cupsd_client_t *con, /* I - Client connection */ | |
897 | ipp_attribute_t *uri) /* I - URI of class */ | |
898 | { | |
899 | http_status_t status; /* Policy status */ | |
900 | int i; /* Looping var */ | |
901 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
902 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
903 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
904 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
905 | int port; /* Port portion of URI */ | |
906 | cupsd_printer_t *pclass, /* Class */ | |
907 | *member; /* Member printer/class */ | |
908 | cups_ptype_t dtype; /* Destination type */ | |
ef416fc2 | 909 | ipp_attribute_t *attr; /* Printer attribute */ |
910 | int modify; /* Non-zero if we just modified */ | |
b423cd4c | 911 | char newname[IPP_MAX_NAME]; /* New class name */ |
e00b005a | 912 | int need_restart_job; /* Need to restart job? */ |
ef416fc2 | 913 | |
914 | ||
915 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con, | |
916 | con->http.fd, uri->values[0].string.text); | |
917 | ||
918 | /* | |
919 | * Do we have a valid URI? | |
920 | */ | |
921 | ||
a4d04587 | 922 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
923 | sizeof(method), username, sizeof(username), host, | |
924 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 925 | |
926 | ||
fa73b229 | 927 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) |
ef416fc2 | 928 | { |
929 | /* | |
930 | * No, return an error... | |
931 | */ | |
932 | ||
933 | send_ipp_status(con, IPP_BAD_REQUEST, | |
934 | _("The printer-uri must be of the form " | |
935 | "\"ipp://HOSTNAME/classes/CLASSNAME\".")); | |
936 | return; | |
937 | } | |
938 | ||
939 | /* | |
940 | * Do we have a valid printer name? | |
941 | */ | |
942 | ||
943 | if (!validate_name(resource + 9)) | |
944 | { | |
945 | /* | |
946 | * No, return an error... | |
947 | */ | |
948 | ||
949 | send_ipp_status(con, IPP_BAD_REQUEST, | |
950 | _("The printer-uri \"%s\" contains invalid characters."), | |
951 | uri->values[0].string.text); | |
952 | return; | |
953 | } | |
954 | ||
ef416fc2 | 955 | /* |
956 | * See if the class already exists; if not, create a new class... | |
957 | */ | |
958 | ||
959 | if ((pclass = cupsdFindClass(resource + 9)) == NULL) | |
960 | { | |
961 | /* | |
962 | * Class doesn't exist; see if we have a printer of the same name... | |
963 | */ | |
964 | ||
965 | if ((pclass = cupsdFindPrinter(resource + 9)) != NULL && | |
bc44d920 | 966 | !(pclass->type & CUPS_PRINTER_DISCOVERED)) |
ef416fc2 | 967 | { |
968 | /* | |
969 | * Yes, return an error... | |
970 | */ | |
971 | ||
972 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
973 | _("A printer named \"%s\" already exists!"), | |
974 | resource + 9); | |
975 | return; | |
976 | } | |
977 | ||
978 | /* | |
2e4ff8af | 979 | * No, check the default policy and then add the class... |
ef416fc2 | 980 | */ |
981 | ||
2e4ff8af MS |
982 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
983 | { | |
984 | send_http_error(con, status, NULL); | |
985 | return; | |
986 | } | |
987 | ||
ef416fc2 | 988 | pclass = cupsdAddClass(resource + 9); |
989 | modify = 0; | |
990 | } | |
991 | else if (pclass->type & CUPS_PRINTER_IMPLICIT) | |
992 | { | |
993 | /* | |
2fb76298 | 994 | * Check the default policy, then rename the implicit class to "AnyClass" |
2e4ff8af | 995 | * or remove it... |
ef416fc2 | 996 | */ |
997 | ||
2e4ff8af MS |
998 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
999 | { | |
1000 | send_http_error(con, status, NULL); | |
1001 | return; | |
1002 | } | |
1003 | ||
ef416fc2 | 1004 | if (ImplicitAnyClasses) |
1005 | { | |
b423cd4c | 1006 | snprintf(newname, sizeof(newname), "Any%s", resource + 9); |
1007 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 1008 | } |
1009 | else | |
1010 | cupsdDeletePrinter(pclass, 1); | |
1011 | ||
1012 | /* | |
1013 | * Add the class as a new local class... | |
1014 | */ | |
1015 | ||
1016 | pclass = cupsdAddClass(resource + 9); | |
1017 | modify = 0; | |
1018 | } | |
bc44d920 | 1019 | else if (pclass->type & CUPS_PRINTER_DISCOVERED) |
ef416fc2 | 1020 | { |
1021 | /* | |
2e4ff8af | 1022 | * Check the default policy, then rename the remote class to "Class"... |
ef416fc2 | 1023 | */ |
1024 | ||
2e4ff8af MS |
1025 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
1026 | { | |
1027 | send_http_error(con, status, NULL); | |
1028 | return; | |
1029 | } | |
1030 | ||
b423cd4c | 1031 | snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname); |
1032 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 1033 | |
1034 | /* | |
1035 | * Add the class as a new local class... | |
1036 | */ | |
1037 | ||
1038 | pclass = cupsdAddClass(resource + 9); | |
1039 | modify = 0; | |
1040 | } | |
2e4ff8af MS |
1041 | else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con, |
1042 | NULL)) != HTTP_OK) | |
1043 | { | |
2fb76298 | 1044 | send_http_error(con, status, pclass); |
2e4ff8af MS |
1045 | return; |
1046 | } | |
ef416fc2 | 1047 | else |
1048 | modify = 1; | |
1049 | ||
1050 | /* | |
1051 | * Look for attributes and copy them over as needed... | |
1052 | */ | |
1053 | ||
e00b005a | 1054 | need_restart_job = 0; |
1055 | ||
fa73b229 | 1056 | if ((attr = ippFindAttribute(con->request, "printer-location", |
1057 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1058 | cupsdSetString(&pclass->location, attr->values[0].string.text); |
1059 | ||
fa73b229 | 1060 | if ((attr = ippFindAttribute(con->request, "printer-info", |
1061 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1062 | cupsdSetString(&pclass->info, attr->values[0].string.text); |
1063 | ||
fa73b229 | 1064 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
1065 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 1066 | { |
bc44d920 | 1067 | cupsdLogMessage(CUPSD_LOG_INFO, |
1068 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
1069 | pclass->name, attr->values[0].boolean, pclass->accepting); | |
ef416fc2 | 1070 | |
1071 | pclass->accepting = attr->values[0].boolean; | |
1072 | cupsdAddPrinterHistory(pclass); | |
1073 | } | |
1074 | ||
fa73b229 | 1075 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
1076 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 1077 | { |
1078 | if (pclass->shared && !attr->values[0].boolean) | |
f7deaa1a | 1079 | cupsdDeregisterPrinter(pclass, 1); |
ef416fc2 | 1080 | |
1081 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1082 | "Setting %s printer-is-shared to %d (was %d.)", | |
1083 | pclass->name, attr->values[0].boolean, pclass->shared); | |
1084 | ||
1085 | pclass->shared = attr->values[0].boolean; | |
1086 | } | |
1087 | ||
fa73b229 | 1088 | if ((attr = ippFindAttribute(con->request, "printer-state", |
1089 | IPP_TAG_ENUM)) != NULL) | |
ef416fc2 | 1090 | { |
1091 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
1092 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
1093 | { | |
1094 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1095 | _("Attempt to set %s printer-state to bad value %d!"), | |
1096 | pclass->name, attr->values[0].integer); | |
1097 | return; | |
1098 | } | |
1099 | ||
bc44d920 | 1100 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
1101 | pclass->name, attr->values[0].integer, pclass->state); | |
ef416fc2 | 1102 | |
1103 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
1104 | cupsdStopPrinter(pclass, 0); | |
1105 | else | |
e00b005a | 1106 | { |
ef416fc2 | 1107 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); |
e00b005a | 1108 | need_restart_job = 1; |
1109 | } | |
ef416fc2 | 1110 | } |
fa73b229 | 1111 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
1112 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1113 | { |
1114 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
1115 | sizeof(pclass->state_message)); | |
1116 | cupsdAddPrinterHistory(pclass); | |
1117 | } | |
fa73b229 | 1118 | if ((attr = ippFindAttribute(con->request, "member-uris", |
1119 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 1120 | { |
1121 | /* | |
1122 | * Clear the printer array as needed... | |
1123 | */ | |
1124 | ||
e00b005a | 1125 | need_restart_job = 1; |
1126 | ||
ef416fc2 | 1127 | if (pclass->num_printers > 0) |
1128 | { | |
1129 | free(pclass->printers); | |
1130 | pclass->num_printers = 0; | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * Add each printer or class that is listed... | |
1135 | */ | |
1136 | ||
1137 | for (i = 0; i < attr->num_values; i ++) | |
1138 | { | |
1139 | /* | |
1140 | * Search for the printer or class URI... | |
1141 | */ | |
1142 | ||
f7deaa1a | 1143 | if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member)) |
ef416fc2 | 1144 | { |
1145 | /* | |
1146 | * Bad URI... | |
1147 | */ | |
1148 | ||
1149 | send_ipp_status(con, IPP_NOT_FOUND, | |
1150 | _("The printer or class was not found.")); | |
1151 | return; | |
1152 | } | |
1153 | ||
1154 | /* | |
1155 | * Add it to the class... | |
1156 | */ | |
1157 | ||
1158 | cupsdAddPrinterToClass(pclass, member); | |
1159 | } | |
1160 | } | |
1161 | ||
b423cd4c | 1162 | set_printer_defaults(con, pclass); |
1163 | ||
09a101d6 | 1164 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
1165 | IPP_TAG_KEYWORD)) != NULL) | |
1166 | cupsdSetAuthInfoRequired(pclass, NULL, attr); | |
1167 | ||
ef416fc2 | 1168 | /* |
1169 | * Update the printer class attributes and return... | |
1170 | */ | |
1171 | ||
1172 | cupsdSetPrinterAttrs(pclass); | |
3dfe78b3 | 1173 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
e00b005a | 1174 | |
1175 | if (need_restart_job && pclass->job) | |
1176 | { | |
1177 | cupsd_job_t *job; | |
1178 | ||
1179 | /* | |
1180 | * Stop the current job and then restart it below... | |
1181 | */ | |
1182 | ||
1183 | job = (cupsd_job_t *)pclass->job; | |
1184 | ||
1185 | cupsdStopJob(job, 1); | |
bd7854cb | 1186 | |
e00b005a | 1187 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 1188 | job->state_value = IPP_JOB_PENDING; |
e00b005a | 1189 | } |
1190 | ||
1191 | if (need_restart_job) | |
1192 | cupsdCheckJobs(); | |
ef416fc2 | 1193 | |
3dfe78b3 | 1194 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 1195 | |
1196 | if (modify) | |
1197 | { | |
1198 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL, | |
1199 | "Class \"%s\" modified by \"%s\".", pclass->name, | |
e00b005a | 1200 | get_username(con)); |
ef416fc2 | 1201 | |
1202 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
e00b005a | 1203 | pclass->name, get_username(con)); |
ef416fc2 | 1204 | } |
1205 | else | |
1206 | { | |
1207 | cupsdAddPrinterHistory(pclass); | |
1208 | ||
1209 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL, | |
1210 | "New class \"%s\" added by \"%s\".", pclass->name, | |
e00b005a | 1211 | get_username(con)); |
ef416fc2 | 1212 | |
1213 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
e00b005a | 1214 | pclass->name, get_username(con)); |
ef416fc2 | 1215 | } |
1216 | ||
1217 | con->response->request.status.status_code = IPP_OK; | |
1218 | } | |
1219 | ||
1220 | ||
1221 | /* | |
1222 | * 'add_file()' - Add a file to a job. | |
1223 | */ | |
1224 | ||
1225 | static int /* O - 0 on success, -1 on error */ | |
1226 | add_file(cupsd_client_t *con, /* I - Connection to client */ | |
1227 | cupsd_job_t *job, /* I - Job to add to */ | |
1228 | mime_type_t *filetype, /* I - Type of file */ | |
1229 | int compression) /* I - Compression */ | |
1230 | { | |
1231 | mime_type_t **filetypes; /* New filetypes array... */ | |
1232 | int *compressions; /* New compressions array... */ | |
1233 | ||
1234 | ||
1235 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
bc44d920 | 1236 | "add_file(con=%p[%d], job=%d, filetype=%s/%s, " |
1237 | "compression=%d)", con, con ? con->http.fd : -1, job->id, | |
1238 | filetype->super, filetype->type, compression); | |
ef416fc2 | 1239 | |
1240 | /* | |
1241 | * Add the file to the job... | |
1242 | */ | |
1243 | ||
1244 | if (job->num_files == 0) | |
1245 | { | |
1246 | compressions = (int *)malloc(sizeof(int)); | |
1247 | filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *)); | |
1248 | } | |
1249 | else | |
1250 | { | |
1251 | compressions = (int *)realloc(job->compressions, | |
1252 | (job->num_files + 1) * sizeof(int)); | |
1253 | filetypes = (mime_type_t **)realloc(job->filetypes, | |
1254 | (job->num_files + 1) * | |
1255 | sizeof(mime_type_t *)); | |
1256 | } | |
1257 | ||
fa73b229 | 1258 | if (!compressions || !filetypes) |
ef416fc2 | 1259 | { |
07725fee | 1260 | cupsdCancelJob(job, 1, IPP_JOB_ABORTED); |
ef416fc2 | 1261 | |
09a101d6 | 1262 | if (con) |
1263 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1264 | _("Unable to allocate memory for file types!")); | |
1265 | ||
ef416fc2 | 1266 | return (-1); |
1267 | } | |
1268 | ||
1269 | job->compressions = compressions; | |
1270 | job->compressions[job->num_files] = compression; | |
1271 | job->filetypes = filetypes; | |
1272 | job->filetypes[job->num_files] = filetype; | |
1273 | ||
1274 | job->num_files ++; | |
1275 | ||
3dfe78b3 MS |
1276 | job->dirty = 1; |
1277 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
1278 | ||
ef416fc2 | 1279 | return (0); |
1280 | } | |
1281 | ||
1282 | ||
1283 | /* | |
b423cd4c | 1284 | * 'add_job()' - Add a job to a print queue. |
ef416fc2 | 1285 | */ |
1286 | ||
b423cd4c | 1287 | static cupsd_job_t * /* O - Job object */ |
1288 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
f7deaa1a | 1289 | cupsd_printer_t *printer, /* I - Destination printer */ |
80ca4592 | 1290 | mime_type_t *filetype) /* I - First print file type, if any */ |
ef416fc2 | 1291 | { |
b423cd4c | 1292 | http_status_t status; /* Policy status */ |
f7deaa1a | 1293 | ipp_attribute_t *attr, /* Current attribute */ |
1294 | *auth_info; /* auth-info attribute */ | |
b423cd4c | 1295 | const char *val; /* Default option value */ |
1296 | int priority; /* Job priority */ | |
1297 | char *title; /* Job name/title */ | |
1298 | cupsd_job_t *job; /* Current job */ | |
f7deaa1a | 1299 | char job_uri[HTTP_MAX_URI]; /* Job URI */ |
b423cd4c | 1300 | int kbytes; /* Size of print file */ |
1301 | int i; /* Looping var */ | |
1302 | int lowerpagerange; /* Page range bound */ | |
ef416fc2 | 1303 | |
1304 | ||
f7deaa1a | 1305 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))", |
1306 | con, con->http.fd, printer, printer->name, | |
0a682745 MS |
1307 | filetype, filetype ? filetype->super : "none", |
1308 | filetype ? filetype->type : "none"); | |
ef416fc2 | 1309 | |
b423cd4c | 1310 | /* |
1311 | * Check remote printing to non-shared printer... | |
1312 | */ | |
ef416fc2 | 1313 | |
b423cd4c | 1314 | if (!printer->shared && |
1315 | strcasecmp(con->http.hostname, "localhost") && | |
1316 | strcasecmp(con->http.hostname, ServerName)) | |
1317 | { | |
1318 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
1319 | _("The printer or class is not shared!")); | |
1320 | return (NULL); | |
1321 | } | |
ef416fc2 | 1322 | |
b423cd4c | 1323 | /* |
1324 | * Check policy... | |
1325 | */ | |
ef416fc2 | 1326 | |
f899b121 | 1327 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
7594b224 | 1328 | |
b423cd4c | 1329 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
1330 | { | |
f899b121 | 1331 | send_http_error(con, status, printer); |
b423cd4c | 1332 | return (NULL); |
1333 | } | |
3dfe78b3 MS |
1334 | else if (printer->num_auth_info_required > 0 && |
1335 | strcmp(printer->auth_info_required[0], "none") && | |
7594b224 | 1336 | !con->username[0] && !auth_info) |
b423cd4c | 1337 | { |
f899b121 | 1338 | send_http_error(con, HTTP_UNAUTHORIZED, printer); |
b423cd4c | 1339 | return (NULL); |
1340 | } | |
7594b224 | 1341 | #ifdef HAVE_SSL |
1342 | else if (auth_info && !con->http.tls && | |
1343 | !httpAddrLocalhost(con->http.hostaddr)) | |
1344 | { | |
1345 | /* | |
1346 | * Require encryption of auth-info over non-local connections... | |
1347 | */ | |
1348 | ||
f899b121 | 1349 | send_http_error(con, HTTP_UPGRADE_REQUIRED, printer); |
7594b224 | 1350 | return (NULL); |
1351 | } | |
1352 | #endif /* HAVE_SSL */ | |
ef416fc2 | 1353 | |
b423cd4c | 1354 | /* |
1355 | * See if the printer is accepting jobs... | |
1356 | */ | |
1357 | ||
1358 | if (!printer->accepting) | |
1359 | { | |
1360 | send_ipp_status(con, IPP_NOT_ACCEPTING, | |
1361 | _("Destination \"%s\" is not accepting jobs."), | |
f7deaa1a | 1362 | printer->name); |
b423cd4c | 1363 | return (NULL); |
ef416fc2 | 1364 | } |
ef416fc2 | 1365 | |
b423cd4c | 1366 | /* |
80ca4592 | 1367 | * Validate job template attributes; for now just document-format, |
f7deaa1a | 1368 | * copies, number-up, and page-ranges... |
b423cd4c | 1369 | */ |
ef416fc2 | 1370 | |
80ca4592 | 1371 | if (filetype && printer->filetypes && |
1372 | !cupsArrayFind(printer->filetypes, filetype)) | |
1373 | { | |
1374 | char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
1375 | /* MIME media type string */ | |
1376 | ||
1377 | ||
1378 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
1379 | filetype->type); | |
1380 | ||
1381 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
1382 | _("Unsupported format \'%s\'!"), mimetype); | |
1383 | ||
1384 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
1385 | "document-format", NULL, mimetype); | |
1386 | ||
1387 | return (NULL); | |
1388 | } | |
1389 | ||
b423cd4c | 1390 | if ((attr = ippFindAttribute(con->request, "copies", |
1391 | IPP_TAG_INTEGER)) != NULL) | |
1392 | { | |
1393 | if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies) | |
1394 | { | |
1395 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."), | |
1396 | attr->values[0].integer); | |
1397 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1398 | "copies", attr->values[0].integer); | |
1399 | return (NULL); | |
1400 | } | |
1401 | } | |
ef416fc2 | 1402 | |
3dfe78b3 MS |
1403 | if ((attr = ippFindAttribute(con->request, "job-sheets", |
1404 | IPP_TAG_ZERO)) != NULL) | |
1405 | { | |
1406 | if (attr->value_tag != IPP_TAG_KEYWORD && | |
1407 | attr->value_tag != IPP_TAG_NAME) | |
1408 | { | |
1409 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type!")); | |
1410 | return (NULL); | |
1411 | } | |
1412 | ||
1413 | if (attr->num_values > 2) | |
1414 | { | |
1415 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1416 | _("Too many job-sheets values (%d > 2)!"), | |
1417 | attr->num_values); | |
1418 | return (NULL); | |
1419 | } | |
1420 | ||
1421 | for (i = 0; i < attr->num_values; i ++) | |
1422 | if (strcmp(attr->values[i].string.text, "none") && | |
1423 | !cupsdFindBanner(attr->values[i].string.text)) | |
1424 | { | |
1425 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"!"), | |
1426 | attr->values[i].string.text); | |
1427 | return (NULL); | |
1428 | } | |
1429 | } | |
1430 | ||
f7deaa1a | 1431 | if ((attr = ippFindAttribute(con->request, "number-up", |
1432 | IPP_TAG_INTEGER)) != NULL) | |
1433 | { | |
1434 | if (attr->values[0].integer != 1 && | |
1435 | attr->values[0].integer != 2 && | |
1436 | attr->values[0].integer != 4 && | |
1437 | attr->values[0].integer != 6 && | |
1438 | attr->values[0].integer != 9 && | |
1439 | attr->values[0].integer != 16) | |
1440 | { | |
1441 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."), | |
1442 | attr->values[0].integer); | |
1443 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1444 | "number-up", attr->values[0].integer); | |
1445 | return (NULL); | |
1446 | } | |
1447 | } | |
1448 | ||
b423cd4c | 1449 | if ((attr = ippFindAttribute(con->request, "page-ranges", |
1450 | IPP_TAG_RANGE)) != NULL) | |
1451 | { | |
1452 | for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++) | |
1453 | { | |
f7deaa1a | 1454 | if (attr->values[i].range.lower < lowerpagerange || |
b423cd4c | 1455 | attr->values[i].range.lower > attr->values[i].range.upper) |
1456 | { | |
1457 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1458 | _("Bad page-ranges values %d-%d."), | |
1459 | attr->values[i].range.lower, | |
1460 | attr->values[i].range.upper); | |
1461 | return (NULL); | |
1462 | } | |
ef416fc2 | 1463 | |
b423cd4c | 1464 | lowerpagerange = attr->values[i].range.upper + 1; |
1465 | } | |
1466 | } | |
ef416fc2 | 1467 | |
1468 | /* | |
b423cd4c | 1469 | * Make sure we aren't over our limit... |
ef416fc2 | 1470 | */ |
1471 | ||
b423cd4c | 1472 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
1473 | cupsdCleanJobs(); | |
ef416fc2 | 1474 | |
f7deaa1a | 1475 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
b423cd4c | 1476 | { |
1477 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
1478 | _("Too many active jobs.")); | |
1479 | return (NULL); | |
1480 | } | |
1481 | ||
bc44d920 | 1482 | if ((i = check_quotas(con, printer)) < 0) |
b423cd4c | 1483 | { |
1484 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1485 | return (NULL); | |
1486 | } | |
bc44d920 | 1487 | else if (i == 0) |
1488 | { | |
1489 | send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print.")); | |
1490 | return (NULL); | |
1491 | } | |
ef416fc2 | 1492 | |
1493 | /* | |
b423cd4c | 1494 | * Create the job and set things up... |
ef416fc2 | 1495 | */ |
1496 | ||
b423cd4c | 1497 | if ((attr = ippFindAttribute(con->request, "job-priority", |
1498 | IPP_TAG_INTEGER)) != NULL) | |
1499 | priority = attr->values[0].integer; | |
1500 | else | |
ef416fc2 | 1501 | { |
b423cd4c | 1502 | if ((val = cupsGetOption("job-priority", printer->num_options, |
1503 | printer->options)) != NULL) | |
1504 | priority = atoi(val); | |
1505 | else | |
1506 | priority = 50; | |
ef416fc2 | 1507 | |
b423cd4c | 1508 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", |
1509 | priority); | |
1510 | } | |
ef416fc2 | 1511 | |
b423cd4c | 1512 | if ((attr = ippFindAttribute(con->request, "job-name", |
1513 | IPP_TAG_NAME)) != NULL) | |
1514 | title = attr->values[0].string.text; | |
1515 | else | |
1516 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1517 | title = "Untitled"); | |
ef416fc2 | 1518 | |
b423cd4c | 1519 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) |
1520 | { | |
1521 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
f7deaa1a | 1522 | _("Unable to add job for destination \"%s\"!"), |
1523 | printer->name); | |
b423cd4c | 1524 | return (NULL); |
1525 | } | |
ef416fc2 | 1526 | |
f7deaa1a | 1527 | job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT | |
1528 | CUPS_PRINTER_REMOTE); | |
b423cd4c | 1529 | job->attrs = con->request; |
3dfe78b3 | 1530 | job->dirty = 1; |
2abf387c | 1531 | con->request = ippNewRequest(job->attrs->request.op.operation_id); |
ef416fc2 | 1532 | |
3dfe78b3 MS |
1533 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); |
1534 | ||
b423cd4c | 1535 | add_job_uuid(con, job); |
1536 | apply_printer_defaults(printer, job); | |
ef416fc2 | 1537 | |
b423cd4c | 1538 | attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME); |
ef416fc2 | 1539 | |
b423cd4c | 1540 | if (con->username[0]) |
1541 | { | |
1542 | cupsdSetString(&job->username, con->username); | |
ef416fc2 | 1543 | |
1544 | if (attr) | |
b423cd4c | 1545 | cupsdSetString(&attr->values[0].string.text, con->username); |
ef416fc2 | 1546 | } |
b423cd4c | 1547 | else if (attr) |
1548 | { | |
1549 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1550 | "add_job: requesting-user-name=\"%s\"", | |
1551 | attr->values[0].string.text); | |
ef416fc2 | 1552 | |
b423cd4c | 1553 | cupsdSetString(&job->username, attr->values[0].string.text); |
1554 | } | |
1555 | else | |
1556 | cupsdSetString(&job->username, "anonymous"); | |
ef416fc2 | 1557 | |
b423cd4c | 1558 | if (!attr) |
1559 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1560 | "job-originating-user-name", NULL, job->username); | |
1561 | else | |
1562 | { | |
1563 | attr->group_tag = IPP_TAG_JOB; | |
757d2cad | 1564 | _cupsStrFree(attr->name); |
1565 | attr->name = _cupsStrAlloc("job-originating-user-name"); | |
b423cd4c | 1566 | } |
ef416fc2 | 1567 | |
f7deaa1a | 1568 | if (con->username[0] || auth_info) |
1569 | { | |
1570 | save_auth_info(con, job, auth_info); | |
1571 | ||
1572 | /* | |
1573 | * Remove the auth-info attribute from the attribute data... | |
1574 | */ | |
1575 | ||
1576 | if (auth_info) | |
a4924f6c | 1577 | ippDeleteAttribute(job->attrs, auth_info); |
f7deaa1a | 1578 | } |
1579 | ||
b423cd4c | 1580 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", |
1581 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 1582 | { |
b423cd4c | 1583 | /* |
1584 | * Request contains a job-originating-host-name attribute; validate it... | |
1585 | */ | |
ef416fc2 | 1586 | |
b423cd4c | 1587 | if (attr->value_tag != IPP_TAG_NAME || |
1588 | attr->num_values != 1 || | |
1589 | strcmp(con->http.hostname, "localhost")) | |
ef416fc2 | 1590 | { |
1591 | /* | |
b423cd4c | 1592 | * Can't override the value if we aren't connected via localhost. |
1593 | * Also, we can only have 1 value and it must be a name value. | |
ef416fc2 | 1594 | */ |
1595 | ||
b423cd4c | 1596 | switch (attr->value_tag) |
1597 | { | |
1598 | case IPP_TAG_STRING : | |
1599 | case IPP_TAG_TEXTLANG : | |
1600 | case IPP_TAG_NAMELANG : | |
1601 | case IPP_TAG_TEXT : | |
1602 | case IPP_TAG_NAME : | |
1603 | case IPP_TAG_KEYWORD : | |
1604 | case IPP_TAG_URI : | |
1605 | case IPP_TAG_URISCHEME : | |
1606 | case IPP_TAG_CHARSET : | |
1607 | case IPP_TAG_LANGUAGE : | |
1608 | case IPP_TAG_MIMETYPE : | |
1609 | /* | |
1610 | * Free old strings... | |
1611 | */ | |
ef416fc2 | 1612 | |
b423cd4c | 1613 | for (i = 0; i < attr->num_values; i ++) |
1614 | { | |
757d2cad | 1615 | _cupsStrFree(attr->values[i].string.text); |
b423cd4c | 1616 | attr->values[i].string.text = NULL; |
1617 | if (attr->values[i].string.charset) | |
1618 | { | |
757d2cad | 1619 | _cupsStrFree(attr->values[i].string.charset); |
b423cd4c | 1620 | attr->values[i].string.charset = NULL; |
1621 | } | |
1622 | } | |
ef416fc2 | 1623 | |
b423cd4c | 1624 | default : |
1625 | break; | |
1626 | } | |
ef416fc2 | 1627 | |
b423cd4c | 1628 | /* |
1629 | * Use the default connection hostname instead... | |
1630 | */ | |
bd7854cb | 1631 | |
b423cd4c | 1632 | attr->value_tag = IPP_TAG_NAME; |
1633 | attr->num_values = 1; | |
757d2cad | 1634 | attr->values[0].string.text = _cupsStrAlloc(con->http.hostname); |
b423cd4c | 1635 | } |
bd7854cb | 1636 | |
b423cd4c | 1637 | attr->group_tag = IPP_TAG_JOB; |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | /* | |
1642 | * No job-originating-host-name attribute, so use the hostname from | |
1643 | * the connection... | |
1644 | */ | |
bd7854cb | 1645 | |
f7deaa1a | 1646 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, |
b423cd4c | 1647 | "job-originating-host-name", NULL, con->http.hostname); |
1648 | } | |
bd7854cb | 1649 | |
b423cd4c | 1650 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", |
1651 | time(NULL)); | |
1652 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1653 | "time-at-processing", 0); | |
1654 | attr->value_tag = IPP_TAG_NOVALUE; | |
1655 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1656 | "time-at-completed", 0); | |
1657 | attr->value_tag = IPP_TAG_NOVALUE; | |
bd7854cb | 1658 | |
1659 | /* | |
b423cd4c | 1660 | * Add remaining job attributes... |
bd7854cb | 1661 | */ |
1662 | ||
b423cd4c | 1663 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
1664 | job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM, | |
1665 | "job-state", IPP_JOB_STOPPED); | |
d09495fa | 1666 | job->state_value = (ipp_jstate_t)job->state->values[0].integer; |
b423cd4c | 1667 | job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, |
1668 | "job-media-sheets-completed", 0); | |
1669 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, | |
1670 | printer->uri); | |
1671 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1672 | title); | |
bd7854cb | 1673 | |
b423cd4c | 1674 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
1675 | IPP_TAG_INTEGER)) != NULL) | |
1676 | attr->values[0].integer = 0; | |
1677 | else | |
1f0275e3 | 1678 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0); |
bd7854cb | 1679 | |
b423cd4c | 1680 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
1681 | IPP_TAG_KEYWORD)) == NULL) | |
1682 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
1683 | if (!attr) | |
ef416fc2 | 1684 | { |
b423cd4c | 1685 | if ((val = cupsGetOption("job-hold-until", printer->num_options, |
1686 | printer->options)) == NULL) | |
1687 | val = "no-hold"; | |
ef416fc2 | 1688 | |
b423cd4c | 1689 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1690 | "job-hold-until", NULL, val); | |
ef416fc2 | 1691 | } |
b423cd4c | 1692 | if (attr && strcmp(attr->values[0].string.text, "no-hold") && |
1693 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 1694 | { |
1695 | /* | |
b423cd4c | 1696 | * Hold job until specified time... |
ef416fc2 | 1697 | */ |
1698 | ||
b423cd4c | 1699 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
e53920b9 | 1700 | |
1701 | job->state->values[0].integer = IPP_JOB_HELD; | |
1702 | job->state_value = IPP_JOB_HELD; | |
ef416fc2 | 1703 | } |
b423cd4c | 1704 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) |
ef416fc2 | 1705 | { |
e53920b9 | 1706 | job->hold_until = time(NULL) + 60; |
b423cd4c | 1707 | job->state->values[0].integer = IPP_JOB_HELD; |
1708 | job->state_value = IPP_JOB_HELD; | |
1709 | } | |
1710 | else | |
1711 | { | |
1712 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1713 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 1714 | } |
1715 | ||
b423cd4c | 1716 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
1717 | Classification) | |
ef416fc2 | 1718 | { |
1719 | /* | |
b423cd4c | 1720 | * Add job sheets options... |
ef416fc2 | 1721 | */ |
1722 | ||
b423cd4c | 1723 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1724 | IPP_TAG_ZERO)) == NULL) | |
ef416fc2 | 1725 | { |
b423cd4c | 1726 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1727 | "Adding default job-sheets values \"%s,%s\"...", | |
1728 | printer->job_sheets[0], printer->job_sheets[1]); | |
ef416fc2 | 1729 | |
b423cd4c | 1730 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", |
1731 | 2, NULL, NULL); | |
757d2cad | 1732 | attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]); |
1733 | attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]); | |
ef416fc2 | 1734 | } |
1735 | ||
b423cd4c | 1736 | job->job_sheets = attr; |
ef416fc2 | 1737 | |
ef416fc2 | 1738 | /* |
b423cd4c | 1739 | * Enforce classification level if set... |
ef416fc2 | 1740 | */ |
1741 | ||
b423cd4c | 1742 | if (Classification) |
ef416fc2 | 1743 | { |
b423cd4c | 1744 | cupsdLogMessage(CUPSD_LOG_INFO, |
1745 | "Classification=\"%s\", ClassifyOverride=%d", | |
1746 | Classification ? Classification : "(null)", | |
1747 | ClassifyOverride); | |
ef416fc2 | 1748 | |
b423cd4c | 1749 | if (ClassifyOverride) |
1750 | { | |
1751 | if (!strcmp(attr->values[0].string.text, "none") && | |
1752 | (attr->num_values == 1 || | |
1753 | !strcmp(attr->values[1].string.text, "none"))) | |
1754 | { | |
1755 | /* | |
1756 | * Force the leading banner to have the classification on it... | |
1757 | */ | |
ef416fc2 | 1758 | |
b423cd4c | 1759 | cupsdSetString(&attr->values[0].string.text, Classification); |
e00b005a | 1760 | |
75bd9771 MS |
1761 | cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED " |
1762 | "job-sheets=\"%s,none\", " | |
1763 | "job-originating-user-name=\"%s\"", | |
1764 | Classification, job->username); | |
b423cd4c | 1765 | } |
1766 | else if (attr->num_values == 2 && | |
1767 | strcmp(attr->values[0].string.text, | |
1768 | attr->values[1].string.text) && | |
1769 | strcmp(attr->values[0].string.text, "none") && | |
1770 | strcmp(attr->values[1].string.text, "none")) | |
1771 | { | |
1772 | /* | |
1773 | * Can't put two different security markings on the same document! | |
1774 | */ | |
ef416fc2 | 1775 | |
b423cd4c | 1776 | cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text); |
ef416fc2 | 1777 | |
75bd9771 MS |
1778 | cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED " |
1779 | "job-sheets=\"%s,%s\", " | |
1780 | "job-originating-user-name=\"%s\"", | |
1781 | attr->values[0].string.text, | |
1782 | attr->values[1].string.text, job->username); | |
b423cd4c | 1783 | } |
1784 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1785 | strcmp(attr->values[0].string.text, "none") && | |
1786 | (attr->num_values == 1 || | |
1787 | (strcmp(attr->values[1].string.text, Classification) && | |
1788 | strcmp(attr->values[1].string.text, "none")))) | |
1789 | { | |
1790 | if (attr->num_values == 1) | |
75bd9771 MS |
1791 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1792 | "CLASSIFICATION OVERRIDDEN " | |
1793 | "job-sheets=\"%s\", " | |
1794 | "job-originating-user-name=\"%s\"", | |
1795 | attr->values[0].string.text, job->username); | |
b423cd4c | 1796 | else |
75bd9771 MS |
1797 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1798 | "CLASSIFICATION OVERRIDDEN " | |
1799 | "job-sheets=\"%s,%s\",fffff " | |
1800 | "job-originating-user-name=\"%s\"", | |
1801 | attr->values[0].string.text, | |
1802 | attr->values[1].string.text, job->username); | |
b423cd4c | 1803 | } |
1804 | } | |
1805 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1806 | (attr->num_values == 1 || | |
1807 | strcmp(attr->values[1].string.text, Classification))) | |
ef416fc2 | 1808 | { |
1809 | /* | |
b423cd4c | 1810 | * Force the banner to have the classification on it... |
ef416fc2 | 1811 | */ |
1812 | ||
b423cd4c | 1813 | if (attr->num_values > 1 && |
1814 | !strcmp(attr->values[0].string.text, attr->values[1].string.text)) | |
1815 | { | |
1816 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
1817 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1818 | } | |
1819 | else | |
1820 | { | |
1821 | if (attr->num_values == 1 || | |
1822 | strcmp(attr->values[0].string.text, "none")) | |
1823 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
ef416fc2 | 1824 | |
b423cd4c | 1825 | if (attr->num_values > 1 && |
1826 | strcmp(attr->values[1].string.text, "none")) | |
1827 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1828 | } | |
ef416fc2 | 1829 | |
b423cd4c | 1830 | if (attr->num_values > 1) |
75bd9771 MS |
1831 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1832 | "CLASSIFICATION FORCED " | |
1833 | "job-sheets=\"%s,%s\", " | |
1834 | "job-originating-user-name=\"%s\"", | |
1835 | attr->values[0].string.text, | |
1836 | attr->values[1].string.text, job->username); | |
b423cd4c | 1837 | else |
75bd9771 MS |
1838 | cupsdLogJob(job, CUPSD_LOG_NOTICE, |
1839 | "CLASSIFICATION FORCED " | |
1840 | "job-sheets=\"%s\", " | |
1841 | "job-originating-user-name=\"%s\"", | |
1842 | Classification, job->username); | |
ef416fc2 | 1843 | } |
1844 | } | |
1845 | ||
b423cd4c | 1846 | /* |
1847 | * See if we need to add the starting sheet... | |
1848 | */ | |
ef416fc2 | 1849 | |
b423cd4c | 1850 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
ef416fc2 | 1851 | { |
75bd9771 MS |
1852 | cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".", |
1853 | attr->values[0].string.text); | |
ef416fc2 | 1854 | |
91c84a35 | 1855 | if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0) |
3dfe78b3 MS |
1856 | { |
1857 | cupsdDeleteJob(job); | |
91c84a35 | 1858 | return (NULL); |
3dfe78b3 | 1859 | } |
ef416fc2 | 1860 | |
b423cd4c | 1861 | cupsdUpdateQuota(printer, job->username, 0, kbytes); |
1862 | } | |
ef416fc2 | 1863 | } |
b423cd4c | 1864 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1865 | IPP_TAG_ZERO)) != NULL) | |
1866 | job->sheets = attr; | |
ef416fc2 | 1867 | |
b423cd4c | 1868 | /* |
1869 | * Fill in the response info... | |
1870 | */ | |
ef416fc2 | 1871 | |
b423cd4c | 1872 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, |
1873 | LocalPort, job->id); | |
ef416fc2 | 1874 | |
b423cd4c | 1875 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
1876 | job_uri); | |
ef416fc2 | 1877 | |
b423cd4c | 1878 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
ef416fc2 | 1879 | |
b423cd4c | 1880 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", |
1881 | job->state_value); | |
1882 | add_job_state_reasons(con, job); | |
ef416fc2 | 1883 | |
b423cd4c | 1884 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 1885 | |
b423cd4c | 1886 | /* |
1887 | * Add any job subscriptions... | |
1888 | */ | |
ef416fc2 | 1889 | |
b423cd4c | 1890 | add_job_subscriptions(con, job); |
ef416fc2 | 1891 | |
b423cd4c | 1892 | /* |
1893 | * Set all but the first two attributes to the job attributes group... | |
1894 | */ | |
ef416fc2 | 1895 | |
b423cd4c | 1896 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) |
1897 | attr->group_tag = IPP_TAG_JOB; | |
ef416fc2 | 1898 | |
1899 | /* | |
b423cd4c | 1900 | * Fire the "job created" event... |
ef416fc2 | 1901 | */ |
1902 | ||
b423cd4c | 1903 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); |
ef416fc2 | 1904 | |
1905 | /* | |
b423cd4c | 1906 | * Return the new job... |
ef416fc2 | 1907 | */ |
1908 | ||
b423cd4c | 1909 | return (job); |
1910 | } | |
ef416fc2 | 1911 | |
ef416fc2 | 1912 | |
b423cd4c | 1913 | /* |
1914 | * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based | |
1915 | * upon the job and printer state... | |
1916 | */ | |
ef416fc2 | 1917 | |
b423cd4c | 1918 | static void |
1919 | add_job_state_reasons( | |
1920 | cupsd_client_t *con, /* I - Client connection */ | |
1921 | cupsd_job_t *job) /* I - Job info */ | |
1922 | { | |
1923 | cupsd_printer_t *dest; /* Destination printer */ | |
ef416fc2 | 1924 | |
ef416fc2 | 1925 | |
b423cd4c | 1926 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)", |
1927 | con, con->http.fd, job ? job->id : 0); | |
ef416fc2 | 1928 | |
d09495fa | 1929 | switch (job ? job->state_value : IPP_JOB_CANCELED) |
ef416fc2 | 1930 | { |
b423cd4c | 1931 | case IPP_JOB_PENDING : |
80ca4592 | 1932 | dest = cupsdFindDest(job->dest); |
bd7854cb | 1933 | |
b423cd4c | 1934 | if (dest && dest->state == IPP_PRINTER_STOPPED) |
1935 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1936 | "job-state-reasons", NULL, "printer-stopped"); | |
1937 | else | |
1938 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1939 | "job-state-reasons", NULL, "none"); | |
1940 | break; | |
ef416fc2 | 1941 | |
b423cd4c | 1942 | case IPP_JOB_HELD : |
1943 | if (ippFindAttribute(job->attrs, "job-hold-until", | |
1944 | IPP_TAG_KEYWORD) != NULL || | |
1945 | ippFindAttribute(job->attrs, "job-hold-until", | |
1946 | IPP_TAG_NAME) != NULL) | |
1947 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1948 | "job-state-reasons", NULL, "job-hold-until-specified"); | |
1949 | else | |
1950 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1951 | "job-state-reasons", NULL, "job-incoming"); | |
1952 | break; | |
ef416fc2 | 1953 | |
b423cd4c | 1954 | case IPP_JOB_PROCESSING : |
1955 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1956 | "job-state-reasons", NULL, "job-printing"); | |
1957 | break; | |
ef416fc2 | 1958 | |
b423cd4c | 1959 | case IPP_JOB_STOPPED : |
1960 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1961 | "job-state-reasons", NULL, "job-stopped"); | |
1962 | break; | |
ef416fc2 | 1963 | |
d09495fa | 1964 | case IPP_JOB_CANCELED : |
b423cd4c | 1965 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1966 | "job-state-reasons", NULL, "job-canceled-by-user"); | |
1967 | break; | |
ef416fc2 | 1968 | |
b423cd4c | 1969 | case IPP_JOB_ABORTED : |
1970 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1971 | "job-state-reasons", NULL, "aborted-by-system"); | |
1972 | break; | |
ef416fc2 | 1973 | |
b423cd4c | 1974 | case IPP_JOB_COMPLETED : |
1975 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1976 | "job-state-reasons", NULL, "job-completed-successfully"); | |
1977 | break; | |
ef416fc2 | 1978 | } |
ef416fc2 | 1979 | } |
1980 | ||
1981 | ||
1982 | /* | |
9a4f8274 | 1983 | * 'add_job_subscriptions()' - Add any subscriptions for a job. |
ef416fc2 | 1984 | */ |
1985 | ||
1986 | static void | |
b423cd4c | 1987 | add_job_subscriptions( |
1988 | cupsd_client_t *con, /* I - Client connection */ | |
1989 | cupsd_job_t *job) /* I - Newly created job */ | |
ef416fc2 | 1990 | { |
b423cd4c | 1991 | int i; /* Looping var */ |
1992 | ipp_attribute_t *prev, /* Previous attribute */ | |
1993 | *next, /* Next attribute */ | |
1994 | *attr; /* Current attribute */ | |
1995 | cupsd_subscription_t *sub; /* Subscription object */ | |
1996 | const char *recipient, /* notify-recipient-uri */ | |
1997 | *pullmethod; /* notify-pull-method */ | |
1998 | ipp_attribute_t *user_data; /* notify-user-data */ | |
1999 | int interval; /* notify-time-interval */ | |
2000 | unsigned mask; /* notify-events */ | |
ef416fc2 | 2001 | |
ef416fc2 | 2002 | |
b423cd4c | 2003 | /* |
2004 | * Find the first subscription group attribute; return if we have | |
2005 | * none... | |
2006 | */ | |
ef416fc2 | 2007 | |
1f0275e3 | 2008 | for (attr = job->attrs->attrs; attr; attr = attr->next) |
b423cd4c | 2009 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) |
2010 | break; | |
ef416fc2 | 2011 | |
b423cd4c | 2012 | if (!attr) |
2013 | return; | |
ef416fc2 | 2014 | |
b423cd4c | 2015 | /* |
2016 | * Process the subscription attributes in the request... | |
2017 | */ | |
ef416fc2 | 2018 | |
b423cd4c | 2019 | while (attr) |
2020 | { | |
2021 | recipient = NULL; | |
2022 | pullmethod = NULL; | |
2023 | user_data = NULL; | |
2024 | interval = 0; | |
2025 | mask = CUPSD_EVENT_NONE; | |
ef416fc2 | 2026 | |
b423cd4c | 2027 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
2028 | { | |
f899b121 | 2029 | if (!strcmp(attr->name, "notify-recipient-uri") && |
b423cd4c | 2030 | attr->value_tag == IPP_TAG_URI) |
3dfe78b3 MS |
2031 | { |
2032 | /* | |
2033 | * Validate the recipient scheme against the ServerBin/notifier | |
2034 | * directory... | |
2035 | */ | |
2036 | ||
2037 | char notifier[1024], /* Notifier filename */ | |
2038 | scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
2039 | userpass[HTTP_MAX_URI], /* Username portion of URI */ | |
2040 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2041 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2042 | int port; /* Port portion of URI */ | |
2043 | ||
2044 | ||
b423cd4c | 2045 | recipient = attr->values[0].string.text; |
3dfe78b3 MS |
2046 | |
2047 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, | |
2048 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
2049 | host, sizeof(host), &port, | |
2050 | resource, sizeof(resource)) < HTTP_URI_OK) | |
2051 | { | |
2052 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2053 | _("Bad notify-recipient-uri URI \"%s\"!"), recipient); | |
2054 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2055 | "notify-status-code", IPP_URI_SCHEME); | |
2056 | return; | |
2057 | } | |
2058 | ||
2059 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
2060 | scheme); | |
2061 | if (access(notifier, X_OK)) | |
2062 | { | |
2063 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2064 | _("notify-recipient-uri URI \"%s\" uses unknown " | |
2065 | "scheme!"), recipient); | |
2066 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2067 | "notify-status-code", IPP_URI_SCHEME); | |
2068 | return; | |
2069 | } | |
2070 | ||
2071 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
2072 | { | |
2073 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2074 | _("notify-recipient-uri URI \"%s\" is already used!"), | |
2075 | recipient); | |
2076 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2077 | "notify-status-code", IPP_ATTRIBUTES); | |
2078 | return; | |
2079 | } | |
2080 | } | |
b423cd4c | 2081 | else if (!strcmp(attr->name, "notify-pull-method") && |
2082 | attr->value_tag == IPP_TAG_KEYWORD) | |
3dfe78b3 | 2083 | { |
b423cd4c | 2084 | pullmethod = attr->values[0].string.text; |
3dfe78b3 MS |
2085 | |
2086 | if (strcmp(pullmethod, "ippget")) | |
2087 | { | |
2088 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2089 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
2090 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2091 | "notify-status-code", IPP_ATTRIBUTES); | |
2092 | return; | |
2093 | } | |
2094 | } | |
b423cd4c | 2095 | else if (!strcmp(attr->name, "notify-charset") && |
2096 | attr->value_tag == IPP_TAG_CHARSET && | |
2097 | strcmp(attr->values[0].string.text, "us-ascii") && | |
2098 | strcmp(attr->values[0].string.text, "utf-8")) | |
2099 | { | |
2100 | send_ipp_status(con, IPP_CHARSET, | |
2101 | _("Character set \"%s\" not supported!"), | |
2102 | attr->values[0].string.text); | |
2103 | return; | |
2104 | } | |
2105 | else if (!strcmp(attr->name, "notify-natural-language") && | |
2106 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
2107 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
2108 | { | |
2109 | send_ipp_status(con, IPP_CHARSET, | |
2110 | _("Language \"%s\" not supported!"), | |
2111 | attr->values[0].string.text); | |
2112 | return; | |
2113 | } | |
2114 | else if (!strcmp(attr->name, "notify-user-data") && | |
2115 | attr->value_tag == IPP_TAG_STRING) | |
2116 | { | |
2117 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
2118 | { | |
2119 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
2120 | _("The notify-user-data value is too large " | |
2121 | "(%d > 63 octets)!"), | |
2122 | attr->values[0].unknown.length); | |
2123 | return; | |
2124 | } | |
ef416fc2 | 2125 | |
b423cd4c | 2126 | user_data = attr; |
2127 | } | |
2128 | else if (!strcmp(attr->name, "notify-events") && | |
2129 | attr->value_tag == IPP_TAG_KEYWORD) | |
2130 | { | |
2131 | for (i = 0; i < attr->num_values; i ++) | |
2132 | mask |= cupsdEventValue(attr->values[i].string.text); | |
2133 | } | |
2134 | else if (!strcmp(attr->name, "notify-lease-duration")) | |
2135 | { | |
2136 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2137 | _("The notify-lease-duration attribute cannot be " | |
2138 | "used with job subscriptions.")); | |
2139 | return; | |
2140 | } | |
2141 | else if (!strcmp(attr->name, "notify-time-interval") && | |
2142 | attr->value_tag == IPP_TAG_INTEGER) | |
2143 | interval = attr->values[0].integer; | |
ef416fc2 | 2144 | |
b423cd4c | 2145 | attr = attr->next; |
2146 | } | |
ef416fc2 | 2147 | |
b423cd4c | 2148 | if (!recipient && !pullmethod) |
2149 | break; | |
ef416fc2 | 2150 | |
b423cd4c | 2151 | if (mask == CUPSD_EVENT_NONE) |
2152 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
ef416fc2 | 2153 | |
b423cd4c | 2154 | sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient, |
2155 | 0); | |
ef416fc2 | 2156 | |
b423cd4c | 2157 | sub->interval = interval; |
ef416fc2 | 2158 | |
b423cd4c | 2159 | cupsdSetString(&sub->owner, job->username); |
ef416fc2 | 2160 | |
b423cd4c | 2161 | if (user_data) |
ef416fc2 | 2162 | { |
b423cd4c | 2163 | sub->user_data_len = user_data->values[0].unknown.length; |
2164 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
2165 | sub->user_data_len); | |
ef416fc2 | 2166 | } |
2167 | ||
b423cd4c | 2168 | ippAddSeparator(con->response); |
2169 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
2170 | "notify-subscription-id", sub->id); | |
ef416fc2 | 2171 | |
b423cd4c | 2172 | if (attr) |
2173 | attr = attr->next; | |
ef416fc2 | 2174 | } |
2175 | ||
3dfe78b3 | 2176 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
b423cd4c | 2177 | |
ef416fc2 | 2178 | /* |
b423cd4c | 2179 | * Remove all of the subscription attributes from the job request... |
1f0275e3 MS |
2180 | * |
2181 | * TODO: Optimize this since subscription groups have to come at the | |
2182 | * end of the request... | |
ef416fc2 | 2183 | */ |
2184 | ||
b423cd4c | 2185 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) |
ef416fc2 | 2186 | { |
b423cd4c | 2187 | next = attr->next; |
ef416fc2 | 2188 | |
b423cd4c | 2189 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || |
2190 | attr->group_tag == IPP_TAG_ZERO) | |
2191 | { | |
2192 | /* | |
2193 | * Free and remove this attribute... | |
2194 | */ | |
ef416fc2 | 2195 | |
757d2cad | 2196 | _ippFreeAttr(attr); |
ef416fc2 | 2197 | |
b423cd4c | 2198 | if (prev) |
2199 | prev->next = next; | |
2200 | else | |
2201 | job->attrs->attrs = next; | |
2202 | } | |
2203 | else | |
2204 | prev = attr; | |
ef416fc2 | 2205 | } |
2206 | ||
b423cd4c | 2207 | job->attrs->last = prev; |
2208 | job->attrs->current = prev; | |
2209 | } | |
ef416fc2 | 2210 | |
ef416fc2 | 2211 | |
b423cd4c | 2212 | /* |
2213 | * 'add_job_uuid()' - Add job-uuid attribute to a job. | |
2214 | * | |
2215 | * See RFC 4122 for the definition of UUIDs and the format. | |
2216 | */ | |
2217 | ||
2218 | static void | |
2219 | add_job_uuid(cupsd_client_t *con, /* I - Client connection */ | |
2220 | cupsd_job_t *job) /* I - Job */ | |
2221 | { | |
2222 | char uuid[1024]; /* job-uuid string */ | |
b423cd4c | 2223 | _cups_md5_state_t md5state; /* MD5 state */ |
2224 | unsigned char md5sum[16]; /* MD5 digest/sum */ | |
ef416fc2 | 2225 | |
ef416fc2 | 2226 | |
2227 | /* | |
b423cd4c | 2228 | * First see if the job already has a job-uuid attribute; if so, return... |
ef416fc2 | 2229 | */ |
2230 | ||
d09495fa | 2231 | if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) |
b423cd4c | 2232 | return; |
ef416fc2 | 2233 | |
2234 | /* | |
b423cd4c | 2235 | * No job-uuid attribute, so build a version 3 UUID with the local job |
2236 | * ID at the end; see RFC 4122 for details. Start with the MD5 sum of | |
2237 | * the ServerName, server name and port that the client connected to, | |
2238 | * and local job ID... | |
ef416fc2 | 2239 | */ |
2240 | ||
b423cd4c | 2241 | snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername, |
2242 | con->serverport, job->id); | |
ef416fc2 | 2243 | |
757d2cad | 2244 | _cupsMD5Init(&md5state); |
2245 | _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid)); | |
2246 | _cupsMD5Finish(&md5state, md5sum); | |
ef416fc2 | 2247 | |
2248 | /* | |
b423cd4c | 2249 | * Format the UUID URI using the MD5 sum and job ID. |
ef416fc2 | 2250 | */ |
2251 | ||
b423cd4c | 2252 | snprintf(uuid, sizeof(uuid), |
2253 | "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" | |
2254 | "%02x%02x%02x%02x%02x%02x", | |
2255 | md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], | |
2256 | (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, | |
2257 | md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], | |
2258 | md5sum[14], md5sum[15]); | |
ef416fc2 | 2259 | |
b423cd4c | 2260 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid); |
ef416fc2 | 2261 | } |
2262 | ||
2263 | ||
2264 | /* | |
b423cd4c | 2265 | * 'add_printer()' - Add a printer to the system. |
ef416fc2 | 2266 | */ |
2267 | ||
2268 | static void | |
b423cd4c | 2269 | add_printer(cupsd_client_t *con, /* I - Client connection */ |
2270 | ipp_attribute_t *uri) /* I - URI of printer */ | |
ef416fc2 | 2271 | { |
2272 | http_status_t status; /* Policy status */ | |
b423cd4c | 2273 | int i; /* Looping var */ |
09a101d6 | 2274 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2275 | username[HTTP_MAX_URI], /* Username portion of URI */ |
ef416fc2 | 2276 | host[HTTP_MAX_URI], /* Host portion of URI */ |
2277 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2278 | int port; /* Port portion of URI */ | |
b423cd4c | 2279 | cupsd_printer_t *printer; /* Printer/class */ |
2280 | ipp_attribute_t *attr; /* Printer attribute */ | |
2281 | cups_file_t *fp; /* Script/PPD file */ | |
2282 | char line[1024]; /* Line from file... */ | |
2283 | char srcfile[1024], /* Source Script/PPD file */ | |
2284 | dstfile[1024]; /* Destination Script/PPD file */ | |
2285 | int modify; /* Non-zero if we are modifying */ | |
2286 | char newname[IPP_MAX_NAME]; /* New printer name */ | |
2287 | int need_restart_job; /* Need to restart job? */ | |
09a101d6 | 2288 | int set_device_uri, /* Did we set the device URI? */ |
2289 | set_port_monitor; /* Did we set the port monitor? */ | |
ef416fc2 | 2290 | |
2291 | ||
b423cd4c | 2292 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, |
ef416fc2 | 2293 | con->http.fd, uri->values[0].string.text); |
2294 | ||
2295 | /* | |
b423cd4c | 2296 | * Do we have a valid URI? |
ef416fc2 | 2297 | */ |
2298 | ||
09a101d6 | 2299 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
2300 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 2301 | sizeof(host), &port, resource, sizeof(resource)); |
2302 | ||
2303 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
ef416fc2 | 2304 | { |
b423cd4c | 2305 | /* |
2306 | * No, return an error... | |
2307 | */ | |
2308 | ||
ef416fc2 | 2309 | send_ipp_status(con, IPP_BAD_REQUEST, |
b423cd4c | 2310 | _("The printer-uri must be of the form " |
2311 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
ef416fc2 | 2312 | return; |
2313 | } | |
2314 | ||
2315 | /* | |
b423cd4c | 2316 | * Do we have a valid printer name? |
ef416fc2 | 2317 | */ |
2318 | ||
b423cd4c | 2319 | if (!validate_name(resource + 10)) |
ef416fc2 | 2320 | { |
b423cd4c | 2321 | /* |
2322 | * No, return an error... | |
2323 | */ | |
2324 | ||
2325 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2326 | _("The printer-uri \"%s\" contains invalid characters."), | |
2327 | uri->values[0].string.text); | |
2328 | return; | |
ef416fc2 | 2329 | } |
ef416fc2 | 2330 | |
ef416fc2 | 2331 | /* |
b423cd4c | 2332 | * See if the printer already exists; if not, create a new printer... |
2333 | */ | |
ef416fc2 | 2334 | |
b423cd4c | 2335 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) |
ef416fc2 | 2336 | { |
2337 | /* | |
b423cd4c | 2338 | * Printer doesn't exist; see if we have a class of the same name... |
ef416fc2 | 2339 | */ |
2340 | ||
b423cd4c | 2341 | if ((printer = cupsdFindClass(resource + 10)) != NULL && |
bc44d920 | 2342 | !(printer->type & CUPS_PRINTER_DISCOVERED)) |
ef416fc2 | 2343 | { |
b423cd4c | 2344 | /* |
2345 | * Yes, return an error... | |
2346 | */ | |
ef416fc2 | 2347 | |
b423cd4c | 2348 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2349 | _("A class named \"%s\" already exists!"), | |
2350 | resource + 10); | |
ef416fc2 | 2351 | return; |
2352 | } | |
2353 | ||
2354 | /* | |
2e4ff8af | 2355 | * No, check the default policy then add the printer... |
ef416fc2 | 2356 | */ |
2357 | ||
2e4ff8af MS |
2358 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2359 | { | |
2360 | send_http_error(con, status, NULL); | |
2361 | return; | |
2362 | } | |
2363 | ||
b423cd4c | 2364 | printer = cupsdAddPrinter(resource + 10); |
2365 | modify = 0; | |
ef416fc2 | 2366 | } |
b423cd4c | 2367 | else if (printer->type & CUPS_PRINTER_IMPLICIT) |
ef416fc2 | 2368 | { |
2369 | /* | |
2e4ff8af MS |
2370 | * Check the default policy, then rename the implicit printer to |
2371 | * "AnyPrinter" or delete it... | |
ef416fc2 | 2372 | */ |
2373 | ||
2e4ff8af MS |
2374 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2375 | { | |
2376 | send_http_error(con, status, NULL); | |
2377 | return; | |
2378 | } | |
2379 | ||
b423cd4c | 2380 | if (ImplicitAnyClasses) |
ef416fc2 | 2381 | { |
b423cd4c | 2382 | snprintf(newname, sizeof(newname), "Any%s", resource + 10); |
2383 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2384 | } |
b423cd4c | 2385 | else |
2386 | cupsdDeletePrinter(printer, 1); | |
ef416fc2 | 2387 | |
2388 | /* | |
b423cd4c | 2389 | * Add the printer as a new local printer... |
ef416fc2 | 2390 | */ |
2391 | ||
b423cd4c | 2392 | printer = cupsdAddPrinter(resource + 10); |
2393 | modify = 0; | |
ef416fc2 | 2394 | } |
bc44d920 | 2395 | else if (printer->type & CUPS_PRINTER_DISCOVERED) |
b423cd4c | 2396 | { |
2397 | /* | |
2e4ff8af MS |
2398 | * Check the default policy, then rename the remote printer to |
2399 | * "Printer@server"... | |
b423cd4c | 2400 | */ |
ef416fc2 | 2401 | |
2e4ff8af MS |
2402 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2403 | { | |
2404 | send_http_error(con, status, NULL); | |
2405 | return; | |
2406 | } | |
2407 | ||
b423cd4c | 2408 | snprintf(newname, sizeof(newname), "%s@%s", resource + 10, |
2409 | printer->hostname); | |
2410 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2411 | |
b423cd4c | 2412 | /* |
2413 | * Add the printer as a new local printer... | |
2414 | */ | |
ef416fc2 | 2415 | |
b423cd4c | 2416 | printer = cupsdAddPrinter(resource + 10); |
2417 | modify = 0; | |
2418 | } | |
2e4ff8af MS |
2419 | else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
2420 | NULL)) != HTTP_OK) | |
2421 | { | |
2fb76298 | 2422 | send_http_error(con, status, printer); |
2e4ff8af MS |
2423 | return; |
2424 | } | |
b423cd4c | 2425 | else |
2426 | modify = 1; | |
ef416fc2 | 2427 | |
b423cd4c | 2428 | /* |
2429 | * Look for attributes and copy them over as needed... | |
2430 | */ | |
ef416fc2 | 2431 | |
b423cd4c | 2432 | need_restart_job = 0; |
ef416fc2 | 2433 | |
b423cd4c | 2434 | if ((attr = ippFindAttribute(con->request, "printer-location", |
2435 | IPP_TAG_TEXT)) != NULL) | |
2436 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
ef416fc2 | 2437 | |
b423cd4c | 2438 | if ((attr = ippFindAttribute(con->request, "printer-info", |
2439 | IPP_TAG_TEXT)) != NULL) | |
2440 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
ef416fc2 | 2441 | |
09a101d6 | 2442 | set_device_uri = 0; |
2443 | ||
b423cd4c | 2444 | if ((attr = ippFindAttribute(con->request, "device-uri", |
2445 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 2446 | { |
2447 | /* | |
b423cd4c | 2448 | * Do we have a valid device URI? |
ef416fc2 | 2449 | */ |
2450 | ||
2fb76298 MS |
2451 | http_uri_status_t uri_status; /* URI separation status */ |
2452 | ||
2453 | ||
b423cd4c | 2454 | need_restart_job = 1; |
ef416fc2 | 2455 | |
2fb76298 MS |
2456 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, |
2457 | attr->values[0].string.text, | |
2458 | scheme, sizeof(scheme), | |
2459 | username, sizeof(username), | |
2460 | host, sizeof(host), &port, | |
2461 | resource, sizeof(resource)); | |
2462 | ||
2463 | if (uri_status < HTTP_URI_OK) | |
2464 | { | |
2465 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), | |
2466 | attr->values[0].string.text); | |
2467 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2468 | "add_printer: httpSeparateURI returned %d", uri_status); | |
2469 | return; | |
2470 | } | |
b423cd4c | 2471 | |
09a101d6 | 2472 | if (!strcmp(scheme, "file")) |
ef416fc2 | 2473 | { |
2474 | /* | |
b423cd4c | 2475 | * See if the administrator has enabled file devices... |
ef416fc2 | 2476 | */ |
2477 | ||
b423cd4c | 2478 | if (!FileDevice && strcmp(resource, "/dev/null")) |
ef416fc2 | 2479 | { |
2480 | /* | |
b423cd4c | 2481 | * File devices are disabled and the URL is not file:/dev/null... |
ef416fc2 | 2482 | */ |
2483 | ||
b423cd4c | 2484 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2485 | _("File device URIs have been disabled! " | |
2486 | "To enable, see the FileDevice directive in " | |
2487 | "\"%s/cupsd.conf\"."), | |
2488 | ServerRoot); | |
ef416fc2 | 2489 | return; |
2490 | } | |
b423cd4c | 2491 | } |
2492 | else | |
2493 | { | |
ef416fc2 | 2494 | /* |
b423cd4c | 2495 | * See if the backend exists and is executable... |
ef416fc2 | 2496 | */ |
2497 | ||
09a101d6 | 2498 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme); |
b423cd4c | 2499 | if (access(srcfile, X_OK)) |
ef416fc2 | 2500 | { |
2501 | /* | |
b423cd4c | 2502 | * Could not find device in list! |
ef416fc2 | 2503 | */ |
ef416fc2 | 2504 | |
b423cd4c | 2505 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), |
2506 | attr->values[0].string.text); | |
2507 | return; | |
ef416fc2 | 2508 | } |
2509 | } | |
ef416fc2 | 2510 | |
b423cd4c | 2511 | cupsdLogMessage(CUPSD_LOG_INFO, |
2512 | "Setting %s device-uri to \"%s\" (was \"%s\".)", | |
2513 | printer->name, | |
2514 | cupsdSanitizeURI(attr->values[0].string.text, line, | |
2515 | sizeof(line)), | |
2516 | cupsdSanitizeURI(printer->device_uri, resource, | |
2517 | sizeof(resource))); | |
ef416fc2 | 2518 | |
b423cd4c | 2519 | cupsdSetString(&printer->device_uri, attr->values[0].string.text); |
09a101d6 | 2520 | set_device_uri = 1; |
ef416fc2 | 2521 | } |
2522 | ||
09a101d6 | 2523 | set_port_monitor = 0; |
2524 | ||
b423cd4c | 2525 | if ((attr = ippFindAttribute(con->request, "port-monitor", |
09a101d6 | 2526 | IPP_TAG_NAME)) != NULL) |
ef416fc2 | 2527 | { |
b423cd4c | 2528 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ |
ef416fc2 | 2529 | |
ef416fc2 | 2530 | |
b423cd4c | 2531 | need_restart_job = 1; |
ef416fc2 | 2532 | |
b423cd4c | 2533 | supported = ippFindAttribute(printer->attrs, "port-monitor-supported", |
a41f09e2 | 2534 | IPP_TAG_NAME); |
080811b1 MS |
2535 | if (supported) |
2536 | { | |
2537 | for (i = 0; i < supported->num_values; i ++) | |
2538 | if (!strcmp(supported->values[i].string.text, | |
2539 | attr->values[0].string.text)) | |
2540 | break; | |
2541 | } | |
ef416fc2 | 2542 | |
080811b1 | 2543 | if (!supported || i >= supported->num_values) |
b423cd4c | 2544 | { |
2545 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"), | |
2546 | attr->values[0].string.text); | |
2547 | return; | |
2548 | } | |
ef416fc2 | 2549 | |
b423cd4c | 2550 | cupsdLogMessage(CUPSD_LOG_INFO, |
2551 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2552 | printer->name, attr->values[0].string.text, | |
09a101d6 | 2553 | printer->port_monitor ? printer->port_monitor : "none"); |
ef416fc2 | 2554 | |
b423cd4c | 2555 | if (strcmp(attr->values[0].string.text, "none")) |
2556 | cupsdSetString(&printer->port_monitor, attr->values[0].string.text); | |
2557 | else | |
2558 | cupsdClearString(&printer->port_monitor); | |
09a101d6 | 2559 | |
2560 | set_port_monitor = 1; | |
b423cd4c | 2561 | } |
ef416fc2 | 2562 | |
b423cd4c | 2563 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
2564 | IPP_TAG_BOOLEAN)) != NULL) | |
2565 | { | |
2566 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2567 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
2568 | printer->name, attr->values[0].boolean, printer->accepting); | |
ef416fc2 | 2569 | |
b423cd4c | 2570 | printer->accepting = attr->values[0].boolean; |
2571 | cupsdAddPrinterHistory(printer); | |
2572 | } | |
bd7854cb | 2573 | |
b423cd4c | 2574 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
2575 | IPP_TAG_BOOLEAN)) != NULL) | |
2576 | { | |
2577 | if (printer->shared && !attr->values[0].boolean) | |
f7deaa1a | 2578 | cupsdDeregisterPrinter(printer, 1); |
bd7854cb | 2579 | |
b423cd4c | 2580 | cupsdLogMessage(CUPSD_LOG_INFO, |
2581 | "Setting %s printer-is-shared to %d (was %d.)", | |
2582 | printer->name, attr->values[0].boolean, printer->shared); | |
bd7854cb | 2583 | |
b423cd4c | 2584 | printer->shared = attr->values[0].boolean; |
2585 | } | |
bd7854cb | 2586 | |
b423cd4c | 2587 | if ((attr = ippFindAttribute(con->request, "printer-state", |
2588 | IPP_TAG_ENUM)) != NULL) | |
bd7854cb | 2589 | { |
b423cd4c | 2590 | if (attr->values[0].integer != IPP_PRINTER_IDLE && |
2591 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
2592 | { | |
2593 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"), | |
2594 | attr->values[0].integer); | |
2595 | return; | |
2596 | } | |
bd7854cb | 2597 | |
bc44d920 | 2598 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
2599 | printer->name, attr->values[0].integer, printer->state); | |
b423cd4c | 2600 | |
2601 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
2602 | cupsdStopPrinter(printer, 0); | |
2603 | else | |
2604 | { | |
2605 | need_restart_job = 1; | |
2606 | cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0); | |
2607 | } | |
2608 | } | |
2609 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
2610 | IPP_TAG_TEXT)) != NULL) | |
2611 | { | |
2612 | strlcpy(printer->state_message, attr->values[0].string.text, | |
2613 | sizeof(printer->state_message)); | |
2614 | cupsdAddPrinterHistory(printer); | |
bd7854cb | 2615 | } |
2616 | ||
b423cd4c | 2617 | set_printer_defaults(con, printer); |
2618 | ||
09a101d6 | 2619 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
2620 | IPP_TAG_KEYWORD)) != NULL) | |
2621 | cupsdSetAuthInfoRequired(printer, NULL, attr); | |
2622 | ||
bd7854cb | 2623 | /* |
b423cd4c | 2624 | * See if we have all required attributes... |
bd7854cb | 2625 | */ |
2626 | ||
b423cd4c | 2627 | if (!printer->device_uri) |
2628 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
bd7854cb | 2629 | |
2630 | /* | |
b423cd4c | 2631 | * See if we have an interface script or PPD file attached to the request... |
bd7854cb | 2632 | */ |
2633 | ||
b423cd4c | 2634 | if (con->filename) |
2635 | { | |
2636 | need_restart_job = 1; | |
bd7854cb | 2637 | |
b423cd4c | 2638 | strlcpy(srcfile, con->filename, sizeof(srcfile)); |
ef416fc2 | 2639 | |
b423cd4c | 2640 | if ((fp = cupsFileOpen(srcfile, "rb"))) |
2641 | { | |
2642 | /* | |
2643 | * Yes; get the first line from it... | |
2644 | */ | |
ef416fc2 | 2645 | |
b423cd4c | 2646 | line[0] = '\0'; |
2647 | cupsFileGets(fp, line, sizeof(line)); | |
2648 | cupsFileClose(fp); | |
ef416fc2 | 2649 | |
b423cd4c | 2650 | /* |
2651 | * Then see what kind of file it is... | |
2652 | */ | |
ef416fc2 | 2653 | |
b423cd4c | 2654 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2655 | printer->name); | |
ef416fc2 | 2656 | |
b423cd4c | 2657 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2658 | { | |
2659 | /* | |
2660 | * The new file is a PPD file, so remove any old interface script | |
2661 | * that might be lying around... | |
2662 | */ | |
ef416fc2 | 2663 | |
b423cd4c | 2664 | unlink(dstfile); |
2665 | } | |
2666 | else | |
2667 | { | |
2668 | /* | |
2669 | * This must be an interface script, so move the file over to the | |
2670 | * interfaces directory and make it executable... | |
2671 | */ | |
ef416fc2 | 2672 | |
b423cd4c | 2673 | if (copy_file(srcfile, dstfile)) |
2674 | { | |
2675 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2676 | _("Unable to copy interface script - %s!"), | |
2677 | strerror(errno)); | |
2678 | return; | |
2679 | } | |
568fa3fa MS |
2680 | |
2681 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2682 | "Copied interface script successfully!"); | |
2683 | chmod(dstfile, 0755); | |
b423cd4c | 2684 | } |
ef416fc2 | 2685 | |
b423cd4c | 2686 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2687 | printer->name); | |
ef416fc2 | 2688 | |
b423cd4c | 2689 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2690 | { | |
2691 | /* | |
2692 | * The new file is a PPD file, so move the file over to the | |
2693 | * ppd directory and make it readable by all... | |
2694 | */ | |
ef416fc2 | 2695 | |
b423cd4c | 2696 | if (copy_file(srcfile, dstfile)) |
2697 | { | |
2698 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2699 | _("Unable to copy PPD file - %s!"), | |
2700 | strerror(errno)); | |
2701 | return; | |
2702 | } | |
568fa3fa MS |
2703 | |
2704 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2705 | "Copied PPD file successfully!"); | |
2706 | chmod(dstfile, 0644); | |
2707 | ||
2708 | #ifdef __APPLE__ | |
2709 | /* | |
2710 | * (Re)register color profiles... | |
2711 | */ | |
2712 | ||
2713 | apple_unregister_profiles(printer); | |
2714 | apple_register_profiles(printer); | |
2715 | #endif /* __APPLE__ */ | |
b423cd4c | 2716 | } |
2717 | else | |
2718 | { | |
2719 | /* | |
2720 | * This must be an interface script, so remove any old PPD file that | |
2721 | * may be lying around... | |
2722 | */ | |
ef416fc2 | 2723 | |
b423cd4c | 2724 | unlink(dstfile); |
2725 | } | |
2726 | } | |
2727 | } | |
2728 | else if ((attr = ippFindAttribute(con->request, "ppd-name", | |
2729 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 2730 | { |
b423cd4c | 2731 | need_restart_job = 1; |
ef416fc2 | 2732 | |
b423cd4c | 2733 | if (!strcmp(attr->values[0].string.text, "raw")) |
ef416fc2 | 2734 | { |
b423cd4c | 2735 | /* |
2736 | * Raw driver, remove any existing PPD or interface script files. | |
2737 | */ | |
ef416fc2 | 2738 | |
b423cd4c | 2739 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2740 | printer->name); | |
2741 | unlink(dstfile); | |
ef416fc2 | 2742 | |
b423cd4c | 2743 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2744 | printer->name); | |
2745 | unlink(dstfile); | |
2746 | } | |
2747 | else | |
ef416fc2 | 2748 | { |
b423cd4c | 2749 | /* |
2750 | * PPD model file... | |
2751 | */ | |
2752 | ||
2753 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2754 | printer->name); | |
2755 | unlink(dstfile); | |
2756 | ||
2757 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2758 | printer->name); | |
2759 | ||
2760 | if (copy_model(con, attr->values[0].string.text, dstfile)) | |
2761 | { | |
2762 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!")); | |
2763 | return; | |
2764 | } | |
568fa3fa MS |
2765 | |
2766 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2767 | "Copied PPD file successfully!"); | |
2768 | chmod(dstfile, 0644); | |
2769 | ||
2770 | #ifdef __APPLE__ | |
2771 | /* | |
2772 | * (Re)register color profiles... | |
2773 | */ | |
2774 | ||
2775 | apple_unregister_profiles(printer); | |
2776 | apple_register_profiles(printer); | |
2777 | #endif /* __APPLE__ */ | |
ef416fc2 | 2778 | } |
2779 | } | |
2780 | ||
09a101d6 | 2781 | /* |
2782 | * If we set the device URI but not the port monitor, check which port | |
2783 | * monitor to use by default... | |
2784 | */ | |
2785 | ||
2786 | if (set_device_uri && !set_port_monitor) | |
2787 | { | |
2788 | ppd_file_t *ppd; /* PPD file */ | |
2789 | ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */ | |
2790 | ||
2791 | ||
2792 | httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme, | |
2793 | sizeof(scheme), username, sizeof(username), host, | |
2794 | sizeof(host), &port, resource, sizeof(resource)); | |
2795 | ||
2796 | snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, | |
2797 | printer->name); | |
2798 | if ((ppd = ppdOpenFile(srcfile)) != NULL) | |
2799 | { | |
2800 | for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL); | |
2801 | ppdattr; | |
2802 | ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL)) | |
2803 | if (!strcmp(scheme, ppdattr->spec)) | |
2804 | { | |
2805 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2806 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2807 | printer->name, ppdattr->value, | |
bc44d920 | 2808 | printer->port_monitor ? printer->port_monitor |
2809 | : "none"); | |
09a101d6 | 2810 | |
2811 | if (strcmp(ppdattr->value, "none")) | |
2812 | cupsdSetString(&printer->port_monitor, ppdattr->value); | |
2813 | else | |
2814 | cupsdClearString(&printer->port_monitor); | |
2815 | ||
2816 | break; | |
2817 | } | |
2818 | ||
2819 | ppdClose(ppd); | |
2820 | } | |
2821 | } | |
2822 | ||
ef416fc2 | 2823 | /* |
b423cd4c | 2824 | * Update the printer attributes and return... |
ef416fc2 | 2825 | */ |
2826 | ||
b423cd4c | 2827 | cupsdSetPrinterAttrs(printer); |
3dfe78b3 | 2828 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 2829 | |
b423cd4c | 2830 | if (need_restart_job && printer->job) |
ef416fc2 | 2831 | { |
b423cd4c | 2832 | cupsd_job_t *job; |
ef416fc2 | 2833 | |
b423cd4c | 2834 | /* |
2835 | * Stop the current job and then restart it below... | |
2836 | */ | |
ef416fc2 | 2837 | |
b423cd4c | 2838 | job = (cupsd_job_t *)printer->job; |
ef416fc2 | 2839 | |
b423cd4c | 2840 | cupsdStopJob(job, 1); |
2841 | ||
2842 | job->state->values[0].integer = IPP_JOB_PENDING; | |
2843 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 2844 | } |
2845 | ||
b423cd4c | 2846 | if (need_restart_job) |
2847 | cupsdCheckJobs(); | |
ef416fc2 | 2848 | |
3dfe78b3 | 2849 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
b423cd4c | 2850 | |
2851 | if (modify) | |
ef416fc2 | 2852 | { |
b423cd4c | 2853 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
2854 | "Printer \"%s\" modified by \"%s\".", printer->name, | |
2855 | get_username(con)); | |
ef416fc2 | 2856 | |
b423cd4c | 2857 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", |
2858 | printer->name, get_username(con)); | |
ef416fc2 | 2859 | } |
b423cd4c | 2860 | else |
2861 | { | |
2862 | cupsdAddPrinterHistory(printer); | |
ef416fc2 | 2863 | |
b423cd4c | 2864 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL, |
2865 | "New printer \"%s\" added by \"%s\".", printer->name, | |
2866 | get_username(con)); | |
ef416fc2 | 2867 | |
b423cd4c | 2868 | cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".", |
2869 | printer->name, get_username(con)); | |
2870 | } | |
2871 | ||
2872 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 2873 | } |
2874 | ||
2875 | ||
2876 | /* | |
b423cd4c | 2877 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute |
2878 | * based upon the printer state... | |
ef416fc2 | 2879 | */ |
2880 | ||
b423cd4c | 2881 | static void |
2882 | add_printer_state_reasons( | |
2883 | cupsd_client_t *con, /* I - Client connection */ | |
2884 | cupsd_printer_t *p) /* I - Printer info */ | |
ef416fc2 | 2885 | { |
b423cd4c | 2886 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2887 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
2888 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2889 | |
b423cd4c | 2890 | if (p->num_reasons == 0) |
2891 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2892 | "printer-state-reasons", NULL, | |
2893 | p->state == IPP_PRINTER_STOPPED ? "paused" : "none"); | |
2894 | else | |
2895 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2896 | "printer-state-reasons", p->num_reasons, NULL, | |
2897 | (const char * const *)p->reasons); | |
2898 | } | |
ef416fc2 | 2899 | |
ef416fc2 | 2900 | |
b423cd4c | 2901 | /* |
2902 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2903 | * the specified printer or class. | |
2904 | */ | |
ef416fc2 | 2905 | |
b423cd4c | 2906 | static void |
2907 | add_queued_job_count( | |
2908 | cupsd_client_t *con, /* I - Client connection */ | |
2909 | cupsd_printer_t *p) /* I - Printer or class */ | |
2910 | { | |
2911 | int count; /* Number of jobs on destination */ | |
ef416fc2 | 2912 | |
ef416fc2 | 2913 | |
b423cd4c | 2914 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", |
2915 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2916 | |
b423cd4c | 2917 | count = cupsdGetPrinterJobCount(p->name); |
ef416fc2 | 2918 | |
b423cd4c | 2919 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, |
2920 | "queued-job-count", count); | |
2921 | } | |
ef416fc2 | 2922 | |
ef416fc2 | 2923 | |
568fa3fa | 2924 | #ifdef __APPLE__ |
568fa3fa MS |
2925 | /* |
2926 | * 'apple_init_profile()' - Initialize a color profile. | |
2927 | */ | |
2928 | ||
2929 | static void | |
2930 | apple_init_profile( | |
a603edef MS |
2931 | ppd_file_t *ppd, /* I - PPD file */ |
2932 | cups_array_t *languages, /* I - Languages in the PPD file */ | |
568fa3fa MS |
2933 | CMDeviceProfileInfo *profile, /* I - Profile record */ |
2934 | unsigned id, /* I - Profile ID */ | |
2935 | const char *name, /* I - Profile name */ | |
a603edef | 2936 | const char *text, /* I - Profile UI text */ |
568fa3fa MS |
2937 | const char *iccfile) /* I - ICC filename */ |
2938 | { | |
2939 | char url[1024]; /* URL for profile filename */ | |
2940 | CFMutableDictionaryRef dict; /* Dictionary for name */ | |
a603edef MS |
2941 | char *language; /* Current language */ |
2942 | ppd_attr_t *attr; /* Profile attribute */ | |
db0bd74a MS |
2943 | CFStringRef cflang, /* Language string */ |
2944 | cftext; /* Localized text */ | |
568fa3fa MS |
2945 | |
2946 | ||
a603edef MS |
2947 | /* |
2948 | * Build the profile name dictionary... | |
2949 | */ | |
568fa3fa MS |
2950 | |
2951 | dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
2952 | &kCFTypeDictionaryKeyCallBacks, | |
2953 | &kCFTypeDictionaryValueCallBacks); | |
2954 | ||
db0bd74a MS |
2955 | cftext = CFStringCreateWithCString(kCFAllocatorDefault, text, |
2956 | kCFStringEncodingUTF8); | |
a603edef | 2957 | |
db0bd74a MS |
2958 | if (cftext) |
2959 | { | |
2960 | CFDictionarySetValue(dict, CFSTR("en"), cftext); | |
2961 | CFRelease(cftext); | |
2962 | } | |
2963 | ||
2964 | if (languages) | |
a603edef MS |
2965 | { |
2966 | /* | |
2967 | * Find localized names for the color profiles... | |
2968 | */ | |
2969 | ||
2970 | cupsArraySave(ppd->sorted_attrs); | |
2971 | ||
2972 | for (language = (char *)cupsArrayFirst(languages); | |
2973 | language; | |
2974 | language = (char *)cupsArrayNext(languages)) | |
2975 | { | |
db0bd74a | 2976 | if (iccfile) |
c9fc04c6 MS |
2977 | { |
2978 | if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name, | |
2979 | language)) == NULL) | |
2980 | attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language); | |
2981 | } | |
db0bd74a MS |
2982 | else |
2983 | attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language); | |
2984 | ||
2985 | if (attr && attr->text[0]) | |
2986 | { | |
2987 | cflang = CFStringCreateWithCString(kCFAllocatorDefault, language, | |
2988 | kCFStringEncodingUTF8); | |
2989 | cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text, | |
2990 | kCFStringEncodingUTF8); | |
2991 | ||
2992 | if (cflang && cftext) | |
2993 | CFDictionarySetValue(dict, cflang, cftext); | |
2994 | ||
2995 | if (cflang) | |
2996 | CFRelease(cflang); | |
2997 | ||
2998 | if (cftext) | |
2999 | CFRelease(cftext); | |
3000 | } | |
a603edef MS |
3001 | } |
3002 | ||
3003 | cupsArrayRestore(ppd->sorted_attrs); | |
3004 | } | |
3005 | ||
3006 | /* | |
3007 | * Fill in the profile data... | |
3008 | */ | |
3009 | ||
3010 | if (iccfile) | |
3011 | httpAssembleURI(HTTP_URI_CODING_ALL, url, sizeof(url), "file", NULL, "", 0, | |
3012 | iccfile); | |
3013 | ||
568fa3fa MS |
3014 | profile->dataVersion = cmDeviceProfileInfoVersion1; |
3015 | profile->profileID = id; | |
3016 | profile->profileLoc.locType = iccfile ? cmPathBasedProfile : cmNoProfileBase; | |
3017 | profile->profileName = dict; | |
3018 | ||
3019 | if (iccfile) | |
3020 | strlcpy(profile->profileLoc.u.pathLoc.path, iccfile, | |
3021 | sizeof(profile->profileLoc.u.pathLoc.path)); | |
568fa3fa MS |
3022 | } |
3023 | ||
3024 | ||
3025 | /* | |
3026 | * 'apple_register_profiles()' - Register color profiles for a printer. | |
3027 | */ | |
3028 | ||
3029 | static void | |
3030 | apple_register_profiles( | |
3031 | cupsd_printer_t *p) /* I - Printer */ | |
3032 | { | |
3033 | int i; /* Looping var */ | |
3034 | char ppdfile[1024], /* PPD filename */ | |
db0bd74a MS |
3035 | iccfile[1024], /* ICC filename */ |
3036 | selector[PPD_MAX_NAME]; | |
3037 | /* Profile selection string */ | |
568fa3fa | 3038 | ppd_file_t *ppd; /* PPD file */ |
01ce6322 MS |
3039 | ppd_attr_t *attr, /* Profile attributes */ |
3040 | *profileid_attr,/* cupsProfileID attribute */ | |
3041 | *q1_attr, /* ColorModel (or other) qualifier */ | |
3042 | *q2_attr, /* MediaType (or other) qualifier */ | |
3043 | *q3_attr; /* Resolution (or other) qualifier */ | |
3044 | char q_keyword[PPD_MAX_NAME]; | |
3045 | /* Qualifier keyword */ | |
3046 | const char *q1_choice, /* ColorModel (or other) choice */ | |
3047 | *q2_choice, /* MediaType (or other) choice */ | |
3048 | *q3_choice; /* Resolution (or other) choice */ | |
c9fc04c6 | 3049 | const char *profile_key; /* Profile keyword */ |
568fa3fa | 3050 | ppd_option_t *cm_option; /* Color model option */ |
01ce6322 | 3051 | ppd_choice_t *cm_choice; /* Color model choice */ |
568fa3fa MS |
3052 | int num_profiles; /* Number of profiles */ |
3053 | CMError error; /* Last error */ | |
db0bd74a MS |
3054 | unsigned device_id, /* Printer device ID */ |
3055 | profile_id, /* Profile ID */ | |
3056 | default_profile_id = 0; | |
3057 | /* Default profile ID */ | |
3058 | CFMutableDictionaryRef device_name; /* Printer device name dictionary */ | |
3059 | CFStringRef printer_name; /* Printer name string */ | |
568fa3fa MS |
3060 | CMDeviceScope scope = /* Scope of the registration */ |
3061 | { | |
3062 | kCFPreferencesAnyUser, | |
3063 | kCFPreferencesCurrentHost | |
3064 | }; | |
3065 | CMDeviceProfileArrayPtr profiles; /* Profiles */ | |
3066 | CMDeviceProfileInfo *profile; /* Current profile */ | |
a603edef | 3067 | cups_array_t *languages; /* Languages array */ |
568fa3fa MS |
3068 | |
3069 | ||
64a69576 MS |
3070 | /* |
3071 | * Make sure ColorSync is available... | |
3072 | */ | |
3073 | ||
3074 | if (CMRegisterColorDevice == NULL) | |
3075 | return; | |
3076 | ||
568fa3fa MS |
3077 | /* |
3078 | * Try opening the PPD file for this printer... | |
3079 | */ | |
3080 | ||
3081 | snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name); | |
3082 | if ((ppd = ppdOpenFile(ppdfile)) == NULL) | |
3083 | return; | |
3084 | ||
3085 | /* | |
3086 | * See if we have any profiles... | |
3087 | */ | |
3088 | ||
c9fc04c6 MS |
3089 | if ((attr = ppdFindAttr(ppd, "APTiogaProfile", NULL)) != NULL) |
3090 | profile_key = "APTiogaProfile"; | |
3091 | else | |
3092 | { | |
3093 | attr = ppdFindAttr(ppd, "cupsICCProfile", NULL); | |
3094 | profile_key = "cupsICCProfile"; | |
3095 | } | |
3096 | ||
3097 | for (num_profiles = 0; attr; attr = ppdFindNextAttr(ppd, profile_key, NULL)) | |
568fa3fa MS |
3098 | if (attr->spec[0] && attr->value && attr->value[0]) |
3099 | { | |
3100 | if (attr->value[0] != '/') | |
3101 | snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir, | |
3102 | attr->value); | |
3103 | else | |
3104 | strlcpy(iccfile, attr->value, sizeof(iccfile)); | |
3105 | ||
3106 | if (access(iccfile, 0)) | |
3107 | continue; | |
3108 | ||
3109 | num_profiles ++; | |
3110 | } | |
3111 | ||
c9fc04c6 | 3112 | |
568fa3fa MS |
3113 | /* |
3114 | * If we have profiles, add them... | |
3115 | */ | |
3116 | ||
3117 | if (num_profiles > 0) | |
3118 | { | |
01ce6322 MS |
3119 | if (profile_key[0] == 'A') |
3120 | { | |
3121 | /* | |
3122 | * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile | |
3123 | * attribute... | |
3124 | */ | |
db0bd74a | 3125 | |
01ce6322 MS |
3126 | if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL && |
3127 | attr->value) | |
3128 | default_profile_id = atoi(attr->value); | |
db0bd74a | 3129 | |
01ce6322 MS |
3130 | q1_choice = q2_choice = q3_choice = NULL; |
3131 | } | |
db0bd74a | 3132 | else |
01ce6322 MS |
3133 | { |
3134 | /* | |
3135 | * For CUPS PPDs, figure out the default profile selector values... | |
3136 | */ | |
db0bd74a | 3137 | |
01ce6322 MS |
3138 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL && |
3139 | attr->value && attr->value[0]) | |
3140 | { | |
3141 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3142 | q1_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3143 | } | |
3144 | else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL) | |
3145 | q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL); | |
3146 | ||
3147 | if (q1_attr && q1_attr->value && q1_attr->value[0]) | |
3148 | q1_choice = q1_attr->value; | |
3149 | else | |
3150 | q1_choice = ""; | |
3151 | ||
3152 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL && | |
3153 | attr->value && attr->value[0]) | |
3154 | { | |
3155 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3156 | q2_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3157 | } | |
3158 | else | |
3159 | q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL); | |
3160 | ||
3161 | if (q2_attr && q2_attr->value && q2_attr->value[0]) | |
3162 | q2_choice = q2_attr->value; | |
3163 | else | |
3164 | q2_choice = NULL; | |
3165 | ||
3166 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL && | |
3167 | attr->value && attr->value[0]) | |
3168 | { | |
3169 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3170 | q3_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3171 | } | |
3172 | else | |
3173 | q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL); | |
3174 | ||
3175 | if (q3_attr && q3_attr->value && q3_attr->value[0]) | |
3176 | q3_choice = q3_attr->value; | |
3177 | else | |
3178 | q3_choice = NULL; | |
3179 | } | |
db0bd74a | 3180 | |
568fa3fa MS |
3181 | /* |
3182 | * Build the array of profiles... | |
3183 | * | |
3184 | * Note: This calloc actually requests slightly more memory than needed. | |
3185 | */ | |
3186 | ||
3187 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) | |
3188 | { | |
3189 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3190 | "Unable to allocate memory for %d profiles!", | |
3191 | num_profiles); | |
3192 | ppdClose(ppd); | |
3193 | return; | |
3194 | } | |
3195 | ||
3196 | profiles->profileCount = num_profiles; | |
a603edef | 3197 | languages = _ppdGetLanguages(ppd); |
568fa3fa MS |
3198 | |
3199 | for (profile = profiles->profiles, | |
c9fc04c6 | 3200 | attr = ppdFindAttr(ppd, profile_key, NULL); |
568fa3fa | 3201 | attr; |
c9fc04c6 | 3202 | attr = ppdFindNextAttr(ppd, profile_key, NULL)) |
568fa3fa MS |
3203 | if (attr->spec[0] && attr->value && attr->value[0]) |
3204 | { | |
3205 | /* | |
3206 | * Add this profile... | |
3207 | */ | |
3208 | ||
3209 | if (attr->value[0] != '/') | |
3210 | snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir, | |
3211 | attr->value); | |
3212 | else | |
3213 | strlcpy(iccfile, attr->value, sizeof(iccfile)); | |
3214 | ||
3215 | if (access(iccfile, 0)) | |
3216 | continue; | |
3217 | ||
c9fc04c6 MS |
3218 | if (profile_key[0] == 'c') |
3219 | { | |
3220 | cupsArraySave(ppd->sorted_attrs); | |
db0bd74a | 3221 | |
c9fc04c6 MS |
3222 | if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID", |
3223 | attr->spec)) != NULL && | |
3224 | profileid_attr->value && isdigit(profileid_attr->value[0] & 255)) | |
3225 | profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10); | |
3226 | else | |
3227 | profile_id = _ppdHashName(attr->spec); | |
db0bd74a | 3228 | |
c9fc04c6 MS |
3229 | cupsArrayRestore(ppd->sorted_attrs); |
3230 | } | |
3231 | else | |
3232 | profile_id = atoi(attr->spec); | |
db0bd74a MS |
3233 | |
3234 | apple_init_profile(ppd, languages, profile, profile_id, attr->spec, | |
3235 | attr->text[0] ? attr->text : attr->spec, iccfile); | |
568fa3fa MS |
3236 | |
3237 | profile ++; | |
db0bd74a MS |
3238 | |
3239 | /* | |
3240 | * See if this is the default profile... | |
3241 | */ | |
3242 | ||
01ce6322 | 3243 | if (!default_profile_id) |
db0bd74a MS |
3244 | { |
3245 | if (q2_choice) | |
3246 | { | |
3247 | if (q3_choice) | |
3248 | { | |
3249 | snprintf(selector, sizeof(selector), "%s.%s.%s", | |
01ce6322 | 3250 | q1_choice, q2_choice, q3_choice); |
db0bd74a MS |
3251 | if (!strcmp(selector, attr->spec)) |
3252 | default_profile_id = profile_id; | |
3253 | } | |
3254 | ||
3255 | if (!default_profile_id) | |
3256 | { | |
01ce6322 MS |
3257 | snprintf(selector, sizeof(selector), "%s.%s.", q1_choice, |
3258 | q2_choice); | |
db0bd74a MS |
3259 | if (!strcmp(selector, attr->spec)) |
3260 | default_profile_id = profile_id; | |
3261 | } | |
3262 | } | |
3263 | ||
3264 | if (!default_profile_id && q3_choice) | |
3265 | { | |
01ce6322 MS |
3266 | snprintf(selector, sizeof(selector), "%s..%s", q1_choice, |
3267 | q3_choice); | |
db0bd74a MS |
3268 | if (!strcmp(selector, attr->spec)) |
3269 | default_profile_id = profile_id; | |
3270 | } | |
3271 | ||
3272 | if (!default_profile_id) | |
3273 | { | |
01ce6322 | 3274 | snprintf(selector, sizeof(selector), "%s..", q1_choice); |
db0bd74a MS |
3275 | if (!strcmp(selector, attr->spec)) |
3276 | default_profile_id = profile_id; | |
3277 | } | |
3278 | } | |
568fa3fa | 3279 | } |
a603edef MS |
3280 | |
3281 | _ppdFreeLanguages(languages); | |
568fa3fa MS |
3282 | } |
3283 | else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL) | |
3284 | { | |
3285 | /* | |
db0bd74a | 3286 | * Extract profiles from ColorModel option... |
568fa3fa MS |
3287 | */ |
3288 | ||
db0bd74a | 3289 | const char *profile_name; /* Name of generic profile */ |
568fa3fa | 3290 | |
db0bd74a MS |
3291 | |
3292 | num_profiles = cm_option->num_choices; | |
568fa3fa MS |
3293 | |
3294 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) | |
3295 | { | |
3296 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3297 | "Unable to allocate memory for %d profiles!", | |
3298 | num_profiles); | |
3299 | ppdClose(ppd); | |
3300 | return; | |
3301 | } | |
3302 | ||
3303 | profiles->profileCount = num_profiles; | |
3304 | ||
3305 | for (profile = profiles->profiles, i = cm_option->num_choices, | |
3306 | cm_choice = cm_option->choices; | |
3307 | i > 0; | |
db0bd74a MS |
3308 | i --, cm_choice ++, profile ++) |
3309 | { | |
3310 | if (!strcmp(cm_choice->choice, "Gray") || | |
3311 | !strcmp(cm_choice->choice, "Black")) | |
3312 | profile_name = "Gray"; | |
3313 | else if (!strcmp(cm_choice->choice, "RGB") || | |
3314 | !strcmp(cm_choice->choice, "CMY")) | |
3315 | profile_name = "RGB"; | |
3316 | else if (!strcmp(cm_choice->choice, "CMYK") || | |
3317 | !strcmp(cm_choice->choice, "KCMY")) | |
3318 | profile_name = "CMYK"; | |
3319 | else | |
3320 | profile_name = "DeviceN"; | |
3321 | ||
3322 | snprintf(selector, sizeof(selector), "%s..", profile_name); | |
3323 | profile_id = _ppdHashName(selector); | |
3324 | ||
3325 | apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice, | |
3326 | cm_choice->text, NULL); | |
3327 | ||
3328 | if (cm_choice->marked) | |
3329 | default_profile_id = profile_id; | |
3330 | } | |
568fa3fa MS |
3331 | } |
3332 | else | |
3333 | { | |
3334 | /* | |
3335 | * Use the default colorspace... | |
3336 | */ | |
3337 | ||
db0bd74a | 3338 | num_profiles = 1 + ppd->colorspace != PPD_CS_GRAY; |
568fa3fa | 3339 | |
db0bd74a | 3340 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) |
568fa3fa MS |
3341 | { |
3342 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3343 | "Unable to allocate memory for %d profiles!", | |
3344 | num_profiles); | |
3345 | ppdClose(ppd); | |
3346 | return; | |
3347 | } | |
3348 | ||
db0bd74a MS |
3349 | profiles->profileCount = num_profiles; |
3350 | ||
3351 | apple_init_profile(ppd, NULL, profiles->profiles, _ppdHashName("Gray.."), | |
3352 | "Gray", "Gray", NULL); | |
568fa3fa MS |
3353 | |
3354 | switch (ppd->colorspace) | |
3355 | { | |
568fa3fa | 3356 | case PPD_CS_RGB : |
db0bd74a MS |
3357 | case PPD_CS_CMY : |
3358 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
a603edef | 3359 | _ppdHashName("RGB.."), "RGB", "RGB", NULL); |
568fa3fa | 3360 | break; |
db0bd74a | 3361 | case PPD_CS_RGBK : |
568fa3fa | 3362 | case PPD_CS_CMYK : |
db0bd74a | 3363 | apple_init_profile(ppd, NULL, profiles->profiles + 1, |
a603edef | 3364 | _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL); |
568fa3fa MS |
3365 | break; |
3366 | ||
db0bd74a MS |
3367 | case PPD_CS_N : |
3368 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
3369 | _ppdHashName("DeviceN.."), "DeviceN", "DeviceN", | |
3370 | NULL); | |
3371 | break; | |
3372 | ||
568fa3fa | 3373 | default : |
568fa3fa MS |
3374 | break; |
3375 | } | |
3376 | } | |
3377 | ||
3378 | if (num_profiles > 0) | |
3379 | { | |
db0bd74a MS |
3380 | /* |
3381 | * Make sure we have a default profile ID... | |
3382 | */ | |
3383 | ||
3384 | if (!default_profile_id) | |
3385 | default_profile_id = profiles->profiles[num_profiles - 1].profileID; | |
3386 | ||
568fa3fa MS |
3387 | /* |
3388 | * Get the device ID hash and pathelogical name dictionary. | |
3389 | */ | |
3390 | ||
3391 | cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"", | |
3392 | p->name); | |
3393 | ||
db0bd74a MS |
3394 | device_id = _ppdHashName(p->name); |
3395 | device_name = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
3396 | &kCFTypeDictionaryKeyCallBacks, | |
3397 | &kCFTypeDictionaryValueCallBacks); | |
9a4f8274 MS |
3398 | printer_name = CFStringCreateWithCString(kCFAllocatorDefault, |
3399 | p->name, kCFStringEncodingUTF8); | |
568fa3fa | 3400 | |
db0bd74a MS |
3401 | if (device_name && printer_name) |
3402 | { | |
3403 | CFDictionarySetValue(device_name, CFSTR("en"), printer_name); | |
568fa3fa | 3404 | |
db0bd74a MS |
3405 | /* |
3406 | * Register the device with ColorSync... | |
3407 | */ | |
568fa3fa | 3408 | |
db0bd74a MS |
3409 | error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id, |
3410 | device_name, &scope); | |
568fa3fa | 3411 | |
db0bd74a MS |
3412 | /* |
3413 | * Register the profiles... | |
3414 | */ | |
568fa3fa | 3415 | |
db0bd74a MS |
3416 | if (error == noErr) |
3417 | error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id, | |
3418 | default_profile_id, profiles); | |
3419 | } | |
3420 | else | |
3421 | error = 1000; | |
568fa3fa MS |
3422 | |
3423 | /* | |
3424 | * Clean up... | |
3425 | */ | |
3426 | ||
3427 | if (error != noErr) | |
3428 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3429 | "Unable to register ICC color profiles for \"%s\" - %d", | |
3430 | p->name, (int)error); | |
3431 | ||
3432 | for (profile = profiles->profiles; | |
3433 | num_profiles > 0; | |
3434 | profile ++, num_profiles --) | |
3435 | CFRelease(profile->profileName); | |
3436 | ||
3437 | free(profiles); | |
db0bd74a MS |
3438 | |
3439 | if (printer_name) | |
3440 | CFRelease(printer_name); | |
3441 | ||
3442 | if (device_name) | |
3443 | CFRelease(device_name); | |
568fa3fa MS |
3444 | } |
3445 | ||
3446 | ppdClose(ppd); | |
3447 | } | |
3448 | ||
3449 | ||
3450 | /* | |
3451 | * 'apple_unregister_profiles()' - Remove color profiles for the specified | |
3452 | * printer. | |
3453 | */ | |
3454 | ||
3455 | static void | |
3456 | apple_unregister_profiles( | |
3457 | cupsd_printer_t *p) /* I - Printer */ | |
3458 | { | |
64a69576 MS |
3459 | /* |
3460 | * Make sure ColorSync is available... | |
3461 | */ | |
3462 | ||
3463 | if (CMUnregisterColorDevice != NULL) | |
3464 | CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name)); | |
568fa3fa MS |
3465 | } |
3466 | #endif /* __APPLE__ */ | |
3467 | ||
b423cd4c | 3468 | /* |
3469 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
3470 | */ | |
ef416fc2 | 3471 | |
b423cd4c | 3472 | static void |
3473 | apply_printer_defaults( | |
3474 | cupsd_printer_t *printer, /* I - Printer */ | |
3475 | cupsd_job_t *job) /* I - Job */ | |
3476 | { | |
3477 | int i, /* Looping var */ | |
3478 | num_options; /* Number of default options */ | |
3479 | cups_option_t *options, /* Default options */ | |
f7deaa1a | 3480 | *option; /* Current option */ |
ef416fc2 | 3481 | |
ef416fc2 | 3482 | |
b423cd4c | 3483 | /* |
3484 | * Collect all of the default options and add the missing ones to the | |
3485 | * job object... | |
3486 | */ | |
ef416fc2 | 3487 | |
1f0275e3 MS |
3488 | for (i = printer->num_options, num_options = 0, options = NULL, |
3489 | option = printer->options; | |
b423cd4c | 3490 | i > 0; |
3491 | i --, option ++) | |
3492 | if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO)) | |
3493 | { | |
3494 | num_options = cupsAddOption(option->name, option->value, num_options, | |
3495 | &options); | |
3496 | } | |
ef416fc2 | 3497 | |
b423cd4c | 3498 | /* |
3499 | * Encode these options as attributes in the job object... | |
3500 | */ | |
ef416fc2 | 3501 | |
b423cd4c | 3502 | cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB); |
3503 | cupsFreeOptions(num_options, options); | |
3504 | } | |
3505 | ||
3506 | ||
3507 | /* | |
3508 | * 'authenticate_job()' - Set job authentication info. | |
3509 | */ | |
3510 | ||
3511 | static void | |
3512 | authenticate_job(cupsd_client_t *con, /* I - Client connection */ | |
3513 | ipp_attribute_t *uri) /* I - Job URI */ | |
3514 | { | |
f7deaa1a | 3515 | ipp_attribute_t *attr, /* job-id attribute */ |
3516 | *auth_info; /* auth-info attribute */ | |
b423cd4c | 3517 | int jobid; /* Job ID */ |
3518 | cupsd_job_t *job; /* Current job */ | |
3519 | char method[HTTP_MAX_URI], | |
3520 | /* Method portion of URI */ | |
3521 | username[HTTP_MAX_URI], | |
3522 | /* Username portion of URI */ | |
3523 | host[HTTP_MAX_URI], | |
3524 | /* Host portion of URI */ | |
3525 | resource[HTTP_MAX_URI]; | |
3526 | /* Resource portion of URI */ | |
3527 | int port; /* Port portion of URI */ | |
ef416fc2 | 3528 | |
ef416fc2 | 3529 | |
b423cd4c | 3530 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", |
3531 | con, con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 3532 | |
b423cd4c | 3533 | /* |
3534 | * Start with "everything is OK" status... | |
3535 | */ | |
ef416fc2 | 3536 | |
b423cd4c | 3537 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 3538 | |
b423cd4c | 3539 | /* |
3540 | * See if we have a job URI or a printer URI... | |
3541 | */ | |
ef416fc2 | 3542 | |
b423cd4c | 3543 | if (!strcmp(uri->name, "printer-uri")) |
3544 | { | |
3545 | /* | |
3546 | * Got a printer URI; see if we also have a job-id attribute... | |
3547 | */ | |
ef416fc2 | 3548 | |
b423cd4c | 3549 | if ((attr = ippFindAttribute(con->request, "job-id", |
3550 | IPP_TAG_INTEGER)) == NULL) | |
3551 | { | |
3552 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3553 | _("Got a printer-uri attribute but no job-id!")); | |
3554 | return; | |
3555 | } | |
3556 | ||
3557 | jobid = attr->values[0].integer; | |
3558 | } | |
3559 | else | |
3560 | { | |
3561 | /* | |
3562 | * Got a job URI; parse it to get the job ID... | |
3563 | */ | |
ef416fc2 | 3564 | |
b423cd4c | 3565 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
3566 | sizeof(method), username, sizeof(username), host, | |
3567 | sizeof(host), &port, resource, sizeof(resource)); | |
f7deaa1a | 3568 | |
b423cd4c | 3569 | if (strncmp(resource, "/jobs/", 6)) |
3570 | { | |
3571 | /* | |
3572 | * Not a valid URI! | |
3573 | */ | |
ef416fc2 | 3574 | |
b423cd4c | 3575 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"), |
3576 | uri->values[0].string.text); | |
3577 | return; | |
3578 | } | |
ef416fc2 | 3579 | |
b423cd4c | 3580 | jobid = atoi(resource + 6); |
3581 | } | |
ef416fc2 | 3582 | |
b423cd4c | 3583 | /* |
3584 | * See if the job exists... | |
3585 | */ | |
ef416fc2 | 3586 | |
b423cd4c | 3587 | if ((job = cupsdFindJob(jobid)) == NULL) |
3588 | { | |
3589 | /* | |
3590 | * Nope - return a "not found" error... | |
3591 | */ | |
ef416fc2 | 3592 | |
b423cd4c | 3593 | send_ipp_status(con, IPP_NOT_FOUND, |
3594 | _("Job #%d does not exist!"), jobid); | |
ef416fc2 | 3595 | return; |
b423cd4c | 3596 | } |
ef416fc2 | 3597 | |
b423cd4c | 3598 | /* |
3599 | * See if the job has been completed... | |
3600 | */ | |
3601 | ||
3602 | if (job->state_value != IPP_JOB_HELD) | |
ef416fc2 | 3603 | { |
3604 | /* | |
b423cd4c | 3605 | * Return a "not-possible" error... |
ef416fc2 | 3606 | */ |
3607 | ||
b423cd4c | 3608 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
3609 | _("Job #%d is not held for authentication!"), | |
3610 | jobid); | |
3611 | return; | |
3612 | } | |
ef416fc2 | 3613 | |
b423cd4c | 3614 | /* |
3615 | * See if we have already authenticated... | |
3616 | */ | |
3617 | ||
f7deaa1a | 3618 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
3619 | ||
3620 | if (!con->username[0] && !auth_info) | |
b423cd4c | 3621 | { |
db1f069b MS |
3622 | cupsd_printer_t *printer; /* Job destination */ |
3623 | ||
3624 | ||
3625 | /* | |
3626 | * No auth data. If we need to authenticate via Kerberos, send a | |
3627 | * HTTP auth challenge, otherwise just return an IPP error... | |
3628 | */ | |
3629 | ||
3630 | printer = cupsdFindDest(job->dest); | |
3631 | ||
3632 | if (printer && printer->num_auth_info_required > 0 && | |
3633 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
3634 | send_http_error(con, HTTP_UNAUTHORIZED, printer); | |
3635 | else | |
3636 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
3637 | _("No authentication information provided!")); | |
b423cd4c | 3638 | return; |
3639 | } | |
3640 | ||
3641 | /* | |
3642 | * See if the job is owned by the requesting user... | |
3643 | */ | |
3644 | ||
3645 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3646 | { | |
2fb76298 | 3647 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 3648 | return; |
3649 | } | |
3650 | ||
3651 | /* | |
3652 | * Save the authentication information for this job... | |
3653 | */ | |
3654 | ||
f7deaa1a | 3655 | save_auth_info(con, job, auth_info); |
b423cd4c | 3656 | |
3657 | /* | |
3658 | * Reset the job-hold-until value to "no-hold"... | |
3659 | */ | |
3660 | ||
3661 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
3662 | IPP_TAG_KEYWORD)) == NULL) | |
3663 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
3664 | ||
3665 | if (attr) | |
3666 | { | |
3667 | attr->value_tag = IPP_TAG_KEYWORD; | |
3668 | cupsdSetString(&(attr->values[0].string.text), "no-hold"); | |
ef416fc2 | 3669 | } |
b423cd4c | 3670 | |
3671 | /* | |
3672 | * Release the job and return... | |
3673 | */ | |
3674 | ||
3675 | cupsdReleaseJob(job); | |
3676 | ||
75bd9771 | 3677 | cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username); |
ef416fc2 | 3678 | } |
3679 | ||
3680 | ||
3681 | /* | |
b423cd4c | 3682 | * 'cancel_all_jobs()' - Cancel all print jobs. |
ef416fc2 | 3683 | */ |
3684 | ||
b423cd4c | 3685 | static void |
3686 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
3687 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 3688 | { |
b423cd4c | 3689 | http_status_t status; /* Policy status */ |
b423cd4c | 3690 | cups_ptype_t dtype; /* Destination type */ |
f7deaa1a | 3691 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 3692 | userpass[HTTP_MAX_URI], /* Username portion of URI */ |
f7deaa1a | 3693 | hostname[HTTP_MAX_URI], /* Host portion of URI */ |
b423cd4c | 3694 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
3695 | int port; /* Port portion of URI */ | |
3696 | ipp_attribute_t *attr; /* Attribute in request */ | |
3697 | const char *username; /* Username */ | |
3698 | int purge; /* Purge? */ | |
3699 | cupsd_printer_t *printer; /* Printer */ | |
ef416fc2 | 3700 | |
3701 | ||
b423cd4c | 3702 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, |
3703 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 3704 | |
3705 | /* | |
b423cd4c | 3706 | * See if we have a printer URI... |
ef416fc2 | 3707 | */ |
3708 | ||
b423cd4c | 3709 | if (strcmp(uri->name, "printer-uri")) |
3710 | { | |
3711 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3712 | _("The printer-uri attribute is required!")); | |
3713 | return; | |
3714 | } | |
ef416fc2 | 3715 | |
3716 | /* | |
b423cd4c | 3717 | * Get the username (if any) for the jobs we want to cancel (only if |
3718 | * "my-jobs" is specified... | |
ef416fc2 | 3719 | */ |
3720 | ||
b423cd4c | 3721 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
3722 | IPP_TAG_BOOLEAN)) != NULL && | |
3723 | attr->values[0].boolean) | |
ef416fc2 | 3724 | { |
b423cd4c | 3725 | if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
3726 | IPP_TAG_NAME)) != NULL) | |
3727 | username = attr->values[0].string.text; | |
3728 | else | |
3729 | { | |
3730 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3731 | _("Missing requesting-user-name attribute!")); | |
3732 | return; | |
3733 | } | |
ef416fc2 | 3734 | } |
b423cd4c | 3735 | else |
3736 | username = NULL; | |
ef416fc2 | 3737 | |
b423cd4c | 3738 | /* |
3739 | * Look for the "purge-jobs" attribute... | |
3740 | */ | |
3741 | ||
3742 | if ((attr = ippFindAttribute(con->request, "purge-jobs", | |
3743 | IPP_TAG_BOOLEAN)) != NULL) | |
3744 | purge = attr->values[0].boolean; | |
3745 | else | |
3746 | purge = 1; | |
ef416fc2 | 3747 | |
3748 | /* | |
b423cd4c | 3749 | * And if the destination is valid... |
ef416fc2 | 3750 | */ |
3751 | ||
f7deaa1a | 3752 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3753 | { |
3754 | /* | |
b423cd4c | 3755 | * Bad URI? |
ef416fc2 | 3756 | */ |
3757 | ||
f7deaa1a | 3758 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, |
3759 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
3760 | hostname, sizeof(hostname), &port, | |
3761 | resource, sizeof(resource)); | |
3762 | ||
b423cd4c | 3763 | if ((!strncmp(resource, "/printers/", 10) && resource[10]) || |
3764 | (!strncmp(resource, "/classes/", 9) && resource[9])) | |
3765 | { | |
3766 | send_ipp_status(con, IPP_NOT_FOUND, | |
3767 | _("The printer or class was not found.")); | |
3768 | return; | |
3769 | } | |
ef416fc2 | 3770 | |
b423cd4c | 3771 | /* |
3772 | * Check policy... | |
3773 | */ | |
3774 | ||
3775 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
3776 | { | |
f899b121 | 3777 | send_http_error(con, status, NULL); |
b423cd4c | 3778 | return; |
3779 | } | |
ef416fc2 | 3780 | |
ef416fc2 | 3781 | /* |
b423cd4c | 3782 | * Cancel all jobs on all printers... |
ef416fc2 | 3783 | */ |
3784 | ||
b423cd4c | 3785 | cupsdCancelJobs(NULL, username, purge); |
ef416fc2 | 3786 | |
b423cd4c | 3787 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", |
d09495fa | 3788 | purge ? "purged" : "canceled", get_username(con)); |
ef416fc2 | 3789 | } |
b423cd4c | 3790 | else |
ef416fc2 | 3791 | { |
3792 | /* | |
b423cd4c | 3793 | * Check policy... |
ef416fc2 | 3794 | */ |
3795 | ||
f7deaa1a | 3796 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
3797 | NULL)) != HTTP_OK) | |
b423cd4c | 3798 | { |
f899b121 | 3799 | send_http_error(con, status, printer); |
b423cd4c | 3800 | return; |
3801 | } | |
ef416fc2 | 3802 | |
b423cd4c | 3803 | /* |
3804 | * Cancel all of the jobs on the named printer... | |
3805 | */ | |
3806 | ||
f7deaa1a | 3807 | cupsdCancelJobs(printer->name, username, purge); |
b423cd4c | 3808 | |
3809 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
f7deaa1a | 3810 | printer->name, purge ? "purged" : "canceled", |
3811 | get_username(con)); | |
ef416fc2 | 3812 | } |
3813 | ||
b423cd4c | 3814 | con->response->request.status.status_code = IPP_OK; |
3815 | } | |
3816 | ||
3817 | ||
3818 | /* | |
3819 | * 'cancel_job()' - Cancel a print job. | |
3820 | */ | |
3821 | ||
3822 | static void | |
3823 | cancel_job(cupsd_client_t *con, /* I - Client connection */ | |
3824 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
3825 | { | |
3826 | ipp_attribute_t *attr; /* Current attribute */ | |
3827 | int jobid; /* Job ID */ | |
f7deaa1a | 3828 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 3829 | username[HTTP_MAX_URI], /* Username portion of URI */ |
3830 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
3831 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
3832 | int port; /* Port portion of URI */ | |
3833 | cupsd_job_t *job; /* Job information */ | |
bc44d920 | 3834 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 3835 | cupsd_printer_t *printer; /* Printer data */ |
0a682745 | 3836 | int purge; /* Purge the job? */ |
b423cd4c | 3837 | |
3838 | ||
3839 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
3840 | con->http.fd, uri->values[0].string.text); | |
3841 | ||
ef416fc2 | 3842 | /* |
b423cd4c | 3843 | * See if we have a job URI or a printer URI... |
ef416fc2 | 3844 | */ |
3845 | ||
b423cd4c | 3846 | if (!strcmp(uri->name, "printer-uri")) |
3847 | { | |
3848 | /* | |
3849 | * Got a printer URI; see if we also have a job-id attribute... | |
3850 | */ | |
3851 | ||
3852 | if ((attr = ippFindAttribute(con->request, "job-id", | |
3853 | IPP_TAG_INTEGER)) == NULL) | |
3854 | { | |
3855 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3856 | _("Got a printer-uri attribute but no job-id!")); | |
3857 | return; | |
3858 | } | |
3859 | ||
3860 | if ((jobid = attr->values[0].integer) == 0) | |
ef416fc2 | 3861 | { |
3862 | /* | |
b423cd4c | 3863 | * Find the current job on the specified printer... |
ef416fc2 | 3864 | */ |
3865 | ||
f7deaa1a | 3866 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3867 | { |
3868 | /* | |
b423cd4c | 3869 | * Bad URI... |
ef416fc2 | 3870 | */ |
3871 | ||
b423cd4c | 3872 | send_ipp_status(con, IPP_NOT_FOUND, |
3873 | _("The printer or class was not found.")); | |
3874 | return; | |
ef416fc2 | 3875 | } |
3876 | ||
3877 | /* | |
b423cd4c | 3878 | * See if the printer is currently printing a job... |
ef416fc2 | 3879 | */ |
3880 | ||
b423cd4c | 3881 | if (printer->job) |
3882 | jobid = ((cupsd_job_t *)printer->job)->id; | |
ef416fc2 | 3883 | else |
ef416fc2 | 3884 | { |
3885 | /* | |
b423cd4c | 3886 | * No, see if there are any pending jobs... |
ef416fc2 | 3887 | */ |
f7deaa1a | 3888 | |
b423cd4c | 3889 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
3890 | job; | |
3891 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
3892 | if (job->state_value <= IPP_JOB_PROCESSING && | |
f7deaa1a | 3893 | !strcasecmp(job->dest, printer->name)) |
b423cd4c | 3894 | break; |
ef416fc2 | 3895 | |
b423cd4c | 3896 | if (job) |
3897 | jobid = job->id; | |
3898 | else | |
ef416fc2 | 3899 | { |
355e94dc MS |
3900 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
3901 | job; | |
3902 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
3903 | if (job->state_value == IPP_JOB_STOPPED && | |
3904 | !strcasecmp(job->dest, printer->name)) | |
3905 | break; | |
3906 | ||
3907 | if (job) | |
3908 | jobid = job->id; | |
3909 | else | |
3910 | { | |
3911 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"), | |
3912 | printer->name); | |
3913 | return; | |
3914 | } | |
b423cd4c | 3915 | } |
3916 | } | |
3917 | } | |
3918 | } | |
3919 | else | |
3920 | { | |
3921 | /* | |
3922 | * Got a job URI; parse it to get the job ID... | |
3923 | */ | |
ef416fc2 | 3924 | |
f7deaa1a | 3925 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
3926 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 3927 | sizeof(host), &port, resource, sizeof(resource)); |
f7deaa1a | 3928 | |
b423cd4c | 3929 | if (strncmp(resource, "/jobs/", 6)) |
3930 | { | |
3931 | /* | |
3932 | * Not a valid URI! | |
3933 | */ | |
ef416fc2 | 3934 | |
b423cd4c | 3935 | send_ipp_status(con, IPP_BAD_REQUEST, |
3936 | _("Bad job-uri attribute \"%s\"!"), | |
3937 | uri->values[0].string.text); | |
3938 | return; | |
3939 | } | |
3940 | ||
3941 | jobid = atoi(resource + 6); | |
3942 | } | |
3943 | ||
0a682745 MS |
3944 | /* |
3945 | * Look for the "purge-job" attribute... | |
3946 | */ | |
3947 | ||
3948 | if ((attr = ippFindAttribute(con->request, "purge-job", | |
3949 | IPP_TAG_BOOLEAN)) != NULL) | |
3950 | purge = attr->values[0].boolean; | |
3951 | else | |
3952 | purge = 0; | |
3953 | ||
b423cd4c | 3954 | /* |
3955 | * See if the job exists... | |
3956 | */ | |
3957 | ||
3958 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3959 | { | |
3960 | /* | |
3961 | * Nope - return a "not found" error... | |
3962 | */ | |
3963 | ||
3964 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
3965 | return; | |
3966 | } | |
3967 | ||
3968 | /* | |
3969 | * See if the job is owned by the requesting user... | |
3970 | */ | |
3971 | ||
3972 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3973 | { | |
2fb76298 | 3974 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 3975 | return; |
3976 | } | |
3977 | ||
3978 | /* | |
d09495fa | 3979 | * See if the job is already completed, canceled, or aborted; if so, |
b423cd4c | 3980 | * we can't cancel... |
3981 | */ | |
3982 | ||
0a682745 | 3983 | if (job->state_value >= IPP_JOB_CANCELED && !purge) |
b423cd4c | 3984 | { |
e1d6a774 | 3985 | switch (job->state_value) |
3986 | { | |
d09495fa | 3987 | case IPP_JOB_CANCELED : |
e1d6a774 | 3988 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
d09495fa | 3989 | _("Job #%d is already canceled - can\'t cancel."), |
e1d6a774 | 3990 | jobid); |
3991 | break; | |
3992 | ||
3993 | case IPP_JOB_ABORTED : | |
3994 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3995 | _("Job #%d is already aborted - can\'t cancel."), | |
3996 | jobid); | |
3997 | break; | |
3998 | ||
3999 | default : | |
4000 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
4001 | _("Job #%d is already completed - can\'t cancel."), | |
4002 | jobid); | |
4003 | break; | |
4004 | } | |
4005 | ||
b423cd4c | 4006 | return; |
4007 | } | |
ef416fc2 | 4008 | |
b423cd4c | 4009 | /* |
4010 | * Cancel the job and return... | |
4011 | */ | |
ef416fc2 | 4012 | |
0a682745 | 4013 | cupsdCancelJob(job, purge, IPP_JOB_CANCELED); |
b423cd4c | 4014 | cupsdCheckJobs(); |
ef416fc2 | 4015 | |
0a682745 MS |
4016 | if (purge) |
4017 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid, | |
75bd9771 | 4018 | username); |
0a682745 MS |
4019 | else |
4020 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid, | |
75bd9771 | 4021 | username); |
ef416fc2 | 4022 | |
b423cd4c | 4023 | con->response->request.status.status_code = IPP_OK; |
4024 | } | |
ef416fc2 | 4025 | |
ef416fc2 | 4026 | |
b423cd4c | 4027 | /* |
4028 | * 'cancel_subscription()' - Cancel a subscription. | |
4029 | */ | |
ef416fc2 | 4030 | |
b423cd4c | 4031 | static void |
4032 | cancel_subscription( | |
4033 | cupsd_client_t *con, /* I - Client connection */ | |
4034 | int sub_id) /* I - Subscription ID */ | |
4035 | { | |
4036 | http_status_t status; /* Policy status */ | |
4037 | cupsd_subscription_t *sub; /* Subscription */ | |
ef416fc2 | 4038 | |
ef416fc2 | 4039 | |
b423cd4c | 4040 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4041 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
4042 | con, con->http.fd, sub_id); | |
ef416fc2 | 4043 | |
b423cd4c | 4044 | /* |
4045 | * Is the subscription ID valid? | |
4046 | */ | |
ef416fc2 | 4047 | |
b423cd4c | 4048 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
4049 | { | |
4050 | /* | |
4051 | * Bad subscription ID... | |
4052 | */ | |
ef416fc2 | 4053 | |
b423cd4c | 4054 | send_ipp_status(con, IPP_NOT_FOUND, |
4055 | _("notify-subscription-id %d no good!"), sub_id); | |
4056 | return; | |
4057 | } | |
ef416fc2 | 4058 | |
b423cd4c | 4059 | /* |
4060 | * Check policy... | |
4061 | */ | |
ef416fc2 | 4062 | |
b423cd4c | 4063 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
4064 | DefaultPolicyPtr, | |
4065 | con, sub->owner)) != HTTP_OK) | |
4066 | { | |
f899b121 | 4067 | send_http_error(con, status, sub->dest); |
b423cd4c | 4068 | return; |
4069 | } | |
ef416fc2 | 4070 | |
b423cd4c | 4071 | /* |
4072 | * Cancel the subscription... | |
4073 | */ | |
ef416fc2 | 4074 | |
b423cd4c | 4075 | cupsdDeleteSubscription(sub, 1); |
ef416fc2 | 4076 | |
b423cd4c | 4077 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 4078 | } |
4079 | ||
4080 | ||
3dfe78b3 MS |
4081 | /* |
4082 | * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI. | |
4083 | */ | |
4084 | ||
4085 | static int /* O - 1 if OK, 0 if not */ | |
4086 | check_rss_recipient( | |
4087 | const char *recipient) /* I - Recipient URI */ | |
4088 | { | |
4089 | cupsd_subscription_t *sub; /* Current subscription */ | |
4090 | ||
4091 | ||
4092 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
4093 | sub; | |
4094 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
4095 | if (sub->recipient) | |
4096 | { | |
4097 | /* | |
4098 | * Compare the URIs up to the first ?... | |
4099 | */ | |
4100 | ||
4101 | const char *r1, *r2; | |
4102 | ||
4103 | for (r1 = recipient, r2 = sub->recipient; | |
4104 | *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?'; | |
4105 | r1 ++, r2 ++); | |
4106 | ||
4107 | if (*r1 == *r2) | |
4108 | return (0); | |
4109 | } | |
4110 | ||
4111 | return (1); | |
4112 | } | |
4113 | ||
4114 | ||
ef416fc2 | 4115 | /* |
b423cd4c | 4116 | * 'check_quotas()' - Check quotas for a printer and user. |
ef416fc2 | 4117 | */ |
4118 | ||
b423cd4c | 4119 | static int /* O - 1 if OK, 0 if not */ |
4120 | check_quotas(cupsd_client_t *con, /* I - Client connection */ | |
4121 | cupsd_printer_t *p) /* I - Printer or class */ | |
ef416fc2 | 4122 | { |
b423cd4c | 4123 | int i; /* Looping var */ |
4124 | char username[33]; /* Username */ | |
4125 | cupsd_quota_t *q; /* Quota data */ | |
7594b224 | 4126 | #ifdef HAVE_MBR_UID_TO_UUID |
4127 | /* | |
4128 | * Use Apple membership APIs which require that all names represent | |
4129 | * valid user account or group records accessible by the server. | |
4130 | */ | |
4131 | ||
4132 | uuid_t usr_uuid; /* UUID for job requesting user */ | |
4133 | uuid_t usr2_uuid; /* UUID for ACL user name entry */ | |
4134 | uuid_t grp_uuid; /* UUID for ACL group name entry */ | |
4135 | int mbr_err; /* Error from membership function */ | |
4136 | int is_member; /* Is this user a member? */ | |
4137 | #else | |
4138 | /* | |
4139 | * Use standard POSIX APIs for checking users and groups... | |
4140 | */ | |
4141 | ||
b423cd4c | 4142 | struct passwd *pw; /* User password data */ |
7594b224 | 4143 | #endif /* HAVE_MBR_UID_TO_UUID */ |
ef416fc2 | 4144 | |
4145 | ||
b423cd4c | 4146 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", |
4147 | con, con->http.fd, p, p->name); | |
ef416fc2 | 4148 | |
b423cd4c | 4149 | /* |
4150 | * Figure out who is printing... | |
4151 | */ | |
4152 | ||
4153 | strlcpy(username, get_username(con), sizeof(username)); | |
4154 | ||
4155 | /* | |
4156 | * Check global active job limits for printers and users... | |
4157 | */ | |
4158 | ||
4159 | if (MaxJobsPerPrinter) | |
ef416fc2 | 4160 | { |
b423cd4c | 4161 | /* |
4162 | * Check if there are too many pending jobs on this printer... | |
4163 | */ | |
4164 | ||
4165 | if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter) | |
4166 | { | |
4167 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...", | |
4168 | p->name); | |
4169 | return (0); | |
4170 | } | |
4171 | } | |
4172 | ||
4173 | if (MaxJobsPerUser) | |
4174 | { | |
4175 | /* | |
4176 | * Check if there are too many pending jobs for this user... | |
4177 | */ | |
4178 | ||
4179 | if (cupsdGetUserJobCount(username) >= MaxJobsPerUser) | |
4180 | { | |
4181 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...", | |
4182 | username); | |
4183 | return (0); | |
4184 | } | |
ef416fc2 | 4185 | } |
4186 | ||
4187 | /* | |
b423cd4c | 4188 | * Check against users... |
ef416fc2 | 4189 | */ |
4190 | ||
b423cd4c | 4191 | if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0) |
4192 | return (1); | |
4193 | ||
4194 | if (p->num_users) | |
4195 | { | |
7594b224 | 4196 | #ifdef HAVE_MBR_UID_TO_UUID |
4197 | /* | |
4198 | * Get UUID for job requesting user... | |
4199 | */ | |
4200 | ||
4201 | if (mbr_user_name_to_uuid((char *)username, usr_uuid)) | |
4202 | { | |
4203 | /* | |
4204 | * Unknown user... | |
4205 | */ | |
4206 | ||
4207 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4208 | "check_quotas: UUID lookup failed for user \"%s\"", | |
4209 | username); | |
4210 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4211 | "Denying user \"%s\" access to printer \"%s\" " | |
4212 | "(unknown user)...", | |
4213 | username, p->name); | |
4214 | return (0); | |
4215 | } | |
4216 | #else | |
4217 | /* | |
4218 | * Get UID and GID of requesting user... | |
4219 | */ | |
4220 | ||
b423cd4c | 4221 | pw = getpwnam(username); |
4222 | endpwent(); | |
7594b224 | 4223 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 4224 | |
4225 | for (i = 0; i < p->num_users; i ++) | |
4226 | if (p->users[i][0] == '@') | |
4227 | { | |
4228 | /* | |
4229 | * Check group membership... | |
4230 | */ | |
4231 | ||
7594b224 | 4232 | #ifdef HAVE_MBR_UID_TO_UUID |
c9fc04c6 MS |
4233 | if (p->users[i][1] == '#') |
4234 | { | |
4235 | if (uuid_parse((char *)p->users[i] + 2, grp_uuid)) | |
4236 | uuid_clear(grp_uuid); | |
4237 | } | |
4238 | else if ((mbr_err = mbr_group_name_to_uuid((char *)p->users[i] + 1, | |
4239 | grp_uuid)) != 0) | |
7594b224 | 4240 | { |
4241 | /* | |
4242 | * Invalid ACL entries are ignored for matching; just record a | |
4243 | * warning in the log... | |
4244 | */ | |
4245 | ||
4246 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4247 | "check_quotas: UUID lookup failed for ACL entry " | |
4248 | "\"%s\" (err=%d)", p->users[i], mbr_err); | |
4249 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4250 | "Access control entry \"%s\" not a valid group name; " | |
4251 | "entry ignored", p->users[i]); | |
4252 | } | |
7594b224 | 4253 | |
c9fc04c6 MS |
4254 | if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid, |
4255 | &is_member)) != 0) | |
4256 | { | |
4257 | /* | |
4258 | * At this point, there should be no errors, but check anyways... | |
7594b224 | 4259 | */ |
4260 | ||
c9fc04c6 MS |
4261 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
4262 | "check_quotas: group \"%s\" membership check " | |
4263 | "failed (err=%d)", p->users[i] + 1, mbr_err); | |
4264 | is_member = 0; | |
7594b224 | 4265 | } |
c9fc04c6 MS |
4266 | |
4267 | /* | |
4268 | * Stop if we found a match... | |
4269 | */ | |
4270 | ||
4271 | if (is_member) | |
4272 | break; | |
4273 | ||
7594b224 | 4274 | #else |
b423cd4c | 4275 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) |
4276 | break; | |
7594b224 | 4277 | #endif /* HAVE_MBR_UID_TO_UUID */ |
4278 | } | |
4279 | #ifdef HAVE_MBR_UID_TO_UUID | |
4280 | else | |
4281 | { | |
c9fc04c6 MS |
4282 | if (p->users[i][0] == '#') |
4283 | { | |
4284 | if (uuid_parse((char *)p->users[i] + 1, usr2_uuid)) | |
4285 | uuid_clear(usr2_uuid); | |
4286 | } | |
4287 | else if ((mbr_err = mbr_user_name_to_uuid((char *)p->users[i], | |
4288 | usr2_uuid)) != 0) | |
7594b224 | 4289 | { |
4290 | /* | |
4291 | * Invalid ACL entries are ignored for matching; just record a | |
4292 | * warning in the log... | |
4293 | */ | |
4294 | ||
4295 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4296 | "check_quotas: UUID lookup failed for ACL entry " | |
4297 | "\"%s\" (err=%d)", p->users[i], mbr_err); | |
4298 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4299 | "Access control entry \"%s\" not a valid user name; " | |
4300 | "entry ignored", p->users[i]); | |
4301 | } | |
7594b224 | 4302 | |
c9fc04c6 MS |
4303 | if ((mbr_err = mbr_check_membership(usr_uuid, usr2_uuid, |
4304 | &is_member)) != 0) | |
4305 | { | |
4306 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4307 | "check_quotas: User \"%s\" identity check failed " | |
4308 | "(err=%d)", p->users[i], mbr_err); | |
4309 | is_member = 0; | |
7594b224 | 4310 | } |
c9fc04c6 MS |
4311 | |
4312 | if (is_member) | |
4313 | break; | |
b423cd4c | 4314 | } |
7594b224 | 4315 | #else |
b423cd4c | 4316 | else if (!strcasecmp(username, p->users[i])) |
4317 | break; | |
7594b224 | 4318 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 4319 | |
4320 | if ((i < p->num_users) == p->deny_users) | |
ef416fc2 | 4321 | { |
b423cd4c | 4322 | cupsdLogMessage(CUPSD_LOG_INFO, |
4323 | "Denying user \"%s\" access to printer \"%s\"...", | |
4324 | username, p->name); | |
4325 | return (0); | |
ef416fc2 | 4326 | } |
b423cd4c | 4327 | } |
ef416fc2 | 4328 | |
4329 | /* | |
b423cd4c | 4330 | * Check quotas... |
ef416fc2 | 4331 | */ |
4332 | ||
7594b224 | 4333 | #ifdef __APPLE__ |
3d8365b8 | 4334 | if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL) |
7594b224 | 4335 | { |
4336 | /* | |
4337 | * TODO: Define these special page count values as constants! | |
4338 | */ | |
4339 | ||
4340 | if (q->page_count == -4) /* special case: unlimited user */ | |
4341 | { | |
4342 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4343 | "User \"%s\" request approved for printer %s (%s): " | |
4344 | "unlimited quota.", | |
4345 | username, p->name, p->info); | |
4346 | q->page_count = 0; /* allow user to print */ | |
4347 | return (1); | |
4348 | } | |
4349 | else if (q->page_count == -3) /* quota exceeded */ | |
4350 | { | |
4351 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4352 | "User \"%s\" request denied for printer %s (%s): " | |
4353 | "quota limit exceeded.", | |
4354 | username, p->name, p->info); | |
4355 | q->page_count = 2; /* force quota exceeded failure */ | |
bc44d920 | 4356 | return (-1); |
7594b224 | 4357 | } |
4358 | else if (q->page_count == -2) /* quota disabled for user */ | |
4359 | { | |
4360 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4361 | "User \"%s\" request denied for printer %s (%s): " | |
4362 | "printing disabled for user.", | |
4363 | username, p->name, p->info); | |
4364 | q->page_count = 2; /* force quota exceeded failure */ | |
bc44d920 | 4365 | return (-1); |
7594b224 | 4366 | } |
4367 | else if (q->page_count == -1) /* quota access error */ | |
4368 | { | |
4369 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4370 | "User \"%s\" request denied for printer %s (%s): " | |
4371 | "unable to determine quota limit.", | |
4372 | username, p->name, p->info); | |
4373 | q->page_count = 2; /* force quota exceeded failure */ | |
bc44d920 | 4374 | return (-1); |
7594b224 | 4375 | } |
4376 | else if (q->page_count < 0) /* user not found or other error */ | |
4377 | { | |
4378 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4379 | "User \"%s\" request denied for printer %s (%s): " | |
4380 | "user disabled / missing quota.", | |
4381 | username, p->name, p->info); | |
4382 | q->page_count = 2; /* force quota exceeded failure */ | |
bc44d920 | 4383 | return (-1); |
7594b224 | 4384 | } |
4385 | else /* page within user limits */ | |
4386 | { | |
4387 | q->page_count = 0; /* allow user to print */ | |
4388 | return (1); | |
4389 | } | |
4390 | } | |
4391 | else | |
4392 | #endif /* __APPLE__ */ | |
b423cd4c | 4393 | if (p->k_limit || p->page_limit) |
4394 | { | |
4395 | if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL) | |
4396 | { | |
4397 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4398 | "Unable to allocate quota data for user \"%s\"!", | |
4399 | username); | |
bc44d920 | 4400 | return (-1); |
b423cd4c | 4401 | } |
ef416fc2 | 4402 | |
b423cd4c | 4403 | if ((q->k_count >= p->k_limit && p->k_limit) || |
4404 | (q->page_count >= p->page_limit && p->page_limit)) | |
4405 | { | |
4406 | cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...", | |
4407 | username); | |
bc44d920 | 4408 | return (-1); |
b423cd4c | 4409 | } |
4410 | } | |
4411 | ||
4412 | /* | |
4413 | * If we have gotten this far, we're done! | |
4414 | */ | |
4415 | ||
4416 | return (1); | |
ef416fc2 | 4417 | } |
4418 | ||
4419 | ||
4420 | /* | |
b423cd4c | 4421 | * 'copy_attribute()' - Copy a single attribute. |
ef416fc2 | 4422 | */ |
4423 | ||
b423cd4c | 4424 | static ipp_attribute_t * /* O - New attribute */ |
4425 | copy_attribute( | |
4426 | ipp_t *to, /* O - Destination request/response */ | |
4427 | ipp_attribute_t *attr, /* I - Attribute to copy */ | |
4428 | int quickcopy) /* I - Do a quick copy? */ | |
ef416fc2 | 4429 | { |
b423cd4c | 4430 | int i; /* Looping var */ |
4431 | ipp_attribute_t *toattr; /* Destination attribute */ | |
ef416fc2 | 4432 | |
4433 | ||
4434 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b423cd4c | 4435 | "copy_attribute(%p, %p[%s,%x,%x])", to, attr, |
4436 | attr->name ? attr->name : "(null)", attr->group_tag, | |
4437 | attr->value_tag); | |
ef416fc2 | 4438 | |
b423cd4c | 4439 | switch (attr->value_tag & ~IPP_TAG_COPY) |
4440 | { | |
4441 | case IPP_TAG_ZERO : | |
4442 | toattr = ippAddSeparator(to); | |
4443 | break; | |
ef416fc2 | 4444 | |
b423cd4c | 4445 | case IPP_TAG_INTEGER : |
4446 | case IPP_TAG_ENUM : | |
4447 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
4448 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 4449 | |
b423cd4c | 4450 | for (i = 0; i < attr->num_values; i ++) |
4451 | toattr->values[i].integer = attr->values[i].integer; | |
4452 | break; | |
ef416fc2 | 4453 | |
b423cd4c | 4454 | case IPP_TAG_BOOLEAN : |
4455 | toattr = ippAddBooleans(to, attr->group_tag, attr->name, | |
4456 | attr->num_values, NULL); | |
ef416fc2 | 4457 | |
b423cd4c | 4458 | for (i = 0; i < attr->num_values; i ++) |
4459 | toattr->values[i].boolean = attr->values[i].boolean; | |
4460 | break; | |
ef416fc2 | 4461 | |
b423cd4c | 4462 | case IPP_TAG_STRING : |
4463 | case IPP_TAG_TEXT : | |
4464 | case IPP_TAG_NAME : | |
4465 | case IPP_TAG_KEYWORD : | |
4466 | case IPP_TAG_URI : | |
4467 | case IPP_TAG_URISCHEME : | |
4468 | case IPP_TAG_CHARSET : | |
4469 | case IPP_TAG_LANGUAGE : | |
4470 | case IPP_TAG_MIMETYPE : | |
4471 | toattr = ippAddStrings(to, attr->group_tag, | |
4472 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
4473 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 4474 | |
b423cd4c | 4475 | if (quickcopy) |
4476 | { | |
4477 | for (i = 0; i < attr->num_values; i ++) | |
4478 | toattr->values[i].string.text = attr->values[i].string.text; | |
4479 | } | |
4480 | else | |
4481 | { | |
4482 | for (i = 0; i < attr->num_values; i ++) | |
bc44d920 | 4483 | toattr->values[i].string.text = |
4484 | _cupsStrAlloc(attr->values[i].string.text); | |
b423cd4c | 4485 | } |
4486 | break; | |
ef416fc2 | 4487 | |
b423cd4c | 4488 | case IPP_TAG_DATE : |
4489 | toattr = ippAddDate(to, attr->group_tag, attr->name, | |
4490 | attr->values[0].date); | |
4491 | break; | |
ef416fc2 | 4492 | |
b423cd4c | 4493 | case IPP_TAG_RESOLUTION : |
4494 | toattr = ippAddResolutions(to, attr->group_tag, attr->name, | |
4495 | attr->num_values, IPP_RES_PER_INCH, | |
4496 | NULL, NULL); | |
ef416fc2 | 4497 | |
b423cd4c | 4498 | for (i = 0; i < attr->num_values; i ++) |
4499 | { | |
4500 | toattr->values[i].resolution.xres = attr->values[i].resolution.xres; | |
4501 | toattr->values[i].resolution.yres = attr->values[i].resolution.yres; | |
4502 | toattr->values[i].resolution.units = attr->values[i].resolution.units; | |
4503 | } | |
4504 | break; | |
ef416fc2 | 4505 | |
b423cd4c | 4506 | case IPP_TAG_RANGE : |
4507 | toattr = ippAddRanges(to, attr->group_tag, attr->name, | |
4508 | attr->num_values, NULL, NULL); | |
ef416fc2 | 4509 | |
b423cd4c | 4510 | for (i = 0; i < attr->num_values; i ++) |
4511 | { | |
4512 | toattr->values[i].range.lower = attr->values[i].range.lower; | |
4513 | toattr->values[i].range.upper = attr->values[i].range.upper; | |
4514 | } | |
4515 | break; | |
ef416fc2 | 4516 | |
b423cd4c | 4517 | case IPP_TAG_TEXTLANG : |
4518 | case IPP_TAG_NAMELANG : | |
4519 | toattr = ippAddStrings(to, attr->group_tag, | |
4520 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
4521 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 4522 | |
b423cd4c | 4523 | if (quickcopy) |
4524 | { | |
4525 | for (i = 0; i < attr->num_values; i ++) | |
4526 | { | |
4527 | toattr->values[i].string.charset = attr->values[i].string.charset; | |
4528 | toattr->values[i].string.text = attr->values[i].string.text; | |
4529 | } | |
4530 | } | |
4531 | else | |
4532 | { | |
4533 | for (i = 0; i < attr->num_values; i ++) | |
4534 | { | |
4535 | if (!i) | |
4536 | toattr->values[i].string.charset = | |
757d2cad | 4537 | _cupsStrAlloc(attr->values[i].string.charset); |
b423cd4c | 4538 | else |
4539 | toattr->values[i].string.charset = | |
4540 | toattr->values[0].string.charset; | |
ef416fc2 | 4541 | |
bc44d920 | 4542 | toattr->values[i].string.text = |
4543 | _cupsStrAlloc(attr->values[i].string.text); | |
b423cd4c | 4544 | } |
4545 | } | |
4546 | break; | |
ef416fc2 | 4547 | |
b423cd4c | 4548 | case IPP_TAG_BEGIN_COLLECTION : |
4549 | toattr = ippAddCollections(to, attr->group_tag, attr->name, | |
4550 | attr->num_values, NULL); | |
ef416fc2 | 4551 | |
b423cd4c | 4552 | for (i = 0; i < attr->num_values; i ++) |
4553 | { | |
4554 | toattr->values[i].collection = ippNew(); | |
4555 | copy_attrs(toattr->values[i].collection, attr->values[i].collection, | |
4556 | NULL, IPP_TAG_ZERO, 0); | |
4557 | } | |
ef416fc2 | 4558 | break; |
ef416fc2 | 4559 | |
b423cd4c | 4560 | default : |
4561 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
4562 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 4563 | |
b423cd4c | 4564 | for (i = 0; i < attr->num_values; i ++) |
4565 | { | |
4566 | toattr->values[i].unknown.length = attr->values[i].unknown.length; | |
ef416fc2 | 4567 | |
b423cd4c | 4568 | if (toattr->values[i].unknown.length > 0) |
4569 | { | |
4570 | if ((toattr->values[i].unknown.data = | |
4571 | malloc(toattr->values[i].unknown.length)) == NULL) | |
4572 | toattr->values[i].unknown.length = 0; | |
4573 | else | |
4574 | memcpy(toattr->values[i].unknown.data, | |
4575 | attr->values[i].unknown.data, | |
4576 | toattr->values[i].unknown.length); | |
4577 | } | |
4578 | } | |
4579 | break; /* anti-compiler-warning-code */ | |
4580 | } | |
ef416fc2 | 4581 | |
b423cd4c | 4582 | return (toattr); |
4583 | } | |
4584 | ||
4585 | ||
4586 | /* | |
4587 | * 'copy_attrs()' - Copy attributes from one request to another. | |
4588 | */ | |
4589 | ||
4590 | static void | |
4591 | copy_attrs(ipp_t *to, /* I - Destination request */ | |
4592 | ipp_t *from, /* I - Source request */ | |
4593 | cups_array_t *ra, /* I - Requested attributes */ | |
4594 | ipp_tag_t group, /* I - Group to copy */ | |
4595 | int quickcopy) /* I - Do a quick copy? */ | |
4596 | { | |
4597 | ipp_attribute_t *fromattr; /* Source attribute */ | |
ef416fc2 | 4598 | |
ef416fc2 | 4599 | |
b423cd4c | 4600 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4601 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
4602 | to, from, ra, group, quickcopy); | |
ef416fc2 | 4603 | |
b423cd4c | 4604 | if (!to || !from) |
4605 | return; | |
e00b005a | 4606 | |
b423cd4c | 4607 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) |
ef416fc2 | 4608 | { |
4609 | /* | |
b423cd4c | 4610 | * Filter attributes as needed... |
ef416fc2 | 4611 | */ |
4612 | ||
2abf387c | 4613 | if ((group != IPP_TAG_ZERO && fromattr->group_tag != group && |
4614 | fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name) | |
b423cd4c | 4615 | continue; |
4616 | ||
4617 | if (!ra || cupsArrayFind(ra, fromattr->name)) | |
4618 | copy_attribute(to, fromattr, quickcopy); | |
ef416fc2 | 4619 | } |
b423cd4c | 4620 | } |
ef416fc2 | 4621 | |
ef416fc2 | 4622 | |
b423cd4c | 4623 | /* |
4624 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
4625 | * specified job. | |
4626 | */ | |
ef416fc2 | 4627 | |
b423cd4c | 4628 | static int /* O - Size of banner file in kbytes */ |
4629 | copy_banner(cupsd_client_t *con, /* I - Client connection */ | |
4630 | cupsd_job_t *job, /* I - Job information */ | |
4631 | const char *name) /* I - Name of banner */ | |
4632 | { | |
4633 | int i; /* Looping var */ | |
4634 | int kbytes; /* Size of banner file in kbytes */ | |
4635 | char filename[1024]; /* Job filename */ | |
4636 | cupsd_banner_t *banner; /* Pointer to banner */ | |
4637 | cups_file_t *in; /* Input file */ | |
4638 | cups_file_t *out; /* Output file */ | |
4639 | int ch; /* Character from file */ | |
4640 | char attrname[255], /* Name of attribute */ | |
4641 | *s; /* Pointer into name */ | |
4642 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 4643 | |
ef416fc2 | 4644 | |
3dfe78b3 MS |
4645 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4646 | "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")", | |
09a101d6 | 4647 | con, con ? con->http.fd : -1, job, job->id, |
4648 | name ? name : "(null)"); | |
ef416fc2 | 4649 | |
b423cd4c | 4650 | /* |
4651 | * Find the banner; return if not found or "none"... | |
4652 | */ | |
ef416fc2 | 4653 | |
b423cd4c | 4654 | if (!name || !strcmp(name, "none") || |
4655 | (banner = cupsdFindBanner(name)) == NULL) | |
4656 | return (0); | |
ef416fc2 | 4657 | |
b423cd4c | 4658 | /* |
4659 | * Open the banner and job files... | |
4660 | */ | |
ef416fc2 | 4661 | |
b423cd4c | 4662 | if (add_file(con, job, banner->filetype, 0)) |
91c84a35 | 4663 | return (-1); |
ef416fc2 | 4664 | |
b423cd4c | 4665 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, |
4666 | job->num_files); | |
4667 | if ((out = cupsFileOpen(filename, "w")) == NULL) | |
4668 | { | |
4669 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 | 4670 | "Unable to create banner job file %s - %s", |
b423cd4c | 4671 | filename, strerror(errno)); |
4672 | job->num_files --; | |
4673 | return (0); | |
4674 | } | |
4675 | ||
4676 | fchmod(cupsFileNumber(out), 0640); | |
4677 | fchown(cupsFileNumber(out), RunUser, Group); | |
ef416fc2 | 4678 | |
4679 | /* | |
b423cd4c | 4680 | * Try the localized banner file under the subdirectory... |
ef416fc2 | 4681 | */ |
4682 | ||
80ca4592 | 4683 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, |
b423cd4c | 4684 | sizeof(attrname)); |
4685 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
ef416fc2 | 4686 | { |
4687 | /* | |
b423cd4c | 4688 | * Convert ll-cc to ll_CC... |
ef416fc2 | 4689 | */ |
4690 | ||
b423cd4c | 4691 | attrname[2] = '_'; |
4692 | attrname[3] = toupper(attrname[3] & 255); | |
4693 | attrname[4] = toupper(attrname[4] & 255); | |
4694 | } | |
ef416fc2 | 4695 | |
b423cd4c | 4696 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4697 | attrname, name); | |
ef416fc2 | 4698 | |
b423cd4c | 4699 | if (access(filename, 0) && strlen(attrname) > 2) |
ef416fc2 | 4700 | { |
4701 | /* | |
b423cd4c | 4702 | * Wasn't able to find "ll_CC" locale file; try the non-national |
4703 | * localization banner directory. | |
ef416fc2 | 4704 | */ |
4705 | ||
b423cd4c | 4706 | attrname[2] = '\0'; |
ef416fc2 | 4707 | |
b423cd4c | 4708 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4709 | attrname, name); | |
ef416fc2 | 4710 | } |
b423cd4c | 4711 | |
4712 | if (access(filename, 0)) | |
ef416fc2 | 4713 | { |
4714 | /* | |
b423cd4c | 4715 | * Use the non-localized banner file. |
ef416fc2 | 4716 | */ |
4717 | ||
b423cd4c | 4718 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); |
4719 | } | |
ef416fc2 | 4720 | |
b423cd4c | 4721 | if ((in = cupsFileOpen(filename, "r")) == NULL) |
4722 | { | |
4723 | cupsFileClose(out); | |
4724 | unlink(filename); | |
4725 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 | 4726 | "Unable to open banner template file %s - %s", |
b423cd4c | 4727 | filename, strerror(errno)); |
4728 | job->num_files --; | |
4729 | return (0); | |
ef416fc2 | 4730 | } |
4731 | ||
4732 | /* | |
b423cd4c | 4733 | * Parse the file to the end... |
ef416fc2 | 4734 | */ |
4735 | ||
b423cd4c | 4736 | while ((ch = cupsFileGetChar(in)) != EOF) |
4737 | if (ch == '{') | |
4738 | { | |
4739 | /* | |
4740 | * Get an attribute name... | |
4741 | */ | |
ef416fc2 | 4742 | |
b423cd4c | 4743 | for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;) |
4744 | if (!isalpha(ch & 255) && ch != '-' && ch != '?') | |
4745 | break; | |
4746 | else if (s < (attrname + sizeof(attrname) - 1)) | |
4747 | *s++ = ch; | |
4748 | else | |
4749 | break; | |
ef416fc2 | 4750 | |
b423cd4c | 4751 | *s = '\0'; |
4752 | ||
4753 | if (ch != '}') | |
4754 | { | |
4755 | /* | |
4756 | * Ignore { followed by stuff that is not an attribute name... | |
4757 | */ | |
4758 | ||
4759 | cupsFilePrintf(out, "{%s%c", attrname, ch); | |
4760 | continue; | |
4761 | } | |
ef416fc2 | 4762 | |
ef416fc2 | 4763 | /* |
b423cd4c | 4764 | * See if it is defined... |
ef416fc2 | 4765 | */ |
4766 | ||
b423cd4c | 4767 | if (attrname[0] == '?') |
4768 | s = attrname + 1; | |
4769 | else | |
4770 | s = attrname; | |
ef416fc2 | 4771 | |
b423cd4c | 4772 | if (!strcmp(s, "printer-name")) |
4773 | { | |
4774 | cupsFilePuts(out, job->dest); | |
4775 | continue; | |
ef416fc2 | 4776 | } |
b423cd4c | 4777 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) |
4778 | { | |
4779 | /* | |
4780 | * See if we have a leading question mark... | |
4781 | */ | |
ef416fc2 | 4782 | |
b423cd4c | 4783 | if (attrname[0] != '?') |
4784 | { | |
4785 | /* | |
4786 | * Nope, write to file as-is; probably a PostScript procedure... | |
4787 | */ | |
ef416fc2 | 4788 | |
b423cd4c | 4789 | cupsFilePrintf(out, "{%s}", attrname); |
4790 | } | |
ef416fc2 | 4791 | |
b423cd4c | 4792 | continue; |
4793 | } | |
ef416fc2 | 4794 | |
b423cd4c | 4795 | /* |
4796 | * Output value(s)... | |
4797 | */ | |
ef416fc2 | 4798 | |
b423cd4c | 4799 | for (i = 0; i < attr->num_values; i ++) |
4800 | { | |
4801 | if (i) | |
4802 | cupsFilePutChar(out, ','); | |
ef416fc2 | 4803 | |
b423cd4c | 4804 | switch (attr->value_tag) |
4805 | { | |
4806 | case IPP_TAG_INTEGER : | |
4807 | case IPP_TAG_ENUM : | |
4808 | if (!strncmp(s, "time-at-", 8)) | |
4809 | cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer)); | |
4810 | else | |
4811 | cupsFilePrintf(out, "%d", attr->values[i].integer); | |
4812 | break; | |
ef416fc2 | 4813 | |
b423cd4c | 4814 | case IPP_TAG_BOOLEAN : |
4815 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
4816 | break; | |
ef416fc2 | 4817 | |
b423cd4c | 4818 | case IPP_TAG_NOVALUE : |
4819 | cupsFilePuts(out, "novalue"); | |
4820 | break; | |
ef416fc2 | 4821 | |
b423cd4c | 4822 | case IPP_TAG_RANGE : |
4823 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
4824 | attr->values[i].range.upper); | |
4825 | break; | |
fa73b229 | 4826 | |
b423cd4c | 4827 | case IPP_TAG_RESOLUTION : |
4828 | cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres, | |
4829 | attr->values[i].resolution.yres, | |
4830 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
4831 | "dpi" : "dpc"); | |
4832 | break; | |
fa73b229 | 4833 | |
b423cd4c | 4834 | case IPP_TAG_URI : |
4835 | case IPP_TAG_STRING : | |
4836 | case IPP_TAG_TEXT : | |
4837 | case IPP_TAG_NAME : | |
4838 | case IPP_TAG_KEYWORD : | |
4839 | case IPP_TAG_CHARSET : | |
4840 | case IPP_TAG_LANGUAGE : | |
4841 | if (!strcasecmp(banner->filetype->type, "postscript")) | |
4842 | { | |
4843 | /* | |
4844 | * Need to quote strings for PS banners... | |
4845 | */ | |
fa73b229 | 4846 | |
b423cd4c | 4847 | const char *p; |
fa73b229 | 4848 | |
b423cd4c | 4849 | for (p = attr->values[i].string.text; *p; p ++) |
4850 | { | |
4851 | if (*p == '(' || *p == ')' || *p == '\\') | |
4852 | { | |
4853 | cupsFilePutChar(out, '\\'); | |
4854 | cupsFilePutChar(out, *p); | |
4855 | } | |
4856 | else if (*p < 32 || *p > 126) | |
4857 | cupsFilePrintf(out, "\\%03o", *p & 255); | |
4858 | else | |
4859 | cupsFilePutChar(out, *p); | |
4860 | } | |
4861 | } | |
4862 | else | |
4863 | cupsFilePuts(out, attr->values[i].string.text); | |
4864 | break; | |
fa73b229 | 4865 | |
b423cd4c | 4866 | default : |
4867 | break; /* anti-compiler-warning-code */ | |
4868 | } | |
4869 | } | |
4870 | } | |
4871 | else if (ch == '\\') /* Quoted char */ | |
4872 | { | |
4873 | ch = cupsFileGetChar(in); | |
fa73b229 | 4874 | |
b423cd4c | 4875 | if (ch != '{') /* Only do special handling for \{ */ |
4876 | cupsFilePutChar(out, '\\'); | |
fa73b229 | 4877 | |
b423cd4c | 4878 | cupsFilePutChar(out, ch); |
4879 | } | |
4880 | else | |
4881 | cupsFilePutChar(out, ch); | |
fa73b229 | 4882 | |
b423cd4c | 4883 | cupsFileClose(in); |
fa73b229 | 4884 | |
b423cd4c | 4885 | kbytes = (cupsFileTell(out) + 1023) / 1024; |
fa73b229 | 4886 | |
b423cd4c | 4887 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
4888 | IPP_TAG_INTEGER)) != NULL) | |
4889 | attr->values[0].integer += kbytes; | |
4890 | ||
4891 | cupsFileClose(out); | |
4892 | ||
4893 | return (kbytes); | |
fa73b229 | 4894 | } |
4895 | ||
4896 | ||
4897 | /* | |
b423cd4c | 4898 | * 'copy_file()' - Copy a PPD file or interface script... |
fa73b229 | 4899 | */ |
4900 | ||
b423cd4c | 4901 | static int /* O - 0 = success, -1 = error */ |
4902 | copy_file(const char *from, /* I - Source file */ | |
4903 | const char *to) /* I - Destination file */ | |
fa73b229 | 4904 | { |
b423cd4c | 4905 | cups_file_t *src, /* Source file */ |
4906 | *dst; /* Destination file */ | |
4907 | int bytes; /* Bytes to read/write */ | |
4908 | char buffer[2048]; /* Copy buffer */ | |
fa73b229 | 4909 | |
4910 | ||
b423cd4c | 4911 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); |
4912 | ||
fa73b229 | 4913 | /* |
b423cd4c | 4914 | * Open the source and destination file for a copy... |
fa73b229 | 4915 | */ |
4916 | ||
b423cd4c | 4917 | if ((src = cupsFileOpen(from, "rb")) == NULL) |
4918 | return (-1); | |
fa73b229 | 4919 | |
b423cd4c | 4920 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
4921 | { | |
4922 | cupsFileClose(src); | |
4923 | return (-1); | |
4924 | } | |
fa73b229 | 4925 | |
b423cd4c | 4926 | /* |
4927 | * Copy the source file to the destination... | |
4928 | */ | |
fa73b229 | 4929 | |
b423cd4c | 4930 | while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0) |
4931 | if (cupsFileWrite(dst, buffer, bytes) < bytes) | |
4932 | { | |
4933 | cupsFileClose(src); | |
4934 | cupsFileClose(dst); | |
4935 | return (-1); | |
4936 | } | |
fa73b229 | 4937 | |
b423cd4c | 4938 | /* |
4939 | * Close both files and return... | |
4940 | */ | |
fa73b229 | 4941 | |
b423cd4c | 4942 | cupsFileClose(src); |
fa73b229 | 4943 | |
b423cd4c | 4944 | return (cupsFileClose(dst)); |
4945 | } | |
fa73b229 | 4946 | |
fa73b229 | 4947 | |
b423cd4c | 4948 | /* |
4949 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
4950 | * as needed... | |
4951 | */ | |
e00b005a | 4952 | |
b423cd4c | 4953 | static int /* O - 0 = success, -1 = error */ |
4954 | copy_model(cupsd_client_t *con, /* I - Client connection */ | |
4955 | const char *from, /* I - Source file */ | |
4956 | const char *to) /* I - Destination file */ | |
4957 | { | |
f7deaa1a | 4958 | fd_set input; /* select() input set */ |
b423cd4c | 4959 | struct timeval timeout; /* select() timeout */ |
bc44d920 | 4960 | int maxfd; /* Max file descriptor for select() */ |
b423cd4c | 4961 | char tempfile[1024]; /* Temporary PPD file */ |
4962 | int tempfd; /* Temporary PPD file descriptor */ | |
4963 | int temppid; /* Process ID of cups-driverd */ | |
4964 | int temppipe[2]; /* Temporary pipes */ | |
4965 | char *argv[4], /* Command-line arguments */ | |
4966 | *envp[MAX_ENV]; /* Environment */ | |
4967 | cups_file_t *src, /* Source file */ | |
4968 | *dst; /* Destination file */ | |
2abf387c | 4969 | ppd_file_t *ppd; /* PPD file */ |
b423cd4c | 4970 | int bytes, /* Bytes from pipe */ |
4971 | total; /* Total bytes from pipe */ | |
2abf387c | 4972 | char buffer[2048]; /* Copy buffer */ |
b423cd4c | 4973 | int i; /* Looping var */ |
4974 | char option[PPD_MAX_NAME], /* Option name */ | |
4975 | choice[PPD_MAX_NAME]; /* Choice name */ | |
4976 | int num_defaults; /* Number of default options */ | |
2abf387c | 4977 | cups_option_t *defaults; /* Default options */ |
b423cd4c | 4978 | char cups_protocol[PPD_MAX_LINE]; |
4979 | /* cupsProtocol attribute */ | |
e00b005a | 4980 | |
e00b005a | 4981 | |
b423cd4c | 4982 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4983 | "copy_model(con=%p, from=\"%s\", to=\"%s\")", | |
4984 | con, from, to); | |
e00b005a | 4985 | |
b423cd4c | 4986 | /* |
4987 | * Run cups-driverd to get the PPD file... | |
4988 | */ | |
e00b005a | 4989 | |
b423cd4c | 4990 | argv[0] = "cups-driverd"; |
4991 | argv[1] = "cat"; | |
4992 | argv[2] = (char *)from; | |
4993 | argv[3] = NULL; | |
e00b005a | 4994 | |
b423cd4c | 4995 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
e00b005a | 4996 | |
b423cd4c | 4997 | snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin); |
4998 | snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd); | |
4999 | tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
5000 | if (tempfd < 0) | |
5001 | return (-1); | |
5002 | ||
5003 | cupsdOpenPipe(temppipe); | |
5004 | ||
b423cd4c | 5005 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
5006 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
fa73b229 | 5007 | |
b423cd4c | 5008 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], |
a4924f6c | 5009 | -1, -1, 0, DefaultProfile, &temppid)) |
fa73b229 | 5010 | { |
b423cd4c | 5011 | close(tempfd); |
5012 | unlink(tempfile); | |
a4924f6c | 5013 | |
b423cd4c | 5014 | return (-1); |
fa73b229 | 5015 | } |
5016 | ||
b423cd4c | 5017 | close(temppipe[1]); |
fa73b229 | 5018 | |
b423cd4c | 5019 | /* |
5020 | * Wait up to 30 seconds for the PPD file to be copied... | |
5021 | */ | |
fa73b229 | 5022 | |
b423cd4c | 5023 | total = 0; |
fa73b229 | 5024 | |
b423cd4c | 5025 | if (temppipe[0] > CGIPipes[0]) |
5026 | maxfd = temppipe[0] + 1; | |
5027 | else | |
5028 | maxfd = CGIPipes[0] + 1; | |
ef416fc2 | 5029 | |
b423cd4c | 5030 | for (;;) |
5031 | { | |
5032 | /* | |
5033 | * See if we have data ready... | |
5034 | */ | |
ef416fc2 | 5035 | |
f7deaa1a | 5036 | FD_ZERO(&input); |
5037 | FD_SET(temppipe[0], &input); | |
5038 | FD_SET(CGIPipes[0], &input); | |
ef416fc2 | 5039 | |
b423cd4c | 5040 | timeout.tv_sec = 30; |
5041 | timeout.tv_usec = 0; | |
5042 | ||
f7deaa1a | 5043 | if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0) |
b423cd4c | 5044 | { |
5045 | if (errno == EINTR) | |
5046 | continue; | |
5047 | else | |
5048 | break; | |
5049 | } | |
5050 | else if (i == 0) | |
ef416fc2 | 5051 | { |
5052 | /* | |
b423cd4c | 5053 | * We have timed out... |
ef416fc2 | 5054 | */ |
5055 | ||
b423cd4c | 5056 | break; |
ef416fc2 | 5057 | } |
b423cd4c | 5058 | |
f7deaa1a | 5059 | if (FD_ISSET(temppipe[0], &input)) |
ef416fc2 | 5060 | { |
5061 | /* | |
b423cd4c | 5062 | * Read the PPD file from the pipe, and write it to the PPD file. |
ef416fc2 | 5063 | */ |
5064 | ||
b423cd4c | 5065 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) |
5066 | { | |
5067 | if (write(tempfd, buffer, bytes) < bytes) | |
5068 | break; | |
ef416fc2 | 5069 | |
b423cd4c | 5070 | total += bytes; |
5071 | } | |
5072 | else | |
5073 | break; | |
ef416fc2 | 5074 | } |
b423cd4c | 5075 | |
f7deaa1a | 5076 | if (FD_ISSET(CGIPipes[0], &input)) |
b423cd4c | 5077 | cupsdUpdateCGI(); |
ef416fc2 | 5078 | } |
5079 | ||
b423cd4c | 5080 | close(temppipe[0]); |
5081 | close(tempfd); | |
ef416fc2 | 5082 | |
b423cd4c | 5083 | if (!total) |
ef416fc2 | 5084 | { |
b423cd4c | 5085 | /* |
5086 | * No data from cups-deviced... | |
5087 | */ | |
5088 | ||
5089 | cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!"); | |
5090 | unlink(tempfile); | |
5091 | return (-1); | |
ef416fc2 | 5092 | } |
5093 | ||
b423cd4c | 5094 | /* |
5095 | * Read the source file and see what page sizes are supported... | |
5096 | */ | |
fa73b229 | 5097 | |
2abf387c | 5098 | if ((ppd = ppdOpenFile(tempfile)) == NULL) |
b423cd4c | 5099 | { |
5100 | unlink(tempfile); | |
5101 | return (-1); | |
5102 | } | |
fa73b229 | 5103 | |
ef416fc2 | 5104 | /* |
b423cd4c | 5105 | * Open the destination (if possible) and set the default options... |
ef416fc2 | 5106 | */ |
5107 | ||
b423cd4c | 5108 | num_defaults = 0; |
5109 | defaults = NULL; | |
5110 | cups_protocol[0] = '\0'; | |
ef416fc2 | 5111 | |
b423cd4c | 5112 | if ((dst = cupsFileOpen(to, "rb")) != NULL) |
ef416fc2 | 5113 | { |
5114 | /* | |
b423cd4c | 5115 | * Read all of the default lines from the old PPD... |
ef416fc2 | 5116 | */ |
5117 | ||
b423cd4c | 5118 | while (cupsFileGets(dst, buffer, sizeof(buffer))) |
5119 | if (!strncmp(buffer, "*Default", 8)) | |
5120 | { | |
5121 | /* | |
5122 | * Add the default option... | |
5123 | */ | |
ef416fc2 | 5124 | |
b423cd4c | 5125 | if (!ppd_parse_line(buffer, option, sizeof(option), |
5126 | choice, sizeof(choice))) | |
2abf387c | 5127 | { |
5128 | ppd_option_t *ppdo; /* PPD option */ | |
5129 | ||
5130 | ||
5131 | /* | |
5132 | * Only add the default if the default hasn't already been | |
5133 | * set and the choice exists in the new PPD... | |
5134 | */ | |
5135 | ||
5136 | if (!cupsGetOption(option, num_defaults, defaults) && | |
5137 | (ppdo = ppdFindOption(ppd, option)) != NULL && | |
5138 | ppdFindChoice(ppdo, choice)) | |
5139 | num_defaults = cupsAddOption(option, choice, num_defaults, | |
b423cd4c | 5140 | &defaults); |
2abf387c | 5141 | } |
b423cd4c | 5142 | } |
5143 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
5144 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
ef416fc2 | 5145 | |
b423cd4c | 5146 | cupsFileClose(dst); |
ef416fc2 | 5147 | } |
c5571a1d | 5148 | else if (ppdPageSize(ppd, DefaultPaperSize)) |
ef416fc2 | 5149 | { |
b423cd4c | 5150 | /* |
5151 | * Add the default media sizes... | |
b423cd4c | 5152 | */ |
5153 | ||
c5571a1d MS |
5154 | num_defaults = cupsAddOption("PageSize", DefaultPaperSize, |
5155 | num_defaults, &defaults); | |
5156 | num_defaults = cupsAddOption("PageRegion", DefaultPaperSize, | |
5157 | num_defaults, &defaults); | |
5158 | num_defaults = cupsAddOption("PaperDimension", DefaultPaperSize, | |
5159 | num_defaults, &defaults); | |
5160 | num_defaults = cupsAddOption("ImageableArea", DefaultPaperSize, | |
5161 | num_defaults, &defaults); | |
ef416fc2 | 5162 | } |
5163 | ||
2abf387c | 5164 | ppdClose(ppd); |
5165 | ||
5166 | /* | |
5167 | * Open the source file for a copy... | |
5168 | */ | |
5169 | ||
5170 | if ((src = cupsFileOpen(tempfile, "rb")) == NULL) | |
5171 | { | |
5172 | cupsFreeOptions(num_defaults, defaults); | |
5173 | unlink(tempfile); | |
5174 | return (-1); | |
5175 | } | |
5176 | ||
ef416fc2 | 5177 | /* |
b423cd4c | 5178 | * Open the destination file for a copy... |
ef416fc2 | 5179 | */ |
5180 | ||
b423cd4c | 5181 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
ef416fc2 | 5182 | { |
2abf387c | 5183 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 5184 | cupsFileClose(src); |
5185 | unlink(tempfile); | |
5186 | return (-1); | |
ef416fc2 | 5187 | } |
5188 | ||
5189 | /* | |
b423cd4c | 5190 | * Copy the source file to the destination... |
ef416fc2 | 5191 | */ |
5192 | ||
b423cd4c | 5193 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
ef416fc2 | 5194 | { |
b423cd4c | 5195 | if (!strncmp(buffer, "*Default", 8)) |
ef416fc2 | 5196 | { |
b423cd4c | 5197 | /* |
5198 | * Check for an previous default option choice... | |
5199 | */ | |
ef416fc2 | 5200 | |
b423cd4c | 5201 | if (!ppd_parse_line(buffer, option, sizeof(option), |
5202 | choice, sizeof(choice))) | |
ef416fc2 | 5203 | { |
2abf387c | 5204 | const char *val; /* Default option value */ |
ef416fc2 | 5205 | |
2abf387c | 5206 | |
5207 | if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL) | |
5208 | { | |
5209 | /* | |
5210 | * Substitute the previous choice... | |
5211 | */ | |
5212 | ||
5213 | snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val); | |
5214 | } | |
b423cd4c | 5215 | } |
ef416fc2 | 5216 | } |
b423cd4c | 5217 | |
5218 | cupsFilePrintf(dst, "%s\n", buffer); | |
ef416fc2 | 5219 | } |
5220 | ||
b423cd4c | 5221 | if (cups_protocol[0]) |
5222 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
5223 | ||
2abf387c | 5224 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 5225 | |
ef416fc2 | 5226 | /* |
b423cd4c | 5227 | * Close both files and return... |
ef416fc2 | 5228 | */ |
5229 | ||
b423cd4c | 5230 | cupsFileClose(src); |
5231 | ||
5232 | unlink(tempfile); | |
5233 | ||
5234 | return (cupsFileClose(dst)); | |
5235 | } | |
5236 | ||
5237 | ||
5238 | /* | |
5239 | * 'copy_job_attrs()' - Copy job attributes. | |
5240 | */ | |
ef416fc2 | 5241 | |
b423cd4c | 5242 | static void |
5243 | copy_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
5244 | cupsd_job_t *job, /* I - Job */ | |
5245 | cups_array_t *ra) /* I - Requested attributes array */ | |
5246 | { | |
5247 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
ef416fc2 | 5248 | |
ef416fc2 | 5249 | |
5250 | /* | |
b423cd4c | 5251 | * Send the requested attributes for each job... |
ef416fc2 | 5252 | */ |
5253 | ||
b423cd4c | 5254 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
5255 | con->servername, con->serverport, "/jobs/%d", | |
5256 | job->id); | |
ef416fc2 | 5257 | |
2e4ff8af MS |
5258 | if (!ra || cupsArrayFind(ra, "document-count")) |
5259 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5260 | "document-count", job->num_files); | |
5261 | ||
9a4f8274 MS |
5262 | if (!ra || cupsArrayFind(ra, "job-media-progress")) |
5263 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5264 | "job-media-progress", job->progress); | |
5265 | ||
b423cd4c | 5266 | if (!ra || cupsArrayFind(ra, "job-more-info")) |
5267 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
5268 | "job-more-info", NULL, job_uri); | |
ef416fc2 | 5269 | |
b423cd4c | 5270 | if (job->state_value > IPP_JOB_PROCESSING && |
5271 | (!ra || cupsArrayFind(ra, "job-preserved"))) | |
5272 | ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved", | |
5273 | job->num_files > 0); | |
ef416fc2 | 5274 | |
b423cd4c | 5275 | if (!ra || cupsArrayFind(ra, "job-printer-up-time")) |
5276 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5277 | "job-printer-up-time", time(NULL)); | |
ef416fc2 | 5278 | |
b423cd4c | 5279 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) |
5280 | add_job_state_reasons(con, job); | |
bd7854cb | 5281 | |
b423cd4c | 5282 | if (!ra || cupsArrayFind(ra, "job-uri")) |
5283 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
5284 | "job-uri", NULL, job_uri); | |
ef416fc2 | 5285 | |
b423cd4c | 5286 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0); |
5287 | } | |
ef416fc2 | 5288 | |
ef416fc2 | 5289 | |
b423cd4c | 5290 | /* |
5291 | * 'copy_printer_attrs()' - Copy printer attributes. | |
5292 | */ | |
ef416fc2 | 5293 | |
b423cd4c | 5294 | static void |
5295 | copy_printer_attrs( | |
5296 | cupsd_client_t *con, /* I - Client connection */ | |
5297 | cupsd_printer_t *printer, /* I - Printer */ | |
5298 | cups_array_t *ra) /* I - Requested attributes array */ | |
5299 | { | |
5300 | char printer_uri[HTTP_MAX_URI]; | |
5301 | /* Printer URI */ | |
5302 | time_t curtime; /* Current time */ | |
5303 | int i; /* Looping var */ | |
5304 | ipp_attribute_t *history; /* History collection */ | |
ef416fc2 | 5305 | |
ef416fc2 | 5306 | |
b423cd4c | 5307 | /* |
5308 | * Copy the printer attributes to the response using requested-attributes | |
5309 | * and document-format attributes that may be provided by the client. | |
5310 | */ | |
ef416fc2 | 5311 | |
b423cd4c | 5312 | curtime = time(NULL); |
ef416fc2 | 5313 | |
b423cd4c | 5314 | #ifdef __APPLE__ |
5315 | if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) && | |
5316 | printer->recoverable) | |
5317 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5318 | "com.apple.print.recoverable-message", NULL, | |
5319 | printer->recoverable); | |
5320 | #endif /* __APPLE__ */ | |
ef416fc2 | 5321 | |
5a738aea MS |
5322 | if (!ra || cupsArrayFind(ra, "marker-change-time")) |
5323 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5324 | "marker-change-time", printer->marker_time); | |
5325 | ||
dd1abb6b MS |
5326 | if (printer->num_printers > 0 && |
5327 | (!ra || cupsArrayFind(ra, "member-uris"))) | |
5328 | { | |
5329 | ipp_attribute_t *member_uris; /* member-uris attribute */ | |
5330 | cupsd_printer_t *p2; /* Printer in class */ | |
5331 | ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */ | |
5332 | ||
5333 | ||
5334 | if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER, | |
5335 | IPP_TAG_URI, "member-uris", | |
5336 | printer->num_printers, NULL, | |
5337 | NULL)) != NULL) | |
5338 | { | |
5339 | for (i = 0; i < printer->num_printers; i ++) | |
5340 | { | |
5341 | p2 = printer->printers[i]; | |
5342 | ||
5343 | if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported", | |
5344 | IPP_TAG_URI)) != NULL) | |
5345 | member_uris->values[i].string.text = | |
5346 | _cupsStrAlloc(p2_uri->values[0].string.text); | |
5347 | else | |
5348 | { | |
5349 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, | |
5350 | sizeof(printer_uri), "ipp", NULL, con->servername, | |
5351 | con->serverport, | |
5352 | (p2->type & CUPS_PRINTER_CLASS) ? | |
5353 | "/classes/%s" : "/printers/%s", p2->name); | |
5354 | member_uris->values[i].string.text = _cupsStrAlloc(printer_uri); | |
5355 | } | |
5356 | } | |
5357 | } | |
5358 | } | |
5359 | ||
323c5de1 | 5360 | if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert"))) |
5361 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING, | |
5362 | "printer-alert", NULL, printer->alert); | |
5363 | ||
5364 | if (printer->alert_description && | |
5365 | (!ra || cupsArrayFind(ra, "printer-alert-description"))) | |
5366 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5367 | "printer-alert-description", NULL, | |
5368 | printer->alert_description); | |
5369 | ||
b423cd4c | 5370 | if (!ra || cupsArrayFind(ra, "printer-current-time")) |
5371 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", | |
5372 | ippTimeToDate(curtime)); | |
ef416fc2 | 5373 | |
d9bca400 MS |
5374 | #ifdef HAVE_DNSSD |
5375 | if (!ra || cupsArrayFind(ra, "printer-dns-sd-name")) | |
5376 | { | |
5377 | if (printer->reg_name) | |
5378 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5379 | "printer-dns-sd-name", NULL, printer->reg_name); | |
5380 | else | |
5381 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE, | |
5382 | "printer-dns-sd-name", 0); | |
5383 | } | |
5384 | #endif /* HAVE_DNSSD */ | |
5385 | ||
b423cd4c | 5386 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) |
5387 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5388 | "printer-error-policy", NULL, printer->error_policy); | |
ef416fc2 | 5389 | |
b423cd4c | 5390 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) |
5391 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", | |
5392 | printer->accepting); | |
ef416fc2 | 5393 | |
b423cd4c | 5394 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) |
5395 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", | |
5396 | printer->shared); | |
ef416fc2 | 5397 | |
b423cd4c | 5398 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) |
5399 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5400 | "printer-op-policy", NULL, printer->op_policy); | |
5401 | ||
5402 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
5403 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
5404 | printer->state); | |
5405 | ||
5406 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) | |
5407 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5408 | "printer-state-change-time", printer->state_time); | |
f7deaa1a | 5409 | |
b423cd4c | 5410 | if (MaxPrinterHistory > 0 && printer->num_history > 0 && |
5411 | cupsArrayFind(ra, "printer-state-history")) | |
ef416fc2 | 5412 | { |
5413 | /* | |
b423cd4c | 5414 | * Printer history is only sent if specifically requested, so that |
5415 | * older CUPS/IPP clients won't barf on the collection attributes. | |
ef416fc2 | 5416 | */ |
5417 | ||
b423cd4c | 5418 | history = ippAddCollections(con->response, IPP_TAG_PRINTER, |
5419 | "printer-state-history", | |
5420 | printer->num_history, NULL); | |
5421 | ||
5422 | for (i = 0; i < printer->num_history; i ++) | |
5423 | copy_attrs(history->values[i].collection = ippNew(), printer->history[i], | |
5424 | NULL, IPP_TAG_ZERO, 0); | |
ef416fc2 | 5425 | } |
5426 | ||
b423cd4c | 5427 | if (!ra || cupsArrayFind(ra, "printer-state-message")) |
5428 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5429 | "printer-state-message", NULL, printer->state_message); | |
ef416fc2 | 5430 | |
b423cd4c | 5431 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) |
5432 | add_printer_state_reasons(con, printer); | |
ef416fc2 | 5433 | |
b423cd4c | 5434 | if (!ra || cupsArrayFind(ra, "printer-type")) |
5435 | { | |
5436 | int type; /* printer-type value */ | |
ef416fc2 | 5437 | |
ef416fc2 | 5438 | |
ef416fc2 | 5439 | /* |
b423cd4c | 5440 | * Add the CUPS-specific printer-type attribute... |
ef416fc2 | 5441 | */ |
5442 | ||
09a101d6 | 5443 | type = printer->type; |
b423cd4c | 5444 | |
5445 | if (printer == DefaultPrinter) | |
5446 | type |= CUPS_PRINTER_DEFAULT; | |
5447 | ||
5448 | if (!printer->accepting) | |
5449 | type |= CUPS_PRINTER_REJECTING; | |
5450 | ||
5451 | if (!printer->shared) | |
5452 | type |= CUPS_PRINTER_NOT_SHARED; | |
5453 | ||
5454 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", | |
5455 | type); | |
ef416fc2 | 5456 | } |
ef416fc2 | 5457 | |
b423cd4c | 5458 | if (!ra || cupsArrayFind(ra, "printer-up-time")) |
5459 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5460 | "printer-up-time", curtime); | |
ef416fc2 | 5461 | |
b423cd4c | 5462 | if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) && |
5463 | !ippFindAttribute(printer->attrs, "printer-uri-supported", | |
5464 | IPP_TAG_URI)) | |
ef416fc2 | 5465 | { |
b423cd4c | 5466 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), |
5467 | "ipp", NULL, con->servername, con->serverport, | |
8ca02f3c | 5468 | (printer->type & CUPS_PRINTER_CLASS) ? |
5469 | "/classes/%s" : "/printers/%s", printer->name); | |
b423cd4c | 5470 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, |
5471 | "printer-uri-supported", NULL, printer_uri); | |
5472 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", | |
5473 | printer_uri); | |
5474 | } | |
ef416fc2 | 5475 | |
b423cd4c | 5476 | if (!ra || cupsArrayFind(ra, "queued-job-count")) |
5477 | add_queued_job_count(con, printer); | |
ef416fc2 | 5478 | |
b423cd4c | 5479 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0); |
5480 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY); | |
5481 | } | |
ef416fc2 | 5482 | |
ef416fc2 | 5483 | |
b423cd4c | 5484 | /* |
5485 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
5486 | */ | |
ef416fc2 | 5487 | |
b423cd4c | 5488 | static void |
5489 | copy_subscription_attrs( | |
5490 | cupsd_client_t *con, /* I - Client connection */ | |
5491 | cupsd_subscription_t *sub, /* I - Subscription */ | |
5492 | cups_array_t *ra) /* I - Requested attributes array */ | |
5493 | { | |
5494 | ipp_attribute_t *attr; /* Current attribute */ | |
5495 | char printer_uri[HTTP_MAX_URI]; | |
5496 | /* Printer URI */ | |
5497 | int count; /* Number of events */ | |
5498 | unsigned mask; /* Current event mask */ | |
5499 | const char *name; /* Current event name */ | |
ef416fc2 | 5500 | |
ef416fc2 | 5501 | |
b423cd4c | 5502 | /* |
5503 | * Copy the subscription attributes to the response using the | |
5504 | * requested-attributes attribute that may be provided by the client. | |
5505 | */ | |
ef416fc2 | 5506 | |
b423cd4c | 5507 | if (!ra || cupsArrayFind(ra, "notify-events")) |
5508 | { | |
5509 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
5510 | { | |
5511 | /* | |
5512 | * Simple event list... | |
5513 | */ | |
ef416fc2 | 5514 | |
b423cd4c | 5515 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, |
d09495fa | 5516 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), |
b423cd4c | 5517 | "notify-events", NULL, name); |
5518 | } | |
5519 | else | |
5520 | { | |
5521 | /* | |
5522 | * Complex event list... | |
5523 | */ | |
ef416fc2 | 5524 | |
b423cd4c | 5525 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5526 | if (sub->mask & mask) | |
5527 | count ++; | |
ef416fc2 | 5528 | |
b423cd4c | 5529 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, |
d09495fa | 5530 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), |
b423cd4c | 5531 | "notify-events", count, NULL, NULL); |
ef416fc2 | 5532 | |
b423cd4c | 5533 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5534 | if (sub->mask & mask) | |
5535 | { | |
5536 | attr->values[count].string.text = | |
5537 | (char *)cupsdEventName((cupsd_eventmask_t)mask); | |
ef416fc2 | 5538 | |
b423cd4c | 5539 | count ++; |
5540 | } | |
ef416fc2 | 5541 | } |
b423cd4c | 5542 | } |
ef416fc2 | 5543 | |
b423cd4c | 5544 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) |
5545 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5546 | "notify-job-id", sub->job->id); | |
ef416fc2 | 5547 | |
b423cd4c | 5548 | if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))) |
5549 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5550 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 5551 | |
b423cd4c | 5552 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) |
5553 | { | |
5554 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
5555 | "ipp", NULL, con->servername, con->serverport, | |
5556 | "/printers/%s", sub->dest->name); | |
5557 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5558 | "notify-printer-uri", NULL, printer_uri); | |
ef416fc2 | 5559 | } |
ef416fc2 | 5560 | |
b423cd4c | 5561 | if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))) |
5562 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5563 | "notify-recipient-uri", NULL, sub->recipient); | |
5564 | else if (!ra || cupsArrayFind(ra, "notify-pull-method")) | |
5565 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
5566 | "notify-pull-method", NULL, "ippget"); | |
ef416fc2 | 5567 | |
b423cd4c | 5568 | if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")) |
5569 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
5570 | "notify-subscriber-user-name", NULL, sub->owner); | |
ef416fc2 | 5571 | |
b423cd4c | 5572 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) |
5573 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5574 | "notify-subscription-id", sub->id); | |
ef416fc2 | 5575 | |
b423cd4c | 5576 | if (!ra || cupsArrayFind(ra, "notify-time-interval")) |
5577 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5578 | "notify-time-interval", sub->interval); | |
ef416fc2 | 5579 | |
b423cd4c | 5580 | if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data"))) |
5581 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
5582 | sub->user_data, sub->user_data_len); | |
5583 | } | |
ef416fc2 | 5584 | |
ef416fc2 | 5585 | |
b423cd4c | 5586 | /* |
5587 | * 'create_job()' - Print a file to a printer or class. | |
5588 | */ | |
5589 | ||
5590 | static void | |
5591 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
5592 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5593 | { | |
f7deaa1a | 5594 | cupsd_printer_t *printer; /* Printer */ |
5595 | cupsd_job_t *job; /* New job */ | |
b423cd4c | 5596 | |
ef416fc2 | 5597 | |
b423cd4c | 5598 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, |
5599 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5600 | |
f7deaa1a | 5601 | /* |
5602 | * Is the destination valid? | |
5603 | */ | |
5604 | ||
5605 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
5606 | { | |
5607 | /* | |
5608 | * Bad URI... | |
5609 | */ | |
5610 | ||
5611 | send_ipp_status(con, IPP_NOT_FOUND, | |
5612 | _("The printer or class was not found.")); | |
5613 | return; | |
5614 | } | |
5615 | ||
ef416fc2 | 5616 | /* |
b423cd4c | 5617 | * Create the job object... |
ef416fc2 | 5618 | */ |
5619 | ||
f7deaa1a | 5620 | if ((job = add_job(con, printer, NULL)) == NULL) |
b423cd4c | 5621 | return; |
ef416fc2 | 5622 | |
09a101d6 | 5623 | job->pending_timeout = 1; |
5624 | ||
ef416fc2 | 5625 | /* |
5626 | * Save and log the job... | |
5627 | */ | |
f7deaa1a | 5628 | |
75bd9771 MS |
5629 | cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".", |
5630 | job->dest, job->username); | |
ef416fc2 | 5631 | } |
5632 | ||
5633 | ||
5634 | /* | |
5635 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
5636 | */ | |
5637 | ||
5638 | static cups_array_t * /* O - Array of attributes or NULL */ | |
5639 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
5640 | { | |
5641 | int i; /* Looping var */ | |
5642 | ipp_attribute_t *requested; /* requested-attributes attribute */ | |
5643 | cups_array_t *ra; /* Requested attributes array */ | |
5644 | char *value; /* Current value */ | |
5645 | ||
5646 | ||
5647 | /* | |
5648 | * Get the requested-attributes attribute, and return NULL if we don't | |
5649 | * have one... | |
5650 | */ | |
5651 | ||
5652 | if ((requested = ippFindAttribute(request, "requested-attributes", | |
5653 | IPP_TAG_KEYWORD)) == NULL) | |
5654 | return (NULL); | |
5655 | ||
5656 | /* | |
5657 | * If the attribute contains a single "all" keyword, return NULL... | |
5658 | */ | |
5659 | ||
5660 | if (requested->num_values == 1 && | |
5661 | !strcmp(requested->values[0].string.text, "all")) | |
5662 | return (NULL); | |
5663 | ||
5664 | /* | |
5665 | * Create an array using "strcmp" as the comparison function... | |
5666 | */ | |
5667 | ||
5668 | ra = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
5669 | ||
5670 | for (i = 0; i < requested->num_values; i ++) | |
5671 | { | |
5672 | value = requested->values[i].string.text; | |
5673 | ||
5674 | if (!strcmp(value, "job-template")) | |
5675 | { | |
5676 | cupsArrayAdd(ra, "copies"); | |
5677 | cupsArrayAdd(ra, "copies-default"); | |
5678 | cupsArrayAdd(ra, "copies-supported"); | |
5679 | cupsArrayAdd(ra, "finishings"); | |
5680 | cupsArrayAdd(ra, "finishings-default"); | |
5681 | cupsArrayAdd(ra, "finishings-supported"); | |
5682 | cupsArrayAdd(ra, "job-hold-until"); | |
5683 | cupsArrayAdd(ra, "job-hold-until-default"); | |
5684 | cupsArrayAdd(ra, "job-hold-until-supported"); | |
5685 | cupsArrayAdd(ra, "job-priority"); | |
5686 | cupsArrayAdd(ra, "job-priority-default"); | |
5687 | cupsArrayAdd(ra, "job-priority-supported"); | |
5688 | cupsArrayAdd(ra, "job-sheets"); | |
5689 | cupsArrayAdd(ra, "job-sheets-default"); | |
5690 | cupsArrayAdd(ra, "job-sheets-supported"); | |
5691 | cupsArrayAdd(ra, "media"); | |
5692 | cupsArrayAdd(ra, "media-default"); | |
5693 | cupsArrayAdd(ra, "media-supported"); | |
5694 | cupsArrayAdd(ra, "multiple-document-handling"); | |
5695 | cupsArrayAdd(ra, "multiple-document-handling-default"); | |
5696 | cupsArrayAdd(ra, "multiple-document-handling-supported"); | |
5697 | cupsArrayAdd(ra, "number-up"); | |
5698 | cupsArrayAdd(ra, "number-up-default"); | |
5699 | cupsArrayAdd(ra, "number-up-supported"); | |
5700 | cupsArrayAdd(ra, "orientation-requested"); | |
5701 | cupsArrayAdd(ra, "orientation-requested-default"); | |
5702 | cupsArrayAdd(ra, "orientation-requested-supported"); | |
5703 | cupsArrayAdd(ra, "page-ranges"); | |
5704 | cupsArrayAdd(ra, "page-ranges-supported"); | |
5705 | cupsArrayAdd(ra, "printer-resolution"); | |
5706 | cupsArrayAdd(ra, "printer-resolution-default"); | |
5707 | cupsArrayAdd(ra, "printer-resolution-supported"); | |
5708 | cupsArrayAdd(ra, "print-quality"); | |
5709 | cupsArrayAdd(ra, "print-quality-default"); | |
5710 | cupsArrayAdd(ra, "print-quality-supported"); | |
5711 | cupsArrayAdd(ra, "sides"); | |
5712 | cupsArrayAdd(ra, "sides-default"); | |
5713 | cupsArrayAdd(ra, "sides-supported"); | |
5714 | } | |
5715 | else if (!strcmp(value, "job-description")) | |
5716 | { | |
5717 | cupsArrayAdd(ra, "date-time-at-completed"); | |
5718 | cupsArrayAdd(ra, "date-time-at-creation"); | |
5719 | cupsArrayAdd(ra, "date-time-at-processing"); | |
5720 | cupsArrayAdd(ra, "job-detailed-status-message"); | |
5721 | cupsArrayAdd(ra, "job-document-access-errors"); | |
5722 | cupsArrayAdd(ra, "job-id"); | |
5723 | cupsArrayAdd(ra, "job-impressions"); | |
5724 | cupsArrayAdd(ra, "job-impressions-completed"); | |
5725 | cupsArrayAdd(ra, "job-k-octets"); | |
5726 | cupsArrayAdd(ra, "job-k-octets-processed"); | |
9a4f8274 | 5727 | cupsArrayAdd(ra, "job-media-progress"); |
ef416fc2 | 5728 | cupsArrayAdd(ra, "job-media-sheets"); |
5729 | cupsArrayAdd(ra, "job-media-sheets-completed"); | |
5730 | cupsArrayAdd(ra, "job-message-from-operator"); | |
5731 | cupsArrayAdd(ra, "job-more-info"); | |
5732 | cupsArrayAdd(ra, "job-name"); | |
5733 | cupsArrayAdd(ra, "job-originating-user-name"); | |
5734 | cupsArrayAdd(ra, "job-printer-up-time"); | |
5735 | cupsArrayAdd(ra, "job-printer-uri"); | |
5736 | cupsArrayAdd(ra, "job-state"); | |
5737 | cupsArrayAdd(ra, "job-state-message"); | |
5738 | cupsArrayAdd(ra, "job-state-reasons"); | |
5739 | cupsArrayAdd(ra, "job-uri"); | |
5740 | cupsArrayAdd(ra, "number-of-documents"); | |
5741 | cupsArrayAdd(ra, "number-of-intervening-jobs"); | |
5742 | cupsArrayAdd(ra, "output-device-assigned"); | |
5743 | cupsArrayAdd(ra, "time-at-completed"); | |
5744 | cupsArrayAdd(ra, "time-at-creation"); | |
5745 | cupsArrayAdd(ra, "time-at-processing"); | |
5746 | } | |
5747 | else if (!strcmp(value, "printer-description")) | |
5748 | { | |
5749 | cupsArrayAdd(ra, "charset-configured"); | |
5750 | cupsArrayAdd(ra, "charset-supported"); | |
5751 | cupsArrayAdd(ra, "color-supported"); | |
5752 | cupsArrayAdd(ra, "compression-supported"); | |
5753 | cupsArrayAdd(ra, "document-format-default"); | |
5754 | cupsArrayAdd(ra, "document-format-supported"); | |
5755 | cupsArrayAdd(ra, "generated-natural-language-supported"); | |
5756 | cupsArrayAdd(ra, "ipp-versions-supported"); | |
5757 | cupsArrayAdd(ra, "job-impressions-supported"); | |
5758 | cupsArrayAdd(ra, "job-k-octets-supported"); | |
5759 | cupsArrayAdd(ra, "job-media-sheets-supported"); | |
5760 | cupsArrayAdd(ra, "multiple-document-jobs-supported"); | |
5761 | cupsArrayAdd(ra, "multiple-operation-time-out"); | |
5762 | cupsArrayAdd(ra, "natural-language-configured"); | |
5763 | cupsArrayAdd(ra, "notify-attributes-supported"); | |
5764 | cupsArrayAdd(ra, "notify-lease-duration-default"); | |
5765 | cupsArrayAdd(ra, "notify-lease-duration-supported"); | |
5766 | cupsArrayAdd(ra, "notify-max-events-supported"); | |
f301802f | 5767 | cupsArrayAdd(ra, "notify-events-default"); |
5768 | cupsArrayAdd(ra, "notify-events-supported"); | |
ef416fc2 | 5769 | cupsArrayAdd(ra, "notify-pull-method-supported"); |
5770 | cupsArrayAdd(ra, "notify-schemes-supported"); | |
5771 | cupsArrayAdd(ra, "operations-supported"); | |
5772 | cupsArrayAdd(ra, "pages-per-minute"); | |
5773 | cupsArrayAdd(ra, "pages-per-minute-color"); | |
5774 | cupsArrayAdd(ra, "pdl-override-supported"); | |
323c5de1 | 5775 | cupsArrayAdd(ra, "printer-alert"); |
5776 | cupsArrayAdd(ra, "printer-alert-description"); | |
01ce6322 | 5777 | cupsArrayAdd(ra, "printer-commands"); |
ef416fc2 | 5778 | cupsArrayAdd(ra, "printer-current-time"); |
5779 | cupsArrayAdd(ra, "printer-driver-installer"); | |
01ce6322 | 5780 | cupsArrayAdd(ra, "printer-dns-sd-name"); |
ef416fc2 | 5781 | cupsArrayAdd(ra, "printer-info"); |
5782 | cupsArrayAdd(ra, "printer-is-accepting-jobs"); | |
5783 | cupsArrayAdd(ra, "printer-location"); | |
5784 | cupsArrayAdd(ra, "printer-make-and-model"); | |
5785 | cupsArrayAdd(ra, "printer-message-from-operator"); | |
5786 | cupsArrayAdd(ra, "printer-more-info"); | |
5787 | cupsArrayAdd(ra, "printer-more-info-manufacturer"); | |
5788 | cupsArrayAdd(ra, "printer-name"); | |
5789 | cupsArrayAdd(ra, "printer-state"); | |
5790 | cupsArrayAdd(ra, "printer-state-message"); | |
5791 | cupsArrayAdd(ra, "printer-state-reasons"); | |
01ce6322 | 5792 | cupsArrayAdd(ra, "printer-type"); |
ef416fc2 | 5793 | cupsArrayAdd(ra, "printer-up-time"); |
5794 | cupsArrayAdd(ra, "printer-uri-supported"); | |
5795 | cupsArrayAdd(ra, "queued-job-count"); | |
5796 | cupsArrayAdd(ra, "reference-uri-schemes-supported"); | |
5797 | cupsArrayAdd(ra, "uri-authentication-supported"); | |
5798 | cupsArrayAdd(ra, "uri-security-supported"); | |
5799 | } | |
f7deaa1a | 5800 | else if (!strcmp(value, "printer-defaults")) |
5801 | { | |
5802 | char *name; /* Option name */ | |
5803 | ||
5804 | ||
5805 | for (name = (char *)cupsArrayFirst(CommonDefaults); | |
5806 | name; | |
5807 | name = (char *)cupsArrayNext(CommonDefaults)) | |
5808 | cupsArrayAdd(ra, name); | |
5809 | } | |
ef416fc2 | 5810 | else if (!strcmp(value, "subscription-template")) |
5811 | { | |
5812 | cupsArrayAdd(ra, "notify-attributes"); | |
5813 | cupsArrayAdd(ra, "notify-charset"); | |
5814 | cupsArrayAdd(ra, "notify-events"); | |
5815 | cupsArrayAdd(ra, "notify-lease-duration"); | |
5816 | cupsArrayAdd(ra, "notify-natural-language"); | |
5817 | cupsArrayAdd(ra, "notify-pull-method"); | |
5818 | cupsArrayAdd(ra, "notify-recipient-uri"); | |
5819 | cupsArrayAdd(ra, "notify-time-interval"); | |
5820 | cupsArrayAdd(ra, "notify-user-data"); | |
5821 | } | |
5822 | else | |
5823 | cupsArrayAdd(ra, value); | |
5824 | } | |
5825 | ||
5826 | return (ra); | |
5827 | } | |
5828 | ||
5829 | ||
5830 | /* | |
5831 | * 'create_subscription()' - Create a notification subscription. | |
5832 | */ | |
5833 | ||
5834 | static void | |
5835 | create_subscription( | |
5836 | cupsd_client_t *con, /* I - Client connection */ | |
5837 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5838 | { | |
5839 | http_status_t status; /* Policy status */ | |
5840 | int i; /* Looping var */ | |
5841 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 5842 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ed486911 | 5843 | char scheme[HTTP_MAX_URI], |
5844 | /* Scheme portion of URI */ | |
ef416fc2 | 5845 | userpass[HTTP_MAX_URI], |
5846 | /* Username portion of URI */ | |
5847 | host[HTTP_MAX_URI], | |
5848 | /* Host portion of URI */ | |
5849 | resource[HTTP_MAX_URI]; | |
5850 | /* Resource portion of URI */ | |
5851 | int port; /* Port portion of URI */ | |
5852 | cupsd_printer_t *printer; /* Printer/class */ | |
5853 | cupsd_job_t *job; /* Job */ | |
5854 | int jobid; /* Job ID */ | |
5855 | cupsd_subscription_t *sub; /* Subscription object */ | |
bc44d920 | 5856 | const char *username, /* requesting-user-name or |
5857 | authenticated username */ | |
ef416fc2 | 5858 | *recipient, /* notify-recipient-uri */ |
5859 | *pullmethod; /* notify-pull-method */ | |
5860 | ipp_attribute_t *user_data; /* notify-user-data */ | |
5861 | int interval, /* notify-time-interval */ | |
5862 | lease; /* notify-lease-duration */ | |
5863 | unsigned mask; /* notify-events */ | |
f7deaa1a | 5864 | ipp_attribute_t *notify_events,/* notify-events(-default) */ |
5865 | *notify_lease; /* notify-lease-duration(-default) */ | |
ef416fc2 | 5866 | |
5867 | ||
bd7854cb | 5868 | #ifdef DEBUG |
5869 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5870 | { | |
5871 | if (attr->group_tag != IPP_TAG_ZERO) | |
3dfe78b3 | 5872 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag, |
bd7854cb | 5873 | attr->value_tag, attr->name); |
5874 | else | |
3dfe78b3 | 5875 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----"); |
bd7854cb | 5876 | } |
5877 | #endif /* DEBUG */ | |
5878 | ||
ef416fc2 | 5879 | /* |
5880 | * Is the destination valid? | |
5881 | */ | |
5882 | ||
fa73b229 | 5883 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
5884 | "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")", | |
5885 | con, con->http.fd, uri->values[0].string.text); | |
5886 | ||
ed486911 | 5887 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
5888 | sizeof(scheme), userpass, sizeof(userpass), host, | |
a4d04587 | 5889 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 5890 | |
5891 | if (!strcmp(resource, "/")) | |
5892 | { | |
ef416fc2 | 5893 | dtype = (cups_ptype_t)0; |
5894 | printer = NULL; | |
5895 | } | |
5896 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
5897 | { | |
ef416fc2 | 5898 | dtype = (cups_ptype_t)0; |
5899 | printer = NULL; | |
5900 | } | |
5901 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
5902 | { | |
ef416fc2 | 5903 | dtype = CUPS_PRINTER_CLASS; |
5904 | printer = NULL; | |
5905 | } | |
f7deaa1a | 5906 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 5907 | { |
5908 | /* | |
5909 | * Bad URI... | |
5910 | */ | |
5911 | ||
5912 | send_ipp_status(con, IPP_NOT_FOUND, | |
5913 | _("The printer or class was not found.")); | |
5914 | return; | |
5915 | } | |
5916 | ||
5917 | /* | |
5918 | * Check policy... | |
5919 | */ | |
5920 | ||
5921 | if (printer) | |
5922 | { | |
f7deaa1a | 5923 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
5924 | NULL)) != HTTP_OK) | |
ef416fc2 | 5925 | { |
f899b121 | 5926 | send_http_error(con, status, printer); |
ef416fc2 | 5927 | return; |
5928 | } | |
5929 | } | |
5930 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5931 | { | |
f899b121 | 5932 | send_http_error(con, status, NULL); |
ef416fc2 | 5933 | return; |
5934 | } | |
5935 | ||
5936 | /* | |
5937 | * Get the user that is requesting the subscription... | |
5938 | */ | |
5939 | ||
e00b005a | 5940 | username = get_username(con); |
ef416fc2 | 5941 | |
5942 | /* | |
5943 | * Find the first subscription group attribute; return if we have | |
5944 | * none... | |
5945 | */ | |
5946 | ||
5947 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5948 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
5949 | break; | |
5950 | ||
5951 | if (!attr) | |
5952 | { | |
5953 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5954 | _("No subscription attributes in request!")); | |
5955 | return; | |
5956 | } | |
5957 | ||
5958 | /* | |
5959 | * Process the subscription attributes in the request... | |
5960 | */ | |
5961 | ||
fa73b229 | 5962 | con->response->request.status.status_code = IPP_BAD_REQUEST; |
5963 | ||
ef416fc2 | 5964 | while (attr) |
5965 | { | |
5966 | recipient = NULL; | |
5967 | pullmethod = NULL; | |
5968 | user_data = NULL; | |
5969 | interval = 0; | |
5970 | lease = DefaultLeaseDuration; | |
5971 | jobid = 0; | |
5972 | mask = CUPSD_EVENT_NONE; | |
5973 | ||
f7deaa1a | 5974 | if (printer) |
5975 | { | |
5976 | notify_events = ippFindAttribute(printer->attrs, "notify-events-default", | |
5977 | IPP_TAG_KEYWORD); | |
5978 | notify_lease = ippFindAttribute(printer->attrs, | |
5979 | "notify-lease-duration-default", | |
5980 | IPP_TAG_INTEGER); | |
5981 | ||
5982 | if (notify_lease) | |
5983 | lease = notify_lease->values[0].integer; | |
5984 | } | |
5985 | else | |
5986 | { | |
5987 | notify_events = NULL; | |
5988 | notify_lease = NULL; | |
5989 | } | |
5990 | ||
ef416fc2 | 5991 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
5992 | { | |
7594b224 | 5993 | if (!strcmp(attr->name, "notify-recipient-uri") && |
ef416fc2 | 5994 | attr->value_tag == IPP_TAG_URI) |
ed486911 | 5995 | { |
5996 | /* | |
5997 | * Validate the recipient scheme against the ServerBin/notifier | |
5998 | * directory... | |
5999 | */ | |
6000 | ||
6001 | char notifier[1024]; /* Notifier filename */ | |
6002 | ||
6003 | ||
ef416fc2 | 6004 | recipient = attr->values[0].string.text; |
ed486911 | 6005 | |
8ca02f3c | 6006 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, |
6007 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
6008 | host, sizeof(host), &port, | |
6009 | resource, sizeof(resource)) < HTTP_URI_OK) | |
ed486911 | 6010 | { |
6011 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7594b224 | 6012 | _("Bad notify-recipient-uri URI \"%s\"!"), recipient); |
ed486911 | 6013 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
6014 | "notify-status-code", IPP_URI_SCHEME); | |
6015 | return; | |
6016 | } | |
6017 | ||
6018 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
6019 | scheme); | |
6020 | if (access(notifier, X_OK)) | |
6021 | { | |
6022 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
bc44d920 | 6023 | _("notify-recipient-uri URI \"%s\" uses unknown " |
6024 | "scheme!"), recipient); | |
ed486911 | 6025 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
6026 | "notify-status-code", IPP_URI_SCHEME); | |
6027 | return; | |
6028 | } | |
3dfe78b3 MS |
6029 | |
6030 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
6031 | { | |
6032 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6033 | _("notify-recipient-uri URI \"%s\" is already used!"), | |
6034 | recipient); | |
6035 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6036 | "notify-status-code", IPP_ATTRIBUTES); | |
6037 | return; | |
6038 | } | |
ed486911 | 6039 | } |
ef416fc2 | 6040 | else if (!strcmp(attr->name, "notify-pull-method") && |
6041 | attr->value_tag == IPP_TAG_KEYWORD) | |
ed486911 | 6042 | { |
ef416fc2 | 6043 | pullmethod = attr->values[0].string.text; |
ed486911 | 6044 | |
6045 | if (strcmp(pullmethod, "ippget")) | |
6046 | { | |
6047 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6048 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
6049 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6050 | "notify-status-code", IPP_ATTRIBUTES); | |
6051 | return; | |
6052 | } | |
6053 | } | |
ef416fc2 | 6054 | else if (!strcmp(attr->name, "notify-charset") && |
6055 | attr->value_tag == IPP_TAG_CHARSET && | |
6056 | strcmp(attr->values[0].string.text, "us-ascii") && | |
6057 | strcmp(attr->values[0].string.text, "utf-8")) | |
6058 | { | |
6059 | send_ipp_status(con, IPP_CHARSET, | |
6060 | _("Character set \"%s\" not supported!"), | |
6061 | attr->values[0].string.text); | |
6062 | return; | |
6063 | } | |
6064 | else if (!strcmp(attr->name, "notify-natural-language") && | |
6065 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
6066 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
6067 | { | |
6068 | send_ipp_status(con, IPP_CHARSET, | |
6069 | _("Language \"%s\" not supported!"), | |
6070 | attr->values[0].string.text); | |
6071 | return; | |
6072 | } | |
6073 | else if (!strcmp(attr->name, "notify-user-data") && | |
6074 | attr->value_tag == IPP_TAG_STRING) | |
6075 | { | |
6076 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
6077 | { | |
6078 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
6079 | _("The notify-user-data value is too large " | |
6080 | "(%d > 63 octets)!"), | |
6081 | attr->values[0].unknown.length); | |
6082 | return; | |
6083 | } | |
6084 | ||
6085 | user_data = attr; | |
6086 | } | |
6087 | else if (!strcmp(attr->name, "notify-events") && | |
6088 | attr->value_tag == IPP_TAG_KEYWORD) | |
f7deaa1a | 6089 | notify_events = attr; |
ef416fc2 | 6090 | else if (!strcmp(attr->name, "notify-lease-duration") && |
6091 | attr->value_tag == IPP_TAG_INTEGER) | |
6092 | lease = attr->values[0].integer; | |
6093 | else if (!strcmp(attr->name, "notify-time-interval") && | |
6094 | attr->value_tag == IPP_TAG_INTEGER) | |
6095 | interval = attr->values[0].integer; | |
6096 | else if (!strcmp(attr->name, "notify-job-id") && | |
6097 | attr->value_tag == IPP_TAG_INTEGER) | |
6098 | jobid = attr->values[0].integer; | |
6099 | ||
6100 | attr = attr->next; | |
6101 | } | |
6102 | ||
f7deaa1a | 6103 | if (notify_events) |
6104 | { | |
6105 | for (i = 0; i < notify_events->num_values; i ++) | |
6106 | mask |= cupsdEventValue(notify_events->values[i].string.text); | |
6107 | } | |
6108 | ||
bd7854cb | 6109 | if (recipient) |
6110 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); | |
6111 | if (pullmethod) | |
6112 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
6113 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
6114 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
fa73b229 | 6115 | |
ef416fc2 | 6116 | if (!recipient && !pullmethod) |
6117 | break; | |
6118 | ||
6119 | if (mask == CUPSD_EVENT_NONE) | |
6120 | { | |
6121 | if (jobid) | |
6122 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
6123 | else if (printer) | |
6124 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
6125 | else | |
6126 | { | |
6127 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6128 | _("notify-events not specified!")); | |
6129 | return; | |
6130 | } | |
6131 | } | |
6132 | ||
bd7854cb | 6133 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) |
ef416fc2 | 6134 | { |
6135 | cupsdLogMessage(CUPSD_LOG_INFO, | |
6136 | "create_subscription: Limiting notify-lease-duration to " | |
6137 | "%d seconds.", | |
6138 | MaxLeaseDuration); | |
6139 | lease = MaxLeaseDuration; | |
6140 | } | |
6141 | ||
6142 | if (jobid) | |
6143 | { | |
6144 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6145 | { | |
6146 | send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid); | |
6147 | return; | |
6148 | } | |
6149 | } | |
6150 | else | |
6151 | job = NULL; | |
6152 | ||
6153 | sub = cupsdAddSubscription(mask, printer, job, recipient, 0); | |
6154 | ||
fa73b229 | 6155 | if (job) |
6156 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
6157 | sub->id, job->id); | |
6158 | else if (printer) | |
6159 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
6160 | "Added subscription %d for printer \"%s\"", | |
6161 | sub->id, printer->name); | |
6162 | else | |
6163 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server", | |
6164 | sub->id); | |
6165 | ||
ef416fc2 | 6166 | sub->interval = interval; |
6167 | sub->lease = lease; | |
bd7854cb | 6168 | sub->expire = lease ? time(NULL) + lease : 0; |
ef416fc2 | 6169 | |
6170 | cupsdSetString(&sub->owner, username); | |
6171 | ||
6172 | if (user_data) | |
6173 | { | |
6174 | sub->user_data_len = user_data->values[0].unknown.length; | |
6175 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
6176 | sub->user_data_len); | |
6177 | } | |
6178 | ||
6179 | ippAddSeparator(con->response); | |
6180 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
6181 | "notify-subscription-id", sub->id); | |
6182 | ||
fa73b229 | 6183 | con->response->request.status.status_code = IPP_OK; |
6184 | ||
ef416fc2 | 6185 | if (attr) |
6186 | attr = attr->next; | |
6187 | } | |
6188 | ||
3dfe78b3 | 6189 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 6190 | } |
6191 | ||
6192 | ||
6193 | /* | |
6194 | * 'delete_printer()' - Remove a printer or class from the system. | |
6195 | */ | |
6196 | ||
6197 | static void | |
6198 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
6199 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
6200 | { | |
6201 | http_status_t status; /* Policy status */ | |
bc44d920 | 6202 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 6203 | cupsd_printer_t *printer; /* Printer/class */ |
6204 | char filename[1024]; /* Script/PPD filename */ | |
6205 | ||
6206 | ||
6207 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
6208 | con->http.fd, uri->values[0].string.text); | |
6209 | ||
6210 | /* | |
6211 | * Do we have a valid URI? | |
6212 | */ | |
6213 | ||
f7deaa1a | 6214 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 6215 | { |
6216 | /* | |
6217 | * Bad URI... | |
6218 | */ | |
6219 | ||
6220 | send_ipp_status(con, IPP_NOT_FOUND, | |
6221 | _("The printer or class was not found.")); | |
6222 | return; | |
6223 | } | |
6224 | ||
6225 | /* | |
6226 | * Check policy... | |
6227 | */ | |
6228 | ||
6229 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6230 | { | |
f899b121 | 6231 | send_http_error(con, status, NULL); |
ef416fc2 | 6232 | return; |
6233 | } | |
6234 | ||
6235 | /* | |
6236 | * Remove old jobs... | |
6237 | */ | |
6238 | ||
f7deaa1a | 6239 | cupsdCancelJobs(printer->name, NULL, 1); |
ef416fc2 | 6240 | |
6241 | /* | |
6242 | * Remove old subscriptions and send a "deleted printer" event... | |
6243 | */ | |
6244 | ||
6245 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
6246 | "%s \"%s\" deleted by \"%s\".", | |
6247 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
f7deaa1a | 6248 | printer->name, get_username(con)); |
ef416fc2 | 6249 | |
6250 | cupsdExpireSubscriptions(printer, NULL); | |
f7deaa1a | 6251 | |
ef416fc2 | 6252 | /* |
6253 | * Remove any old PPD or script files... | |
6254 | */ | |
6255 | ||
f7deaa1a | 6256 | snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, |
6257 | printer->name); | |
ef416fc2 | 6258 | unlink(filename); |
6259 | ||
f7deaa1a | 6260 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, |
6261 | printer->name); | |
ef416fc2 | 6262 | unlink(filename); |
6263 | ||
568fa3fa MS |
6264 | #ifdef __APPLE__ |
6265 | /* | |
6266 | * Unregister color profiles... | |
6267 | */ | |
6268 | ||
6269 | apple_unregister_profiles(printer); | |
6270 | #endif /* __APPLE__ */ | |
6271 | ||
ef416fc2 | 6272 | if (dtype & CUPS_PRINTER_CLASS) |
6273 | { | |
f7deaa1a | 6274 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", |
6275 | printer->name, get_username(con)); | |
ef416fc2 | 6276 | |
6277 | cupsdDeletePrinter(printer, 0); | |
3dfe78b3 | 6278 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
ef416fc2 | 6279 | } |
6280 | else | |
6281 | { | |
f7deaa1a | 6282 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", |
6283 | printer->name, get_username(con)); | |
ef416fc2 | 6284 | |
6285 | cupsdDeletePrinter(printer, 0); | |
3dfe78b3 | 6286 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 6287 | } |
6288 | ||
3dfe78b3 | 6289 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 6290 | |
6291 | /* | |
6292 | * Return with no errors... | |
6293 | */ | |
6294 | ||
6295 | con->response->request.status.status_code = IPP_OK; | |
6296 | } | |
6297 | ||
6298 | ||
6299 | /* | |
6300 | * 'get_default()' - Get the default destination. | |
6301 | */ | |
6302 | ||
6303 | static void | |
6304 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
6305 | { | |
fa73b229 | 6306 | http_status_t status; /* Policy status */ |
6307 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 6308 | |
6309 | ||
6310 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd); | |
6311 | ||
6312 | /* | |
6313 | * Check policy... | |
6314 | */ | |
6315 | ||
6316 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6317 | { | |
f899b121 | 6318 | send_http_error(con, status, NULL); |
ef416fc2 | 6319 | return; |
6320 | } | |
6321 | ||
fa73b229 | 6322 | if (DefaultPrinter) |
ef416fc2 | 6323 | { |
fa73b229 | 6324 | ra = create_requested_array(con->request); |
ef416fc2 | 6325 | |
fa73b229 | 6326 | copy_printer_attrs(con, DefaultPrinter, ra); |
ef416fc2 | 6327 | |
fa73b229 | 6328 | cupsArrayDelete(ra); |
ef416fc2 | 6329 | |
fa73b229 | 6330 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6331 | } |
6332 | else | |
6333 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer")); | |
6334 | } | |
6335 | ||
6336 | ||
6337 | /* | |
6338 | * 'get_devices()' - Get the list of available devices on the local system. | |
6339 | */ | |
6340 | ||
6341 | static void | |
6342 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
6343 | { | |
6344 | http_status_t status; /* Policy status */ | |
ae71f5de MS |
6345 | ipp_attribute_t *limit, /* limit attribute */ |
6346 | *timeout, /* timeout attribute */ | |
6347 | *requested, /* requested-attributes attribute */ | |
6348 | *exclude; /* exclude-schemes attribute */ | |
ef416fc2 | 6349 | char command[1024], /* cups-deviced command */ |
6350 | options[1024], /* Options to pass to command */ | |
ae71f5de | 6351 | requested_str[256], |
89d46774 | 6352 | /* String for requested attributes */ |
ae71f5de MS |
6353 | exclude_str[512]; |
6354 | /* String for excluded attributes */ | |
ef416fc2 | 6355 | |
6356 | ||
6357 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd); | |
6358 | ||
6359 | /* | |
6360 | * Check policy... | |
6361 | */ | |
6362 | ||
6363 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6364 | { | |
f899b121 | 6365 | send_http_error(con, status, NULL); |
ef416fc2 | 6366 | return; |
6367 | } | |
6368 | ||
6369 | /* | |
6370 | * Run cups-deviced command with the given options... | |
6371 | */ | |
6372 | ||
ae71f5de MS |
6373 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
6374 | timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER); | |
ef416fc2 | 6375 | requested = ippFindAttribute(con->request, "requested-attributes", |
6376 | IPP_TAG_KEYWORD); | |
ae71f5de | 6377 | exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME); |
ef416fc2 | 6378 | |
6379 | if (requested) | |
89d46774 | 6380 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
ef416fc2 | 6381 | else |
89d46774 | 6382 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); |
ef416fc2 | 6383 | |
ae71f5de MS |
6384 | if (exclude) |
6385 | url_encode_attr(exclude, exclude_str, sizeof(exclude_str)); | |
6386 | else | |
6387 | exclude_str[0] = '\0'; | |
6388 | ||
ef416fc2 | 6389 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); |
6390 | snprintf(options, sizeof(options), | |
ae71f5de | 6391 | "%d+%d+%d+%d+%s%s%s", |
ef416fc2 | 6392 | con->request->request.op.request_id, |
ae71f5de MS |
6393 | limit ? limit->values[0].integer : 0, |
6394 | timeout ? timeout->values[0].integer : 10, | |
6395 | (int)User, | |
6396 | requested_str, | |
6397 | exclude_str[0] ? "%20" : "", exclude_str); | |
ef416fc2 | 6398 | |
6399 | if (cupsdSendCommand(con, command, options, 1)) | |
6400 | { | |
6401 | /* | |
6402 | * Command started successfully, don't send an IPP response here... | |
6403 | */ | |
6404 | ||
6405 | ippDelete(con->response); | |
6406 | con->response = NULL; | |
6407 | } | |
6408 | else | |
6409 | { | |
6410 | /* | |
6411 | * Command failed, return "internal error" so the user knows something | |
6412 | * went wrong... | |
6413 | */ | |
6414 | ||
6415 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
6416 | _("cups-deviced failed to execute.")); | |
6417 | } | |
6418 | } | |
6419 | ||
6420 | ||
2e4ff8af MS |
6421 | /* |
6422 | * 'get_document()' - Get a copy of a job file. | |
6423 | */ | |
6424 | ||
6425 | static void | |
6426 | get_document(cupsd_client_t *con, /* I - Client connection */ | |
6427 | ipp_attribute_t *uri) /* I - Job URI */ | |
6428 | { | |
6429 | http_status_t status; /* Policy status */ | |
6430 | ipp_attribute_t *attr; /* Current attribute */ | |
6431 | int jobid; /* Job ID */ | |
6432 | int docnum; /* Document number */ | |
6433 | cupsd_job_t *job; /* Current job */ | |
6434 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6435 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6436 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6437 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6438 | int port; /* Port portion of URI */ | |
6439 | char filename[1024], /* Filename for document */ | |
6440 | format[1024]; /* Format for document */ | |
6441 | ||
6442 | ||
6443 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con, | |
6444 | con->http.fd, uri->values[0].string.text); | |
6445 | ||
6446 | /* | |
6447 | * See if we have a job URI or a printer URI... | |
6448 | */ | |
6449 | ||
6450 | if (!strcmp(uri->name, "printer-uri")) | |
6451 | { | |
6452 | /* | |
6453 | * Got a printer URI; see if we also have a job-id attribute... | |
6454 | */ | |
6455 | ||
6456 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6457 | IPP_TAG_INTEGER)) == NULL) | |
6458 | { | |
6459 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6460 | _("Got a printer-uri attribute but no job-id!")); | |
6461 | return; | |
6462 | } | |
6463 | ||
6464 | jobid = attr->values[0].integer; | |
6465 | } | |
6466 | else | |
6467 | { | |
6468 | /* | |
6469 | * Got a job URI; parse it to get the job ID... | |
6470 | */ | |
6471 | ||
6472 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
6473 | sizeof(method), username, sizeof(username), host, | |
6474 | sizeof(host), &port, resource, sizeof(resource)); | |
6475 | ||
6476 | if (strncmp(resource, "/jobs/", 6)) | |
6477 | { | |
6478 | /* | |
6479 | * Not a valid URI! | |
6480 | */ | |
6481 | ||
6482 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6483 | _("Bad job-uri attribute \"%s\"!"), | |
6484 | uri->values[0].string.text); | |
6485 | return; | |
6486 | } | |
6487 | ||
6488 | jobid = atoi(resource + 6); | |
6489 | } | |
6490 | ||
6491 | /* | |
6492 | * See if the job exists... | |
6493 | */ | |
6494 | ||
6495 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6496 | { | |
6497 | /* | |
6498 | * Nope - return a "not found" error... | |
6499 | */ | |
6500 | ||
6501 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6502 | return; | |
6503 | } | |
6504 | ||
6505 | /* | |
6506 | * Check policy... | |
6507 | */ | |
6508 | ||
6509 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6510 | { | |
6511 | send_http_error(con, status, NULL); | |
6512 | return; | |
6513 | } | |
6514 | ||
6515 | /* | |
6516 | * Get the document number... | |
6517 | */ | |
6518 | ||
6519 | if ((attr = ippFindAttribute(con->request, "document-number", | |
6520 | IPP_TAG_INTEGER)) == NULL) | |
6521 | { | |
6522 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6523 | _("Missing document-number attribute!")); | |
6524 | return; | |
6525 | } | |
6526 | ||
6527 | if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files || | |
6528 | attr->num_values > 1) | |
6529 | { | |
6530 | send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."), | |
6531 | docnum, jobid); | |
6532 | return; | |
6533 | } | |
6534 | ||
6535 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid, | |
6536 | docnum); | |
6537 | if ((con->file = open(filename, O_RDONLY)) == -1) | |
6538 | { | |
6539 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6540 | "Unable to open document %d in job %d - %s", docnum, jobid, | |
6541 | strerror(errno)); | |
6542 | send_ipp_status(con, IPP_NOT_FOUND, | |
6543 | _("Unable to open document %d in job %d!"), docnum, jobid); | |
6544 | return; | |
6545 | } | |
6546 | ||
6547 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
6548 | ||
6549 | cupsdLoadJob(job); | |
6550 | ||
6551 | snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super, | |
6552 | job->filetypes[docnum - 1]->type); | |
6553 | ||
6554 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
6555 | NULL, format); | |
6556 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number", | |
6557 | docnum); | |
6558 | if ((attr = ippFindAttribute(job->attrs, "document-name", | |
6559 | IPP_TAG_NAME)) != NULL) | |
6560 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name", | |
6561 | NULL, attr->values[0].string.text); | |
6562 | } | |
6563 | ||
6564 | ||
ef416fc2 | 6565 | /* |
fa73b229 | 6566 | * 'get_job_attrs()' - Get job attributes. |
ef416fc2 | 6567 | */ |
6568 | ||
6569 | static void | |
fa73b229 | 6570 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ |
6571 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 6572 | { |
6573 | http_status_t status; /* Policy status */ | |
fa73b229 | 6574 | ipp_attribute_t *attr; /* Current attribute */ |
6575 | int jobid; /* Job ID */ | |
6576 | cupsd_job_t *job; /* Current job */ | |
ef416fc2 | 6577 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
6578 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6579 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6580 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6581 | int port; /* Port portion of URI */ | |
fa73b229 | 6582 | cups_array_t *ra; /* Requested attributes array */ |
ef416fc2 | 6583 | |
6584 | ||
fa73b229 | 6585 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, |
6586 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 6587 | |
6588 | /* | |
fa73b229 | 6589 | * See if we have a job URI or a printer URI... |
6590 | */ | |
6591 | ||
6592 | if (!strcmp(uri->name, "printer-uri")) | |
6593 | { | |
6594 | /* | |
6595 | * Got a printer URI; see if we also have a job-id attribute... | |
6596 | */ | |
6597 | ||
6598 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6599 | IPP_TAG_INTEGER)) == NULL) | |
6600 | { | |
6601 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6602 | _("Got a printer-uri attribute but no job-id!")); | |
6603 | return; | |
6604 | } | |
6605 | ||
6606 | jobid = attr->values[0].integer; | |
6607 | } | |
6608 | else | |
6609 | { | |
6610 | /* | |
6611 | * Got a job URI; parse it to get the job ID... | |
6612 | */ | |
6613 | ||
a4d04587 | 6614 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6615 | sizeof(method), username, sizeof(username), host, | |
6616 | sizeof(host), &port, resource, sizeof(resource)); | |
fa73b229 | 6617 | |
6618 | if (strncmp(resource, "/jobs/", 6)) | |
6619 | { | |
6620 | /* | |
6621 | * Not a valid URI! | |
6622 | */ | |
6623 | ||
6624 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6625 | _("Bad job-uri attribute \"%s\"!"), | |
6626 | uri->values[0].string.text); | |
6627 | return; | |
6628 | } | |
6629 | ||
6630 | jobid = atoi(resource + 6); | |
6631 | } | |
6632 | ||
6633 | /* | |
6634 | * See if the job exists... | |
6635 | */ | |
6636 | ||
6637 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6638 | { | |
6639 | /* | |
6640 | * Nope - return a "not found" error... | |
6641 | */ | |
6642 | ||
6643 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6644 | return; | |
6645 | } | |
6646 | ||
6647 | /* | |
6648 | * Check policy... | |
6649 | */ | |
6650 | ||
6651 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6652 | { | |
f899b121 | 6653 | send_http_error(con, status, NULL); |
fa73b229 | 6654 | return; |
6655 | } | |
6656 | ||
6657 | /* | |
6658 | * Copy attributes... | |
6659 | */ | |
6660 | ||
bd7854cb | 6661 | cupsdLoadJob(job); |
6662 | ||
fa73b229 | 6663 | ra = create_requested_array(con->request); |
6664 | copy_job_attrs(con, job, ra); | |
6665 | cupsArrayDelete(ra); | |
6666 | ||
6667 | con->response->request.status.status_code = IPP_OK; | |
6668 | } | |
6669 | ||
6670 | ||
6671 | /* | |
6672 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
6673 | */ | |
6674 | ||
6675 | static void | |
6676 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
6677 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6678 | { | |
6679 | http_status_t status; /* Policy status */ | |
6680 | ipp_attribute_t *attr; /* Current attribute */ | |
6681 | const char *dest; /* Destination */ | |
bc44d920 | 6682 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
fa73b229 | 6683 | cups_ptype_t dmask; /* Destination type mask */ |
f7deaa1a | 6684 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
fa73b229 | 6685 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6686 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6687 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6688 | int port; /* Port portion of URI */ | |
6689 | int completed; /* Completed jobs? */ | |
6690 | int first_job_id; /* First job ID */ | |
6691 | int limit; /* Maximum number of jobs to return */ | |
6692 | int count; /* Number of jobs that match */ | |
6693 | cupsd_job_t *job; /* Current job pointer */ | |
6694 | cupsd_printer_t *printer; /* Printer */ | |
6695 | cups_array_t *list; /* Which job list... */ | |
6696 | cups_array_t *ra; /* Requested attributes array */ | |
6697 | ||
6698 | ||
6699 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd, | |
6700 | uri->values[0].string.text); | |
6701 | ||
6702 | /* | |
6703 | * Is the destination valid? | |
ef416fc2 | 6704 | */ |
6705 | ||
f7deaa1a | 6706 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
6707 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 6708 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 6709 | |
fa73b229 | 6710 | if (!strcmp(resource, "/") || |
6711 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6)) | |
ef416fc2 | 6712 | { |
6713 | dest = NULL; | |
6714 | dtype = (cups_ptype_t)0; | |
6715 | dmask = (cups_ptype_t)0; | |
6716 | printer = NULL; | |
6717 | } | |
fa73b229 | 6718 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) |
ef416fc2 | 6719 | { |
6720 | dest = NULL; | |
6721 | dtype = (cups_ptype_t)0; | |
6722 | dmask = CUPS_PRINTER_CLASS; | |
6723 | printer = NULL; | |
6724 | } | |
fa73b229 | 6725 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) |
ef416fc2 | 6726 | { |
6727 | dest = NULL; | |
6728 | dtype = CUPS_PRINTER_CLASS; | |
6729 | dmask = CUPS_PRINTER_CLASS; | |
6730 | printer = NULL; | |
6731 | } | |
f7deaa1a | 6732 | else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype, |
6733 | &printer)) == NULL) | |
ef416fc2 | 6734 | { |
6735 | /* | |
6736 | * Bad URI... | |
6737 | */ | |
6738 | ||
6739 | send_ipp_status(con, IPP_NOT_FOUND, | |
6740 | _("The printer or class was not found.")); | |
6741 | return; | |
6742 | } | |
6743 | else | |
b86bc4cf | 6744 | { |
6745 | dtype &= CUPS_PRINTER_CLASS; | |
ef416fc2 | 6746 | dmask = CUPS_PRINTER_CLASS; |
b86bc4cf | 6747 | } |
ef416fc2 | 6748 | |
6749 | /* | |
6750 | * Check policy... | |
6751 | */ | |
6752 | ||
6753 | if (printer) | |
6754 | { | |
f7deaa1a | 6755 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
6756 | NULL)) != HTTP_OK) | |
ef416fc2 | 6757 | { |
f899b121 | 6758 | send_http_error(con, status, printer); |
ef416fc2 | 6759 | return; |
6760 | } | |
6761 | } | |
6762 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6763 | { | |
f899b121 | 6764 | send_http_error(con, status, NULL); |
ef416fc2 | 6765 | return; |
6766 | } | |
6767 | ||
6768 | /* | |
6769 | * See if the "which-jobs" attribute have been specified... | |
6770 | */ | |
6771 | ||
fa73b229 | 6772 | if ((attr = ippFindAttribute(con->request, "which-jobs", |
6773 | IPP_TAG_KEYWORD)) != NULL && | |
ef416fc2 | 6774 | !strcmp(attr->values[0].string.text, "completed")) |
6775 | { | |
6776 | completed = 1; | |
6777 | list = Jobs; | |
6778 | } | |
6779 | else if (attr && !strcmp(attr->values[0].string.text, "all")) | |
6780 | { | |
6781 | completed = 0; | |
6782 | list = Jobs; | |
6783 | } | |
3dfe78b3 MS |
6784 | else if (attr && !strcmp(attr->values[0].string.text, "printing")) |
6785 | { | |
6786 | completed = 0; | |
6787 | list = PrintingJobs; | |
6788 | } | |
ef416fc2 | 6789 | else |
6790 | { | |
6791 | completed = 0; | |
6792 | list = ActiveJobs; | |
6793 | } | |
6794 | ||
6795 | /* | |
6796 | * See if they want to limit the number of jobs reported... | |
6797 | */ | |
6798 | ||
fa73b229 | 6799 | if ((attr = ippFindAttribute(con->request, "limit", |
6800 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 6801 | limit = attr->values[0].integer; |
6802 | else | |
3dfe78b3 | 6803 | limit = 0; |
ef416fc2 | 6804 | |
6805 | if ((attr = ippFindAttribute(con->request, "first-job-id", | |
6806 | IPP_TAG_INTEGER)) != NULL) | |
6807 | first_job_id = attr->values[0].integer; | |
6808 | else | |
6809 | first_job_id = 1; | |
6810 | ||
6811 | /* | |
6812 | * See if we only want to see jobs for a specific user... | |
6813 | */ | |
6814 | ||
fa73b229 | 6815 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
6816 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 6817 | attr->values[0].boolean) |
e00b005a | 6818 | strlcpy(username, get_username(con), sizeof(username)); |
ef416fc2 | 6819 | else |
6820 | username[0] = '\0'; | |
6821 | ||
fa73b229 | 6822 | ra = create_requested_array(con->request); |
ef416fc2 | 6823 | |
6824 | /* | |
6825 | * OK, build a list of jobs for this printer... | |
6826 | */ | |
6827 | ||
6828 | for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list); | |
3dfe78b3 | 6829 | (limit <= 0 || count < limit) && job; |
ef416fc2 | 6830 | job = (cupsd_job_t *)cupsArrayNext(list)) |
6831 | { | |
6832 | /* | |
6833 | * Filter out jobs that don't match... | |
6834 | */ | |
6835 | ||
6836 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id); | |
6837 | ||
0a682745 MS |
6838 | if (!job->dest || !job->username) |
6839 | cupsdLoadJob(job); | |
6840 | ||
6841 | if (!job->dest || !job->username) | |
6842 | continue; | |
6843 | ||
fa73b229 | 6844 | if ((dest && strcmp(job->dest, dest)) && |
6845 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
ef416fc2 | 6846 | continue; |
6847 | if ((job->dtype & dmask) != dtype && | |
fa73b229 | 6848 | (!job->printer || (job->printer->type & dmask) != dtype)) |
ef416fc2 | 6849 | continue; |
bd7854cb | 6850 | if (completed && job->state_value <= IPP_JOB_STOPPED) |
ef416fc2 | 6851 | continue; |
6852 | ||
6853 | if (job->id < first_job_id) | |
6854 | continue; | |
6855 | ||
bd7854cb | 6856 | cupsdLoadJob(job); |
6857 | ||
6858 | if (!job->attrs) | |
6859 | continue; | |
6860 | ||
7594b224 | 6861 | if (username[0] && strcasecmp(username, job->username)) |
6862 | continue; | |
6863 | ||
fa73b229 | 6864 | if (count > 0) |
6865 | ippAddSeparator(con->response); | |
6866 | ||
ef416fc2 | 6867 | count ++; |
6868 | ||
6869 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count); | |
6870 | ||
fa73b229 | 6871 | copy_job_attrs(con, job, ra); |
ef416fc2 | 6872 | } |
6873 | ||
fa73b229 | 6874 | cupsArrayDelete(ra); |
ef416fc2 | 6875 | |
fa73b229 | 6876 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6877 | } |
6878 | ||
6879 | ||
6880 | /* | |
6881 | * 'get_notifications()' - Get events for a subscription. | |
6882 | */ | |
6883 | ||
6884 | static void | |
bd7854cb | 6885 | get_notifications(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 6886 | { |
bd7854cb | 6887 | int i, j; /* Looping vars */ |
6888 | http_status_t status; /* Policy status */ | |
6889 | cupsd_subscription_t *sub; /* Subscription */ | |
6890 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
6891 | *sequences; /* notify-sequence-numbers */ | |
6892 | int min_seq; /* Minimum sequence number */ | |
6893 | int interval; /* Poll interval */ | |
6894 | ||
6895 | ||
f7deaa1a | 6896 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])", |
bd7854cb | 6897 | con, con->http.fd); |
6898 | ||
6899 | /* | |
6900 | * Get subscription attributes... | |
6901 | */ | |
6902 | ||
6903 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
6904 | IPP_TAG_INTEGER); | |
6905 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
6906 | IPP_TAG_INTEGER); | |
6907 | ||
6908 | if (!ids) | |
6909 | { | |
6910 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6911 | _("Missing notify-subscription-ids attribute!")); | |
6912 | return; | |
6913 | } | |
6914 | ||
6915 | /* | |
6916 | * Are the subscription IDs valid? | |
6917 | */ | |
6918 | ||
6919 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
6920 | { | |
6921 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
6922 | { | |
6923 | /* | |
6924 | * Bad subscription ID... | |
6925 | */ | |
6926 | ||
6927 | send_ipp_status(con, IPP_NOT_FOUND, | |
6928 | _("notify-subscription-id %d no good!"), | |
6929 | ids->values[i].integer); | |
6930 | return; | |
6931 | } | |
6932 | ||
6933 | /* | |
6934 | * Check policy... | |
6935 | */ | |
6936 | ||
6937 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
6938 | DefaultPolicyPtr, | |
6939 | con, sub->owner)) != HTTP_OK) | |
6940 | { | |
f899b121 | 6941 | send_http_error(con, status, sub->dest); |
bd7854cb | 6942 | return; |
6943 | } | |
6944 | ||
6945 | /* | |
6946 | * Check the subscription type and update the interval accordingly. | |
6947 | */ | |
6948 | ||
6949 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
6950 | interval > 10) | |
6951 | interval = 10; | |
6952 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
6953 | interval = 0; | |
6954 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
6955 | interval > 30) | |
6956 | interval = 30; | |
6957 | } | |
6958 | ||
6959 | /* | |
6960 | * Tell the client to poll again in N seconds... | |
6961 | */ | |
6962 | ||
6963 | if (interval > 0) | |
6964 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6965 | "notify-get-interval", interval); | |
6966 | ||
6967 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6968 | "printer-up-time", time(NULL)); | |
6969 | ||
6970 | /* | |
6971 | * Copy the subscription event attributes to the response. | |
6972 | */ | |
6973 | ||
6974 | con->response->request.status.status_code = | |
6975 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
6976 | ||
6977 | for (i = 0; i < ids->num_values; i ++) | |
6978 | { | |
6979 | /* | |
6980 | * Get the subscription and sequence number... | |
6981 | */ | |
6982 | ||
6983 | sub = cupsdFindSubscription(ids->values[i].integer); | |
6984 | ||
6985 | if (sequences && i < sequences->num_values) | |
6986 | min_seq = sequences->values[i].integer; | |
6987 | else | |
6988 | min_seq = 1; | |
6989 | ||
6990 | /* | |
6991 | * If we don't have any new events, nothing to do here... | |
6992 | */ | |
6993 | ||
6994 | if (min_seq > (sub->first_event_id + sub->num_events)) | |
6995 | continue; | |
6996 | ||
6997 | /* | |
6998 | * Otherwise copy all of the new events... | |
6999 | */ | |
7000 | ||
7001 | if (sub->first_event_id > min_seq) | |
7002 | j = 0; | |
7003 | else | |
7004 | j = min_seq - sub->first_event_id; | |
7005 | ||
b423cd4c | 7006 | for (; j < sub->num_events; j ++) |
7007 | { | |
7008 | ippAddSeparator(con->response); | |
ef416fc2 | 7009 | |
b423cd4c | 7010 | copy_attrs(con->response, sub->events[j]->attrs, NULL, |
7011 | IPP_TAG_EVENT_NOTIFICATION, 0); | |
7012 | } | |
7013 | } | |
7014 | } | |
ef416fc2 | 7015 | |
ef416fc2 | 7016 | |
b94498cf | 7017 | /* |
7018 | * 'get_ppd()' - Get a named PPD from the local system. | |
7019 | */ | |
7020 | ||
7021 | static void | |
7022 | get_ppd(cupsd_client_t *con, /* I - Client connection */ | |
7023 | ipp_attribute_t *uri) /* I - Printer URI or PPD name */ | |
7024 | { | |
7025 | http_status_t status; /* Policy status */ | |
7026 | cupsd_printer_t *dest; /* Destination */ | |
7027 | cups_ptype_t dtype; /* Destination type */ | |
7028 | ||
7029 | ||
7030 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con, | |
7031 | con->http.fd, uri, uri->name, uri->values[0].string.text); | |
7032 | ||
7033 | if (!strcmp(uri->name, "ppd-name")) | |
7034 | { | |
7035 | /* | |
7036 | * Return a PPD file from cups-driverd... | |
7037 | */ | |
7038 | ||
7039 | char command[1024], /* cups-driverd command */ | |
7040 | options[1024], /* Options to pass to command */ | |
7041 | ppd_name[1024]; /* ppd-name */ | |
7042 | ||
7043 | ||
7044 | /* | |
7045 | * Check policy... | |
7046 | */ | |
7047 | ||
7048 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7049 | { | |
7050 | send_http_error(con, status, NULL); | |
7051 | return; | |
7052 | } | |
7053 | ||
7054 | /* | |
7055 | * Run cups-driverd command with the given options... | |
7056 | */ | |
7057 | ||
7058 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); | |
7059 | url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name)); | |
7060 | snprintf(options, sizeof(options), "get+%d+%s", | |
7061 | con->request->request.op.request_id, ppd_name); | |
7062 | ||
7063 | if (cupsdSendCommand(con, command, options, 0)) | |
7064 | { | |
7065 | /* | |
7066 | * Command started successfully, don't send an IPP response here... | |
7067 | */ | |
7068 | ||
7069 | ippDelete(con->response); | |
7070 | con->response = NULL; | |
7071 | } | |
7072 | else | |
7073 | { | |
7074 | /* | |
7075 | * Command failed, return "internal error" so the user knows something | |
7076 | * went wrong... | |
7077 | */ | |
7078 | ||
7079 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7080 | _("cups-driverd failed to execute.")); | |
7081 | } | |
7082 | } | |
7083 | else if (!strcmp(uri->name, "printer-uri") && | |
7084 | cupsdValidateDest(uri->values[0].string.text, &dtype, &dest)) | |
7085 | { | |
7086 | int i; /* Looping var */ | |
7087 | char filename[1024]; /* PPD filename */ | |
7088 | ||
7089 | ||
7090 | /* | |
7091 | * Check policy... | |
7092 | */ | |
7093 | ||
7094 | if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7095 | { | |
2fb76298 | 7096 | send_http_error(con, status, dest); |
b94498cf | 7097 | return; |
7098 | } | |
7099 | ||
7100 | /* | |
7101 | * See if we need the PPD for a class or remote printer... | |
7102 | */ | |
7103 | ||
bc44d920 | 7104 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, |
7105 | dest->name); | |
7106 | ||
7107 | if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0)) | |
b94498cf | 7108 | { |
3d8365b8 | 7109 | con->response->request.status.status_code = CUPS_SEE_OTHER; |
b94498cf | 7110 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, |
7111 | "printer-uri", NULL, dest->uri); | |
7112 | return; | |
7113 | } | |
7114 | else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) | |
7115 | { | |
7116 | for (i = 0; i < dest->num_printers; i ++) | |
7117 | if (!(dest->printers[i]->type & | |
bc44d920 | 7118 | (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))) |
7119 | { | |
7120 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, | |
7121 | dest->printers[i]->name); | |
7122 | ||
7123 | if (!access(filename, 0)) | |
7124 | break; | |
7125 | } | |
b94498cf | 7126 | |
7127 | if (i < dest->num_printers) | |
7128 | dest = dest->printers[i]; | |
7129 | else | |
7130 | { | |
3d8365b8 | 7131 | con->response->request.status.status_code = CUPS_SEE_OTHER; |
b94498cf | 7132 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, |
7133 | "printer-uri", NULL, dest->printers[0]->uri); | |
7134 | return; | |
7135 | } | |
7136 | } | |
7137 | ||
7138 | /* | |
7139 | * Found the printer with the PPD file, now see if there is one... | |
7140 | */ | |
7141 | ||
b94498cf | 7142 | if ((con->file = open(filename, O_RDONLY)) < 0) |
7143 | { | |
7144 | send_ipp_status(con, IPP_NOT_FOUND, | |
7145 | _("The PPD file \"%s\" could not be opened: %s"), | |
3d8365b8 | 7146 | uri->values[0].string.text, strerror(errno)); |
b94498cf | 7147 | return; |
7148 | } | |
7149 | ||
7150 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
7151 | ||
7152 | con->pipe_pid = 0; | |
7153 | ||
3d8365b8 | 7154 | con->response->request.status.status_code = IPP_OK; |
b94498cf | 7155 | } |
7156 | else | |
7157 | send_ipp_status(con, IPP_NOT_FOUND, | |
7158 | _("The PPD file \"%s\" could not be found."), | |
7159 | uri->values[0].string.text); | |
7160 | } | |
7161 | ||
7162 | ||
b423cd4c | 7163 | /* |
7164 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
7165 | */ | |
ef416fc2 | 7166 | |
b423cd4c | 7167 | static void |
7168 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
7169 | { | |
7170 | http_status_t status; /* Policy status */ | |
b423cd4c | 7171 | ipp_attribute_t *limit, /* Limit attribute */ |
b94498cf | 7172 | *device, /* ppd-device-id attribute */ |
7173 | *language, /* ppd-natural-language attribute */ | |
b423cd4c | 7174 | *make, /* ppd-make attribute */ |
b94498cf | 7175 | *model, /* ppd-make-and-model attribute */ |
3d8365b8 | 7176 | *model_number, /* ppd-model-number attribute */ |
b94498cf | 7177 | *product, /* ppd-product attribute */ |
7178 | *psversion, /* ppd-psverion attribute */ | |
3d8365b8 | 7179 | *type, /* ppd-type attribute */ |
b423cd4c | 7180 | *requested; /* requested-attributes attribute */ |
b94498cf | 7181 | char command[1024], /* cups-driverd command */ |
b423cd4c | 7182 | options[1024], /* Options to pass to command */ |
b94498cf | 7183 | device_str[256],/* Escaped ppd-device-id string */ |
7184 | language_str[256], | |
bc44d920 | 7185 | /* Escaped ppd-natural-language */ |
b94498cf | 7186 | make_str[256], /* Escaped ppd-make string */ |
7187 | model_str[256], /* Escaped ppd-make-and-model string */ | |
3d8365b8 | 7188 | model_number_str[256], |
7189 | /* ppd-model-number string */ | |
b94498cf | 7190 | product_str[256], |
7191 | /* Escaped ppd-product string */ | |
7192 | psversion_str[256], | |
7193 | /* Escaped ppd-psversion string */ | |
3d8365b8 | 7194 | type_str[256], /* Escaped ppd-type string */ |
b94498cf | 7195 | requested_str[256]; |
89d46774 | 7196 | /* String for requested attributes */ |
ef416fc2 | 7197 | |
ef416fc2 | 7198 | |
b423cd4c | 7199 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd); |
ef416fc2 | 7200 | |
7201 | /* | |
b423cd4c | 7202 | * Check policy... |
ef416fc2 | 7203 | */ |
7204 | ||
b423cd4c | 7205 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7206 | { |
f899b121 | 7207 | send_http_error(con, status, NULL); |
b423cd4c | 7208 | return; |
ef416fc2 | 7209 | } |
ef416fc2 | 7210 | |
b423cd4c | 7211 | /* |
7212 | * Run cups-driverd command with the given options... | |
7213 | */ | |
7214 | ||
3d8365b8 | 7215 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
7216 | device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT); | |
7217 | language = ippFindAttribute(con->request, "ppd-natural-language", | |
7218 | IPP_TAG_LANGUAGE); | |
7219 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
7220 | model = ippFindAttribute(con->request, "ppd-make-and-model", | |
7221 | IPP_TAG_TEXT); | |
7222 | model_number = ippFindAttribute(con->request, "ppd-model-number", | |
7223 | IPP_TAG_INTEGER); | |
7224 | product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT); | |
7225 | psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT); | |
7226 | type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD); | |
7227 | requested = ippFindAttribute(con->request, "requested-attributes", | |
7228 | IPP_TAG_KEYWORD); | |
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 | ||
b423cd4c | 7276 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); |
3d8365b8 | 7277 | snprintf(options, sizeof(options), |
7278 | "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", | |
b423cd4c | 7279 | con->request->request.op.request_id, |
7280 | limit ? limit->values[0].integer : 0, | |
b94498cf | 7281 | requested_str, |
7282 | device ? "%20" : "", device_str, | |
7283 | language ? "%20" : "", language_str, | |
7284 | make ? "%20" : "", make_str, | |
7285 | model ? "%20" : "", model_str, | |
3d8365b8 | 7286 | model_number ? "%20" : "", model_number_str, |
b94498cf | 7287 | product ? "%20" : "", product_str, |
3d8365b8 | 7288 | psversion ? "%20" : "", psversion_str, |
7289 | type ? "%20" : "", type_str); | |
ef416fc2 | 7290 | |
b423cd4c | 7291 | if (cupsdSendCommand(con, command, options, 0)) |
7292 | { | |
7293 | /* | |
7294 | * Command started successfully, don't send an IPP response here... | |
7295 | */ | |
7296 | ||
7297 | ippDelete(con->response); | |
7298 | con->response = NULL; | |
7299 | } | |
7300 | else | |
7301 | { | |
7302 | /* | |
7303 | * Command failed, return "internal error" so the user knows something | |
7304 | * went wrong... | |
7305 | */ | |
7306 | ||
7307 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7308 | _("cups-driverd failed to execute.")); | |
7309 | } | |
ef416fc2 | 7310 | } |
7311 | ||
7312 | ||
7313 | /* | |
b423cd4c | 7314 | * 'get_printer_attrs()' - Get printer attributes. |
ef416fc2 | 7315 | */ |
7316 | ||
7317 | static void | |
b423cd4c | 7318 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ |
7319 | ipp_attribute_t *uri) /* I - Printer URI */ | |
ef416fc2 | 7320 | { |
7321 | http_status_t status; /* Policy status */ | |
bc44d920 | 7322 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 7323 | cupsd_printer_t *printer; /* Printer/class */ |
ef416fc2 | 7324 | cups_array_t *ra; /* Requested attributes array */ |
7325 | ||
7326 | ||
b423cd4c | 7327 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, |
7328 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 7329 | |
7330 | /* | |
b423cd4c | 7331 | * Is the destination valid? |
ef416fc2 | 7332 | */ |
7333 | ||
f7deaa1a | 7334 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 7335 | { |
7336 | /* | |
b423cd4c | 7337 | * Bad URI... |
ef416fc2 | 7338 | */ |
7339 | ||
7340 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 7341 | _("The printer or class was not found.")); |
ef416fc2 | 7342 | return; |
7343 | } | |
7344 | ||
7345 | /* | |
7346 | * Check policy... | |
7347 | */ | |
7348 | ||
b423cd4c | 7349 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7350 | { |
f899b121 | 7351 | send_http_error(con, status, printer); |
ef416fc2 | 7352 | return; |
7353 | } | |
7354 | ||
7355 | /* | |
b423cd4c | 7356 | * Send the attributes... |
ef416fc2 | 7357 | */ |
7358 | ||
7359 | ra = create_requested_array(con->request); | |
7360 | ||
b423cd4c | 7361 | copy_printer_attrs(con, printer, ra); |
ef416fc2 | 7362 | |
7363 | cupsArrayDelete(ra); | |
7364 | ||
7365 | con->response->request.status.status_code = IPP_OK; | |
7366 | } | |
7367 | ||
7368 | ||
7369 | /* | |
b423cd4c | 7370 | * 'get_printers()' - Get a list of printers or classes. |
ef416fc2 | 7371 | */ |
7372 | ||
7373 | static void | |
b423cd4c | 7374 | get_printers(cupsd_client_t *con, /* I - Client connection */ |
7375 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
ef416fc2 | 7376 | { |
b423cd4c | 7377 | http_status_t status; /* Policy status */ |
7378 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 7379 | int limit; /* Max number of printers to return */ |
b423cd4c | 7380 | int count; /* Number of printers that match */ |
7381 | cupsd_printer_t *printer; /* Current printer pointer */ | |
7382 | int printer_type, /* printer-type attribute */ | |
7383 | printer_mask; /* printer-type-mask attribute */ | |
7384 | char *location; /* Location string */ | |
7385 | const char *username; /* Current user */ | |
7386 | char *first_printer_name; /* first-printer-name attribute */ | |
7387 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 7388 | |
ef416fc2 | 7389 | |
b423cd4c | 7390 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, |
7391 | con->http.fd, type); | |
ef416fc2 | 7392 | |
7393 | /* | |
7394 | * Check policy... | |
7395 | */ | |
7396 | ||
b423cd4c | 7397 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7398 | { |
f899b121 | 7399 | send_http_error(con, status, NULL); |
ef416fc2 | 7400 | return; |
7401 | } | |
7402 | ||
7403 | /* | |
b423cd4c | 7404 | * Check for printers... |
ef416fc2 | 7405 | */ |
7406 | ||
b423cd4c | 7407 | if (!Printers || !cupsArrayCount(Printers)) |
7408 | { | |
7409 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
7410 | return; | |
7411 | } | |
7412 | ||
7413 | /* | |
7414 | * See if they want to limit the number of printers reported... | |
7415 | */ | |
ef416fc2 | 7416 | |
fa73b229 | 7417 | if ((attr = ippFindAttribute(con->request, "limit", |
7418 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 7419 | limit = attr->values[0].integer; |
7420 | else | |
b423cd4c | 7421 | limit = 10000000; |
7422 | ||
7423 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
7424 | IPP_TAG_NAME)) != NULL) | |
7425 | first_printer_name = attr->values[0].string.text; | |
7426 | else | |
7427 | first_printer_name = NULL; | |
ef416fc2 | 7428 | |
7429 | /* | |
b423cd4c | 7430 | * Support filtering... |
ef416fc2 | 7431 | */ |
7432 | ||
b423cd4c | 7433 | if ((attr = ippFindAttribute(con->request, "printer-type", |
7434 | IPP_TAG_ENUM)) != NULL) | |
7435 | printer_type = attr->values[0].integer; | |
ef416fc2 | 7436 | else |
b423cd4c | 7437 | printer_type = 0; |
ef416fc2 | 7438 | |
b423cd4c | 7439 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", |
7440 | IPP_TAG_ENUM)) != NULL) | |
7441 | printer_mask = attr->values[0].integer; | |
ef416fc2 | 7442 | else |
b423cd4c | 7443 | printer_mask = 0; |
e00b005a | 7444 | |
b423cd4c | 7445 | if ((attr = ippFindAttribute(con->request, "printer-location", |
7446 | IPP_TAG_TEXT)) != NULL) | |
7447 | location = attr->values[0].string.text; | |
7448 | else | |
7449 | location = NULL; | |
e00b005a | 7450 | |
7451 | if (con->username[0]) | |
b423cd4c | 7452 | username = con->username; |
e00b005a | 7453 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
7454 | IPP_TAG_NAME)) != NULL) | |
b423cd4c | 7455 | username = attr->values[0].string.text; |
e00b005a | 7456 | else |
b423cd4c | 7457 | username = NULL; |
ef416fc2 | 7458 | |
b423cd4c | 7459 | ra = create_requested_array(con->request); |
ef416fc2 | 7460 | |
7461 | /* | |
b423cd4c | 7462 | * OK, build a list of printers for this printer... |
ef416fc2 | 7463 | */ |
7464 | ||
b423cd4c | 7465 | if (first_printer_name) |
ef416fc2 | 7466 | { |
b423cd4c | 7467 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) |
7468 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 7469 | } |
7470 | else | |
b423cd4c | 7471 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
ef416fc2 | 7472 | |
b423cd4c | 7473 | for (count = 0; |
7474 | count < limit && printer; | |
7475 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
7476 | { | |
7477 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && | |
7478 | (printer->type & printer_mask) == printer_type && | |
7479 | (!location || !printer->location || | |
7480 | !strcasecmp(printer->location, location))) | |
ef416fc2 | 7481 | { |
7482 | /* | |
b423cd4c | 7483 | * If HideImplicitMembers is enabled, see if this printer or class |
7484 | * is a member of an implicit class... | |
ef416fc2 | 7485 | */ |
7486 | ||
b423cd4c | 7487 | if (ImplicitClasses && HideImplicitMembers && |
7488 | printer->in_implicit_class) | |
7489 | continue; | |
ef416fc2 | 7490 | |
b423cd4c | 7491 | /* |
7492 | * If a username is specified, see if it is allowed or denied | |
7493 | * access... | |
7494 | */ | |
ef416fc2 | 7495 | |
dd1abb6b MS |
7496 | if (!(printer->type & CUPS_PRINTER_AUTHENTICATED) && |
7497 | printer->num_users && username && !user_allowed(printer, username)) | |
b423cd4c | 7498 | continue; |
ef416fc2 | 7499 | |
b423cd4c | 7500 | /* |
7501 | * Add the group separator as needed... | |
7502 | */ | |
ef416fc2 | 7503 | |
b423cd4c | 7504 | if (count > 0) |
7505 | ippAddSeparator(con->response); | |
ef416fc2 | 7506 | |
b423cd4c | 7507 | count ++; |
ef416fc2 | 7508 | |
b423cd4c | 7509 | /* |
7510 | * Send the attributes... | |
7511 | */ | |
ef416fc2 | 7512 | |
b423cd4c | 7513 | copy_printer_attrs(con, printer, ra); |
7514 | } | |
ef416fc2 | 7515 | } |
7516 | ||
b423cd4c | 7517 | cupsArrayDelete(ra); |
ef416fc2 | 7518 | |
7519 | con->response->request.status.status_code = IPP_OK; | |
7520 | } | |
7521 | ||
7522 | ||
7523 | /* | |
b423cd4c | 7524 | * 'get_subscription_attrs()' - Get subscription attributes. |
ef416fc2 | 7525 | */ |
7526 | ||
7527 | static void | |
b423cd4c | 7528 | get_subscription_attrs( |
7529 | cupsd_client_t *con, /* I - Client connection */ | |
7530 | int sub_id) /* I - Subscription ID */ | |
ef416fc2 | 7531 | { |
b423cd4c | 7532 | http_status_t status; /* Policy status */ |
7533 | cupsd_subscription_t *sub; /* Subscription */ | |
7534 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 7535 | |
7536 | ||
b423cd4c | 7537 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7538 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
7539 | con, con->http.fd, sub_id); | |
ef416fc2 | 7540 | |
fa73b229 | 7541 | /* |
b423cd4c | 7542 | * Is the subscription ID valid? |
fa73b229 | 7543 | */ |
7544 | ||
b423cd4c | 7545 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
fa73b229 | 7546 | { |
7547 | /* | |
b423cd4c | 7548 | * Bad subscription ID... |
fa73b229 | 7549 | */ |
7550 | ||
7551 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 7552 | _("notify-subscription-id %d no good!"), sub_id); |
fa73b229 | 7553 | return; |
7554 | } | |
7555 | ||
7556 | /* | |
7557 | * Check policy... | |
7558 | */ | |
7559 | ||
b423cd4c | 7560 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
7561 | DefaultPolicyPtr, | |
7562 | con, sub->owner)) != HTTP_OK) | |
fa73b229 | 7563 | { |
f899b121 | 7564 | send_http_error(con, status, sub->dest); |
fa73b229 | 7565 | return; |
7566 | } | |
7567 | ||
ef416fc2 | 7568 | /* |
b423cd4c | 7569 | * Copy the subscription attributes to the response using the |
7570 | * requested-attributes attribute that may be provided by the client. | |
ef416fc2 | 7571 | */ |
7572 | ||
b423cd4c | 7573 | ra = create_requested_array(con->request); |
fa73b229 | 7574 | |
b423cd4c | 7575 | copy_subscription_attrs(con, sub, ra); |
ef416fc2 | 7576 | |
b423cd4c | 7577 | cupsArrayDelete(ra); |
fa73b229 | 7578 | |
b423cd4c | 7579 | con->response->request.status.status_code = IPP_OK; |
7580 | } | |
fa73b229 | 7581 | |
fa73b229 | 7582 | |
b423cd4c | 7583 | /* |
7584 | * 'get_subscriptions()' - Get subscriptions. | |
7585 | */ | |
ef416fc2 | 7586 | |
b423cd4c | 7587 | static void |
7588 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
7589 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
7590 | { | |
7591 | http_status_t status; /* Policy status */ | |
7592 | int count; /* Number of subscriptions */ | |
7593 | int limit; /* Limit */ | |
7594 | cupsd_subscription_t *sub; /* Subscription */ | |
7595 | cups_array_t *ra; /* Requested attributes array */ | |
7596 | ipp_attribute_t *attr; /* Attribute */ | |
bc44d920 | 7597 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
f7deaa1a | 7598 | char scheme[HTTP_MAX_URI], |
7599 | /* Scheme portion of URI */ | |
b423cd4c | 7600 | username[HTTP_MAX_URI], |
7601 | /* Username portion of URI */ | |
7602 | host[HTTP_MAX_URI], | |
7603 | /* Host portion of URI */ | |
7604 | resource[HTTP_MAX_URI]; | |
7605 | /* Resource portion of URI */ | |
7606 | int port; /* Port portion of URI */ | |
7607 | cupsd_job_t *job; /* Job pointer */ | |
7608 | cupsd_printer_t *printer; /* Printer */ | |
fa73b229 | 7609 | |
fa73b229 | 7610 | |
b423cd4c | 7611 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7612 | "get_subscriptions(con=%p[%d], uri=%s)", | |
7613 | con, con->http.fd, uri->values[0].string.text); | |
7614 | ||
7615 | /* | |
7616 | * Is the destination valid? | |
7617 | */ | |
7618 | ||
f7deaa1a | 7619 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7620 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7621 | sizeof(host), &port, resource, sizeof(resource)); |
7622 | ||
7623 | if (!strcmp(resource, "/") || | |
7624 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
7625 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
7626 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
7627 | { | |
7628 | printer = NULL; | |
7629 | job = NULL; | |
ef416fc2 | 7630 | } |
b423cd4c | 7631 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) |
ef416fc2 | 7632 | { |
b423cd4c | 7633 | printer = NULL; |
7634 | job = cupsdFindJob(atoi(resource + 6)); | |
ef416fc2 | 7635 | |
b423cd4c | 7636 | if (!job) |
ef416fc2 | 7637 | { |
b423cd4c | 7638 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"), |
7639 | resource + 6); | |
ef416fc2 | 7640 | return; |
7641 | } | |
b423cd4c | 7642 | } |
f7deaa1a | 7643 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
b423cd4c | 7644 | { |
fa73b229 | 7645 | /* |
b423cd4c | 7646 | * Bad URI... |
fa73b229 | 7647 | */ |
7648 | ||
b423cd4c | 7649 | send_ipp_status(con, IPP_NOT_FOUND, |
7650 | _("The printer or class was not found.")); | |
7651 | return; | |
7652 | } | |
7653 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
7654 | IPP_TAG_INTEGER)) != NULL) | |
7655 | { | |
7656 | job = cupsdFindJob(attr->values[0].integer); | |
ef416fc2 | 7657 | |
b423cd4c | 7658 | if (!job) |
fa73b229 | 7659 | { |
b423cd4c | 7660 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), |
7661 | attr->values[0].integer); | |
fa73b229 | 7662 | return; |
7663 | } | |
ef416fc2 | 7664 | } |
b423cd4c | 7665 | else |
7666 | job = NULL; | |
ef416fc2 | 7667 | |
7668 | /* | |
b423cd4c | 7669 | * Check policy... |
ef416fc2 | 7670 | */ |
7671 | ||
b423cd4c | 7672 | if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr : |
7673 | DefaultPolicyPtr, | |
7674 | con, NULL)) != HTTP_OK) | |
ef416fc2 | 7675 | { |
f899b121 | 7676 | send_http_error(con, status, printer); |
b423cd4c | 7677 | return; |
7678 | } | |
ef416fc2 | 7679 | |
b423cd4c | 7680 | /* |
7681 | * Copy the subscription attributes to the response using the | |
7682 | * requested-attributes attribute that may be provided by the client. | |
7683 | */ | |
ef416fc2 | 7684 | |
b423cd4c | 7685 | ra = create_requested_array(con->request); |
ef416fc2 | 7686 | |
b423cd4c | 7687 | if ((attr = ippFindAttribute(con->request, "limit", |
7688 | IPP_TAG_INTEGER)) != NULL) | |
7689 | limit = attr->values[0].integer; | |
7690 | else | |
7691 | limit = 0; | |
ef416fc2 | 7692 | |
b423cd4c | 7693 | /* |
7694 | * See if we only want to see subscriptions for a specific user... | |
7695 | */ | |
ef416fc2 | 7696 | |
b423cd4c | 7697 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", |
7698 | IPP_TAG_BOOLEAN)) != NULL && | |
7699 | attr->values[0].boolean) | |
7700 | strlcpy(username, get_username(con), sizeof(username)); | |
fa73b229 | 7701 | else |
b423cd4c | 7702 | username[0] = '\0'; |
ef416fc2 | 7703 | |
b423cd4c | 7704 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; |
7705 | sub; | |
7706 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
7707 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
7708 | (!username[0] || !strcasecmp(username, sub->owner))) | |
fa73b229 | 7709 | { |
b423cd4c | 7710 | ippAddSeparator(con->response); |
7711 | copy_subscription_attrs(con, sub, ra); | |
ef416fc2 | 7712 | |
b423cd4c | 7713 | count ++; |
7714 | if (limit && count >= limit) | |
7715 | break; | |
7716 | } | |
ef416fc2 | 7717 | |
b423cd4c | 7718 | cupsArrayDelete(ra); |
fa73b229 | 7719 | |
b423cd4c | 7720 | if (count) |
7721 | con->response->request.status.status_code = IPP_OK; | |
7722 | else | |
7723 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
7724 | } | |
ef416fc2 | 7725 | |
ef416fc2 | 7726 | |
b423cd4c | 7727 | /* |
7728 | * 'get_username()' - Get the username associated with a request. | |
7729 | */ | |
ef416fc2 | 7730 | |
b423cd4c | 7731 | static const char * /* O - Username */ |
7732 | get_username(cupsd_client_t *con) /* I - Connection */ | |
7733 | { | |
7734 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 7735 | |
ef416fc2 | 7736 | |
b423cd4c | 7737 | if (con->username[0]) |
7738 | return (con->username); | |
7739 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
7740 | IPP_TAG_NAME)) != NULL) | |
7741 | return (attr->values[0].string.text); | |
7742 | else | |
7743 | return ("anonymous"); | |
ef416fc2 | 7744 | } |
7745 | ||
7746 | ||
7747 | /* | |
b423cd4c | 7748 | * 'hold_job()' - Hold a print job. |
ef416fc2 | 7749 | */ |
7750 | ||
b423cd4c | 7751 | static void |
7752 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
7753 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 7754 | { |
b423cd4c | 7755 | ipp_attribute_t *attr, /* Current job-hold-until */ |
7756 | *newattr; /* New job-hold-until */ | |
7757 | int jobid; /* Job ID */ | |
7758 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7759 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7760 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7761 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7762 | int port; /* Port portion of URI */ | |
7763 | cupsd_job_t *job; /* Job information */ | |
7764 | ||
ef416fc2 | 7765 | |
b423cd4c | 7766 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd, |
7767 | uri->values[0].string.text); | |
ef416fc2 | 7768 | |
7769 | /* | |
b423cd4c | 7770 | * See if we have a job URI or a printer URI... |
ef416fc2 | 7771 | */ |
7772 | ||
b423cd4c | 7773 | if (!strcmp(uri->name, "printer-uri")) |
7774 | { | |
7775 | /* | |
7776 | * Got a printer URI; see if we also have a job-id attribute... | |
7777 | */ | |
ef416fc2 | 7778 | |
b423cd4c | 7779 | if ((attr = ippFindAttribute(con->request, "job-id", |
7780 | IPP_TAG_INTEGER)) == NULL) | |
7781 | { | |
7782 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7783 | _("Got a printer-uri attribute but no job-id!")); | |
7784 | return; | |
7785 | } | |
ef416fc2 | 7786 | |
b423cd4c | 7787 | jobid = attr->values[0].integer; |
7788 | } | |
ef416fc2 | 7789 | else |
ef416fc2 | 7790 | { |
b423cd4c | 7791 | /* |
7792 | * Got a job URI; parse it to get the job ID... | |
7793 | */ | |
ef416fc2 | 7794 | |
b423cd4c | 7795 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7796 | sizeof(method), username, sizeof(username), host, | |
7797 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7798 | |
b423cd4c | 7799 | if (strncmp(resource, "/jobs/", 6)) |
7800 | { | |
7801 | /* | |
7802 | * Not a valid URI! | |
7803 | */ | |
ef416fc2 | 7804 | |
b423cd4c | 7805 | send_ipp_status(con, IPP_BAD_REQUEST, |
7806 | _("Bad job-uri attribute \"%s\"!"), | |
7807 | uri->values[0].string.text); | |
7808 | return; | |
7809 | } | |
ef416fc2 | 7810 | |
b423cd4c | 7811 | jobid = atoi(resource + 6); |
7812 | } | |
ef416fc2 | 7813 | |
ef416fc2 | 7814 | /* |
b423cd4c | 7815 | * See if the job exists... |
ef416fc2 | 7816 | */ |
7817 | ||
b423cd4c | 7818 | if ((job = cupsdFindJob(jobid)) == NULL) |
7819 | { | |
7820 | /* | |
7821 | * Nope - return a "not found" error... | |
7822 | */ | |
7823 | ||
7824 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7825 | return; | |
7826 | } | |
ef416fc2 | 7827 | |
7828 | /* | |
b423cd4c | 7829 | * See if the job is owned by the requesting user... |
ef416fc2 | 7830 | */ |
7831 | ||
b423cd4c | 7832 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
7833 | { | |
2fb76298 | 7834 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 7835 | return; |
7836 | } | |
ef416fc2 | 7837 | |
7838 | /* | |
b423cd4c | 7839 | * Hold the job and return... |
ef416fc2 | 7840 | */ |
7841 | ||
b423cd4c | 7842 | cupsdHoldJob(job); |
ef416fc2 | 7843 | |
01ce6322 | 7844 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 7845 | "Job held by user."); |
7846 | ||
b423cd4c | 7847 | if ((newattr = ippFindAttribute(con->request, "job-hold-until", |
7848 | IPP_TAG_KEYWORD)) == NULL) | |
7849 | newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 7850 | |
b423cd4c | 7851 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7852 | IPP_TAG_KEYWORD)) == NULL) | |
7853 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 7854 | |
b423cd4c | 7855 | if (attr) |
7856 | { | |
7857 | /* | |
7858 | * Free the old hold value and copy the new one over... | |
7859 | */ | |
ef416fc2 | 7860 | |
757d2cad | 7861 | _cupsStrFree(attr->values[0].string.text); |
ef416fc2 | 7862 | |
b423cd4c | 7863 | if (newattr) |
ef416fc2 | 7864 | { |
b423cd4c | 7865 | attr->value_tag = newattr->value_tag; |
7866 | attr->values[0].string.text = | |
757d2cad | 7867 | _cupsStrAlloc(newattr->values[0].string.text); |
b423cd4c | 7868 | } |
7869 | else | |
7870 | { | |
7871 | attr->value_tag = IPP_TAG_KEYWORD; | |
757d2cad | 7872 | attr->values[0].string.text = _cupsStrAlloc("indefinite"); |
ef416fc2 | 7873 | } |
7874 | ||
b423cd4c | 7875 | /* |
7876 | * Hold job until specified time... | |
7877 | */ | |
ef416fc2 | 7878 | |
b423cd4c | 7879 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
d09495fa | 7880 | |
01ce6322 | 7881 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 7882 | "Job job-hold-until value changed by user."); |
b423cd4c | 7883 | } |
ef416fc2 | 7884 | |
75bd9771 | 7885 | cupsdLogJob(job, CUPSD_LOG_INFO, "Held by \"%s\".", username); |
b423cd4c | 7886 | |
7887 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 7888 | } |
7889 | ||
7890 | ||
7891 | /* | |
b423cd4c | 7892 | * 'move_job()' - Move a job to a new destination. |
ef416fc2 | 7893 | */ |
7894 | ||
7895 | static void | |
b423cd4c | 7896 | move_job(cupsd_client_t *con, /* I - Client connection */ |
7897 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 7898 | { |
7899 | http_status_t status; /* Policy status */ | |
7900 | ipp_attribute_t *attr; /* Current attribute */ | |
b423cd4c | 7901 | int jobid; /* Job ID */ |
ef416fc2 | 7902 | cupsd_job_t *job; /* Current job */ |
d09495fa | 7903 | const char *src; /* Source printer/class */ |
b423cd4c | 7904 | cups_ptype_t stype, /* Source type (printer or class) */ |
bc44d920 | 7905 | dtype; /* Destination type (printer/class) */ |
f7deaa1a | 7906 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
ef416fc2 | 7907 | username[HTTP_MAX_URI], /* Username portion of URI */ |
7908 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
b423cd4c | 7909 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
ef416fc2 | 7910 | int port; /* Port portion of URI */ |
b423cd4c | 7911 | cupsd_printer_t *sprinter, /* Source printer */ |
7912 | *dprinter; /* Destination printer */ | |
ef416fc2 | 7913 | |
7914 | ||
b423cd4c | 7915 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd, |
ef416fc2 | 7916 | uri->values[0].string.text); |
7917 | ||
7918 | /* | |
b423cd4c | 7919 | * Get the new printer or class... |
ef416fc2 | 7920 | */ |
7921 | ||
b423cd4c | 7922 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", |
7923 | IPP_TAG_URI)) == NULL) | |
ef416fc2 | 7924 | { |
b423cd4c | 7925 | /* |
7926 | * Need job-printer-uri... | |
7927 | */ | |
ef416fc2 | 7928 | |
b423cd4c | 7929 | send_ipp_status(con, IPP_BAD_REQUEST, |
7930 | _("job-printer-uri attribute missing!")); | |
7931 | return; | |
ef416fc2 | 7932 | } |
f7faf1f5 | 7933 | |
f7deaa1a | 7934 | if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter)) |
ef416fc2 | 7935 | { |
b423cd4c | 7936 | /* |
7937 | * Bad URI... | |
7938 | */ | |
ef416fc2 | 7939 | |
b423cd4c | 7940 | send_ipp_status(con, IPP_NOT_FOUND, |
7941 | _("The printer or class was not found.")); | |
7942 | return; | |
ef416fc2 | 7943 | } |
7944 | ||
ef416fc2 | 7945 | /* |
b423cd4c | 7946 | * See if we have a job URI or a printer URI... |
ef416fc2 | 7947 | */ |
7948 | ||
f7deaa1a | 7949 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7950 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7951 | sizeof(host), &port, resource, sizeof(resource)); |
7952 | ||
7953 | if (!strcmp(uri->name, "printer-uri")) | |
ef416fc2 | 7954 | { |
7955 | /* | |
b423cd4c | 7956 | * Got a printer URI; see if we also have a job-id attribute... |
ef416fc2 | 7957 | */ |
7958 | ||
b423cd4c | 7959 | if ((attr = ippFindAttribute(con->request, "job-id", |
7960 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7961 | { |
b423cd4c | 7962 | /* |
7963 | * Move all jobs... | |
7964 | */ | |
7965 | ||
f7deaa1a | 7966 | if ((src = cupsdValidateDest(uri->values[0].string.text, &stype, |
7967 | &sprinter)) == NULL) | |
b423cd4c | 7968 | { |
7969 | /* | |
7970 | * Bad URI... | |
7971 | */ | |
7972 | ||
7973 | send_ipp_status(con, IPP_NOT_FOUND, | |
7974 | _("The printer or class was not found.")); | |
7975 | return; | |
7976 | } | |
7977 | ||
7978 | job = NULL; | |
7979 | } | |
7980 | else | |
7981 | { | |
7982 | /* | |
7983 | * Otherwise, just move a single job... | |
7984 | */ | |
7985 | ||
7986 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
7987 | { | |
7988 | /* | |
7989 | * Nope - return a "not found" error... | |
7990 | */ | |
7991 | ||
7992 | send_ipp_status(con, IPP_NOT_FOUND, | |
7993 | _("Job #%d does not exist!"), attr->values[0].integer); | |
7994 | return; | |
7995 | } | |
7996 | else | |
7997 | { | |
7998 | /* | |
7999 | * Job found, initialize source pointers... | |
8000 | */ | |
8001 | ||
8002 | src = NULL; | |
8003 | sprinter = NULL; | |
8004 | } | |
ef416fc2 | 8005 | } |
8006 | } | |
8007 | else | |
8008 | { | |
8009 | /* | |
b423cd4c | 8010 | * Got a job URI; parse it to get the job ID... |
ef416fc2 | 8011 | */ |
8012 | ||
b423cd4c | 8013 | if (strncmp(resource, "/jobs/", 6)) |
8014 | { | |
8015 | /* | |
8016 | * Not a valid URI! | |
8017 | */ | |
8018 | ||
8019 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8020 | _("Bad job-uri attribute \"%s\"!"), | |
8021 | uri->values[0].string.text); | |
8022 | return; | |
8023 | } | |
ef416fc2 | 8024 | |
ef416fc2 | 8025 | /* |
b423cd4c | 8026 | * See if the job exists... |
ef416fc2 | 8027 | */ |
8028 | ||
b423cd4c | 8029 | jobid = atoi(resource + 6); |
ef416fc2 | 8030 | |
b423cd4c | 8031 | if ((job = cupsdFindJob(jobid)) == NULL) |
ef416fc2 | 8032 | { |
8033 | /* | |
b423cd4c | 8034 | * Nope - return a "not found" error... |
ef416fc2 | 8035 | */ |
8036 | ||
b423cd4c | 8037 | send_ipp_status(con, IPP_NOT_FOUND, |
8038 | _("Job #%d does not exist!"), jobid); | |
8039 | return; | |
ef416fc2 | 8040 | } |
8041 | else | |
b423cd4c | 8042 | { |
8043 | /* | |
8044 | * Job found, initialize source pointers... | |
8045 | */ | |
ef416fc2 | 8046 | |
b423cd4c | 8047 | src = NULL; |
8048 | sprinter = NULL; | |
8049 | } | |
ef416fc2 | 8050 | } |
8051 | ||
ac884b6a MS |
8052 | /* |
8053 | * Check the policy of the destination printer... | |
8054 | */ | |
8055 | ||
8056 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, | |
8057 | job ? job->username : NULL)) != HTTP_OK) | |
8058 | { | |
8059 | send_http_error(con, status, dprinter); | |
8060 | return; | |
8061 | } | |
8062 | ||
ef416fc2 | 8063 | /* |
b423cd4c | 8064 | * Now move the job or jobs... |
ef416fc2 | 8065 | */ |
8066 | ||
b423cd4c | 8067 | if (job) |
8068 | { | |
8069 | /* | |
8070 | * See if the job has been completed... | |
8071 | */ | |
8072 | ||
8073 | if (job->state_value > IPP_JOB_STOPPED) | |
8074 | { | |
8075 | /* | |
8076 | * Return a "not-possible" error... | |
8077 | */ | |
8078 | ||
8079 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8080 | _("Job #%d is finished and cannot be altered!"), | |
8081 | job->id); | |
8082 | return; | |
8083 | } | |
ef416fc2 | 8084 | |
b423cd4c | 8085 | /* |
8086 | * See if the job is owned by the requesting user... | |
8087 | */ | |
ef416fc2 | 8088 | |
b423cd4c | 8089 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8090 | { | |
2fb76298 | 8091 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 8092 | return; |
8093 | } | |
ef416fc2 | 8094 | |
ef416fc2 | 8095 | /* |
b423cd4c | 8096 | * Move the job to a different printer or class... |
ef416fc2 | 8097 | */ |
8098 | ||
e53920b9 | 8099 | cupsdMoveJob(job, dprinter); |
ef416fc2 | 8100 | } |
b423cd4c | 8101 | else |
8102 | { | |
8103 | /* | |
8104 | * Got the source printer, now look through the jobs... | |
8105 | */ | |
ef416fc2 | 8106 | |
b423cd4c | 8107 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
8108 | job; | |
8109 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
8110 | { | |
8111 | /* | |
8112 | * See if the job is pointing at the source printer or has not been | |
8113 | * completed... | |
8114 | */ | |
ef416fc2 | 8115 | |
b423cd4c | 8116 | if (strcasecmp(job->dest, src) || |
8117 | job->state_value > IPP_JOB_STOPPED) | |
8118 | continue; | |
ef416fc2 | 8119 | |
b423cd4c | 8120 | /* |
8121 | * See if the job can be moved by the requesting user... | |
8122 | */ | |
ef416fc2 | 8123 | |
b423cd4c | 8124 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8125 | continue; | |
ef416fc2 | 8126 | |
b423cd4c | 8127 | /* |
8128 | * Move the job to a different printer or class... | |
8129 | */ | |
ef416fc2 | 8130 | |
e53920b9 | 8131 | cupsdMoveJob(job, dprinter); |
b423cd4c | 8132 | } |
ef416fc2 | 8133 | } |
8134 | ||
8135 | /* | |
b423cd4c | 8136 | * Start jobs if possible... |
ef416fc2 | 8137 | */ |
8138 | ||
b423cd4c | 8139 | cupsdCheckJobs(); |
ef416fc2 | 8140 | |
8141 | /* | |
b423cd4c | 8142 | * Return with "everything is OK" status... |
ef416fc2 | 8143 | */ |
8144 | ||
b423cd4c | 8145 | con->response->request.status.status_code = IPP_OK; |
8146 | } | |
ef416fc2 | 8147 | |
ef416fc2 | 8148 | |
b423cd4c | 8149 | /* |
8150 | * 'ppd_parse_line()' - Parse a PPD default line. | |
8151 | */ | |
ef416fc2 | 8152 | |
b423cd4c | 8153 | static int /* O - 0 on success, -1 on failure */ |
8154 | ppd_parse_line(const char *line, /* I - Line */ | |
8155 | char *option, /* O - Option name */ | |
8156 | int olen, /* I - Size of option name */ | |
8157 | char *choice, /* O - Choice name */ | |
8158 | int clen) /* I - Size of choice name */ | |
8159 | { | |
8160 | /* | |
8161 | * Verify this is a default option line... | |
8162 | */ | |
ef416fc2 | 8163 | |
b423cd4c | 8164 | if (strncmp(line, "*Default", 8)) |
8165 | return (-1); | |
ef416fc2 | 8166 | |
b423cd4c | 8167 | /* |
8168 | * Read the option name... | |
8169 | */ | |
8170 | ||
8171 | for (line += 8, olen --; isalnum(*line & 255); line ++) | |
8172 | if (olen > 0) | |
8173 | { | |
8174 | *option++ = *line; | |
8175 | olen --; | |
ef416fc2 | 8176 | } |
8177 | ||
b423cd4c | 8178 | *option = '\0'; |
ef416fc2 | 8179 | |
b423cd4c | 8180 | /* |
8181 | * Skip everything else up to the colon (:)... | |
8182 | */ | |
ef416fc2 | 8183 | |
b423cd4c | 8184 | while (*line && *line != ':') |
8185 | line ++; | |
ef416fc2 | 8186 | |
b423cd4c | 8187 | if (!*line) |
8188 | return (-1); | |
ef416fc2 | 8189 | |
b423cd4c | 8190 | line ++; |
ef416fc2 | 8191 | |
b423cd4c | 8192 | /* |
8193 | * Now grab the option choice, skipping leading whitespace... | |
8194 | */ | |
ef416fc2 | 8195 | |
b423cd4c | 8196 | while (isspace(*line & 255)) |
8197 | line ++; | |
ef416fc2 | 8198 | |
b423cd4c | 8199 | for (clen --; isalnum(*line & 255); line ++) |
8200 | if (clen > 0) | |
8201 | { | |
8202 | *choice++ = *line; | |
8203 | clen --; | |
8204 | } | |
ef416fc2 | 8205 | |
b423cd4c | 8206 | *choice = '\0'; |
ef416fc2 | 8207 | |
b423cd4c | 8208 | /* |
8209 | * Return with no errors... | |
8210 | */ | |
ef416fc2 | 8211 | |
b423cd4c | 8212 | return (0); |
8213 | } | |
ef416fc2 | 8214 | |
ef416fc2 | 8215 | |
b423cd4c | 8216 | /* |
8217 | * 'print_job()' - Print a file to a printer or class. | |
8218 | */ | |
ef416fc2 | 8219 | |
b423cd4c | 8220 | static void |
8221 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
8222 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8223 | { | |
8224 | ipp_attribute_t *attr; /* Current attribute */ | |
8225 | ipp_attribute_t *format; /* Document-format attribute */ | |
f7deaa1a | 8226 | const char *default_format; /* document-format-default value */ |
b423cd4c | 8227 | cupsd_job_t *job; /* New job */ |
8228 | char filename[1024]; /* Job filename */ | |
8229 | mime_type_t *filetype; /* Type of file */ | |
8230 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
8231 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
8232 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
8233 | /* Textual name of mime type */ | |
8234 | cupsd_printer_t *printer; /* Printer data */ | |
8235 | struct stat fileinfo; /* File information */ | |
8236 | int kbytes; /* Size of file */ | |
8237 | int compression; /* Document compression */ | |
ef416fc2 | 8238 | |
ef416fc2 | 8239 | |
b423cd4c | 8240 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd, |
8241 | uri->values[0].string.text); | |
ef416fc2 | 8242 | |
b423cd4c | 8243 | /* |
8244 | * Validate print file attributes, for now just document-format and | |
8245 | * compression (CUPS only supports "none" and "gzip")... | |
8246 | */ | |
ef416fc2 | 8247 | |
b423cd4c | 8248 | compression = CUPS_FILE_NONE; |
ef416fc2 | 8249 | |
b423cd4c | 8250 | if ((attr = ippFindAttribute(con->request, "compression", |
8251 | IPP_TAG_KEYWORD)) != NULL) | |
8252 | { | |
8253 | if (strcmp(attr->values[0].string.text, "none") | |
8254 | #ifdef HAVE_LIBZ | |
8255 | && strcmp(attr->values[0].string.text, "gzip") | |
8256 | #endif /* HAVE_LIBZ */ | |
8257 | ) | |
8258 | { | |
8259 | send_ipp_status(con, IPP_ATTRIBUTES, | |
8260 | _("Unsupported compression \"%s\"!"), | |
8261 | attr->values[0].string.text); | |
8262 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
8263 | "compression", NULL, attr->values[0].string.text); | |
8264 | return; | |
8265 | } | |
ef416fc2 | 8266 | |
b423cd4c | 8267 | #ifdef HAVE_LIBZ |
8268 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
8269 | compression = CUPS_FILE_GZIP; | |
8270 | #endif /* HAVE_LIBZ */ | |
8271 | } | |
ef416fc2 | 8272 | |
b423cd4c | 8273 | /* |
8274 | * Do we have a file to print? | |
8275 | */ | |
ef416fc2 | 8276 | |
b423cd4c | 8277 | if (!con->filename) |
8278 | { | |
8279 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
8280 | return; | |
8281 | } | |
ef416fc2 | 8282 | |
f7deaa1a | 8283 | /* |
8284 | * Is the destination valid? | |
8285 | */ | |
8286 | ||
8287 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
8288 | { | |
8289 | /* | |
8290 | * Bad URI... | |
8291 | */ | |
8292 | ||
8293 | send_ipp_status(con, IPP_NOT_FOUND, | |
8294 | _("The printer or class was not found.")); | |
8295 | return; | |
8296 | } | |
8297 | ||
b423cd4c | 8298 | /* |
8299 | * Is it a format we support? | |
8300 | */ | |
ef416fc2 | 8301 | |
b423cd4c | 8302 | if ((format = ippFindAttribute(con->request, "document-format", |
8303 | IPP_TAG_MIMETYPE)) != NULL) | |
8304 | { | |
8305 | /* | |
8306 | * Grab format from client... | |
8307 | */ | |
8308 | ||
f7deaa1a | 8309 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, |
8310 | type) != 2) | |
b423cd4c | 8311 | { |
8312 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8313 | _("Could not scan type \"%s\"!"), | |
8314 | format->values[0].string.text); | |
8315 | return; | |
ef416fc2 | 8316 | } |
b423cd4c | 8317 | } |
f7deaa1a | 8318 | else if ((default_format = cupsGetOption("document-format", |
8319 | printer->num_options, | |
8320 | printer->options)) != NULL) | |
8321 | { | |
8322 | /* | |
8323 | * Use default document format... | |
8324 | */ | |
8325 | ||
8326 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
8327 | { | |
8328 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8329 | _("Could not scan type \"%s\"!"), | |
8330 | default_format); | |
8331 | return; | |
8332 | } | |
8333 | } | |
b423cd4c | 8334 | else |
8335 | { | |
8336 | /* | |
f7deaa1a | 8337 | * Auto-type it! |
b423cd4c | 8338 | */ |
8339 | ||
8340 | strcpy(super, "application"); | |
8341 | strcpy(type, "octet-stream"); | |
8342 | } | |
ef416fc2 | 8343 | |
b423cd4c | 8344 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
8345 | { | |
ef416fc2 | 8346 | /* |
b423cd4c | 8347 | * Auto-type the file... |
ef416fc2 | 8348 | */ |
8349 | ||
b423cd4c | 8350 | ipp_attribute_t *doc_name; /* document-name attribute */ |
8351 | ||
8352 | ||
a0f6818e | 8353 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file..."); |
b423cd4c | 8354 | |
8355 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
8356 | filetype = mimeFileType(MimeDatabase, con->filename, | |
8357 | doc_name ? doc_name->values[0].string.text : NULL, | |
8358 | &compression); | |
8359 | ||
f7deaa1a | 8360 | if (!filetype) |
8361 | filetype = mimeType(MimeDatabase, super, type); | |
a0f6818e MS |
8362 | |
8363 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.", | |
8364 | filetype->super, filetype->type); | |
f7deaa1a | 8365 | } |
8366 | else | |
8367 | filetype = mimeType(MimeDatabase, super, type); | |
ef416fc2 | 8368 | |
f7deaa1a | 8369 | if (filetype && |
8370 | (!format || | |
8371 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) | |
8372 | { | |
8373 | /* | |
8374 | * Replace the document-format attribute value with the auto-typed or | |
8375 | * default one. | |
8376 | */ | |
b423cd4c | 8377 | |
f7deaa1a | 8378 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
8379 | filetype->type); | |
ed486911 | 8380 | |
f7deaa1a | 8381 | if (format) |
8382 | { | |
8383 | _cupsStrFree(format->values[0].string.text); | |
8384 | ||
8385 | format->values[0].string.text = _cupsStrAlloc(mimetype); | |
ef416fc2 | 8386 | } |
b423cd4c | 8387 | else |
f7deaa1a | 8388 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
8389 | "document-format", NULL, mimetype); | |
ef416fc2 | 8390 | } |
f7deaa1a | 8391 | else if (!filetype) |
b423cd4c | 8392 | { |
8393 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
8394 | _("Unsupported format \'%s/%s\'!"), super, type); | |
8395 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8396 | "Hint: Do you have the raw file printing rules enabled?"); | |
8397 | ||
8398 | if (format) | |
8399 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
8400 | "document-format", NULL, format->values[0].string.text); | |
8401 | ||
8402 | return; | |
8403 | } | |
8404 | ||
b423cd4c | 8405 | /* |
8406 | * Read any embedded job ticket info from PS files... | |
8407 | */ | |
8408 | ||
8409 | if (!strcasecmp(filetype->super, "application") && | |
c5571a1d MS |
8410 | (!strcasecmp(filetype->type, "postscript") || |
8411 | !strcasecmp(filetype->type, "pdf"))) | |
8412 | read_job_ticket(con); | |
b423cd4c | 8413 | |
8414 | /* | |
8415 | * Create the job object... | |
8416 | */ | |
8417 | ||
f7deaa1a | 8418 | if ((job = add_job(con, printer, filetype)) == NULL) |
b423cd4c | 8419 | return; |
8420 | ||
8421 | /* | |
8422 | * Update quota data... | |
8423 | */ | |
8424 | ||
8425 | if (stat(con->filename, &fileinfo)) | |
8426 | kbytes = 0; | |
8427 | else | |
8428 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
8429 | ||
8430 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
8431 | ||
8432 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
8433 | IPP_TAG_INTEGER)) != NULL) | |
8434 | attr->values[0].integer += kbytes; | |
8435 | ||
ef416fc2 | 8436 | /* |
8437 | * Add the job file... | |
8438 | */ | |
8439 | ||
8440 | if (add_file(con, job, filetype, compression)) | |
8441 | return; | |
8442 | ||
8443 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
8444 | job->num_files); | |
8445 | rename(con->filename, filename); | |
8446 | cupsdClearString(&con->filename); | |
8447 | ||
8448 | /* | |
8449 | * See if we need to add the ending sheet... | |
8450 | */ | |
8451 | ||
91c84a35 MS |
8452 | if (cupsdTimeoutJob(job)) |
8453 | return; | |
ef416fc2 | 8454 | |
ef416fc2 | 8455 | /* |
8456 | * Log and save the job... | |
8457 | */ | |
8458 | ||
75bd9771 MS |
8459 | cupsdLogJob(job, CUPSD_LOG_INFO, |
8460 | "File of type %s/%s queued by \"%s\".", | |
8461 | filetype->super, filetype->type, job->username); | |
8462 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until); | |
ef416fc2 | 8463 | |
ef416fc2 | 8464 | /* |
8465 | * Start the job if possible... | |
8466 | */ | |
8467 | ||
8468 | cupsdCheckJobs(); | |
8469 | } | |
8470 | ||
8471 | ||
8472 | /* | |
c5571a1d | 8473 | * 'read_job_ticket()' - Read a job ticket embedded in a print file. |
ef416fc2 | 8474 | * |
c5571a1d | 8475 | * This function only gets called when printing a single PDF or PostScript |
ef416fc2 | 8476 | * file using the Print-Job operation. It doesn't work for Create-Job + |
8477 | * Send-File, since the job attributes need to be set at job creation | |
c5571a1d MS |
8478 | * time for banners to work. The embedded job ticket stuff is here |
8479 | * primarily to allow the Windows printer driver for CUPS to pass in JCL | |
ef416fc2 | 8480 | * options and IPP attributes which otherwise would be lost. |
8481 | * | |
c5571a1d | 8482 | * The format of a job ticket is simple: |
ef416fc2 | 8483 | * |
8484 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
8485 | * | |
8486 | * %cupsJobTicket: attr1=value1 | |
8487 | * %cupsJobTicket: attr2=value2 | |
8488 | * ... | |
8489 | * %cupsJobTicket: attrN=valueN | |
8490 | * | |
8491 | * Job ticket lines must appear immediately after the first line that | |
c5571a1d MS |
8492 | * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS |
8493 | * stops looking for job ticket info when it finds a line that does not begin | |
ef416fc2 | 8494 | * with "%cupsJobTicket:". |
8495 | * | |
8496 | * The maximum length of a job ticket line, including the prefix, is | |
8497 | * 255 characters to conform with the Adobe DSC. | |
8498 | * | |
8499 | * Read-only attributes are rejected with a notice to the error log in | |
8500 | * case a malicious user tries anything. Since the job ticket is read | |
8501 | * prior to attribute validation in print_job(), job ticket attributes | |
8502 | * will go through the same validation as IPP attributes... | |
8503 | */ | |
8504 | ||
8505 | static void | |
c5571a1d | 8506 | read_job_ticket(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 8507 | { |
8508 | cups_file_t *fp; /* File to read from */ | |
8509 | char line[256]; /* Line data */ | |
8510 | int num_options; /* Number of options */ | |
8511 | cups_option_t *options; /* Options */ | |
8512 | ipp_t *ticket; /* New attributes */ | |
8513 | ipp_attribute_t *attr, /* Current attribute */ | |
8514 | *attr2, /* Job attribute */ | |
8515 | *prev2; /* Previous job attribute */ | |
8516 | ||
8517 | ||
8518 | /* | |
8519 | * First open the print file... | |
8520 | */ | |
8521 | ||
8522 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
8523 | { | |
8524 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
c5571a1d | 8525 | "Unable to open print file for job ticket - %s", |
ef416fc2 | 8526 | strerror(errno)); |
8527 | return; | |
8528 | } | |
8529 | ||
8530 | /* | |
8531 | * Skip the first line... | |
8532 | */ | |
8533 | ||
8534 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
8535 | { | |
8536 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
c5571a1d | 8537 | "Unable to read from print file for job ticket - %s", |
ef416fc2 | 8538 | strerror(errno)); |
8539 | cupsFileClose(fp); | |
8540 | return; | |
8541 | } | |
8542 | ||
c5571a1d | 8543 | if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5)) |
ef416fc2 | 8544 | { |
8545 | /* | |
8546 | * Not a DSC-compliant file, so no job ticket info will be available... | |
8547 | */ | |
8548 | ||
8549 | cupsFileClose(fp); | |
8550 | return; | |
8551 | } | |
8552 | ||
8553 | /* | |
8554 | * Read job ticket info from the file... | |
8555 | */ | |
8556 | ||
8557 | num_options = 0; | |
8558 | options = NULL; | |
8559 | ||
fa73b229 | 8560 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 8561 | { |
8562 | /* | |
8563 | * Stop at the first non-ticket line... | |
8564 | */ | |
8565 | ||
fa73b229 | 8566 | if (strncmp(line, "%cupsJobTicket:", 15)) |
ef416fc2 | 8567 | break; |
8568 | ||
8569 | /* | |
8570 | * Add the options to the option array... | |
8571 | */ | |
8572 | ||
8573 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
8574 | } | |
8575 | ||
8576 | /* | |
8577 | * Done with the file; see if we have any options... | |
8578 | */ | |
8579 | ||
8580 | cupsFileClose(fp); | |
8581 | ||
8582 | if (num_options == 0) | |
8583 | return; | |
8584 | ||
8585 | /* | |
8586 | * OK, convert the options to an attribute list, and apply them to | |
8587 | * the request... | |
8588 | */ | |
8589 | ||
8590 | ticket = ippNew(); | |
8591 | cupsEncodeOptions(ticket, num_options, options); | |
8592 | ||
8593 | /* | |
8594 | * See what the user wants to change. | |
8595 | */ | |
8596 | ||
fa73b229 | 8597 | for (attr = ticket->attrs; attr; attr = attr->next) |
ef416fc2 | 8598 | { |
8599 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8600 | continue; | |
8601 | ||
8602 | if (!strcmp(attr->name, "job-originating-host-name") || | |
8603 | !strcmp(attr->name, "job-originating-user-name") || | |
8604 | !strcmp(attr->name, "job-media-sheets-completed") || | |
8605 | !strcmp(attr->name, "job-k-octets") || | |
8606 | !strcmp(attr->name, "job-id") || | |
8607 | !strncmp(attr->name, "job-state", 9) || | |
8608 | !strncmp(attr->name, "time-at-", 8)) | |
8609 | continue; /* Read-only attrs */ | |
8610 | ||
fa73b229 | 8611 | if ((attr2 = ippFindAttribute(con->request, attr->name, |
8612 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 8613 | { |
8614 | /* | |
8615 | * Some other value; first free the old value... | |
8616 | */ | |
8617 | ||
8618 | if (con->request->attrs == attr2) | |
8619 | { | |
8620 | con->request->attrs = attr2->next; | |
8621 | prev2 = NULL; | |
8622 | } | |
8623 | else | |
8624 | { | |
fa73b229 | 8625 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) |
ef416fc2 | 8626 | if (prev2->next == attr2) |
8627 | { | |
8628 | prev2->next = attr2->next; | |
8629 | break; | |
8630 | } | |
8631 | } | |
8632 | ||
8633 | if (con->request->last == attr2) | |
8634 | con->request->last = prev2; | |
8635 | ||
757d2cad | 8636 | _ippFreeAttr(attr2); |
ef416fc2 | 8637 | } |
8638 | ||
8639 | /* | |
8640 | * Add new option by copying it... | |
8641 | */ | |
8642 | ||
8643 | copy_attribute(con->request, attr, 0); | |
8644 | } | |
8645 | ||
8646 | /* | |
8647 | * Then free the attribute list and option array... | |
8648 | */ | |
8649 | ||
8650 | ippDelete(ticket); | |
8651 | cupsFreeOptions(num_options, options); | |
8652 | } | |
8653 | ||
8654 | ||
8655 | /* | |
8656 | * 'reject_jobs()' - Reject print jobs to a printer. | |
8657 | */ | |
8658 | ||
8659 | static void | |
8660 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
8661 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
8662 | { | |
8663 | http_status_t status; /* Policy status */ | |
bc44d920 | 8664 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 8665 | cupsd_printer_t *printer; /* Printer data */ |
8666 | ipp_attribute_t *attr; /* printer-state-message text */ | |
8667 | ||
8668 | ||
8669 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
8670 | con->http.fd, uri->values[0].string.text); | |
8671 | ||
8672 | /* | |
8673 | * Is the destination valid? | |
8674 | */ | |
8675 | ||
f7deaa1a | 8676 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 8677 | { |
8678 | /* | |
8679 | * Bad URI... | |
8680 | */ | |
8681 | ||
8682 | send_ipp_status(con, IPP_NOT_FOUND, | |
8683 | _("The printer or class was not found.")); | |
8684 | return; | |
8685 | } | |
8686 | ||
8687 | /* | |
8688 | * Check policy... | |
8689 | */ | |
8690 | ||
8691 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8692 | { | |
f899b121 | 8693 | send_http_error(con, status, printer); |
ef416fc2 | 8694 | return; |
8695 | } | |
8696 | ||
8697 | /* | |
8698 | * Reject jobs sent to the printer... | |
8699 | */ | |
8700 | ||
8701 | printer->accepting = 0; | |
8702 | ||
8703 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8704 | IPP_TAG_TEXT)) == NULL) | |
8705 | strcpy(printer->state_message, "Rejecting Jobs"); | |
8706 | else | |
8707 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8708 | sizeof(printer->state_message)); | |
8709 | ||
8710 | cupsdAddPrinterHistory(printer); | |
8711 | ||
8712 | if (dtype & CUPS_PRINTER_CLASS) | |
8713 | { | |
3dfe78b3 | 8714 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
ef416fc2 | 8715 | |
8716 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8717 | printer->name, get_username(con)); |
ef416fc2 | 8718 | } |
8719 | else | |
8720 | { | |
3dfe78b3 | 8721 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 8722 | |
8723 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8724 | printer->name, get_username(con)); |
ef416fc2 | 8725 | } |
8726 | ||
8727 | /* | |
8728 | * Everything was ok, so return OK status... | |
8729 | */ | |
8730 | ||
8731 | con->response->request.status.status_code = IPP_OK; | |
8732 | } | |
8733 | ||
8734 | ||
8735 | /* | |
8736 | * 'release_job()' - Release a held print job. | |
8737 | */ | |
8738 | ||
8739 | static void | |
8740 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
8741 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
8742 | { | |
8743 | ipp_attribute_t *attr; /* Current attribute */ | |
8744 | int jobid; /* Job ID */ | |
8745 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
8746 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
8747 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
8748 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
8749 | int port; /* Port portion of URI */ | |
8750 | cupsd_job_t *job; /* Job information */ | |
8751 | ||
8752 | ||
8753 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
8754 | con->http.fd, uri->values[0].string.text); | |
8755 | ||
8756 | /* | |
8757 | * See if we have a job URI or a printer URI... | |
8758 | */ | |
8759 | ||
8760 | if (!strcmp(uri->name, "printer-uri")) | |
8761 | { | |
8762 | /* | |
8763 | * Got a printer URI; see if we also have a job-id attribute... | |
8764 | */ | |
8765 | ||
fa73b229 | 8766 | if ((attr = ippFindAttribute(con->request, "job-id", |
8767 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8768 | { |
8769 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8770 | _("Got a printer-uri attribute but no job-id!")); | |
8771 | return; | |
8772 | } | |
8773 | ||
8774 | jobid = attr->values[0].integer; | |
8775 | } | |
8776 | else | |
8777 | { | |
8778 | /* | |
8779 | * Got a job URI; parse it to get the job ID... | |
8780 | */ | |
8781 | ||
a4d04587 | 8782 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8783 | sizeof(method), username, sizeof(username), host, | |
8784 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8785 | |
8786 | if (strncmp(resource, "/jobs/", 6)) | |
8787 | { | |
8788 | /* | |
8789 | * Not a valid URI! | |
8790 | */ | |
8791 | ||
8792 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8793 | _("Bad job-uri attribute \"%s\"!"), | |
8794 | uri->values[0].string.text); | |
8795 | return; | |
8796 | } | |
8797 | ||
8798 | jobid = atoi(resource + 6); | |
8799 | } | |
8800 | ||
8801 | /* | |
8802 | * See if the job exists... | |
8803 | */ | |
8804 | ||
8805 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8806 | { | |
8807 | /* | |
8808 | * Nope - return a "not found" error... | |
8809 | */ | |
8810 | ||
8811 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
8812 | return; | |
8813 | } | |
8814 | ||
8815 | /* | |
8816 | * See if job is "held"... | |
8817 | */ | |
8818 | ||
bd7854cb | 8819 | if (job->state_value != IPP_JOB_HELD) |
ef416fc2 | 8820 | { |
8821 | /* | |
8822 | * Nope - return a "not possible" error... | |
8823 | */ | |
8824 | ||
8825 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid); | |
8826 | return; | |
8827 | } | |
8828 | ||
8829 | /* | |
8830 | * See if the job is owned by the requesting user... | |
8831 | */ | |
8832 | ||
8833 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8834 | { | |
2fb76298 | 8835 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 8836 | return; |
8837 | } | |
8838 | ||
8839 | /* | |
8840 | * Reset the job-hold-until value to "no-hold"... | |
8841 | */ | |
8842 | ||
fa73b229 | 8843 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
8844 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 8845 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
8846 | ||
fa73b229 | 8847 | if (attr) |
ef416fc2 | 8848 | { |
757d2cad | 8849 | _cupsStrFree(attr->values[0].string.text); |
4400e98d | 8850 | |
ef416fc2 | 8851 | attr->value_tag = IPP_TAG_KEYWORD; |
757d2cad | 8852 | attr->values[0].string.text = _cupsStrAlloc("no-hold"); |
d09495fa | 8853 | |
01ce6322 | 8854 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 8855 | "Job job-hold-until value changed by user."); |
ef416fc2 | 8856 | } |
8857 | ||
8858 | /* | |
8859 | * Release the job and return... | |
8860 | */ | |
8861 | ||
8862 | cupsdReleaseJob(job); | |
8863 | ||
01ce6322 | 8864 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 8865 | "Job released by user."); |
8866 | ||
75bd9771 | 8867 | cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username); |
ef416fc2 | 8868 | |
8869 | con->response->request.status.status_code = IPP_OK; | |
005dd1eb MS |
8870 | |
8871 | cupsdCheckJobs(); | |
ef416fc2 | 8872 | } |
8873 | ||
8874 | ||
8875 | /* | |
8876 | * 'renew_subscription()' - Renew an existing subscription... | |
8877 | */ | |
8878 | ||
8879 | static void | |
8880 | renew_subscription( | |
8881 | cupsd_client_t *con, /* I - Client connection */ | |
8882 | int sub_id) /* I - Subscription ID */ | |
8883 | { | |
bd7854cb | 8884 | http_status_t status; /* Policy status */ |
8885 | cupsd_subscription_t *sub; /* Subscription */ | |
8886 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
8887 | ||
8888 | ||
8889 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
8890 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
8891 | con, con->http.fd, sub_id); | |
8892 | ||
8893 | /* | |
8894 | * Is the subscription ID valid? | |
8895 | */ | |
8896 | ||
8897 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
8898 | { | |
8899 | /* | |
8900 | * Bad subscription ID... | |
8901 | */ | |
8902 | ||
8903 | send_ipp_status(con, IPP_NOT_FOUND, | |
8904 | _("notify-subscription-id %d no good!"), sub_id); | |
8905 | return; | |
8906 | } | |
8907 | ||
8908 | if (sub->job) | |
8909 | { | |
8910 | /* | |
8911 | * Job subscriptions cannot be renewed... | |
8912 | */ | |
8913 | ||
8914 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8915 | _("Job subscriptions cannot be renewed!")); | |
8916 | return; | |
8917 | } | |
8918 | ||
8919 | /* | |
8920 | * Check policy... | |
8921 | */ | |
8922 | ||
8923 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
8924 | DefaultPolicyPtr, | |
8925 | con, sub->owner)) != HTTP_OK) | |
8926 | { | |
f899b121 | 8927 | send_http_error(con, status, sub->dest); |
bd7854cb | 8928 | return; |
8929 | } | |
8930 | ||
8931 | /* | |
8932 | * Renew the subscription... | |
8933 | */ | |
8934 | ||
8935 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
8936 | IPP_TAG_INTEGER); | |
8937 | ||
8938 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
8939 | ||
8940 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
8941 | { | |
8942 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8943 | "renew_subscription: Limiting notify-lease-duration to " | |
8944 | "%d seconds.", | |
8945 | MaxLeaseDuration); | |
8946 | sub->lease = MaxLeaseDuration; | |
8947 | } | |
8948 | ||
8949 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
8950 | ||
3dfe78b3 | 8951 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
bd7854cb | 8952 | |
8953 | con->response->request.status.status_code = IPP_OK; | |
b94498cf | 8954 | |
8955 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
8956 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 8957 | } |
8958 | ||
8959 | ||
8960 | /* | |
8961 | * 'restart_job()' - Restart an old print job. | |
8962 | */ | |
8963 | ||
8964 | static void | |
8965 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
8966 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
8967 | { | |
8968 | ipp_attribute_t *attr; /* Current attribute */ | |
8969 | int jobid; /* Job ID */ | |
8970 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
8971 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
8972 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
8973 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
8974 | int port; /* Port portion of URI */ | |
8975 | cupsd_job_t *job; /* Job information */ | |
8976 | ||
8977 | ||
8978 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
8979 | con->http.fd, uri->values[0].string.text); | |
8980 | ||
8981 | /* | |
8982 | * See if we have a job URI or a printer URI... | |
8983 | */ | |
8984 | ||
8985 | if (!strcmp(uri->name, "printer-uri")) | |
8986 | { | |
8987 | /* | |
8988 | * Got a printer URI; see if we also have a job-id attribute... | |
8989 | */ | |
8990 | ||
fa73b229 | 8991 | if ((attr = ippFindAttribute(con->request, "job-id", |
8992 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8993 | { |
8994 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8995 | _("Got a printer-uri attribute but no job-id!")); | |
8996 | return; | |
8997 | } | |
8998 | ||
8999 | jobid = attr->values[0].integer; | |
9000 | } | |
9001 | else | |
9002 | { | |
9003 | /* | |
9004 | * Got a job URI; parse it to get the job ID... | |
9005 | */ | |
9006 | ||
a4d04587 | 9007 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9008 | sizeof(method), username, sizeof(username), host, | |
9009 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9010 | |
fa73b229 | 9011 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 9012 | { |
9013 | /* | |
9014 | * Not a valid URI! | |
9015 | */ | |
9016 | ||
9017 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9018 | _("Bad job-uri attribute \"%s\"!"), | |
9019 | uri->values[0].string.text); | |
9020 | return; | |
9021 | } | |
9022 | ||
9023 | jobid = atoi(resource + 6); | |
9024 | } | |
9025 | ||
9026 | /* | |
9027 | * See if the job exists... | |
9028 | */ | |
9029 | ||
9030 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9031 | { | |
9032 | /* | |
9033 | * Nope - return a "not found" error... | |
9034 | */ | |
9035 | ||
9036 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9037 | return; | |
9038 | } | |
9039 | ||
9040 | /* | |
9041 | * See if job is in any of the "completed" states... | |
9042 | */ | |
9043 | ||
bd7854cb | 9044 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 9045 | { |
9046 | /* | |
9047 | * Nope - return a "not possible" error... | |
9048 | */ | |
9049 | ||
9050 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"), | |
9051 | jobid); | |
9052 | return; | |
9053 | } | |
9054 | ||
9055 | /* | |
9056 | * See if we have retained the job files... | |
9057 | */ | |
9058 | ||
bd7854cb | 9059 | cupsdLoadJob(job); |
9060 | ||
07725fee | 9061 | if (!job->attrs || job->num_files == 0) |
ef416fc2 | 9062 | { |
9063 | /* | |
9064 | * Nope - return a "not possible" error... | |
9065 | */ | |
9066 | ||
9067 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
9068 | _("Job #%d cannot be restarted - no files!"), jobid); | |
9069 | return; | |
9070 | } | |
9071 | ||
9072 | /* | |
9073 | * See if the job is owned by the requesting user... | |
9074 | */ | |
9075 | ||
9076 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9077 | { | |
2fb76298 | 9078 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 9079 | return; |
9080 | } | |
9081 | ||
9082 | /* | |
9083 | * Restart the job and return... | |
9084 | */ | |
9085 | ||
9086 | cupsdRestartJob(job); | |
9087 | ||
75bd9771 | 9088 | cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username); |
ef416fc2 | 9089 | |
9090 | con->response->request.status.status_code = IPP_OK; | |
9091 | } | |
9092 | ||
9093 | ||
9094 | /* | |
9095 | * 'save_auth_info()' - Save authentication information for a job. | |
9096 | */ | |
9097 | ||
9098 | static void | |
f7deaa1a | 9099 | save_auth_info( |
9100 | cupsd_client_t *con, /* I - Client connection */ | |
9101 | cupsd_job_t *job, /* I - Job */ | |
9102 | ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */ | |
ef416fc2 | 9103 | { |
09a101d6 | 9104 | int i; /* Looping var */ |
9105 | char filename[1024]; /* Job authentication filename */ | |
9106 | cups_file_t *fp; /* Job authentication file */ | |
9107 | char line[2048]; /* Line for file */ | |
9108 | cupsd_printer_t *dest; /* Destination printer/class */ | |
ef416fc2 | 9109 | |
9110 | ||
9111 | /* | |
9112 | * This function saves the in-memory authentication information for | |
9113 | * a job so that it can be used to authenticate with a remote host. | |
9114 | * The information is stored in a file that is readable only by the | |
f7deaa1a | 9115 | * root user. The fields are Base-64 encoded, each on a separate line, |
9116 | * followed by random number (up to 1024) of newlines to limit the | |
9117 | * amount of information that is exposed. | |
ef416fc2 | 9118 | * |
9119 | * Because of the potential for exposing of authentication information, | |
9120 | * this functionality is only enabled when running cupsd as root. | |
9121 | * | |
9122 | * This caching only works for the Basic and BasicDigest authentication | |
9123 | * types. Digest authentication cannot be cached this way, and in | |
9124 | * the future Kerberos authentication may make all of this obsolete. | |
9125 | * | |
9126 | * Authentication information is saved whenever an authenticated | |
9127 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
9128 | * performed. | |
9129 | * | |
9130 | * This information is deleted after a job is completed or canceled, | |
9131 | * so reprints may require subsequent re-authentication. | |
9132 | */ | |
9133 | ||
9134 | if (RunUser) | |
9135 | return; | |
9136 | ||
09a101d6 | 9137 | if ((dest = cupsdFindDest(job->dest)) == NULL) |
9138 | return; | |
9139 | ||
ef416fc2 | 9140 | /* |
9141 | * Create the authentication file and change permissions... | |
9142 | */ | |
9143 | ||
9144 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
9145 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
9146 | { | |
9147 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9148 | "Unable to save authentication info to \"%s\" - %s", | |
9149 | filename, strerror(errno)); | |
9150 | return; | |
9151 | } | |
9152 | ||
9153 | fchown(cupsFileNumber(fp), 0, 0); | |
9154 | fchmod(cupsFileNumber(fp), 0400); | |
9155 | ||
09a101d6 | 9156 | if (auth_info && auth_info->num_values == dest->num_auth_info_required) |
f7deaa1a | 9157 | { |
9158 | /* | |
09a101d6 | 9159 | * Write 1 to 3 auth values... |
f7deaa1a | 9160 | */ |
ef416fc2 | 9161 | |
09a101d6 | 9162 | cupsdClearString(&job->auth_username); |
9163 | cupsdClearString(&job->auth_domain); | |
9164 | cupsdClearString(&job->auth_password); | |
9165 | ||
f7deaa1a | 9166 | for (i = 0; i < auth_info->num_values; i ++) |
9167 | { | |
9168 | httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, | |
9169 | strlen(auth_info->values[i].string.text)); | |
9170 | cupsFilePrintf(fp, "%s\n", line); | |
09a101d6 | 9171 | |
9172 | if (!strcmp(dest->auth_info_required[i], "username")) | |
839a51c8 | 9173 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", |
09a101d6 | 9174 | auth_info->values[i].string.text); |
9175 | else if (!strcmp(dest->auth_info_required[i], "domain")) | |
839a51c8 | 9176 | cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", |
09a101d6 | 9177 | auth_info->values[i].string.text); |
9178 | else if (!strcmp(dest->auth_info_required[i], "password")) | |
839a51c8 | 9179 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", |
09a101d6 | 9180 | auth_info->values[i].string.text); |
f7deaa1a | 9181 | } |
9182 | } | |
09a101d6 | 9183 | else if (con->username[0]) |
f7deaa1a | 9184 | { |
9185 | /* | |
9186 | * Write the authenticated username... | |
9187 | */ | |
ef416fc2 | 9188 | |
f7deaa1a | 9189 | httpEncode64_2(line, sizeof(line), con->username, strlen(con->username)); |
9190 | cupsFilePrintf(fp, "%s\n", line); | |
ef416fc2 | 9191 | |
839a51c8 | 9192 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username); |
09a101d6 | 9193 | cupsdClearString(&job->auth_domain); |
9194 | ||
f7deaa1a | 9195 | /* |
9196 | * Write the authenticated password... | |
9197 | */ | |
9198 | ||
9199 | httpEncode64_2(line, sizeof(line), con->password, strlen(con->password)); | |
9200 | cupsFilePrintf(fp, "%s\n", line); | |
09a101d6 | 9201 | |
839a51c8 | 9202 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password); |
f7deaa1a | 9203 | } |
ef416fc2 | 9204 | |
9205 | /* | |
9206 | * Write a random number of newlines to the end of the file... | |
9207 | */ | |
9208 | ||
9209 | for (i = (rand() % 1024); i >= 0; i --) | |
9210 | cupsFilePutChar(fp, '\n'); | |
9211 | ||
9212 | /* | |
9213 | * Close the file and return... | |
9214 | */ | |
9215 | ||
9216 | cupsFileClose(fp); | |
f7deaa1a | 9217 | |
9218 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
09a101d6 | 9219 | if (con->gss_have_creds) |
9220 | save_krb5_creds(con, job); | |
9221 | else if (job->ccname) | |
9222 | cupsdClearString(&(job->ccname)); | |
f7deaa1a | 9223 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ |
9224 | } | |
9225 | ||
9226 | ||
9227 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
9228 | /* | |
9229 | * 'save_krb5_creds()' - Save Kerberos credentials for the job. | |
9230 | */ | |
9231 | ||
9232 | static void | |
9233 | save_krb5_creds(cupsd_client_t *con, /* I - Client connection */ | |
9234 | cupsd_job_t *job) /* I - Job */ | |
9235 | { | |
7ff4fea9 MS |
9236 | # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL) |
9237 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9238 | "Sorry, your version of Kerberos does not support delegated " | |
9239 | "credentials!"); | |
9240 | return; | |
9241 | ||
9242 | # else | |
db1f069b MS |
9243 | krb5_error_code error; /* Kerberos error code */ |
9244 | OM_uint32 major_status, /* Major status code */ | |
9245 | minor_status; /* Minor status code */ | |
db1f069b | 9246 | krb5_principal principal; /* Kerberos principal */ |
f7deaa1a | 9247 | |
9248 | ||
7ff4fea9 | 9249 | # ifdef __APPLE__ |
f7deaa1a | 9250 | /* |
c24d2134 MS |
9251 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try |
9252 | * to use it... | |
f7deaa1a | 9253 | */ |
9254 | ||
c24d2134 | 9255 | if (krb5_init_context == NULL) |
f7deaa1a | 9256 | return; |
7ff4fea9 | 9257 | # endif /* __APPLE__ */ |
f7deaa1a | 9258 | |
cc0d019f MS |
9259 | /* |
9260 | * We MUST create a file-based cache because memory-based caches are | |
9261 | * only valid for the current process/address space. | |
7ff4fea9 MS |
9262 | * |
9263 | * Due to various bugs/features in different versions of Kerberos, we | |
9264 | * need either the krb5_cc_new_unique() function or Heimdal's version | |
9265 | * of krb5_cc_gen_new() to create a new FILE: credential cache that | |
9266 | * can be passed to the backend. These functions create a temporary | |
9267 | * file (typically in /tmp) containing the cached credentials, which | |
9268 | * are removed when we have successfully printed a job. | |
9269 | */ | |
9270 | ||
9271 | # ifdef HAVE_KRB5_CC_NEW_UNIQUE | |
9272 | if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL, | |
9273 | &(job->ccache))) != 0) | |
9274 | # else /* HAVE_HEIMDAL */ | |
0a682745 | 9275 | if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops, |
7ff4fea9 MS |
9276 | &(job->ccache))) != 0) |
9277 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE */ | |
9278 | { | |
db1f069b MS |
9279 | cupsdLogMessage(CUPSD_LOG_ERROR, |
9280 | "Unable to create new credentials cache (%d/%s)", | |
7ff4fea9 | 9281 | error, strerror(errno)); |
76cd9e37 | 9282 | job->ccache = NULL; |
f7deaa1a | 9283 | return; |
9284 | } | |
9285 | ||
db1f069b MS |
9286 | if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0) |
9287 | { | |
9288 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)", | |
9289 | error, strerror(errno)); | |
9290 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9291 | job->ccache = NULL; | |
9292 | return; | |
9293 | } | |
9294 | ||
9295 | if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal))) | |
9296 | { | |
9297 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9298 | "Unable to initialize credentials cache (%d/%s)", error, | |
9299 | strerror(errno)); | |
9300 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9301 | krb5_free_principal(KerberosContext, principal); | |
9302 | job->ccache = NULL; | |
9303 | return; | |
9304 | } | |
9305 | ||
9306 | krb5_free_principal(KerberosContext, principal); | |
9307 | ||
7ff4fea9 MS |
9308 | /* |
9309 | * Copy the user's credentials to the new cache file... | |
9310 | */ | |
9311 | ||
f7deaa1a | 9312 | major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred, |
c24d2134 | 9313 | job->ccache); |
f7deaa1a | 9314 | |
9315 | if (GSS_ERROR(major_status)) | |
9316 | { | |
9317 | cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status, | |
9318 | "Unable to import client credentials cache"); | |
c24d2134 | 9319 | krb5_cc_destroy(KerberosContext, job->ccache); |
76cd9e37 | 9320 | job->ccache = NULL; |
f7deaa1a | 9321 | return; |
9322 | } | |
9323 | ||
7ff4fea9 MS |
9324 | /* |
9325 | * Add the KRB5CCNAME environment variable to the job so that the | |
9326 | * backend can use the credentials when printing. | |
9327 | */ | |
9328 | ||
9329 | cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s", | |
c24d2134 | 9330 | krb5_cc_get_name(KerberosContext, job->ccache)); |
cc0d019f | 9331 | |
75bd9771 | 9332 | cupsdLogJob(job, CUPSD_LOG_DEBUG2, "save_krb5_creds: %s", job->ccname); |
7ff4fea9 | 9333 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */ |
ef416fc2 | 9334 | } |
f7deaa1a | 9335 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ |
ef416fc2 | 9336 | |
9337 | ||
9338 | /* | |
9339 | * 'send_document()' - Send a file to a printer or class. | |
9340 | */ | |
9341 | ||
9342 | static void | |
9343 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
9344 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9345 | { | |
9346 | ipp_attribute_t *attr; /* Current attribute */ | |
a0f6818e MS |
9347 | ipp_attribute_t *format; /* Request's document-format attribute */ |
9348 | ipp_attribute_t *jformat; /* Job's document-format attribute */ | |
f7deaa1a | 9349 | const char *default_format;/* document-format-default value */ |
ef416fc2 | 9350 | int jobid; /* Job ID number */ |
9351 | cupsd_job_t *job; /* Current job */ | |
9352 | char job_uri[HTTP_MAX_URI], | |
9353 | /* Job URI */ | |
9354 | method[HTTP_MAX_URI], | |
9355 | /* Method portion of URI */ | |
9356 | username[HTTP_MAX_URI], | |
9357 | /* Username portion of URI */ | |
9358 | host[HTTP_MAX_URI], | |
9359 | /* Host portion of URI */ | |
9360 | resource[HTTP_MAX_URI]; | |
9361 | /* Resource portion of URI */ | |
9362 | int port; /* Port portion of URI */ | |
9363 | mime_type_t *filetype; /* Type of file */ | |
9364 | char super[MIME_MAX_SUPER], | |
9365 | /* Supertype of file */ | |
9366 | type[MIME_MAX_TYPE], | |
9367 | /* Subtype of file */ | |
9368 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
9369 | /* Textual name of mime type */ | |
9370 | char filename[1024]; /* Job filename */ | |
9371 | cupsd_printer_t *printer; /* Current printer */ | |
9372 | struct stat fileinfo; /* File information */ | |
9373 | int kbytes; /* Size of file */ | |
9374 | int compression; /* Type of compression */ | |
75bd9771 | 9375 | int start_job; /* Start the job? */ |
ef416fc2 | 9376 | |
9377 | ||
9378 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
9379 | con->http.fd, uri->values[0].string.text); | |
9380 | ||
9381 | /* | |
9382 | * See if we have a job URI or a printer URI... | |
9383 | */ | |
9384 | ||
9385 | if (!strcmp(uri->name, "printer-uri")) | |
9386 | { | |
9387 | /* | |
9388 | * Got a printer URI; see if we also have a job-id attribute... | |
9389 | */ | |
9390 | ||
fa73b229 | 9391 | if ((attr = ippFindAttribute(con->request, "job-id", |
9392 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9393 | { |
9394 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9395 | _("Got a printer-uri attribute but no job-id!")); | |
9396 | return; | |
9397 | } | |
9398 | ||
9399 | jobid = attr->values[0].integer; | |
9400 | } | |
9401 | else | |
9402 | { | |
9403 | /* | |
9404 | * Got a job URI; parse it to get the job ID... | |
9405 | */ | |
9406 | ||
a4d04587 | 9407 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9408 | sizeof(method), username, sizeof(username), host, | |
9409 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9410 | |
9411 | if (strncmp(resource, "/jobs/", 6)) | |
9412 | { | |
9413 | /* | |
9414 | * Not a valid URI! | |
9415 | */ | |
9416 | ||
9417 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9418 | _("Bad job-uri attribute \"%s\"!"), | |
9419 | uri->values[0].string.text); | |
9420 | return; | |
9421 | } | |
9422 | ||
9423 | jobid = atoi(resource + 6); | |
9424 | } | |
9425 | ||
9426 | /* | |
9427 | * See if the job exists... | |
9428 | */ | |
9429 | ||
9430 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9431 | { | |
9432 | /* | |
9433 | * Nope - return a "not found" error... | |
9434 | */ | |
9435 | ||
9436 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9437 | return; | |
9438 | } | |
9439 | ||
f301802f | 9440 | printer = cupsdFindDest(job->dest); |
9441 | ||
ef416fc2 | 9442 | /* |
9443 | * See if the job is owned by the requesting user... | |
9444 | */ | |
9445 | ||
9446 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9447 | { | |
2fb76298 | 9448 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 9449 | return; |
9450 | } | |
9451 | ||
9452 | /* | |
9453 | * OK, see if the client is sending the document compressed - CUPS | |
9454 | * only supports "none" and "gzip". | |
9455 | */ | |
9456 | ||
9457 | compression = CUPS_FILE_NONE; | |
9458 | ||
fa73b229 | 9459 | if ((attr = ippFindAttribute(con->request, "compression", |
9460 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 9461 | { |
9462 | if (strcmp(attr->values[0].string.text, "none") | |
9463 | #ifdef HAVE_LIBZ | |
9464 | && strcmp(attr->values[0].string.text, "gzip") | |
9465 | #endif /* HAVE_LIBZ */ | |
9466 | ) | |
9467 | { | |
9468 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"), | |
9469 | attr->values[0].string.text); | |
9470 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9471 | "compression", NULL, attr->values[0].string.text); | |
9472 | return; | |
9473 | } | |
9474 | ||
9475 | #ifdef HAVE_LIBZ | |
9476 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
9477 | compression = CUPS_FILE_GZIP; | |
9478 | #endif /* HAVE_LIBZ */ | |
9479 | } | |
9480 | ||
9481 | /* | |
9482 | * Do we have a file to print? | |
9483 | */ | |
9484 | ||
9485 | if (!con->filename) | |
9486 | { | |
9487 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
9488 | return; | |
9489 | } | |
9490 | ||
9491 | /* | |
9492 | * Is it a format we support? | |
9493 | */ | |
9494 | ||
9495 | if ((format = ippFindAttribute(con->request, "document-format", | |
9496 | IPP_TAG_MIMETYPE)) != NULL) | |
9497 | { | |
9498 | /* | |
9499 | * Grab format from client... | |
9500 | */ | |
9501 | ||
bc44d920 | 9502 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", |
9503 | super, type) != 2) | |
ef416fc2 | 9504 | { |
9505 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
9506 | format->values[0].string.text); | |
9507 | return; | |
9508 | } | |
9509 | } | |
f7deaa1a | 9510 | else if ((default_format = cupsGetOption("document-format", |
9511 | printer->num_options, | |
9512 | printer->options)) != NULL) | |
9513 | { | |
9514 | /* | |
9515 | * Use default document format... | |
9516 | */ | |
9517 | ||
9518 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
9519 | { | |
9520 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9521 | _("Could not scan type \"%s\"!"), | |
9522 | default_format); | |
9523 | return; | |
9524 | } | |
9525 | } | |
ef416fc2 | 9526 | else |
9527 | { | |
9528 | /* | |
9529 | * No document format attribute? Auto-type it! | |
9530 | */ | |
9531 | ||
9532 | strcpy(super, "application"); | |
9533 | strcpy(type, "octet-stream"); | |
9534 | } | |
9535 | ||
fa73b229 | 9536 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
ef416fc2 | 9537 | { |
9538 | /* | |
9539 | * Auto-type the file... | |
9540 | */ | |
9541 | ||
bd7854cb | 9542 | ipp_attribute_t *doc_name; /* document-name attribute */ |
9543 | ||
9544 | ||
75bd9771 | 9545 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file..."); |
ef416fc2 | 9546 | |
bd7854cb | 9547 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
9548 | filetype = mimeFileType(MimeDatabase, con->filename, | |
9549 | doc_name ? doc_name->values[0].string.text : NULL, | |
9550 | &compression); | |
ef416fc2 | 9551 | |
f7deaa1a | 9552 | if (!filetype) |
f7faf1f5 | 9553 | filetype = mimeType(MimeDatabase, super, type); |
a0f6818e | 9554 | |
75bd9771 MS |
9555 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.", |
9556 | filetype->super, filetype->type); | |
ed486911 | 9557 | } |
f7faf1f5 | 9558 | else |
9559 | filetype = mimeType(MimeDatabase, super, type); | |
9560 | ||
49d87452 | 9561 | if (filetype) |
f7deaa1a | 9562 | { |
9563 | /* | |
9564 | * Replace the document-format attribute value with the auto-typed or | |
9565 | * default one. | |
9566 | */ | |
9567 | ||
9568 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9569 | filetype->type); | |
9570 | ||
49d87452 MS |
9571 | if ((jformat = ippFindAttribute(job->attrs, "document-format", |
9572 | IPP_TAG_MIMETYPE)) != NULL) | |
f7deaa1a | 9573 | { |
a0f6818e | 9574 | _cupsStrFree(jformat->values[0].string.text); |
f7deaa1a | 9575 | |
a0f6818e | 9576 | jformat->values[0].string.text = _cupsStrAlloc(mimetype); |
f7deaa1a | 9577 | } |
9578 | else | |
a0f6818e | 9579 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
f7deaa1a | 9580 | "document-format", NULL, mimetype); |
9581 | } | |
9582 | else if (!filetype) | |
ef416fc2 | 9583 | { |
9584 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9585 | _("Unsupported format \'%s/%s\'!"), super, type); | |
9586 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9587 | "Hint: Do you have the raw file printing rules enabled?"); | |
9588 | ||
9589 | if (format) | |
9590 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9591 | "document-format", NULL, format->values[0].string.text); | |
9592 | ||
9593 | return; | |
9594 | } | |
9595 | ||
80ca4592 | 9596 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) |
9597 | { | |
9598 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9599 | filetype->type); | |
9600 | ||
9601 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9602 | _("Unsupported format \'%s\'!"), mimetype); | |
9603 | ||
9604 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9605 | "document-format", NULL, mimetype); | |
9606 | ||
9607 | return; | |
9608 | } | |
9609 | ||
ef416fc2 | 9610 | /* |
9611 | * Add the file to the job... | |
9612 | */ | |
9613 | ||
bd7854cb | 9614 | cupsdLoadJob(job); |
9615 | ||
ef416fc2 | 9616 | if (add_file(con, job, filetype, compression)) |
9617 | return; | |
9618 | ||
ef416fc2 | 9619 | if (stat(con->filename, &fileinfo)) |
9620 | kbytes = 0; | |
9621 | else | |
9622 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
9623 | ||
9624 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
9625 | ||
fa73b229 | 9626 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
9627 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 9628 | attr->values[0].integer += kbytes; |
9629 | ||
9630 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
9631 | job->num_files); | |
9632 | rename(con->filename, filename); | |
9633 | ||
9634 | cupsdClearString(&con->filename); | |
9635 | ||
75bd9771 MS |
9636 | cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".", |
9637 | filetype->super, filetype->type, job->username); | |
ef416fc2 | 9638 | |
9639 | /* | |
9640 | * Start the job if this is the last document... | |
9641 | */ | |
9642 | ||
fa73b229 | 9643 | if ((attr = ippFindAttribute(con->request, "last-document", |
9644 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 9645 | attr->values[0].boolean) |
9646 | { | |
9647 | /* | |
9648 | * See if we need to add the ending sheet... | |
9649 | */ | |
9650 | ||
91c84a35 MS |
9651 | if (cupsdTimeoutJob(job)) |
9652 | return; | |
ef416fc2 | 9653 | |
bd7854cb | 9654 | if (job->state_value == IPP_JOB_STOPPED) |
9655 | { | |
ef416fc2 | 9656 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9657 | job->state_value = IPP_JOB_PENDING; |
9658 | } | |
9659 | else if (job->state_value == IPP_JOB_HELD) | |
ef416fc2 | 9660 | { |
fa73b229 | 9661 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9662 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9663 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9664 | ||
fa73b229 | 9665 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
bd7854cb | 9666 | { |
ef416fc2 | 9667 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9668 | job->state_value = IPP_JOB_PENDING; |
9669 | } | |
ef416fc2 | 9670 | } |
9671 | ||
3dfe78b3 MS |
9672 | job->dirty = 1; |
9673 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 9674 | |
75bd9771 | 9675 | start_job = 1; |
ef416fc2 | 9676 | } |
9677 | else | |
9678 | { | |
fa73b229 | 9679 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9680 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9681 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9682 | ||
fa73b229 | 9683 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
ef416fc2 | 9684 | { |
9685 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 9686 | job->state_value = IPP_JOB_HELD; |
ef416fc2 | 9687 | job->hold_until = time(NULL) + 60; |
3dfe78b3 MS |
9688 | job->dirty = 1; |
9689 | ||
9690 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 9691 | } |
75bd9771 MS |
9692 | |
9693 | start_job = 0; | |
ef416fc2 | 9694 | } |
9695 | ||
9696 | /* | |
9697 | * Fill in the response info... | |
9698 | */ | |
9699 | ||
9700 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
9701 | LocalPort, jobid); | |
9702 | ||
9703 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
9704 | job_uri); | |
9705 | ||
9706 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
9707 | ||
9708 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
75bd9771 | 9709 | job->state_value); |
ef416fc2 | 9710 | add_job_state_reasons(con, job); |
9711 | ||
9712 | con->response->request.status.status_code = IPP_OK; | |
75bd9771 MS |
9713 | |
9714 | /* | |
9715 | * Start the job if necessary... | |
9716 | */ | |
9717 | ||
9718 | if (start_job) | |
9719 | cupsdCheckJobs(); | |
ef416fc2 | 9720 | } |
9721 | ||
9722 | ||
9723 | /* | |
9724 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
9725 | */ | |
9726 | ||
9727 | static void | |
f899b121 | 9728 | send_http_error( |
9729 | cupsd_client_t *con, /* I - Client connection */ | |
9730 | http_status_t status, /* I - HTTP status code */ | |
9731 | cupsd_printer_t *printer) /* I - Printer, if any */ | |
ef416fc2 | 9732 | { |
9f5eb9be MS |
9733 | ipp_attribute_t *uri; /* Request URI, if any */ |
9734 | ||
9735 | ||
9736 | if ((uri = ippFindAttribute(con->request, "printer-uri", | |
9737 | IPP_TAG_URI)) == NULL) | |
9738 | uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI); | |
9739 | ||
9740 | cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG, | |
9741 | "Returning HTTP %s for %s (%s) from %s", | |
9742 | httpStatus(status), | |
9743 | ippOpString(con->request->request.op.operation_id), | |
9744 | uri ? uri->values[0].string.text : "no URI", | |
9745 | con->http.hostname); | |
ef416fc2 | 9746 | |
5f64df29 MS |
9747 | if (printer) |
9748 | { | |
db0bd74a | 9749 | int auth_type; /* Type of authentication required */ |
2fb76298 MS |
9750 | |
9751 | ||
5f64df29 MS |
9752 | auth_type = CUPSD_AUTH_NONE; |
9753 | ||
9754 | if (status == HTTP_UNAUTHORIZED && | |
9755 | printer->num_auth_info_required > 0 && | |
9756 | !strcmp(printer->auth_info_required[0], "negotiate") && | |
9757 | con->request && | |
9758 | (con->request->request.op.operation_id == IPP_PRINT_JOB || | |
9759 | con->request->request.op.operation_id == IPP_CREATE_JOB || | |
9760 | con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB)) | |
9761 | { | |
9762 | /* | |
9763 | * Creating and authenticating jobs requires Kerberos... | |
9764 | */ | |
9765 | ||
9766 | auth_type = CUPSD_AUTH_NEGOTIATE; | |
9767 | } | |
db0bd74a | 9768 | else |
5f64df29 MS |
9769 | { |
9770 | /* | |
9771 | * Use policy/location-defined authentication requirements... | |
9772 | */ | |
9773 | ||
9774 | char resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9775 | cupsd_location_t *auth; /* Pointer to authentication element */ | |
9776 | ||
9777 | ||
9778 | if (printer->type & CUPS_PRINTER_CLASS) | |
9779 | snprintf(resource, sizeof(resource), "/classes/%s", printer->name); | |
9780 | else | |
9781 | snprintf(resource, sizeof(resource), "/printers/%s", printer->name); | |
9782 | ||
9783 | if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL || | |
9784 | auth->type == CUPSD_AUTH_NONE) | |
9785 | auth = cupsdFindPolicyOp(printer->op_policy_ptr, | |
9786 | con->request ? | |
9787 | con->request->request.op.operation_id : | |
9788 | IPP_PRINT_JOB); | |
9789 | ||
9790 | if (auth) | |
9791 | { | |
9792 | if (auth->type == CUPSD_AUTH_DEFAULT) | |
9793 | auth_type = DefaultAuthType; | |
9794 | else | |
9795 | auth_type = auth->type; | |
9796 | } | |
9797 | } | |
2fb76298 | 9798 | |
db0bd74a | 9799 | cupsdSendError(con, status, auth_type); |
2fb76298 | 9800 | } |
f899b121 | 9801 | else |
5bd77a73 | 9802 | cupsdSendError(con, status, CUPSD_AUTH_NONE); |
ef416fc2 | 9803 | |
9804 | ippDelete(con->response); | |
9805 | con->response = NULL; | |
9806 | ||
9807 | return; | |
9808 | } | |
9809 | ||
9810 | ||
9811 | /* | |
9812 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
9813 | */ | |
9814 | ||
9815 | static void | |
9816 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
1f0275e3 MS |
9817 | ipp_status_t status, /* I - IPP status code */ |
9818 | const char *message,/* I - Status message */ | |
9819 | ...) /* I - Additional args as needed */ | |
ef416fc2 | 9820 | { |
9821 | va_list ap; /* Pointer to additional args */ | |
9822 | char formatted[1024]; /* Formatted errror message */ | |
9823 | ||
9824 | ||
3d8365b8 | 9825 | va_start(ap, message); |
9826 | vsnprintf(formatted, sizeof(formatted), | |
9827 | _cupsLangString(con->language, message), ap); | |
9828 | va_end(ap); | |
ef416fc2 | 9829 | |
3d8365b8 | 9830 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s", |
9831 | ippOpString(con->request->request.op.operation_id), | |
9832 | ippErrorString(status), formatted); | |
ef416fc2 | 9833 | |
9834 | con->response->request.status.status_code = status; | |
9835 | ||
fa73b229 | 9836 | if (ippFindAttribute(con->response, "attributes-charset", |
9837 | IPP_TAG_ZERO) == NULL) | |
ef416fc2 | 9838 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
9839 | "attributes-charset", NULL, DefaultCharset); | |
9840 | ||
9841 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
9842 | IPP_TAG_ZERO) == NULL) | |
9843 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
9844 | "attributes-natural-language", NULL, DefaultLanguage); | |
9845 | ||
3d8365b8 | 9846 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
9847 | "status-message", NULL, formatted); | |
ef416fc2 | 9848 | } |
9849 | ||
9850 | ||
9851 | /* | |
9852 | * 'set_default()' - Set the default destination... | |
9853 | */ | |
9854 | ||
9855 | static void | |
9856 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
9857 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9858 | { | |
9859 | http_status_t status; /* Policy status */ | |
bc44d920 | 9860 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
dd1abb6b MS |
9861 | cupsd_printer_t *printer, /* Printer */ |
9862 | *oldprinter; /* Old default printer */ | |
ef416fc2 | 9863 | |
9864 | ||
9865 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
9866 | con->http.fd, uri->values[0].string.text); | |
9867 | ||
9868 | /* | |
9869 | * Is the destination valid? | |
9870 | */ | |
9871 | ||
f7deaa1a | 9872 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 9873 | { |
9874 | /* | |
9875 | * Bad URI... | |
9876 | */ | |
9877 | ||
9878 | send_ipp_status(con, IPP_NOT_FOUND, | |
9879 | _("The printer or class was not found.")); | |
9880 | return; | |
9881 | } | |
9882 | ||
9883 | /* | |
9884 | * Check policy... | |
9885 | */ | |
9886 | ||
9887 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
9888 | { | |
f899b121 | 9889 | send_http_error(con, status, NULL); |
ef416fc2 | 9890 | return; |
9891 | } | |
9892 | ||
9893 | /* | |
9894 | * Set it as the default... | |
9895 | */ | |
9896 | ||
dd1abb6b | 9897 | oldprinter = DefaultPrinter; |
ef416fc2 | 9898 | DefaultPrinter = printer; |
9899 | ||
dd1abb6b MS |
9900 | if (oldprinter) |
9901 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL, | |
9902 | "%s is no longer the default printer.", oldprinter->name); | |
9903 | ||
9904 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, | |
9905 | "%s is now the default printer.", printer->name); | |
9906 | ||
3dfe78b3 MS |
9907 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES | |
9908 | CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP); | |
ef416fc2 | 9909 | |
9910 | cupsdLogMessage(CUPSD_LOG_INFO, | |
f7deaa1a | 9911 | "Default destination set to \"%s\" by \"%s\".", |
9912 | printer->name, get_username(con)); | |
ef416fc2 | 9913 | |
9914 | /* | |
9915 | * Everything was ok, so return OK status... | |
9916 | */ | |
9917 | ||
9918 | con->response->request.status.status_code = IPP_OK; | |
9919 | } | |
9920 | ||
9921 | ||
9922 | /* | |
9923 | * 'set_job_attrs()' - Set job attributes. | |
9924 | */ | |
9925 | ||
9926 | static void | |
9927 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
9928 | ipp_attribute_t *uri) /* I - Job URI */ | |
9929 | { | |
9930 | ipp_attribute_t *attr, /* Current attribute */ | |
9931 | *attr2; /* Job attribute */ | |
9932 | int jobid; /* Job ID */ | |
9933 | cupsd_job_t *job; /* Current job */ | |
9934 | char method[HTTP_MAX_URI], | |
9935 | /* Method portion of URI */ | |
9936 | username[HTTP_MAX_URI], | |
9937 | /* Username portion of URI */ | |
9938 | host[HTTP_MAX_URI], | |
9939 | /* Host portion of URI */ | |
9940 | resource[HTTP_MAX_URI]; | |
9941 | /* Resource portion of URI */ | |
9942 | int port; /* Port portion of URI */ | |
d09495fa | 9943 | int event; /* Events? */ |
005dd1eb | 9944 | int check_jobs; /* Check jobs? */ |
ef416fc2 | 9945 | |
9946 | ||
9947 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
9948 | con->http.fd, uri->values[0].string.text); | |
9949 | ||
9950 | /* | |
9951 | * Start with "everything is OK" status... | |
9952 | */ | |
9953 | ||
9954 | con->response->request.status.status_code = IPP_OK; | |
9955 | ||
9956 | /* | |
9957 | * See if we have a job URI or a printer URI... | |
9958 | */ | |
9959 | ||
fa73b229 | 9960 | if (!strcmp(uri->name, "printer-uri")) |
ef416fc2 | 9961 | { |
9962 | /* | |
9963 | * Got a printer URI; see if we also have a job-id attribute... | |
9964 | */ | |
9965 | ||
fa73b229 | 9966 | if ((attr = ippFindAttribute(con->request, "job-id", |
9967 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9968 | { |
9969 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9970 | _("Got a printer-uri attribute but no job-id!")); | |
9971 | return; | |
9972 | } | |
9973 | ||
9974 | jobid = attr->values[0].integer; | |
9975 | } | |
9976 | else | |
9977 | { | |
9978 | /* | |
9979 | * Got a job URI; parse it to get the job ID... | |
9980 | */ | |
9981 | ||
a4d04587 | 9982 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9983 | sizeof(method), username, sizeof(username), host, | |
9984 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9985 | |
fa73b229 | 9986 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 9987 | { |
9988 | /* | |
9989 | * Not a valid URI! | |
9990 | */ | |
9991 | ||
9992 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9993 | _("Bad job-uri attribute \"%s\"!"), | |
9994 | uri->values[0].string.text); | |
9995 | return; | |
9996 | } | |
9997 | ||
9998 | jobid = atoi(resource + 6); | |
9999 | } | |
10000 | ||
10001 | /* | |
10002 | * See if the job exists... | |
10003 | */ | |
10004 | ||
10005 | if ((job = cupsdFindJob(jobid)) == NULL) | |
10006 | { | |
10007 | /* | |
10008 | * Nope - return a "not found" error... | |
10009 | */ | |
10010 | ||
10011 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
10012 | return; | |
10013 | } | |
10014 | ||
10015 | /* | |
10016 | * See if the job has been completed... | |
10017 | */ | |
10018 | ||
bd7854cb | 10019 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 10020 | { |
10021 | /* | |
10022 | * Return a "not-possible" error... | |
10023 | */ | |
10024 | ||
10025 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10026 | _("Job #%d is finished and cannot be altered!"), jobid); | |
10027 | return; | |
10028 | } | |
10029 | ||
10030 | /* | |
10031 | * See if the job is owned by the requesting user... | |
10032 | */ | |
10033 | ||
10034 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
10035 | { | |
2fb76298 | 10036 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 10037 | return; |
10038 | } | |
10039 | ||
10040 | /* | |
10041 | * See what the user wants to change. | |
10042 | */ | |
10043 | ||
bd7854cb | 10044 | cupsdLoadJob(job); |
10045 | ||
005dd1eb MS |
10046 | check_jobs = 0; |
10047 | event = 0; | |
d09495fa | 10048 | |
fa73b229 | 10049 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 10050 | { |
10051 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
10052 | continue; | |
10053 | ||
10054 | if (!strcmp(attr->name, "attributes-charset") || | |
10055 | !strcmp(attr->name, "attributes-natural-language") || | |
10056 | !strcmp(attr->name, "document-compression") || | |
10057 | !strcmp(attr->name, "document-format") || | |
10058 | !strcmp(attr->name, "job-detailed-status-messages") || | |
10059 | !strcmp(attr->name, "job-document-access-errors") || | |
10060 | !strcmp(attr->name, "job-id") || | |
5d6412a9 | 10061 | !strcmp(attr->name, "job-impressions-completed") || |
ef416fc2 | 10062 | !strcmp(attr->name, "job-k-octets") || |
10063 | !strcmp(attr->name, "job-originating-host-name") || | |
10064 | !strcmp(attr->name, "job-originating-user-name") || | |
10065 | !strcmp(attr->name, "job-printer-up-time") || | |
10066 | !strcmp(attr->name, "job-printer-uri") || | |
10067 | !strcmp(attr->name, "job-sheets") || | |
10068 | !strcmp(attr->name, "job-state-message") || | |
10069 | !strcmp(attr->name, "job-state-reasons") || | |
10070 | !strcmp(attr->name, "job-uri") || | |
10071 | !strcmp(attr->name, "number-of-documents") || | |
10072 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
10073 | !strcmp(attr->name, "output-device-assigned") || | |
10074 | !strncmp(attr->name, "date-time-at-", 13) || | |
ef416fc2 | 10075 | !strncmp(attr->name, "job-k-octets", 12) || |
10076 | !strncmp(attr->name, "job-media-sheets", 16) || | |
10077 | !strncmp(attr->name, "time-at-", 8)) | |
10078 | { | |
10079 | /* | |
10080 | * Read-only attrs! | |
10081 | */ | |
10082 | ||
10083 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
10084 | _("%s cannot be changed."), attr->name); | |
10085 | ||
10086 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10087 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10088 | ||
10089 | continue; | |
10090 | } | |
10091 | ||
10092 | if (!strcmp(attr->name, "job-priority")) | |
10093 | { | |
10094 | /* | |
10095 | * Change the job priority... | |
10096 | */ | |
10097 | ||
10098 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10099 | { | |
10100 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!")); | |
10101 | ||
10102 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10103 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10104 | } | |
bd7854cb | 10105 | else if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 10106 | { |
10107 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10108 | _("Job is completed and cannot be changed.")); | |
10109 | return; | |
10110 | } | |
10111 | else if (con->response->request.status.status_code == IPP_OK) | |
d09495fa | 10112 | { |
005dd1eb MS |
10113 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d", |
10114 | attr->values[0].integer); | |
ef416fc2 | 10115 | cupsdSetJobPriority(job, attr->values[0].integer); |
005dd1eb MS |
10116 | |
10117 | check_jobs = 1; | |
10118 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
10119 | CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED; | |
d09495fa | 10120 | } |
ef416fc2 | 10121 | } |
10122 | else if (!strcmp(attr->name, "job-state")) | |
10123 | { | |
10124 | /* | |
10125 | * Change the job state... | |
10126 | */ | |
10127 | ||
10128 | if (attr->value_tag != IPP_TAG_ENUM) | |
10129 | { | |
10130 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!")); | |
10131 | ||
10132 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10133 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10134 | } | |
10135 | else | |
10136 | { | |
10137 | switch (attr->values[0].integer) | |
10138 | { | |
10139 | case IPP_JOB_PENDING : | |
10140 | case IPP_JOB_HELD : | |
bd7854cb | 10141 | if (job->state_value > IPP_JOB_HELD) |
ef416fc2 | 10142 | { |
10143 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10144 | _("Job state cannot be changed.")); | |
10145 | return; | |
10146 | } | |
10147 | else if (con->response->request.status.status_code == IPP_OK) | |
bd7854cb | 10148 | { |
005dd1eb MS |
10149 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d", |
10150 | attr->values[0].integer); | |
10151 | ||
ef416fc2 | 10152 | job->state->values[0].integer = attr->values[0].integer; |
bc44d920 | 10153 | job->state_value = (ipp_jstate_t)attr->values[0].integer; |
d09495fa | 10154 | |
10155 | event |= CUPSD_EVENT_JOB_STATE; | |
005dd1eb | 10156 | check_jobs = 1; |
bd7854cb | 10157 | } |
ef416fc2 | 10158 | break; |
10159 | ||
10160 | case IPP_JOB_PROCESSING : | |
10161 | case IPP_JOB_STOPPED : | |
bd7854cb | 10162 | if (job->state_value != attr->values[0].integer) |
ef416fc2 | 10163 | { |
10164 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10165 | _("Job state cannot be changed.")); | |
10166 | return; | |
10167 | } | |
10168 | break; | |
10169 | ||
d09495fa | 10170 | case IPP_JOB_CANCELED : |
ef416fc2 | 10171 | case IPP_JOB_ABORTED : |
10172 | case IPP_JOB_COMPLETED : | |
bd7854cb | 10173 | if (job->state_value > IPP_JOB_PROCESSING) |
ef416fc2 | 10174 | { |
10175 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10176 | _("Job state cannot be changed.")); | |
10177 | return; | |
10178 | } | |
10179 | else if (con->response->request.status.status_code == IPP_OK) | |
005dd1eb MS |
10180 | { |
10181 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d", | |
10182 | attr->values[0].integer); | |
07725fee | 10183 | cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer); |
005dd1eb MS |
10184 | |
10185 | check_jobs = 1; | |
10186 | } | |
ef416fc2 | 10187 | break; |
10188 | } | |
10189 | } | |
10190 | } | |
10191 | else if (con->response->request.status.status_code != IPP_OK) | |
10192 | continue; | |
fa73b229 | 10193 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, |
10194 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 10195 | { |
10196 | /* | |
10197 | * Some other value; first free the old value... | |
10198 | */ | |
10199 | ||
10200 | if (job->attrs->prev) | |
10201 | job->attrs->prev->next = attr2->next; | |
10202 | else | |
10203 | job->attrs->attrs = attr2->next; | |
10204 | ||
10205 | if (job->attrs->last == attr2) | |
10206 | job->attrs->last = job->attrs->prev; | |
10207 | ||
757d2cad | 10208 | _ippFreeAttr(attr2); |
ef416fc2 | 10209 | |
10210 | /* | |
10211 | * Then copy the attribute... | |
10212 | */ | |
10213 | ||
10214 | copy_attribute(job->attrs, attr, 0); | |
10215 | ||
10216 | /* | |
10217 | * See if the job-name or job-hold-until is being changed. | |
10218 | */ | |
10219 | ||
10220 | if (!strcmp(attr->name, "job-hold-until")) | |
10221 | { | |
005dd1eb MS |
10222 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s", |
10223 | attr->values[0].string.text); | |
ef416fc2 | 10224 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
10225 | ||
10226 | if (!strcmp(attr->values[0].string.text, "no-hold")) | |
10227 | cupsdReleaseJob(job); | |
10228 | else | |
10229 | cupsdHoldJob(job); | |
d09495fa | 10230 | |
005dd1eb MS |
10231 | check_jobs = 1; |
10232 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE; | |
ef416fc2 | 10233 | } |
10234 | } | |
10235 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
10236 | { | |
10237 | /* | |
10238 | * Delete the attribute... | |
10239 | */ | |
10240 | ||
10241 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
10242 | IPP_TAG_ZERO)) != NULL) | |
10243 | { | |
10244 | if (job->attrs->prev) | |
10245 | job->attrs->prev->next = attr2->next; | |
10246 | else | |
10247 | job->attrs->attrs = attr2->next; | |
10248 | ||
10249 | if (attr2 == job->attrs->last) | |
10250 | job->attrs->last = job->attrs->prev; | |
10251 | ||
757d2cad | 10252 | _ippFreeAttr(attr2); |
d09495fa | 10253 | |
10254 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10255 | } |
10256 | } | |
10257 | else | |
10258 | { | |
10259 | /* | |
10260 | * Add new option by copying it... | |
10261 | */ | |
10262 | ||
10263 | copy_attribute(job->attrs, attr, 0); | |
d09495fa | 10264 | |
10265 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10266 | } |
10267 | } | |
10268 | ||
10269 | /* | |
10270 | * Save the job... | |
10271 | */ | |
10272 | ||
3dfe78b3 MS |
10273 | job->dirty = 1; |
10274 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 10275 | |
d09495fa | 10276 | /* |
10277 | * Send events as needed... | |
10278 | */ | |
10279 | ||
d9bca400 | 10280 | if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED) |
01ce6322 MS |
10281 | cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED, |
10282 | cupsdFindDest(job->dest), job, | |
d9bca400 MS |
10283 | "Job priority changed by user."); |
10284 | ||
d09495fa | 10285 | if (event & CUPSD_EVENT_JOB_STATE) |
01ce6322 | 10286 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 10287 | job->state_value == IPP_JOB_HELD ? |
10288 | "Job held by user." : "Job restarted by user."); | |
10289 | ||
10290 | if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED) | |
01ce6322 | 10291 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 10292 | "Job options changed by user."); |
10293 | ||
ef416fc2 | 10294 | /* |
10295 | * Start jobs if possible... | |
10296 | */ | |
10297 | ||
005dd1eb MS |
10298 | if (check_jobs) |
10299 | cupsdCheckJobs(); | |
ef416fc2 | 10300 | } |
10301 | ||
10302 | ||
b423cd4c | 10303 | /* |
10304 | * 'set_printer_defaults()' - Set printer default options from a request. | |
10305 | */ | |
10306 | ||
10307 | static void | |
10308 | set_printer_defaults( | |
10309 | cupsd_client_t *con, /* I - Client connection */ | |
10310 | cupsd_printer_t *printer) /* I - Printer */ | |
10311 | { | |
10312 | int i; /* Looping var */ | |
10313 | ipp_attribute_t *attr; /* Current attribute */ | |
10314 | int namelen; /* Length of attribute name */ | |
10315 | char name[256], /* New attribute name */ | |
10316 | value[256]; /* String version of integer attrs */ | |
10317 | ||
10318 | ||
10319 | for (attr = con->request->attrs; attr; attr = attr->next) | |
10320 | { | |
10321 | /* | |
10322 | * Skip non-printer attributes... | |
10323 | */ | |
10324 | ||
10325 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
10326 | continue; | |
10327 | ||
10328 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
10329 | ||
10330 | if (!strcmp(attr->name, "job-sheets-default")) | |
10331 | { | |
10332 | /* | |
10333 | * Only allow keywords and names... | |
10334 | */ | |
10335 | ||
10336 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10337 | continue; | |
10338 | ||
10339 | /* | |
10340 | * Only allow job-sheets-default to be set when running without a | |
10341 | * system high classification level... | |
10342 | */ | |
10343 | ||
10344 | if (Classification) | |
10345 | continue; | |
10346 | ||
10347 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
10348 | ||
10349 | if (attr->num_values > 1) | |
10350 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
10351 | else | |
10352 | cupsdSetString(&printer->job_sheets[1], "none"); | |
10353 | } | |
10354 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
10355 | { | |
10356 | cupsdFreePrinterUsers(printer); | |
10357 | ||
10358 | printer->deny_users = 0; | |
10359 | ||
10360 | if (attr->value_tag == IPP_TAG_NAME && | |
10361 | (attr->num_values > 1 || | |
10362 | strcmp(attr->values[0].string.text, "all"))) | |
10363 | { | |
10364 | for (i = 0; i < attr->num_values; i ++) | |
10365 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10366 | } | |
10367 | } | |
10368 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
10369 | { | |
10370 | cupsdFreePrinterUsers(printer); | |
10371 | ||
10372 | printer->deny_users = 1; | |
10373 | ||
10374 | if (attr->value_tag == IPP_TAG_NAME && | |
10375 | (attr->num_values > 1 || | |
10376 | strcmp(attr->values[0].string.text, "none"))) | |
10377 | { | |
10378 | for (i = 0; i < attr->num_values; i ++) | |
10379 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10380 | } | |
10381 | } | |
10382 | else if (!strcmp(attr->name, "job-quota-period")) | |
10383 | { | |
10384 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10385 | continue; | |
10386 | ||
10387 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
10388 | attr->values[0].integer); | |
10389 | cupsdFreeQuotas(printer); | |
10390 | ||
10391 | printer->quota_period = attr->values[0].integer; | |
10392 | } | |
10393 | else if (!strcmp(attr->name, "job-k-limit")) | |
10394 | { | |
10395 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10396 | continue; | |
10397 | ||
10398 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
10399 | attr->values[0].integer); | |
10400 | cupsdFreeQuotas(printer); | |
10401 | ||
10402 | printer->k_limit = attr->values[0].integer; | |
10403 | } | |
10404 | else if (!strcmp(attr->name, "job-page-limit")) | |
10405 | { | |
10406 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10407 | continue; | |
10408 | ||
10409 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
10410 | attr->values[0].integer); | |
10411 | cupsdFreeQuotas(printer); | |
10412 | ||
10413 | printer->page_limit = attr->values[0].integer; | |
10414 | } | |
10415 | else if (!strcmp(attr->name, "printer-op-policy")) | |
10416 | { | |
10417 | cupsd_policy_t *p; /* Policy */ | |
10418 | ||
10419 | ||
10420 | if (attr->value_tag != IPP_TAG_NAME) | |
10421 | continue; | |
10422 | ||
10423 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
10424 | { | |
10425 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10426 | "Setting printer-op-policy to \"%s\"...", | |
10427 | attr->values[0].string.text); | |
10428 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
10429 | printer->op_policy_ptr = p; | |
10430 | } | |
10431 | else | |
10432 | { | |
10433 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10434 | _("Unknown printer-op-policy \"%s\"."), | |
10435 | attr->values[0].string.text); | |
10436 | return; | |
10437 | } | |
10438 | } | |
10439 | else if (!strcmp(attr->name, "printer-error-policy")) | |
10440 | { | |
10441 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10442 | continue; | |
10443 | ||
10444 | if (strcmp(attr->values[0].string.text, "abort-job") && | |
ee571f26 | 10445 | strcmp(attr->values[0].string.text, "retry-current-job") && |
b423cd4c | 10446 | strcmp(attr->values[0].string.text, "retry-job") && |
10447 | strcmp(attr->values[0].string.text, "stop-printer")) | |
10448 | { | |
10449 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10450 | _("Unknown printer-error-policy \"%s\"."), | |
10451 | attr->values[0].string.text); | |
10452 | return; | |
10453 | } | |
10454 | ||
10455 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10456 | "Setting printer-error-policy to \"%s\"...", | |
10457 | attr->values[0].string.text); | |
10458 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
10459 | } | |
b423cd4c | 10460 | |
10461 | /* | |
10462 | * Skip any other non-default attributes... | |
10463 | */ | |
10464 | ||
10465 | namelen = strlen(attr->name); | |
10466 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
10467 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
10468 | continue; | |
10469 | ||
10470 | /* | |
10471 | * OK, anything else must be a user-defined default... | |
10472 | */ | |
10473 | ||
10474 | strlcpy(name, attr->name, sizeof(name)); | |
10475 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
10476 | ||
10477 | switch (attr->value_tag) | |
10478 | { | |
10479 | case IPP_TAG_DELETEATTR : | |
10480 | printer->num_options = cupsRemoveOption(name, | |
10481 | printer->num_options, | |
10482 | &(printer->options)); | |
10483 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10484 | "Deleting %s", attr->name); | |
10485 | break; | |
10486 | ||
10487 | case IPP_TAG_NAME : | |
10488 | case IPP_TAG_KEYWORD : | |
10489 | case IPP_TAG_URI : | |
10490 | printer->num_options = cupsAddOption(name, | |
10491 | attr->values[0].string.text, | |
10492 | printer->num_options, | |
10493 | &(printer->options)); | |
10494 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10495 | "Setting %s to \"%s\"...", attr->name, | |
10496 | attr->values[0].string.text); | |
10497 | break; | |
10498 | ||
10499 | case IPP_TAG_BOOLEAN : | |
10500 | printer->num_options = cupsAddOption(name, | |
10501 | attr->values[0].boolean ? | |
10502 | "true" : "false", | |
10503 | printer->num_options, | |
10504 | &(printer->options)); | |
10505 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10506 | "Setting %s to %s...", attr->name, | |
10507 | attr->values[0].boolean ? "true" : "false"); | |
10508 | break; | |
10509 | ||
10510 | case IPP_TAG_INTEGER : | |
10511 | case IPP_TAG_ENUM : | |
10512 | sprintf(value, "%d", attr->values[0].integer); | |
10513 | printer->num_options = cupsAddOption(name, value, | |
10514 | printer->num_options, | |
10515 | &(printer->options)); | |
10516 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10517 | "Setting %s to %s...", attr->name, value); | |
10518 | break; | |
10519 | ||
10520 | case IPP_TAG_RANGE : | |
10521 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
10522 | attr->values[0].range.upper); | |
10523 | printer->num_options = cupsAddOption(name, value, | |
10524 | printer->num_options, | |
10525 | &(printer->options)); | |
10526 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10527 | "Setting %s to %s...", attr->name, value); | |
10528 | break; | |
10529 | ||
10530 | case IPP_TAG_RESOLUTION : | |
10531 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
10532 | attr->values[0].resolution.yres, | |
10533 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
10534 | "dpi" : "dpc"); | |
10535 | printer->num_options = cupsAddOption(name, value, | |
10536 | printer->num_options, | |
10537 | &(printer->options)); | |
10538 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10539 | "Setting %s to %s...", attr->name, value); | |
10540 | break; | |
10541 | ||
10542 | default : | |
10543 | /* Do nothing for other values */ | |
10544 | break; | |
10545 | } | |
10546 | } | |
10547 | } | |
10548 | ||
10549 | ||
ef416fc2 | 10550 | /* |
10551 | * 'start_printer()' - Start a printer. | |
10552 | */ | |
10553 | ||
10554 | static void | |
10555 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
10556 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10557 | { | |
10558 | http_status_t status; /* Policy status */ | |
bc44d920 | 10559 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10560 | cupsd_printer_t *printer; /* Printer data */ |
10561 | ||
10562 | ||
10563 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
10564 | con->http.fd, uri->values[0].string.text); | |
10565 | ||
10566 | /* | |
10567 | * Is the destination valid? | |
10568 | */ | |
10569 | ||
f7deaa1a | 10570 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10571 | { |
10572 | /* | |
10573 | * Bad URI... | |
10574 | */ | |
10575 | ||
10576 | send_ipp_status(con, IPP_NOT_FOUND, | |
10577 | _("The printer or class was not found.")); | |
10578 | return; | |
10579 | } | |
10580 | ||
10581 | /* | |
10582 | * Check policy... | |
10583 | */ | |
10584 | ||
10585 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10586 | { | |
f899b121 | 10587 | send_http_error(con, status, printer); |
ef416fc2 | 10588 | return; |
10589 | } | |
10590 | ||
10591 | /* | |
10592 | * Start the printer... | |
10593 | */ | |
10594 | ||
10595 | printer->state_message[0] = '\0'; | |
10596 | ||
10597 | cupsdStartPrinter(printer, 1); | |
10598 | ||
10599 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 10600 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", |
10601 | printer->name, get_username(con)); | |
e00b005a | 10602 | else |
f7deaa1a | 10603 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", |
10604 | printer->name, get_username(con)); | |
ef416fc2 | 10605 | |
10606 | cupsdCheckJobs(); | |
10607 | ||
10608 | /* | |
10609 | * Everything was ok, so return OK status... | |
10610 | */ | |
10611 | ||
10612 | con->response->request.status.status_code = IPP_OK; | |
10613 | } | |
10614 | ||
10615 | ||
10616 | /* | |
10617 | * 'stop_printer()' - Stop a printer. | |
10618 | */ | |
10619 | ||
10620 | static void | |
10621 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
10622 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10623 | { | |
10624 | http_status_t status; /* Policy status */ | |
bc44d920 | 10625 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10626 | cupsd_printer_t *printer; /* Printer data */ |
10627 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
10628 | ||
10629 | ||
10630 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
10631 | con->http.fd, uri->values[0].string.text); | |
10632 | ||
10633 | /* | |
10634 | * Is the destination valid? | |
10635 | */ | |
10636 | ||
f7deaa1a | 10637 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10638 | { |
10639 | /* | |
10640 | * Bad URI... | |
10641 | */ | |
10642 | ||
10643 | send_ipp_status(con, IPP_NOT_FOUND, | |
10644 | _("The printer or class was not found.")); | |
10645 | return; | |
10646 | } | |
10647 | ||
10648 | /* | |
10649 | * Check policy... | |
10650 | */ | |
10651 | ||
10652 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10653 | { | |
f899b121 | 10654 | send_http_error(con, status, printer); |
ef416fc2 | 10655 | return; |
10656 | } | |
10657 | ||
10658 | /* | |
10659 | * Stop the printer... | |
10660 | */ | |
10661 | ||
10662 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
10663 | IPP_TAG_TEXT)) == NULL) | |
10664 | strcpy(printer->state_message, "Paused"); | |
10665 | else | |
10666 | { | |
10667 | strlcpy(printer->state_message, attr->values[0].string.text, | |
10668 | sizeof(printer->state_message)); | |
10669 | } | |
10670 | ||
10671 | cupsdStopPrinter(printer, 1); | |
10672 | ||
10673 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 10674 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", |
10675 | printer->name, get_username(con)); | |
ef416fc2 | 10676 | else |
f7deaa1a | 10677 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", |
10678 | printer->name, get_username(con)); | |
ef416fc2 | 10679 | |
10680 | /* | |
10681 | * Everything was ok, so return OK status... | |
10682 | */ | |
10683 | ||
10684 | con->response->request.status.status_code = IPP_OK; | |
10685 | } | |
10686 | ||
10687 | ||
89d46774 | 10688 | /* |
10689 | * 'url_encode_attr()' - URL-encode a string attribute. | |
10690 | */ | |
10691 | ||
10692 | static void | |
10693 | url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */ | |
10694 | char *buffer,/* I - String buffer */ | |
10695 | int bufsize)/* I - Size of buffer */ | |
10696 | { | |
10697 | int i; /* Looping var */ | |
10698 | char *bufptr, /* Pointer into buffer */ | |
b94498cf | 10699 | *bufend; /* End of buffer */ |
89d46774 | 10700 | |
10701 | ||
10702 | strlcpy(buffer, attr->name, bufsize); | |
10703 | bufptr = buffer + strlen(buffer); | |
10704 | bufend = buffer + bufsize - 1; | |
10705 | ||
10706 | for (i = 0; i < attr->num_values; i ++) | |
10707 | { | |
10708 | if (bufptr >= bufend) | |
10709 | break; | |
10710 | ||
10711 | if (i) | |
10712 | *bufptr++ = ','; | |
10713 | else | |
10714 | *bufptr++ = '='; | |
10715 | ||
10716 | if (bufptr >= bufend) | |
10717 | break; | |
10718 | ||
10719 | *bufptr++ = '\''; | |
10720 | ||
b94498cf | 10721 | bufptr = url_encode_string(attr->values[i].string.text, |
10722 | bufptr, bufend - bufptr + 1); | |
89d46774 | 10723 | |
10724 | if (bufptr >= bufend) | |
10725 | break; | |
10726 | ||
10727 | *bufptr++ = '\''; | |
10728 | } | |
10729 | ||
10730 | *bufptr = '\0'; | |
10731 | } | |
10732 | ||
10733 | ||
b94498cf | 10734 | /* |
10735 | * 'url_encode_string()' - URL-encode a string. | |
10736 | */ | |
10737 | ||
10738 | static char * /* O - End of string */ | |
10739 | url_encode_string(const char *s, /* I - String */ | |
10740 | char *buffer, /* I - String buffer */ | |
10741 | int bufsize) /* I - Size of buffer */ | |
10742 | { | |
10743 | char *bufptr, /* Pointer into buffer */ | |
10744 | *bufend; /* End of buffer */ | |
10745 | static const char *hex = "0123456789ABCDEF"; | |
10746 | /* Hex digits */ | |
10747 | ||
10748 | ||
10749 | bufptr = buffer; | |
10750 | bufend = buffer + bufsize - 1; | |
10751 | ||
10752 | while (*s && bufptr < bufend) | |
10753 | { | |
10754 | if (*s == ' ' || *s == '%') | |
10755 | { | |
10756 | if (bufptr >= (bufend - 2)) | |
10757 | break; | |
10758 | ||
10759 | *bufptr++ = '%'; | |
10760 | *bufptr++ = hex[(*s >> 4) & 15]; | |
10761 | *bufptr++ = hex[*s & 15]; | |
10762 | ||
10763 | s ++; | |
10764 | } | |
10765 | else if (*s == '\'' || *s == '\\') | |
10766 | { | |
10767 | if (bufptr >= (bufend - 1)) | |
10768 | break; | |
10769 | ||
10770 | *bufptr++ = '\\'; | |
10771 | *bufptr++ = *s++; | |
10772 | } | |
10773 | else | |
10774 | *bufptr++ = *s++; | |
10775 | } | |
10776 | ||
10777 | *bufptr = '\0'; | |
10778 | ||
10779 | return (bufptr); | |
10780 | } | |
10781 | ||
10782 | ||
ef416fc2 | 10783 | /* |
10784 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
10785 | */ | |
10786 | ||
10787 | static int /* O - 0 if not allowed, 1 if allowed */ | |
10788 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
10789 | const char *username) /* I - Username */ | |
10790 | { | |
10791 | int i; /* Looping var */ | |
10792 | struct passwd *pw; /* User password data */ | |
5bd77a73 MS |
10793 | char baseuser[256], /* Base username */ |
10794 | *baseptr; /* Pointer to "@" in base username */ | |
ef416fc2 | 10795 | |
10796 | ||
10797 | if (p->num_users == 0) | |
10798 | return (1); | |
10799 | ||
10800 | if (!strcmp(username, "root")) | |
10801 | return (1); | |
10802 | ||
5bd77a73 MS |
10803 | if (strchr(username, '@')) |
10804 | { | |
10805 | /* | |
10806 | * Strip @REALM for username check... | |
10807 | */ | |
10808 | ||
10809 | strlcpy(baseuser, username, sizeof(baseuser)); | |
10810 | ||
10811 | if ((baseptr = strchr(baseuser, '@')) != NULL) | |
10812 | *baseptr = '\0'; | |
10813 | ||
10814 | username = baseuser; | |
10815 | } | |
10816 | ||
ef416fc2 | 10817 | pw = getpwnam(username); |
10818 | endpwent(); | |
10819 | ||
10820 | for (i = 0; i < p->num_users; i ++) | |
10821 | { | |
8922323b | 10822 | if (p->users[i][0] == '@') |
ef416fc2 | 10823 | { |
10824 | /* | |
10825 | * Check group membership... | |
10826 | */ | |
10827 | ||
10828 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
10829 | break; | |
10830 | } | |
8922323b MS |
10831 | else if (p->users[i][0] == '#') |
10832 | { | |
10833 | /* | |
10834 | * Check UUID... | |
10835 | */ | |
10836 | ||
10837 | if (cupsdCheckGroup(username, pw, p->users[i])) | |
10838 | break; | |
10839 | } | |
ef416fc2 | 10840 | else if (!strcasecmp(username, p->users[i])) |
10841 | break; | |
10842 | } | |
10843 | ||
10844 | return ((i < p->num_users) != p->deny_users); | |
10845 | } | |
10846 | ||
10847 | ||
10848 | /* | |
10849 | * 'validate_job()' - Validate printer options and destination. | |
10850 | */ | |
10851 | ||
10852 | static void | |
10853 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
10854 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10855 | { | |
10856 | http_status_t status; /* Policy status */ | |
10857 | ipp_attribute_t *attr; /* Current attribute */ | |
10858 | ipp_attribute_t *format; /* Document-format attribute */ | |
bc44d920 | 10859 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10860 | char super[MIME_MAX_SUPER], |
10861 | /* Supertype of file */ | |
10862 | type[MIME_MAX_TYPE]; | |
10863 | /* Subtype of file */ | |
10864 | cupsd_printer_t *printer; /* Printer */ | |
10865 | ||
10866 | ||
10867 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
10868 | con->http.fd, uri->values[0].string.text); | |
10869 | ||
10870 | /* | |
10871 | * OK, see if the client is sending the document compressed - CUPS | |
10872 | * doesn't support compression yet... | |
10873 | */ | |
10874 | ||
fa73b229 | 10875 | if ((attr = ippFindAttribute(con->request, "compression", |
10876 | IPP_TAG_KEYWORD)) != NULL && | |
10877 | !strcmp(attr->values[0].string.text, "none")) | |
ef416fc2 | 10878 | { |
10879 | send_ipp_status(con, IPP_ATTRIBUTES, | |
10880 | _("Unsupported compression attribute %s!"), | |
10881 | attr->values[0].string.text); | |
10882 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
10883 | "compression", NULL, attr->values[0].string.text); | |
10884 | return; | |
10885 | } | |
10886 | ||
10887 | /* | |
10888 | * Is it a format we support? | |
10889 | */ | |
10890 | ||
10891 | if ((format = ippFindAttribute(con->request, "document-format", | |
10892 | IPP_TAG_MIMETYPE)) != NULL) | |
10893 | { | |
bc44d920 | 10894 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", |
10895 | super, type) != 2) | |
ef416fc2 | 10896 | { |
10897 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
10898 | format->values[0].string.text); | |
10899 | return; | |
10900 | } | |
10901 | ||
fa73b229 | 10902 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && |
10903 | !mimeType(MimeDatabase, super, type)) | |
ef416fc2 | 10904 | { |
10905 | cupsdLogMessage(CUPSD_LOG_INFO, | |
10906 | "Hint: Do you have the raw file printing rules enabled?"); | |
10907 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
10908 | _("Unsupported format \"%s\"!"), | |
10909 | format->values[0].string.text); | |
10910 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
10911 | "document-format", NULL, format->values[0].string.text); | |
10912 | return; | |
10913 | } | |
10914 | } | |
10915 | ||
10916 | /* | |
10917 | * Is the destination valid? | |
10918 | */ | |
10919 | ||
f7deaa1a | 10920 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10921 | { |
10922 | /* | |
10923 | * Bad URI... | |
10924 | */ | |
10925 | ||
10926 | send_ipp_status(con, IPP_NOT_FOUND, | |
10927 | _("The printer or class was not found.")); | |
10928 | return; | |
10929 | } | |
10930 | ||
10931 | /* | |
10932 | * Check policy... | |
10933 | */ | |
10934 | ||
10935 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10936 | { | |
f899b121 | 10937 | send_http_error(con, status, printer); |
ef416fc2 | 10938 | return; |
10939 | } | |
10940 | ||
10941 | /* | |
10942 | * Everything was ok, so return OK status... | |
10943 | */ | |
10944 | ||
10945 | con->response->request.status.status_code = IPP_OK; | |
10946 | } | |
10947 | ||
10948 | ||
10949 | /* | |
10950 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
10951 | */ | |
10952 | ||
bc44d920 | 10953 | static int /* O - 0 if name is no good, 1 if good */ |
ef416fc2 | 10954 | validate_name(const char *name) /* I - Name to check */ |
10955 | { | |
10956 | const char *ptr; /* Pointer into name */ | |
10957 | ||
10958 | ||
10959 | /* | |
10960 | * Scan the whole name... | |
10961 | */ | |
10962 | ||
10963 | for (ptr = name; *ptr; ptr ++) | |
f7deaa1a | 10964 | if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') |
ef416fc2 | 10965 | return (0); |
10966 | ||
10967 | /* | |
10968 | * All the characters are good; validate the length, too... | |
10969 | */ | |
10970 | ||
10971 | return ((ptr - name) < 128); | |
10972 | } | |
10973 | ||
10974 | ||
10975 | /* | |
10976 | * 'validate_user()' - Validate the user for the request. | |
10977 | */ | |
10978 | ||
10979 | static int /* O - 1 if permitted, 0 otherwise */ | |
10980 | validate_user(cupsd_job_t *job, /* I - Job */ | |
10981 | cupsd_client_t *con, /* I - Client connection */ | |
10982 | const char *owner, /* I - Owner of job/resource */ | |
10983 | char *username, /* O - Authenticated username */ | |
10984 | int userlen) /* I - Length of username */ | |
10985 | { | |
ef416fc2 | 10986 | cupsd_printer_t *printer; /* Printer for job */ |
10987 | ||
10988 | ||
10989 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
10990 | "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, " | |
10991 | "userlen=%d)", | |
91c84a35 MS |
10992 | job->id, con ? con->http.fd : 0, |
10993 | owner ? owner : "(null)", username, userlen); | |
ef416fc2 | 10994 | |
10995 | /* | |
10996 | * Validate input... | |
10997 | */ | |
10998 | ||
10999 | if (!con || !owner || !username || userlen <= 0) | |
11000 | return (0); | |
11001 | ||
11002 | /* | |
11003 | * Get the best authenticated username that is available. | |
11004 | */ | |
11005 | ||
e00b005a | 11006 | strlcpy(username, get_username(con), userlen); |
ef416fc2 | 11007 | |
11008 | /* | |
11009 | * Check the username against the owner... | |
11010 | */ | |
11011 | ||
80ca4592 | 11012 | printer = cupsdFindDest(job->dest); |
ef416fc2 | 11013 | |
11014 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
11015 | con, owner) == HTTP_OK); | |
11016 | } | |
11017 | ||
11018 | ||
11019 | /* | |
75bd9771 | 11020 | * End of "$Id: ipp.c 7682 2008-06-21 00:06:02Z mike $". |
ef416fc2 | 11021 | */ |