]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $" | |
3 | * | |
4 | * IPP routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
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. | |
11 | * | |
12 | * These coded instructions, statements, and computer programs are the | |
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/". | |
17 | * | |
18 | * Contents: | |
19 | * | |
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... | |
28 | * add_job_subscriptions() - Add any subcriptions for a job. | |
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. | |
45 | * check_rss_recipient() - Check that we do not have a duplicate RSS | |
46 | * feed URI. | |
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. | |
80 | * read_ps_job_ticket() - Reads a job ticket embedded in a PS file. | |
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. | |
102 | */ | |
103 | ||
104 | /* | |
105 | * Include necessary headers... | |
106 | */ | |
107 | ||
108 | #include "cupsd.h" | |
109 | #include <cups/ppd-private.h> | |
110 | ||
111 | #ifdef HAVE_LIBPAPER | |
112 | # include <paper.h> | |
113 | #endif /* HAVE_LIBPAPER */ | |
114 | ||
115 | #ifdef __APPLE__ | |
116 | # include <ApplicationServices/ApplicationServices.h> | |
117 | # include <CoreFoundation/CoreFoundation.h> | |
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 | |
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); | |
127 | # endif /* HAVE_MEMBERSHIPPRIV_H */ | |
128 | #endif /* __APPLE__ */ | |
129 | ||
130 | ||
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); | |
137 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, | |
138 | mime_type_t *filetype, int compression); | |
139 | static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer, | |
140 | mime_type_t *filetype); | |
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); | |
143 | static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job); | |
144 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
145 | static void add_printer_state_reasons(cupsd_client_t *con, | |
146 | cupsd_printer_t *p); | |
147 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); | |
148 | #ifdef __APPLE__ | |
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); | |
153 | static void apple_register_profiles(cupsd_printer_t *p); | |
154 | static void apple_unregister_profiles(cupsd_printer_t *p); | |
155 | #endif /* __APPLE__ */ | |
156 | static void apply_printer_defaults(cupsd_printer_t *printer, | |
157 | cupsd_job_t *job); | |
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); | |
162 | static int check_rss_recipient(const char *recipient); | |
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); | |
166 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, | |
167 | ipp_tag_t group, int quickcopy); | |
168 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, | |
169 | const char *name); | |
170 | static int copy_file(const char *from, const char *to); | |
171 | static int copy_model(cupsd_client_t *con, const char *from, | |
172 | const char *to); | |
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); | |
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); | |
188 | static void get_document(cupsd_client_t *con, ipp_attribute_t *uri); | |
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); | |
191 | static void get_notifications(cupsd_client_t *con); | |
192 | static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri); | |
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); | |
198 | static const char *get_username(cupsd_client_t *con); | |
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); | |
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); | |
204 | static void read_ps_job_ticket(cupsd_client_t *con); | |
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); | |
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 */ | |
214 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); | |
215 | static void send_http_error(cupsd_client_t *con, http_status_t status, | |
216 | cupsd_printer_t *printer); | |
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); | |
225 | static void set_printer_defaults(cupsd_client_t *con, | |
226 | cupsd_printer_t *printer); | |
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); | |
229 | static void url_encode_attr(ipp_attribute_t *attr, char *buffer, | |
230 | int bufsize); | |
231 | static char *url_encode_string(const char *s, char *buffer, int bufsize); | |
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 | /* | |
241 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request. | |
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 */ | |
252 | ipp_attribute_t *uri; /* Printer URI attribute */ | |
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 | ||
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; | |
273 | ||
274 | /* | |
275 | * Then validate the request header and required attributes... | |
276 | */ | |
277 | ||
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]); | |
294 | } | |
295 | else if (!con->request->attrs) | |
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; | |
311 | attr; | |
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 | ||
331 | if (!attr) | |
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") && | |
343 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET) | |
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") && | |
352 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE) | |
353 | language = attr; | |
354 | else | |
355 | language = NULL; | |
356 | ||
357 | if ((attr = ippFindAttribute(con->request, "printer-uri", | |
358 | IPP_TAG_URI)) != NULL) | |
359 | uri = attr; | |
360 | else if ((attr = ippFindAttribute(con->request, "job-uri", | |
361 | IPP_TAG_URI)) != NULL) | |
362 | uri = attr; | |
363 | else if (con->request->request.op.operation_id == CUPS_GET_PPD) | |
364 | uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME); | |
365 | else | |
366 | uri = NULL; | |
367 | ||
368 | if (charset) | |
369 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
370 | "attributes-charset", NULL, | |
371 | charset->values[0].string.text); | |
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 | ||
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)) | |
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, | |
439 | "Missing printer-uri, job-uri, or ppd-name " | |
440 | "attribute!"); | |
441 | ||
442 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
443 | "%04X %s Missing printer-uri, job-uri, or ppd-name " | |
444 | "attribute", IPP_BAD_REQUEST, con->http.hostname); | |
445 | } | |
446 | ||
447 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
448 | ||
449 | for (attr = con->request->attrs; attr; attr = attr->next) | |
450 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
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 | ||
482 | _cupsStrFree(username->values[0].string.text); | |
483 | username->values[0].string.text = _cupsStrAlloc(RemoteRoot); | |
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) | |
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)); | |
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 | ||
613 | case CUPS_GET_DOCUMENT : | |
614 | get_document(con, uri); | |
615 | break; | |
616 | ||
617 | case CUPS_GET_PPD : | |
618 | get_ppd(con, uri); | |
619 | break; | |
620 | ||
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 : | |
655 | get_notifications(con); | |
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!"), | |
667 | ippOpString( | |
668 | con->request->request.op.operation_id)); | |
669 | break; | |
670 | } | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | if (con->response) | |
676 | { | |
677 | /* | |
678 | * Sending data from the scheduler... | |
679 | */ | |
680 | ||
681 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
682 | "cupsdProcessIPPRequest: %d status_code=%x (%s)", | |
683 | con->http.fd, con->response->request.status.status_code, | |
684 | ippErrorString(con->response->request.status.status_code)); | |
685 | ||
686 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE)) | |
687 | { | |
688 | #ifdef CUPSD_USE_CHUNKING | |
689 | /* | |
690 | * Because older versions of CUPS (1.1.17 and older) and some IPP | |
691 | * clients do not implement chunking properly, we cannot use | |
692 | * chunking by default. This may become the default in future | |
693 | * CUPS releases, or we might add a configuration directive for | |
694 | * it. | |
695 | */ | |
696 | ||
697 | if (con->http.version == HTTP_1_1) | |
698 | { | |
699 | if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n") < 0) | |
700 | return (0); | |
701 | ||
702 | if (cupsdFlushHeader(con) < 0) | |
703 | return (0); | |
704 | ||
705 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
706 | } | |
707 | else | |
708 | #endif /* CUPSD_USE_CHUNKING */ | |
709 | { | |
710 | size_t length; /* Length of response */ | |
711 | ||
712 | ||
713 | length = ippLength(con->response); | |
714 | ||
715 | if (con->file >= 0 && !con->pipe_pid) | |
716 | { | |
717 | struct stat fileinfo; /* File information */ | |
718 | ||
719 | ||
720 | if (!fstat(con->file, &fileinfo)) | |
721 | length += fileinfo.st_size; | |
722 | } | |
723 | ||
724 | if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n", | |
725 | CUPS_LLCAST length) < 0) | |
726 | return (0); | |
727 | ||
728 | if (cupsdFlushHeader(con) < 0) | |
729 | return (0); | |
730 | ||
731 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
732 | con->http.data_remaining = length; | |
733 | ||
734 | if (con->http.data_remaining <= INT_MAX) | |
735 | con->http._data_remaining = con->http.data_remaining; | |
736 | else | |
737 | con->http._data_remaining = INT_MAX; | |
738 | } | |
739 | ||
740 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, | |
741 | (cupsd_selfunc_t)cupsdWriteClient, con); | |
742 | ||
743 | /* | |
744 | * Tell the caller the response header was sent successfully... | |
745 | */ | |
746 | ||
747 | return (1); | |
748 | } | |
749 | else | |
750 | { | |
751 | /* | |
752 | * Tell the caller the response header could not be sent... | |
753 | */ | |
754 | ||
755 | return (0); | |
756 | } | |
757 | } | |
758 | else | |
759 | { | |
760 | /* | |
761 | * Sending data from a subprocess like cups-deviced; tell the caller | |
762 | * everything is A-OK so far... | |
763 | */ | |
764 | ||
765 | return (1); | |
766 | } | |
767 | } | |
768 | ||
769 | ||
770 | /* | |
771 | * 'cupsdTimeoutJob()' - Timeout a job waiting on job files. | |
772 | */ | |
773 | ||
774 | int /* O - 0 on success, -1 on error */ | |
775 | cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */ | |
776 | { | |
777 | cupsd_printer_t *printer; /* Destination printer or class */ | |
778 | ipp_attribute_t *attr; /* job-sheets attribute */ | |
779 | int kbytes; /* Kilobytes in banner */ | |
780 | ||
781 | ||
782 | job->pending_timeout = 0; | |
783 | ||
784 | /* | |
785 | * See if we need to add the ending sheet... | |
786 | */ | |
787 | ||
788 | printer = cupsdFindDest(job->dest); | |
789 | attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); | |
790 | ||
791 | if (printer && | |
792 | !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && | |
793 | attr && attr->num_values > 1) | |
794 | { | |
795 | /* | |
796 | * Yes... | |
797 | */ | |
798 | ||
799 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Adding end banner page \"%s\".", | |
800 | job->id, attr->values[1].string.text); | |
801 | ||
802 | if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0) | |
803 | return (-1); | |
804 | ||
805 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
806 | } | |
807 | ||
808 | return (0); | |
809 | } | |
810 | ||
811 | ||
812 | /* | |
813 | * 'accept_jobs()' - Accept print jobs to a printer. | |
814 | */ | |
815 | ||
816 | static void | |
817 | accept_jobs(cupsd_client_t *con, /* I - Client connection */ | |
818 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
819 | { | |
820 | http_status_t status; /* Policy status */ | |
821 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
822 | cupsd_printer_t *printer; /* Printer data */ | |
823 | ||
824 | ||
825 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con, | |
826 | con->http.fd, uri->values[0].string.text); | |
827 | ||
828 | /* | |
829 | * Is the destination valid? | |
830 | */ | |
831 | ||
832 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
833 | { | |
834 | /* | |
835 | * Bad URI... | |
836 | */ | |
837 | ||
838 | send_ipp_status(con, IPP_NOT_FOUND, | |
839 | _("The printer or class was not found.")); | |
840 | return; | |
841 | } | |
842 | ||
843 | /* | |
844 | * Check policy... | |
845 | */ | |
846 | ||
847 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
848 | { | |
849 | send_http_error(con, status, printer); | |
850 | return; | |
851 | } | |
852 | ||
853 | /* | |
854 | * Accept jobs sent to the printer... | |
855 | */ | |
856 | ||
857 | printer->accepting = 1; | |
858 | printer->state_message[0] = '\0'; | |
859 | ||
860 | cupsdAddPrinterHistory(printer); | |
861 | ||
862 | if (dtype & CUPS_PRINTER_CLASS) | |
863 | { | |
864 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); | |
865 | ||
866 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").", | |
867 | printer->name, get_username(con)); | |
868 | } | |
869 | else | |
870 | { | |
871 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
872 | ||
873 | cupsdLogMessage(CUPSD_LOG_INFO, | |
874 | "Printer \"%s\" now accepting jobs (\"%s\").", | |
875 | printer->name, get_username(con)); | |
876 | } | |
877 | ||
878 | /* | |
879 | * Everything was ok, so return OK status... | |
880 | */ | |
881 | ||
882 | con->response->request.status.status_code = IPP_OK; | |
883 | } | |
884 | ||
885 | ||
886 | /* | |
887 | * 'add_class()' - Add a class to the system. | |
888 | */ | |
889 | ||
890 | static void | |
891 | add_class(cupsd_client_t *con, /* I - Client connection */ | |
892 | ipp_attribute_t *uri) /* I - URI of class */ | |
893 | { | |
894 | http_status_t status; /* Policy status */ | |
895 | int i; /* Looping var */ | |
896 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
897 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
898 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
899 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
900 | int port; /* Port portion of URI */ | |
901 | cupsd_printer_t *pclass, /* Class */ | |
902 | *member; /* Member printer/class */ | |
903 | cups_ptype_t dtype; /* Destination type */ | |
904 | ipp_attribute_t *attr; /* Printer attribute */ | |
905 | int modify; /* Non-zero if we just modified */ | |
906 | char newname[IPP_MAX_NAME]; /* New class name */ | |
907 | int need_restart_job; /* Need to restart job? */ | |
908 | ||
909 | ||
910 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con, | |
911 | con->http.fd, uri->values[0].string.text); | |
912 | ||
913 | /* | |
914 | * Do we have a valid URI? | |
915 | */ | |
916 | ||
917 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
918 | sizeof(method), username, sizeof(username), host, | |
919 | sizeof(host), &port, resource, sizeof(resource)); | |
920 | ||
921 | ||
922 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) | |
923 | { | |
924 | /* | |
925 | * No, return an error... | |
926 | */ | |
927 | ||
928 | send_ipp_status(con, IPP_BAD_REQUEST, | |
929 | _("The printer-uri must be of the form " | |
930 | "\"ipp://HOSTNAME/classes/CLASSNAME\".")); | |
931 | return; | |
932 | } | |
933 | ||
934 | /* | |
935 | * Do we have a valid printer name? | |
936 | */ | |
937 | ||
938 | if (!validate_name(resource + 9)) | |
939 | { | |
940 | /* | |
941 | * No, return an error... | |
942 | */ | |
943 | ||
944 | send_ipp_status(con, IPP_BAD_REQUEST, | |
945 | _("The printer-uri \"%s\" contains invalid characters."), | |
946 | uri->values[0].string.text); | |
947 | return; | |
948 | } | |
949 | ||
950 | /* | |
951 | * See if the class already exists; if not, create a new class... | |
952 | */ | |
953 | ||
954 | if ((pclass = cupsdFindClass(resource + 9)) == NULL) | |
955 | { | |
956 | /* | |
957 | * Class doesn't exist; see if we have a printer of the same name... | |
958 | */ | |
959 | ||
960 | if ((pclass = cupsdFindPrinter(resource + 9)) != NULL && | |
961 | !(pclass->type & CUPS_PRINTER_DISCOVERED)) | |
962 | { | |
963 | /* | |
964 | * Yes, return an error... | |
965 | */ | |
966 | ||
967 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
968 | _("A printer named \"%s\" already exists!"), | |
969 | resource + 9); | |
970 | return; | |
971 | } | |
972 | ||
973 | /* | |
974 | * No, check the default policy and then add the class... | |
975 | */ | |
976 | ||
977 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
978 | { | |
979 | send_http_error(con, status, NULL); | |
980 | return; | |
981 | } | |
982 | ||
983 | pclass = cupsdAddClass(resource + 9); | |
984 | modify = 0; | |
985 | } | |
986 | else if (pclass->type & CUPS_PRINTER_IMPLICIT) | |
987 | { | |
988 | /* | |
989 | * Check the default policy, then rename the implicit class to "AnyClass" | |
990 | * or remove it... | |
991 | */ | |
992 | ||
993 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
994 | { | |
995 | send_http_error(con, status, NULL); | |
996 | return; | |
997 | } | |
998 | ||
999 | if (ImplicitAnyClasses) | |
1000 | { | |
1001 | snprintf(newname, sizeof(newname), "Any%s", resource + 9); | |
1002 | cupsdRenamePrinter(pclass, newname); | |
1003 | } | |
1004 | else | |
1005 | cupsdDeletePrinter(pclass, 1); | |
1006 | ||
1007 | /* | |
1008 | * Add the class as a new local class... | |
1009 | */ | |
1010 | ||
1011 | pclass = cupsdAddClass(resource + 9); | |
1012 | modify = 0; | |
1013 | } | |
1014 | else if (pclass->type & CUPS_PRINTER_DISCOVERED) | |
1015 | { | |
1016 | /* | |
1017 | * Check the default policy, then rename the remote class to "Class"... | |
1018 | */ | |
1019 | ||
1020 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
1021 | { | |
1022 | send_http_error(con, status, NULL); | |
1023 | return; | |
1024 | } | |
1025 | ||
1026 | snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname); | |
1027 | cupsdRenamePrinter(pclass, newname); | |
1028 | ||
1029 | /* | |
1030 | * Add the class as a new local class... | |
1031 | */ | |
1032 | ||
1033 | pclass = cupsdAddClass(resource + 9); | |
1034 | modify = 0; | |
1035 | } | |
1036 | else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con, | |
1037 | NULL)) != HTTP_OK) | |
1038 | { | |
1039 | send_http_error(con, status, pclass); | |
1040 | return; | |
1041 | } | |
1042 | else | |
1043 | modify = 1; | |
1044 | ||
1045 | /* | |
1046 | * Look for attributes and copy them over as needed... | |
1047 | */ | |
1048 | ||
1049 | need_restart_job = 0; | |
1050 | ||
1051 | if ((attr = ippFindAttribute(con->request, "printer-location", | |
1052 | IPP_TAG_TEXT)) != NULL) | |
1053 | cupsdSetString(&pclass->location, attr->values[0].string.text); | |
1054 | ||
1055 | if ((attr = ippFindAttribute(con->request, "printer-info", | |
1056 | IPP_TAG_TEXT)) != NULL) | |
1057 | cupsdSetString(&pclass->info, attr->values[0].string.text); | |
1058 | ||
1059 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", | |
1060 | IPP_TAG_BOOLEAN)) != NULL) | |
1061 | { | |
1062 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1063 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
1064 | pclass->name, attr->values[0].boolean, pclass->accepting); | |
1065 | ||
1066 | pclass->accepting = attr->values[0].boolean; | |
1067 | cupsdAddPrinterHistory(pclass); | |
1068 | } | |
1069 | ||
1070 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", | |
1071 | IPP_TAG_BOOLEAN)) != NULL) | |
1072 | { | |
1073 | if (pclass->shared && !attr->values[0].boolean) | |
1074 | cupsdDeregisterPrinter(pclass, 1); | |
1075 | ||
1076 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1077 | "Setting %s printer-is-shared to %d (was %d.)", | |
1078 | pclass->name, attr->values[0].boolean, pclass->shared); | |
1079 | ||
1080 | pclass->shared = attr->values[0].boolean; | |
1081 | } | |
1082 | ||
1083 | if ((attr = ippFindAttribute(con->request, "printer-state", | |
1084 | IPP_TAG_ENUM)) != NULL) | |
1085 | { | |
1086 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
1087 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
1088 | { | |
1089 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1090 | _("Attempt to set %s printer-state to bad value %d!"), | |
1091 | pclass->name, attr->values[0].integer); | |
1092 | return; | |
1093 | } | |
1094 | ||
1095 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", | |
1096 | pclass->name, attr->values[0].integer, pclass->state); | |
1097 | ||
1098 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
1099 | cupsdStopPrinter(pclass, 0); | |
1100 | else | |
1101 | { | |
1102 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); | |
1103 | need_restart_job = 1; | |
1104 | } | |
1105 | } | |
1106 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
1107 | IPP_TAG_TEXT)) != NULL) | |
1108 | { | |
1109 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
1110 | sizeof(pclass->state_message)); | |
1111 | cupsdAddPrinterHistory(pclass); | |
1112 | } | |
1113 | if ((attr = ippFindAttribute(con->request, "member-uris", | |
1114 | IPP_TAG_URI)) != NULL) | |
1115 | { | |
1116 | /* | |
1117 | * Clear the printer array as needed... | |
1118 | */ | |
1119 | ||
1120 | need_restart_job = 1; | |
1121 | ||
1122 | if (pclass->num_printers > 0) | |
1123 | { | |
1124 | free(pclass->printers); | |
1125 | pclass->num_printers = 0; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * Add each printer or class that is listed... | |
1130 | */ | |
1131 | ||
1132 | for (i = 0; i < attr->num_values; i ++) | |
1133 | { | |
1134 | /* | |
1135 | * Search for the printer or class URI... | |
1136 | */ | |
1137 | ||
1138 | if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member)) | |
1139 | { | |
1140 | /* | |
1141 | * Bad URI... | |
1142 | */ | |
1143 | ||
1144 | send_ipp_status(con, IPP_NOT_FOUND, | |
1145 | _("The printer or class was not found.")); | |
1146 | return; | |
1147 | } | |
1148 | ||
1149 | /* | |
1150 | * Add it to the class... | |
1151 | */ | |
1152 | ||
1153 | cupsdAddPrinterToClass(pclass, member); | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | set_printer_defaults(con, pclass); | |
1158 | ||
1159 | if ((attr = ippFindAttribute(con->request, "auth-info-required", | |
1160 | IPP_TAG_KEYWORD)) != NULL) | |
1161 | cupsdSetAuthInfoRequired(pclass, NULL, attr); | |
1162 | ||
1163 | /* | |
1164 | * Update the printer class attributes and return... | |
1165 | */ | |
1166 | ||
1167 | cupsdSetPrinterAttrs(pclass); | |
1168 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); | |
1169 | ||
1170 | if (need_restart_job && pclass->job) | |
1171 | { | |
1172 | cupsd_job_t *job; | |
1173 | ||
1174 | /* | |
1175 | * Stop the current job and then restart it below... | |
1176 | */ | |
1177 | ||
1178 | job = (cupsd_job_t *)pclass->job; | |
1179 | ||
1180 | cupsdStopJob(job, 1); | |
1181 | ||
1182 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1183 | job->state_value = IPP_JOB_PENDING; | |
1184 | } | |
1185 | ||
1186 | if (need_restart_job) | |
1187 | cupsdCheckJobs(); | |
1188 | ||
1189 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); | |
1190 | ||
1191 | if (modify) | |
1192 | { | |
1193 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL, | |
1194 | "Class \"%s\" modified by \"%s\".", pclass->name, | |
1195 | get_username(con)); | |
1196 | ||
1197 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
1198 | pclass->name, get_username(con)); | |
1199 | } | |
1200 | else | |
1201 | { | |
1202 | cupsdAddPrinterHistory(pclass); | |
1203 | ||
1204 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL, | |
1205 | "New class \"%s\" added by \"%s\".", pclass->name, | |
1206 | get_username(con)); | |
1207 | ||
1208 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
1209 | pclass->name, get_username(con)); | |
1210 | } | |
1211 | ||
1212 | con->response->request.status.status_code = IPP_OK; | |
1213 | } | |
1214 | ||
1215 | ||
1216 | /* | |
1217 | * 'add_file()' - Add a file to a job. | |
1218 | */ | |
1219 | ||
1220 | static int /* O - 0 on success, -1 on error */ | |
1221 | add_file(cupsd_client_t *con, /* I - Connection to client */ | |
1222 | cupsd_job_t *job, /* I - Job to add to */ | |
1223 | mime_type_t *filetype, /* I - Type of file */ | |
1224 | int compression) /* I - Compression */ | |
1225 | { | |
1226 | mime_type_t **filetypes; /* New filetypes array... */ | |
1227 | int *compressions; /* New compressions array... */ | |
1228 | ||
1229 | ||
1230 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1231 | "add_file(con=%p[%d], job=%d, filetype=%s/%s, " | |
1232 | "compression=%d)", con, con ? con->http.fd : -1, job->id, | |
1233 | filetype->super, filetype->type, compression); | |
1234 | ||
1235 | /* | |
1236 | * Add the file to the job... | |
1237 | */ | |
1238 | ||
1239 | if (job->num_files == 0) | |
1240 | { | |
1241 | compressions = (int *)malloc(sizeof(int)); | |
1242 | filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *)); | |
1243 | } | |
1244 | else | |
1245 | { | |
1246 | compressions = (int *)realloc(job->compressions, | |
1247 | (job->num_files + 1) * sizeof(int)); | |
1248 | filetypes = (mime_type_t **)realloc(job->filetypes, | |
1249 | (job->num_files + 1) * | |
1250 | sizeof(mime_type_t *)); | |
1251 | } | |
1252 | ||
1253 | if (!compressions || !filetypes) | |
1254 | { | |
1255 | cupsdCancelJob(job, 1, IPP_JOB_ABORTED); | |
1256 | ||
1257 | if (con) | |
1258 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1259 | _("Unable to allocate memory for file types!")); | |
1260 | ||
1261 | return (-1); | |
1262 | } | |
1263 | ||
1264 | job->compressions = compressions; | |
1265 | job->compressions[job->num_files] = compression; | |
1266 | job->filetypes = filetypes; | |
1267 | job->filetypes[job->num_files] = filetype; | |
1268 | ||
1269 | job->num_files ++; | |
1270 | ||
1271 | job->dirty = 1; | |
1272 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
1273 | ||
1274 | return (0); | |
1275 | } | |
1276 | ||
1277 | ||
1278 | /* | |
1279 | * 'add_job()' - Add a job to a print queue. | |
1280 | */ | |
1281 | ||
1282 | static cupsd_job_t * /* O - Job object */ | |
1283 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
1284 | cupsd_printer_t *printer, /* I - Destination printer */ | |
1285 | mime_type_t *filetype) /* I - First print file type, if any */ | |
1286 | { | |
1287 | http_status_t status; /* Policy status */ | |
1288 | ipp_attribute_t *attr, /* Current attribute */ | |
1289 | *auth_info; /* auth-info attribute */ | |
1290 | const char *val; /* Default option value */ | |
1291 | int priority; /* Job priority */ | |
1292 | char *title; /* Job name/title */ | |
1293 | cupsd_job_t *job; /* Current job */ | |
1294 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
1295 | int kbytes; /* Size of print file */ | |
1296 | int i; /* Looping var */ | |
1297 | int lowerpagerange; /* Page range bound */ | |
1298 | ||
1299 | ||
1300 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))", | |
1301 | con, con->http.fd, printer, printer->name, | |
1302 | filetype, filetype ? filetype->super : "none", | |
1303 | filetype ? filetype->type : "none"); | |
1304 | ||
1305 | /* | |
1306 | * Check remote printing to non-shared printer... | |
1307 | */ | |
1308 | ||
1309 | if (!printer->shared && | |
1310 | strcasecmp(con->http.hostname, "localhost") && | |
1311 | strcasecmp(con->http.hostname, ServerName)) | |
1312 | { | |
1313 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
1314 | _("The printer or class is not shared!")); | |
1315 | return (NULL); | |
1316 | } | |
1317 | ||
1318 | /* | |
1319 | * Check policy... | |
1320 | */ | |
1321 | ||
1322 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); | |
1323 | ||
1324 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
1325 | { | |
1326 | send_http_error(con, status, printer); | |
1327 | return (NULL); | |
1328 | } | |
1329 | else if (printer->num_auth_info_required > 0 && | |
1330 | strcmp(printer->auth_info_required[0], "none") && | |
1331 | !con->username[0] && !auth_info) | |
1332 | { | |
1333 | send_http_error(con, HTTP_UNAUTHORIZED, printer); | |
1334 | return (NULL); | |
1335 | } | |
1336 | #ifdef HAVE_SSL | |
1337 | else if (auth_info && !con->http.tls && | |
1338 | !httpAddrLocalhost(con->http.hostaddr)) | |
1339 | { | |
1340 | /* | |
1341 | * Require encryption of auth-info over non-local connections... | |
1342 | */ | |
1343 | ||
1344 | send_http_error(con, HTTP_UPGRADE_REQUIRED, printer); | |
1345 | return (NULL); | |
1346 | } | |
1347 | #endif /* HAVE_SSL */ | |
1348 | ||
1349 | /* | |
1350 | * See if the printer is accepting jobs... | |
1351 | */ | |
1352 | ||
1353 | if (!printer->accepting) | |
1354 | { | |
1355 | send_ipp_status(con, IPP_NOT_ACCEPTING, | |
1356 | _("Destination \"%s\" is not accepting jobs."), | |
1357 | printer->name); | |
1358 | return (NULL); | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * Validate job template attributes; for now just document-format, | |
1363 | * copies, number-up, and page-ranges... | |
1364 | */ | |
1365 | ||
1366 | if (filetype && printer->filetypes && | |
1367 | !cupsArrayFind(printer->filetypes, filetype)) | |
1368 | { | |
1369 | char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
1370 | /* MIME media type string */ | |
1371 | ||
1372 | ||
1373 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
1374 | filetype->type); | |
1375 | ||
1376 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
1377 | _("Unsupported format \'%s\'!"), mimetype); | |
1378 | ||
1379 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
1380 | "document-format", NULL, mimetype); | |
1381 | ||
1382 | return (NULL); | |
1383 | } | |
1384 | ||
1385 | if ((attr = ippFindAttribute(con->request, "copies", | |
1386 | IPP_TAG_INTEGER)) != NULL) | |
1387 | { | |
1388 | if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies) | |
1389 | { | |
1390 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."), | |
1391 | attr->values[0].integer); | |
1392 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1393 | "copies", attr->values[0].integer); | |
1394 | return (NULL); | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | if ((attr = ippFindAttribute(con->request, "job-sheets", | |
1399 | IPP_TAG_ZERO)) != NULL) | |
1400 | { | |
1401 | if (attr->value_tag != IPP_TAG_KEYWORD && | |
1402 | attr->value_tag != IPP_TAG_NAME) | |
1403 | { | |
1404 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type!")); | |
1405 | return (NULL); | |
1406 | } | |
1407 | ||
1408 | if (attr->num_values > 2) | |
1409 | { | |
1410 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1411 | _("Too many job-sheets values (%d > 2)!"), | |
1412 | attr->num_values); | |
1413 | return (NULL); | |
1414 | } | |
1415 | ||
1416 | for (i = 0; i < attr->num_values; i ++) | |
1417 | if (strcmp(attr->values[i].string.text, "none") && | |
1418 | !cupsdFindBanner(attr->values[i].string.text)) | |
1419 | { | |
1420 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"!"), | |
1421 | attr->values[i].string.text); | |
1422 | return (NULL); | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | if ((attr = ippFindAttribute(con->request, "number-up", | |
1427 | IPP_TAG_INTEGER)) != NULL) | |
1428 | { | |
1429 | if (attr->values[0].integer != 1 && | |
1430 | attr->values[0].integer != 2 && | |
1431 | attr->values[0].integer != 4 && | |
1432 | attr->values[0].integer != 6 && | |
1433 | attr->values[0].integer != 9 && | |
1434 | attr->values[0].integer != 16) | |
1435 | { | |
1436 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."), | |
1437 | attr->values[0].integer); | |
1438 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1439 | "number-up", attr->values[0].integer); | |
1440 | return (NULL); | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | if ((attr = ippFindAttribute(con->request, "page-ranges", | |
1445 | IPP_TAG_RANGE)) != NULL) | |
1446 | { | |
1447 | for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++) | |
1448 | { | |
1449 | if (attr->values[i].range.lower < lowerpagerange || | |
1450 | attr->values[i].range.lower > attr->values[i].range.upper) | |
1451 | { | |
1452 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1453 | _("Bad page-ranges values %d-%d."), | |
1454 | attr->values[i].range.lower, | |
1455 | attr->values[i].range.upper); | |
1456 | return (NULL); | |
1457 | } | |
1458 | ||
1459 | lowerpagerange = attr->values[i].range.upper + 1; | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * Make sure we aren't over our limit... | |
1465 | */ | |
1466 | ||
1467 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) | |
1468 | cupsdCleanJobs(); | |
1469 | ||
1470 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) | |
1471 | { | |
1472 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
1473 | _("Too many active jobs.")); | |
1474 | return (NULL); | |
1475 | } | |
1476 | ||
1477 | if ((i = check_quotas(con, printer)) < 0) | |
1478 | { | |
1479 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1480 | return (NULL); | |
1481 | } | |
1482 | else if (i == 0) | |
1483 | { | |
1484 | send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print.")); | |
1485 | return (NULL); | |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Create the job and set things up... | |
1490 | */ | |
1491 | ||
1492 | if ((attr = ippFindAttribute(con->request, "job-priority", | |
1493 | IPP_TAG_INTEGER)) != NULL) | |
1494 | priority = attr->values[0].integer; | |
1495 | else | |
1496 | { | |
1497 | if ((val = cupsGetOption("job-priority", printer->num_options, | |
1498 | printer->options)) != NULL) | |
1499 | priority = atoi(val); | |
1500 | else | |
1501 | priority = 50; | |
1502 | ||
1503 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", | |
1504 | priority); | |
1505 | } | |
1506 | ||
1507 | if ((attr = ippFindAttribute(con->request, "job-name", | |
1508 | IPP_TAG_NAME)) != NULL) | |
1509 | title = attr->values[0].string.text; | |
1510 | else | |
1511 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1512 | title = "Untitled"); | |
1513 | ||
1514 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) | |
1515 | { | |
1516 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1517 | _("Unable to add job for destination \"%s\"!"), | |
1518 | printer->name); | |
1519 | return (NULL); | |
1520 | } | |
1521 | ||
1522 | job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT | | |
1523 | CUPS_PRINTER_REMOTE); | |
1524 | job->attrs = con->request; | |
1525 | job->dirty = 1; | |
1526 | con->request = ippNewRequest(job->attrs->request.op.operation_id); | |
1527 | ||
1528 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
1529 | ||
1530 | add_job_uuid(con, job); | |
1531 | apply_printer_defaults(printer, job); | |
1532 | ||
1533 | attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME); | |
1534 | ||
1535 | if (con->username[0]) | |
1536 | { | |
1537 | cupsdSetString(&job->username, con->username); | |
1538 | ||
1539 | if (attr) | |
1540 | cupsdSetString(&attr->values[0].string.text, con->username); | |
1541 | } | |
1542 | else if (attr) | |
1543 | { | |
1544 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1545 | "add_job: requesting-user-name=\"%s\"", | |
1546 | attr->values[0].string.text); | |
1547 | ||
1548 | cupsdSetString(&job->username, attr->values[0].string.text); | |
1549 | } | |
1550 | else | |
1551 | cupsdSetString(&job->username, "anonymous"); | |
1552 | ||
1553 | if (!attr) | |
1554 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1555 | "job-originating-user-name", NULL, job->username); | |
1556 | else | |
1557 | { | |
1558 | attr->group_tag = IPP_TAG_JOB; | |
1559 | _cupsStrFree(attr->name); | |
1560 | attr->name = _cupsStrAlloc("job-originating-user-name"); | |
1561 | } | |
1562 | ||
1563 | if (con->username[0] || auth_info) | |
1564 | { | |
1565 | save_auth_info(con, job, auth_info); | |
1566 | ||
1567 | /* | |
1568 | * Remove the auth-info attribute from the attribute data... | |
1569 | */ | |
1570 | ||
1571 | if (auth_info) | |
1572 | ippDeleteAttribute(job->attrs, auth_info); | |
1573 | } | |
1574 | ||
1575 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", | |
1576 | IPP_TAG_ZERO)) != NULL) | |
1577 | { | |
1578 | /* | |
1579 | * Request contains a job-originating-host-name attribute; validate it... | |
1580 | */ | |
1581 | ||
1582 | if (attr->value_tag != IPP_TAG_NAME || | |
1583 | attr->num_values != 1 || | |
1584 | strcmp(con->http.hostname, "localhost")) | |
1585 | { | |
1586 | /* | |
1587 | * Can't override the value if we aren't connected via localhost. | |
1588 | * Also, we can only have 1 value and it must be a name value. | |
1589 | */ | |
1590 | ||
1591 | switch (attr->value_tag) | |
1592 | { | |
1593 | case IPP_TAG_STRING : | |
1594 | case IPP_TAG_TEXTLANG : | |
1595 | case IPP_TAG_NAMELANG : | |
1596 | case IPP_TAG_TEXT : | |
1597 | case IPP_TAG_NAME : | |
1598 | case IPP_TAG_KEYWORD : | |
1599 | case IPP_TAG_URI : | |
1600 | case IPP_TAG_URISCHEME : | |
1601 | case IPP_TAG_CHARSET : | |
1602 | case IPP_TAG_LANGUAGE : | |
1603 | case IPP_TAG_MIMETYPE : | |
1604 | /* | |
1605 | * Free old strings... | |
1606 | */ | |
1607 | ||
1608 | for (i = 0; i < attr->num_values; i ++) | |
1609 | { | |
1610 | _cupsStrFree(attr->values[i].string.text); | |
1611 | attr->values[i].string.text = NULL; | |
1612 | if (attr->values[i].string.charset) | |
1613 | { | |
1614 | _cupsStrFree(attr->values[i].string.charset); | |
1615 | attr->values[i].string.charset = NULL; | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | default : | |
1620 | break; | |
1621 | } | |
1622 | ||
1623 | /* | |
1624 | * Use the default connection hostname instead... | |
1625 | */ | |
1626 | ||
1627 | attr->value_tag = IPP_TAG_NAME; | |
1628 | attr->num_values = 1; | |
1629 | attr->values[0].string.text = _cupsStrAlloc(con->http.hostname); | |
1630 | } | |
1631 | ||
1632 | attr->group_tag = IPP_TAG_JOB; | |
1633 | } | |
1634 | else | |
1635 | { | |
1636 | /* | |
1637 | * No job-originating-host-name attribute, so use the hostname from | |
1638 | * the connection... | |
1639 | */ | |
1640 | ||
1641 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1642 | "job-originating-host-name", NULL, con->http.hostname); | |
1643 | } | |
1644 | ||
1645 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", | |
1646 | time(NULL)); | |
1647 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1648 | "time-at-processing", 0); | |
1649 | attr->value_tag = IPP_TAG_NOVALUE; | |
1650 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1651 | "time-at-completed", 0); | |
1652 | attr->value_tag = IPP_TAG_NOVALUE; | |
1653 | ||
1654 | /* | |
1655 | * Add remaining job attributes... | |
1656 | */ | |
1657 | ||
1658 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); | |
1659 | job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM, | |
1660 | "job-state", IPP_JOB_STOPPED); | |
1661 | job->state_value = (ipp_jstate_t)job->state->values[0].integer; | |
1662 | job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1663 | "job-media-sheets-completed", 0); | |
1664 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, | |
1665 | printer->uri); | |
1666 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1667 | title); | |
1668 | ||
1669 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
1670 | IPP_TAG_INTEGER)) != NULL) | |
1671 | attr->values[0].integer = 0; | |
1672 | else | |
1673 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1674 | "job-k-octets", 0); | |
1675 | ||
1676 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
1677 | IPP_TAG_KEYWORD)) == NULL) | |
1678 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
1679 | if (!attr) | |
1680 | { | |
1681 | if ((val = cupsGetOption("job-hold-until", printer->num_options, | |
1682 | printer->options)) == NULL) | |
1683 | val = "no-hold"; | |
1684 | ||
1685 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1686 | "job-hold-until", NULL, val); | |
1687 | } | |
1688 | if (attr && strcmp(attr->values[0].string.text, "no-hold") && | |
1689 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
1690 | { | |
1691 | /* | |
1692 | * Hold job until specified time... | |
1693 | */ | |
1694 | ||
1695 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
1696 | ||
1697 | job->state->values[0].integer = IPP_JOB_HELD; | |
1698 | job->state_value = IPP_JOB_HELD; | |
1699 | } | |
1700 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) | |
1701 | { | |
1702 | job->hold_until = time(NULL) + 60; | |
1703 | job->state->values[0].integer = IPP_JOB_HELD; | |
1704 | job->state_value = IPP_JOB_HELD; | |
1705 | } | |
1706 | else | |
1707 | { | |
1708 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1709 | job->state_value = IPP_JOB_PENDING; | |
1710 | } | |
1711 | ||
1712 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || | |
1713 | Classification) | |
1714 | { | |
1715 | /* | |
1716 | * Add job sheets options... | |
1717 | */ | |
1718 | ||
1719 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", | |
1720 | IPP_TAG_ZERO)) == NULL) | |
1721 | { | |
1722 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1723 | "Adding default job-sheets values \"%s,%s\"...", | |
1724 | printer->job_sheets[0], printer->job_sheets[1]); | |
1725 | ||
1726 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", | |
1727 | 2, NULL, NULL); | |
1728 | attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]); | |
1729 | attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]); | |
1730 | } | |
1731 | ||
1732 | job->job_sheets = attr; | |
1733 | ||
1734 | /* | |
1735 | * Enforce classification level if set... | |
1736 | */ | |
1737 | ||
1738 | if (Classification) | |
1739 | { | |
1740 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1741 | "Classification=\"%s\", ClassifyOverride=%d", | |
1742 | Classification ? Classification : "(null)", | |
1743 | ClassifyOverride); | |
1744 | ||
1745 | if (ClassifyOverride) | |
1746 | { | |
1747 | if (!strcmp(attr->values[0].string.text, "none") && | |
1748 | (attr->num_values == 1 || | |
1749 | !strcmp(attr->values[1].string.text, "none"))) | |
1750 | { | |
1751 | /* | |
1752 | * Force the leading banner to have the classification on it... | |
1753 | */ | |
1754 | ||
1755 | cupsdSetString(&attr->values[0].string.text, Classification); | |
1756 | ||
1757 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " | |
1758 | "job-sheets=\"%s,none\", " | |
1759 | "job-originating-user-name=\"%s\"", | |
1760 | job->id, Classification, job->username); | |
1761 | } | |
1762 | else if (attr->num_values == 2 && | |
1763 | strcmp(attr->values[0].string.text, | |
1764 | attr->values[1].string.text) && | |
1765 | strcmp(attr->values[0].string.text, "none") && | |
1766 | strcmp(attr->values[1].string.text, "none")) | |
1767 | { | |
1768 | /* | |
1769 | * Can't put two different security markings on the same document! | |
1770 | */ | |
1771 | ||
1772 | cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text); | |
1773 | ||
1774 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " | |
1775 | "job-sheets=\"%s,%s\", " | |
1776 | "job-originating-user-name=\"%s\"", | |
1777 | job->id, attr->values[0].string.text, | |
1778 | attr->values[1].string.text, job->username); | |
1779 | } | |
1780 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1781 | strcmp(attr->values[0].string.text, "none") && | |
1782 | (attr->num_values == 1 || | |
1783 | (strcmp(attr->values[1].string.text, Classification) && | |
1784 | strcmp(attr->values[1].string.text, "none")))) | |
1785 | { | |
1786 | if (attr->num_values == 1) | |
1787 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1788 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1789 | "job-sheets=\"%s\", " | |
1790 | "job-originating-user-name=\"%s\"", | |
1791 | job->id, attr->values[0].string.text, job->username); | |
1792 | else | |
1793 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1794 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1795 | "job-sheets=\"%s,%s\",fffff " | |
1796 | "job-originating-user-name=\"%s\"", | |
1797 | job->id, attr->values[0].string.text, | |
1798 | attr->values[1].string.text, job->username); | |
1799 | } | |
1800 | } | |
1801 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1802 | (attr->num_values == 1 || | |
1803 | strcmp(attr->values[1].string.text, Classification))) | |
1804 | { | |
1805 | /* | |
1806 | * Force the banner to have the classification on it... | |
1807 | */ | |
1808 | ||
1809 | if (attr->num_values > 1 && | |
1810 | !strcmp(attr->values[0].string.text, attr->values[1].string.text)) | |
1811 | { | |
1812 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
1813 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1814 | } | |
1815 | else | |
1816 | { | |
1817 | if (attr->num_values == 1 || | |
1818 | strcmp(attr->values[0].string.text, "none")) | |
1819 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
1820 | ||
1821 | if (attr->num_values > 1 && | |
1822 | strcmp(attr->values[1].string.text, "none")) | |
1823 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1824 | } | |
1825 | ||
1826 | if (attr->num_values > 1) | |
1827 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1828 | "[Job %d] CLASSIFICATION FORCED " | |
1829 | "job-sheets=\"%s,%s\", " | |
1830 | "job-originating-user-name=\"%s\"", | |
1831 | job->id, attr->values[0].string.text, | |
1832 | attr->values[1].string.text, job->username); | |
1833 | else | |
1834 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1835 | "[Job %d] CLASSIFICATION FORCED " | |
1836 | "job-sheets=\"%s\", " | |
1837 | "job-originating-user-name=\"%s\"", | |
1838 | job->id, Classification, job->username); | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | /* | |
1843 | * See if we need to add the starting sheet... | |
1844 | */ | |
1845 | ||
1846 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) | |
1847 | { | |
1848 | cupsdLogMessage(CUPSD_LOG_INFO, | |
1849 | "[Job %d] Adding start banner page \"%s\".", | |
1850 | job->id, attr->values[0].string.text); | |
1851 | ||
1852 | if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0) | |
1853 | { | |
1854 | cupsdDeleteJob(job); | |
1855 | return (NULL); | |
1856 | } | |
1857 | ||
1858 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
1859 | } | |
1860 | } | |
1861 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", | |
1862 | IPP_TAG_ZERO)) != NULL) | |
1863 | job->sheets = attr; | |
1864 | ||
1865 | /* | |
1866 | * Fill in the response info... | |
1867 | */ | |
1868 | ||
1869 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
1870 | LocalPort, job->id); | |
1871 | ||
1872 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
1873 | job_uri); | |
1874 | ||
1875 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); | |
1876 | ||
1877 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
1878 | job->state_value); | |
1879 | add_job_state_reasons(con, job); | |
1880 | ||
1881 | con->response->request.status.status_code = IPP_OK; | |
1882 | ||
1883 | /* | |
1884 | * Add any job subscriptions... | |
1885 | */ | |
1886 | ||
1887 | add_job_subscriptions(con, job); | |
1888 | ||
1889 | /* | |
1890 | * Set all but the first two attributes to the job attributes group... | |
1891 | */ | |
1892 | ||
1893 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) | |
1894 | attr->group_tag = IPP_TAG_JOB; | |
1895 | ||
1896 | /* | |
1897 | * Fire the "job created" event... | |
1898 | */ | |
1899 | ||
1900 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); | |
1901 | ||
1902 | /* | |
1903 | * Return the new job... | |
1904 | */ | |
1905 | ||
1906 | return (job); | |
1907 | } | |
1908 | ||
1909 | ||
1910 | /* | |
1911 | * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based | |
1912 | * upon the job and printer state... | |
1913 | */ | |
1914 | ||
1915 | static void | |
1916 | add_job_state_reasons( | |
1917 | cupsd_client_t *con, /* I - Client connection */ | |
1918 | cupsd_job_t *job) /* I - Job info */ | |
1919 | { | |
1920 | cupsd_printer_t *dest; /* Destination printer */ | |
1921 | ||
1922 | ||
1923 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)", | |
1924 | con, con->http.fd, job ? job->id : 0); | |
1925 | ||
1926 | switch (job ? job->state_value : IPP_JOB_CANCELED) | |
1927 | { | |
1928 | case IPP_JOB_PENDING : | |
1929 | dest = cupsdFindDest(job->dest); | |
1930 | ||
1931 | if (dest && dest->state == IPP_PRINTER_STOPPED) | |
1932 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1933 | "job-state-reasons", NULL, "printer-stopped"); | |
1934 | else | |
1935 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1936 | "job-state-reasons", NULL, "none"); | |
1937 | break; | |
1938 | ||
1939 | case IPP_JOB_HELD : | |
1940 | if (ippFindAttribute(job->attrs, "job-hold-until", | |
1941 | IPP_TAG_KEYWORD) != NULL || | |
1942 | ippFindAttribute(job->attrs, "job-hold-until", | |
1943 | IPP_TAG_NAME) != NULL) | |
1944 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1945 | "job-state-reasons", NULL, "job-hold-until-specified"); | |
1946 | else | |
1947 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1948 | "job-state-reasons", NULL, "job-incoming"); | |
1949 | break; | |
1950 | ||
1951 | case IPP_JOB_PROCESSING : | |
1952 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1953 | "job-state-reasons", NULL, "job-printing"); | |
1954 | break; | |
1955 | ||
1956 | case IPP_JOB_STOPPED : | |
1957 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1958 | "job-state-reasons", NULL, "job-stopped"); | |
1959 | break; | |
1960 | ||
1961 | case IPP_JOB_CANCELED : | |
1962 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1963 | "job-state-reasons", NULL, "job-canceled-by-user"); | |
1964 | break; | |
1965 | ||
1966 | case IPP_JOB_ABORTED : | |
1967 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1968 | "job-state-reasons", NULL, "aborted-by-system"); | |
1969 | break; | |
1970 | ||
1971 | case IPP_JOB_COMPLETED : | |
1972 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1973 | "job-state-reasons", NULL, "job-completed-successfully"); | |
1974 | break; | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | ||
1979 | /* | |
1980 | * 'add_job_subscriptions()' - Add any subcriptions for a job. | |
1981 | */ | |
1982 | ||
1983 | static void | |
1984 | add_job_subscriptions( | |
1985 | cupsd_client_t *con, /* I - Client connection */ | |
1986 | cupsd_job_t *job) /* I - Newly created job */ | |
1987 | { | |
1988 | int i; /* Looping var */ | |
1989 | ipp_attribute_t *prev, /* Previous attribute */ | |
1990 | *next, /* Next attribute */ | |
1991 | *attr; /* Current attribute */ | |
1992 | cupsd_subscription_t *sub; /* Subscription object */ | |
1993 | const char *recipient, /* notify-recipient-uri */ | |
1994 | *pullmethod; /* notify-pull-method */ | |
1995 | ipp_attribute_t *user_data; /* notify-user-data */ | |
1996 | int interval; /* notify-time-interval */ | |
1997 | unsigned mask; /* notify-events */ | |
1998 | ||
1999 | ||
2000 | /* | |
2001 | * Find the first subscription group attribute; return if we have | |
2002 | * none... | |
2003 | */ | |
2004 | ||
2005 | for (attr = job->attrs->attrs, prev = NULL; | |
2006 | attr; | |
2007 | prev = attr, attr = attr->next) | |
2008 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
2009 | break; | |
2010 | ||
2011 | if (!attr) | |
2012 | return; | |
2013 | ||
2014 | /* | |
2015 | * Process the subscription attributes in the request... | |
2016 | */ | |
2017 | ||
2018 | while (attr) | |
2019 | { | |
2020 | recipient = NULL; | |
2021 | pullmethod = NULL; | |
2022 | user_data = NULL; | |
2023 | interval = 0; | |
2024 | mask = CUPSD_EVENT_NONE; | |
2025 | ||
2026 | while (attr && attr->group_tag != IPP_TAG_ZERO) | |
2027 | { | |
2028 | if (!strcmp(attr->name, "notify-recipient-uri") && | |
2029 | attr->value_tag == IPP_TAG_URI) | |
2030 | { | |
2031 | /* | |
2032 | * Validate the recipient scheme against the ServerBin/notifier | |
2033 | * directory... | |
2034 | */ | |
2035 | ||
2036 | char notifier[1024], /* Notifier filename */ | |
2037 | scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
2038 | userpass[HTTP_MAX_URI], /* Username portion of URI */ | |
2039 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2040 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2041 | int port; /* Port portion of URI */ | |
2042 | ||
2043 | ||
2044 | recipient = attr->values[0].string.text; | |
2045 | ||
2046 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, | |
2047 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
2048 | host, sizeof(host), &port, | |
2049 | resource, sizeof(resource)) < HTTP_URI_OK) | |
2050 | { | |
2051 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2052 | _("Bad notify-recipient-uri URI \"%s\"!"), recipient); | |
2053 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2054 | "notify-status-code", IPP_URI_SCHEME); | |
2055 | return; | |
2056 | } | |
2057 | ||
2058 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
2059 | scheme); | |
2060 | if (access(notifier, X_OK)) | |
2061 | { | |
2062 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2063 | _("notify-recipient-uri URI \"%s\" uses unknown " | |
2064 | "scheme!"), recipient); | |
2065 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2066 | "notify-status-code", IPP_URI_SCHEME); | |
2067 | return; | |
2068 | } | |
2069 | ||
2070 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
2071 | { | |
2072 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2073 | _("notify-recipient-uri URI \"%s\" is already used!"), | |
2074 | recipient); | |
2075 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2076 | "notify-status-code", IPP_ATTRIBUTES); | |
2077 | return; | |
2078 | } | |
2079 | } | |
2080 | else if (!strcmp(attr->name, "notify-pull-method") && | |
2081 | attr->value_tag == IPP_TAG_KEYWORD) | |
2082 | { | |
2083 | pullmethod = attr->values[0].string.text; | |
2084 | ||
2085 | if (strcmp(pullmethod, "ippget")) | |
2086 | { | |
2087 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2088 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
2089 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
2090 | "notify-status-code", IPP_ATTRIBUTES); | |
2091 | return; | |
2092 | } | |
2093 | } | |
2094 | else if (!strcmp(attr->name, "notify-charset") && | |
2095 | attr->value_tag == IPP_TAG_CHARSET && | |
2096 | strcmp(attr->values[0].string.text, "us-ascii") && | |
2097 | strcmp(attr->values[0].string.text, "utf-8")) | |
2098 | { | |
2099 | send_ipp_status(con, IPP_CHARSET, | |
2100 | _("Character set \"%s\" not supported!"), | |
2101 | attr->values[0].string.text); | |
2102 | return; | |
2103 | } | |
2104 | else if (!strcmp(attr->name, "notify-natural-language") && | |
2105 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
2106 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
2107 | { | |
2108 | send_ipp_status(con, IPP_CHARSET, | |
2109 | _("Language \"%s\" not supported!"), | |
2110 | attr->values[0].string.text); | |
2111 | return; | |
2112 | } | |
2113 | else if (!strcmp(attr->name, "notify-user-data") && | |
2114 | attr->value_tag == IPP_TAG_STRING) | |
2115 | { | |
2116 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
2117 | { | |
2118 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
2119 | _("The notify-user-data value is too large " | |
2120 | "(%d > 63 octets)!"), | |
2121 | attr->values[0].unknown.length); | |
2122 | return; | |
2123 | } | |
2124 | ||
2125 | user_data = attr; | |
2126 | } | |
2127 | else if (!strcmp(attr->name, "notify-events") && | |
2128 | attr->value_tag == IPP_TAG_KEYWORD) | |
2129 | { | |
2130 | for (i = 0; i < attr->num_values; i ++) | |
2131 | mask |= cupsdEventValue(attr->values[i].string.text); | |
2132 | } | |
2133 | else if (!strcmp(attr->name, "notify-lease-duration")) | |
2134 | { | |
2135 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2136 | _("The notify-lease-duration attribute cannot be " | |
2137 | "used with job subscriptions.")); | |
2138 | return; | |
2139 | } | |
2140 | else if (!strcmp(attr->name, "notify-time-interval") && | |
2141 | attr->value_tag == IPP_TAG_INTEGER) | |
2142 | interval = attr->values[0].integer; | |
2143 | ||
2144 | attr = attr->next; | |
2145 | } | |
2146 | ||
2147 | if (!recipient && !pullmethod) | |
2148 | break; | |
2149 | ||
2150 | if (mask == CUPSD_EVENT_NONE) | |
2151 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
2152 | ||
2153 | sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient, | |
2154 | 0); | |
2155 | ||
2156 | sub->interval = interval; | |
2157 | ||
2158 | cupsdSetString(&sub->owner, job->username); | |
2159 | ||
2160 | if (user_data) | |
2161 | { | |
2162 | sub->user_data_len = user_data->values[0].unknown.length; | |
2163 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
2164 | sub->user_data_len); | |
2165 | } | |
2166 | ||
2167 | ippAddSeparator(con->response); | |
2168 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
2169 | "notify-subscription-id", sub->id); | |
2170 | ||
2171 | if (attr) | |
2172 | attr = attr->next; | |
2173 | } | |
2174 | ||
2175 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); | |
2176 | ||
2177 | /* | |
2178 | * Remove all of the subscription attributes from the job request... | |
2179 | */ | |
2180 | ||
2181 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) | |
2182 | { | |
2183 | next = attr->next; | |
2184 | ||
2185 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || | |
2186 | attr->group_tag == IPP_TAG_ZERO) | |
2187 | { | |
2188 | /* | |
2189 | * Free and remove this attribute... | |
2190 | */ | |
2191 | ||
2192 | _ippFreeAttr(attr); | |
2193 | ||
2194 | if (prev) | |
2195 | prev->next = next; | |
2196 | else | |
2197 | job->attrs->attrs = next; | |
2198 | } | |
2199 | else | |
2200 | prev = attr; | |
2201 | } | |
2202 | ||
2203 | job->attrs->last = prev; | |
2204 | job->attrs->current = prev; | |
2205 | } | |
2206 | ||
2207 | ||
2208 | /* | |
2209 | * 'add_job_uuid()' - Add job-uuid attribute to a job. | |
2210 | * | |
2211 | * See RFC 4122 for the definition of UUIDs and the format. | |
2212 | */ | |
2213 | ||
2214 | static void | |
2215 | add_job_uuid(cupsd_client_t *con, /* I - Client connection */ | |
2216 | cupsd_job_t *job) /* I - Job */ | |
2217 | { | |
2218 | char uuid[1024]; /* job-uuid string */ | |
2219 | _cups_md5_state_t md5state; /* MD5 state */ | |
2220 | unsigned char md5sum[16]; /* MD5 digest/sum */ | |
2221 | ||
2222 | ||
2223 | /* | |
2224 | * First see if the job already has a job-uuid attribute; if so, return... | |
2225 | */ | |
2226 | ||
2227 | if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) | |
2228 | return; | |
2229 | ||
2230 | /* | |
2231 | * No job-uuid attribute, so build a version 3 UUID with the local job | |
2232 | * ID at the end; see RFC 4122 for details. Start with the MD5 sum of | |
2233 | * the ServerName, server name and port that the client connected to, | |
2234 | * and local job ID... | |
2235 | */ | |
2236 | ||
2237 | snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername, | |
2238 | con->serverport, job->id); | |
2239 | ||
2240 | _cupsMD5Init(&md5state); | |
2241 | _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid)); | |
2242 | _cupsMD5Finish(&md5state, md5sum); | |
2243 | ||
2244 | /* | |
2245 | * Format the UUID URI using the MD5 sum and job ID. | |
2246 | */ | |
2247 | ||
2248 | snprintf(uuid, sizeof(uuid), | |
2249 | "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" | |
2250 | "%02x%02x%02x%02x%02x%02x", | |
2251 | md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], | |
2252 | (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, | |
2253 | md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], | |
2254 | md5sum[14], md5sum[15]); | |
2255 | ||
2256 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid); | |
2257 | } | |
2258 | ||
2259 | ||
2260 | /* | |
2261 | * 'add_printer()' - Add a printer to the system. | |
2262 | */ | |
2263 | ||
2264 | static void | |
2265 | add_printer(cupsd_client_t *con, /* I - Client connection */ | |
2266 | ipp_attribute_t *uri) /* I - URI of printer */ | |
2267 | { | |
2268 | http_status_t status; /* Policy status */ | |
2269 | int i; /* Looping var */ | |
2270 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ | |
2271 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
2272 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2273 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2274 | int port; /* Port portion of URI */ | |
2275 | cupsd_printer_t *printer; /* Printer/class */ | |
2276 | ipp_attribute_t *attr; /* Printer attribute */ | |
2277 | cups_file_t *fp; /* Script/PPD file */ | |
2278 | char line[1024]; /* Line from file... */ | |
2279 | char srcfile[1024], /* Source Script/PPD file */ | |
2280 | dstfile[1024]; /* Destination Script/PPD file */ | |
2281 | int modify; /* Non-zero if we are modifying */ | |
2282 | char newname[IPP_MAX_NAME]; /* New printer name */ | |
2283 | int need_restart_job; /* Need to restart job? */ | |
2284 | int set_device_uri, /* Did we set the device URI? */ | |
2285 | set_port_monitor; /* Did we set the port monitor? */ | |
2286 | ||
2287 | ||
2288 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, | |
2289 | con->http.fd, uri->values[0].string.text); | |
2290 | ||
2291 | /* | |
2292 | * Do we have a valid URI? | |
2293 | */ | |
2294 | ||
2295 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
2296 | sizeof(scheme), username, sizeof(username), host, | |
2297 | sizeof(host), &port, resource, sizeof(resource)); | |
2298 | ||
2299 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
2300 | { | |
2301 | /* | |
2302 | * No, return an error... | |
2303 | */ | |
2304 | ||
2305 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2306 | _("The printer-uri must be of the form " | |
2307 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
2308 | return; | |
2309 | } | |
2310 | ||
2311 | /* | |
2312 | * Do we have a valid printer name? | |
2313 | */ | |
2314 | ||
2315 | if (!validate_name(resource + 10)) | |
2316 | { | |
2317 | /* | |
2318 | * No, return an error... | |
2319 | */ | |
2320 | ||
2321 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2322 | _("The printer-uri \"%s\" contains invalid characters."), | |
2323 | uri->values[0].string.text); | |
2324 | return; | |
2325 | } | |
2326 | ||
2327 | /* | |
2328 | * See if the printer already exists; if not, create a new printer... | |
2329 | */ | |
2330 | ||
2331 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) | |
2332 | { | |
2333 | /* | |
2334 | * Printer doesn't exist; see if we have a class of the same name... | |
2335 | */ | |
2336 | ||
2337 | if ((printer = cupsdFindClass(resource + 10)) != NULL && | |
2338 | !(printer->type & CUPS_PRINTER_DISCOVERED)) | |
2339 | { | |
2340 | /* | |
2341 | * Yes, return an error... | |
2342 | */ | |
2343 | ||
2344 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2345 | _("A class named \"%s\" already exists!"), | |
2346 | resource + 10); | |
2347 | return; | |
2348 | } | |
2349 | ||
2350 | /* | |
2351 | * No, check the default policy then add the printer... | |
2352 | */ | |
2353 | ||
2354 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
2355 | { | |
2356 | send_http_error(con, status, NULL); | |
2357 | return; | |
2358 | } | |
2359 | ||
2360 | printer = cupsdAddPrinter(resource + 10); | |
2361 | modify = 0; | |
2362 | } | |
2363 | else if (printer->type & CUPS_PRINTER_IMPLICIT) | |
2364 | { | |
2365 | /* | |
2366 | * Check the default policy, then rename the implicit printer to | |
2367 | * "AnyPrinter" or delete it... | |
2368 | */ | |
2369 | ||
2370 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
2371 | { | |
2372 | send_http_error(con, status, NULL); | |
2373 | return; | |
2374 | } | |
2375 | ||
2376 | if (ImplicitAnyClasses) | |
2377 | { | |
2378 | snprintf(newname, sizeof(newname), "Any%s", resource + 10); | |
2379 | cupsdRenamePrinter(printer, newname); | |
2380 | } | |
2381 | else | |
2382 | cupsdDeletePrinter(printer, 1); | |
2383 | ||
2384 | /* | |
2385 | * Add the printer as a new local printer... | |
2386 | */ | |
2387 | ||
2388 | printer = cupsdAddPrinter(resource + 10); | |
2389 | modify = 0; | |
2390 | } | |
2391 | else if (printer->type & CUPS_PRINTER_DISCOVERED) | |
2392 | { | |
2393 | /* | |
2394 | * Check the default policy, then rename the remote printer to | |
2395 | * "Printer@server"... | |
2396 | */ | |
2397 | ||
2398 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
2399 | { | |
2400 | send_http_error(con, status, NULL); | |
2401 | return; | |
2402 | } | |
2403 | ||
2404 | snprintf(newname, sizeof(newname), "%s@%s", resource + 10, | |
2405 | printer->hostname); | |
2406 | cupsdRenamePrinter(printer, newname); | |
2407 | ||
2408 | /* | |
2409 | * Add the printer as a new local printer... | |
2410 | */ | |
2411 | ||
2412 | printer = cupsdAddPrinter(resource + 10); | |
2413 | modify = 0; | |
2414 | } | |
2415 | else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, | |
2416 | NULL)) != HTTP_OK) | |
2417 | { | |
2418 | send_http_error(con, status, printer); | |
2419 | return; | |
2420 | } | |
2421 | else | |
2422 | modify = 1; | |
2423 | ||
2424 | /* | |
2425 | * Look for attributes and copy them over as needed... | |
2426 | */ | |
2427 | ||
2428 | need_restart_job = 0; | |
2429 | ||
2430 | if ((attr = ippFindAttribute(con->request, "printer-location", | |
2431 | IPP_TAG_TEXT)) != NULL) | |
2432 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
2433 | ||
2434 | if ((attr = ippFindAttribute(con->request, "printer-info", | |
2435 | IPP_TAG_TEXT)) != NULL) | |
2436 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
2437 | ||
2438 | set_device_uri = 0; | |
2439 | ||
2440 | if ((attr = ippFindAttribute(con->request, "device-uri", | |
2441 | IPP_TAG_URI)) != NULL) | |
2442 | { | |
2443 | /* | |
2444 | * Do we have a valid device URI? | |
2445 | */ | |
2446 | ||
2447 | http_uri_status_t uri_status; /* URI separation status */ | |
2448 | ||
2449 | ||
2450 | need_restart_job = 1; | |
2451 | ||
2452 | uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, | |
2453 | attr->values[0].string.text, | |
2454 | scheme, sizeof(scheme), | |
2455 | username, sizeof(username), | |
2456 | host, sizeof(host), &port, | |
2457 | resource, sizeof(resource)); | |
2458 | ||
2459 | if (uri_status < HTTP_URI_OK) | |
2460 | { | |
2461 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), | |
2462 | attr->values[0].string.text); | |
2463 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2464 | "add_printer: httpSeparateURI returned %d", uri_status); | |
2465 | return; | |
2466 | } | |
2467 | ||
2468 | if (!strcmp(scheme, "file")) | |
2469 | { | |
2470 | /* | |
2471 | * See if the administrator has enabled file devices... | |
2472 | */ | |
2473 | ||
2474 | if (!FileDevice && strcmp(resource, "/dev/null")) | |
2475 | { | |
2476 | /* | |
2477 | * File devices are disabled and the URL is not file:/dev/null... | |
2478 | */ | |
2479 | ||
2480 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
2481 | _("File device URIs have been disabled! " | |
2482 | "To enable, see the FileDevice directive in " | |
2483 | "\"%s/cupsd.conf\"."), | |
2484 | ServerRoot); | |
2485 | return; | |
2486 | } | |
2487 | } | |
2488 | else | |
2489 | { | |
2490 | /* | |
2491 | * See if the backend exists and is executable... | |
2492 | */ | |
2493 | ||
2494 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme); | |
2495 | if (access(srcfile, X_OK)) | |
2496 | { | |
2497 | /* | |
2498 | * Could not find device in list! | |
2499 | */ | |
2500 | ||
2501 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), | |
2502 | attr->values[0].string.text); | |
2503 | return; | |
2504 | } | |
2505 | } | |
2506 | ||
2507 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2508 | "Setting %s device-uri to \"%s\" (was \"%s\".)", | |
2509 | printer->name, | |
2510 | cupsdSanitizeURI(attr->values[0].string.text, line, | |
2511 | sizeof(line)), | |
2512 | cupsdSanitizeURI(printer->device_uri, resource, | |
2513 | sizeof(resource))); | |
2514 | ||
2515 | cupsdSetString(&printer->device_uri, attr->values[0].string.text); | |
2516 | set_device_uri = 1; | |
2517 | } | |
2518 | ||
2519 | set_port_monitor = 0; | |
2520 | ||
2521 | if ((attr = ippFindAttribute(con->request, "port-monitor", | |
2522 | IPP_TAG_NAME)) != NULL) | |
2523 | { | |
2524 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ | |
2525 | ||
2526 | ||
2527 | need_restart_job = 1; | |
2528 | ||
2529 | supported = ippFindAttribute(printer->attrs, "port-monitor-supported", | |
2530 | IPP_TAG_NAME); | |
2531 | if (supported) | |
2532 | { | |
2533 | for (i = 0; i < supported->num_values; i ++) | |
2534 | if (!strcmp(supported->values[i].string.text, | |
2535 | attr->values[0].string.text)) | |
2536 | break; | |
2537 | } | |
2538 | ||
2539 | if (!supported || i >= supported->num_values) | |
2540 | { | |
2541 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"), | |
2542 | attr->values[0].string.text); | |
2543 | return; | |
2544 | } | |
2545 | ||
2546 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2547 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2548 | printer->name, attr->values[0].string.text, | |
2549 | printer->port_monitor ? printer->port_monitor : "none"); | |
2550 | ||
2551 | if (strcmp(attr->values[0].string.text, "none")) | |
2552 | cupsdSetString(&printer->port_monitor, attr->values[0].string.text); | |
2553 | else | |
2554 | cupsdClearString(&printer->port_monitor); | |
2555 | ||
2556 | set_port_monitor = 1; | |
2557 | } | |
2558 | ||
2559 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", | |
2560 | IPP_TAG_BOOLEAN)) != NULL) | |
2561 | { | |
2562 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2563 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
2564 | printer->name, attr->values[0].boolean, printer->accepting); | |
2565 | ||
2566 | printer->accepting = attr->values[0].boolean; | |
2567 | cupsdAddPrinterHistory(printer); | |
2568 | } | |
2569 | ||
2570 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", | |
2571 | IPP_TAG_BOOLEAN)) != NULL) | |
2572 | { | |
2573 | if (printer->shared && !attr->values[0].boolean) | |
2574 | cupsdDeregisterPrinter(printer, 1); | |
2575 | ||
2576 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2577 | "Setting %s printer-is-shared to %d (was %d.)", | |
2578 | printer->name, attr->values[0].boolean, printer->shared); | |
2579 | ||
2580 | printer->shared = attr->values[0].boolean; | |
2581 | } | |
2582 | ||
2583 | if ((attr = ippFindAttribute(con->request, "printer-state", | |
2584 | IPP_TAG_ENUM)) != NULL) | |
2585 | { | |
2586 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
2587 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
2588 | { | |
2589 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"), | |
2590 | attr->values[0].integer); | |
2591 | return; | |
2592 | } | |
2593 | ||
2594 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", | |
2595 | printer->name, attr->values[0].integer, printer->state); | |
2596 | ||
2597 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
2598 | cupsdStopPrinter(printer, 0); | |
2599 | else | |
2600 | { | |
2601 | need_restart_job = 1; | |
2602 | cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0); | |
2603 | } | |
2604 | } | |
2605 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
2606 | IPP_TAG_TEXT)) != NULL) | |
2607 | { | |
2608 | strlcpy(printer->state_message, attr->values[0].string.text, | |
2609 | sizeof(printer->state_message)); | |
2610 | cupsdAddPrinterHistory(printer); | |
2611 | } | |
2612 | ||
2613 | set_printer_defaults(con, printer); | |
2614 | ||
2615 | if ((attr = ippFindAttribute(con->request, "auth-info-required", | |
2616 | IPP_TAG_KEYWORD)) != NULL) | |
2617 | cupsdSetAuthInfoRequired(printer, NULL, attr); | |
2618 | ||
2619 | /* | |
2620 | * See if we have all required attributes... | |
2621 | */ | |
2622 | ||
2623 | if (!printer->device_uri) | |
2624 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
2625 | ||
2626 | /* | |
2627 | * See if we have an interface script or PPD file attached to the request... | |
2628 | */ | |
2629 | ||
2630 | if (con->filename) | |
2631 | { | |
2632 | need_restart_job = 1; | |
2633 | ||
2634 | strlcpy(srcfile, con->filename, sizeof(srcfile)); | |
2635 | ||
2636 | if ((fp = cupsFileOpen(srcfile, "rb"))) | |
2637 | { | |
2638 | /* | |
2639 | * Yes; get the first line from it... | |
2640 | */ | |
2641 | ||
2642 | line[0] = '\0'; | |
2643 | cupsFileGets(fp, line, sizeof(line)); | |
2644 | cupsFileClose(fp); | |
2645 | ||
2646 | /* | |
2647 | * Then see what kind of file it is... | |
2648 | */ | |
2649 | ||
2650 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2651 | printer->name); | |
2652 | ||
2653 | if (!strncmp(line, "*PPD-Adobe", 10)) | |
2654 | { | |
2655 | /* | |
2656 | * The new file is a PPD file, so remove any old interface script | |
2657 | * that might be lying around... | |
2658 | */ | |
2659 | ||
2660 | unlink(dstfile); | |
2661 | } | |
2662 | else | |
2663 | { | |
2664 | /* | |
2665 | * This must be an interface script, so move the file over to the | |
2666 | * interfaces directory and make it executable... | |
2667 | */ | |
2668 | ||
2669 | if (copy_file(srcfile, dstfile)) | |
2670 | { | |
2671 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2672 | _("Unable to copy interface script - %s!"), | |
2673 | strerror(errno)); | |
2674 | return; | |
2675 | } | |
2676 | ||
2677 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2678 | "Copied interface script successfully!"); | |
2679 | chmod(dstfile, 0755); | |
2680 | } | |
2681 | ||
2682 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2683 | printer->name); | |
2684 | ||
2685 | if (!strncmp(line, "*PPD-Adobe", 10)) | |
2686 | { | |
2687 | /* | |
2688 | * The new file is a PPD file, so move the file over to the | |
2689 | * ppd directory and make it readable by all... | |
2690 | */ | |
2691 | ||
2692 | if (copy_file(srcfile, dstfile)) | |
2693 | { | |
2694 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2695 | _("Unable to copy PPD file - %s!"), | |
2696 | strerror(errno)); | |
2697 | return; | |
2698 | } | |
2699 | ||
2700 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2701 | "Copied PPD file successfully!"); | |
2702 | chmod(dstfile, 0644); | |
2703 | ||
2704 | #ifdef __APPLE__ | |
2705 | /* | |
2706 | * (Re)register color profiles... | |
2707 | */ | |
2708 | ||
2709 | apple_unregister_profiles(printer); | |
2710 | apple_register_profiles(printer); | |
2711 | #endif /* __APPLE__ */ | |
2712 | } | |
2713 | else | |
2714 | { | |
2715 | /* | |
2716 | * This must be an interface script, so remove any old PPD file that | |
2717 | * may be lying around... | |
2718 | */ | |
2719 | ||
2720 | unlink(dstfile); | |
2721 | } | |
2722 | } | |
2723 | } | |
2724 | else if ((attr = ippFindAttribute(con->request, "ppd-name", | |
2725 | IPP_TAG_NAME)) != NULL) | |
2726 | { | |
2727 | need_restart_job = 1; | |
2728 | ||
2729 | if (!strcmp(attr->values[0].string.text, "raw")) | |
2730 | { | |
2731 | /* | |
2732 | * Raw driver, remove any existing PPD or interface script files. | |
2733 | */ | |
2734 | ||
2735 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2736 | printer->name); | |
2737 | unlink(dstfile); | |
2738 | ||
2739 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2740 | printer->name); | |
2741 | unlink(dstfile); | |
2742 | } | |
2743 | else | |
2744 | { | |
2745 | /* | |
2746 | * PPD model file... | |
2747 | */ | |
2748 | ||
2749 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2750 | printer->name); | |
2751 | unlink(dstfile); | |
2752 | ||
2753 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2754 | printer->name); | |
2755 | ||
2756 | if (copy_model(con, attr->values[0].string.text, dstfile)) | |
2757 | { | |
2758 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!")); | |
2759 | return; | |
2760 | } | |
2761 | ||
2762 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2763 | "Copied PPD file successfully!"); | |
2764 | chmod(dstfile, 0644); | |
2765 | ||
2766 | #ifdef __APPLE__ | |
2767 | /* | |
2768 | * (Re)register color profiles... | |
2769 | */ | |
2770 | ||
2771 | apple_unregister_profiles(printer); | |
2772 | apple_register_profiles(printer); | |
2773 | #endif /* __APPLE__ */ | |
2774 | } | |
2775 | } | |
2776 | ||
2777 | /* | |
2778 | * If we set the device URI but not the port monitor, check which port | |
2779 | * monitor to use by default... | |
2780 | */ | |
2781 | ||
2782 | if (set_device_uri && !set_port_monitor) | |
2783 | { | |
2784 | ppd_file_t *ppd; /* PPD file */ | |
2785 | ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */ | |
2786 | ||
2787 | ||
2788 | httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme, | |
2789 | sizeof(scheme), username, sizeof(username), host, | |
2790 | sizeof(host), &port, resource, sizeof(resource)); | |
2791 | ||
2792 | snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, | |
2793 | printer->name); | |
2794 | if ((ppd = ppdOpenFile(srcfile)) != NULL) | |
2795 | { | |
2796 | for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL); | |
2797 | ppdattr; | |
2798 | ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL)) | |
2799 | if (!strcmp(scheme, ppdattr->spec)) | |
2800 | { | |
2801 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2802 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2803 | printer->name, ppdattr->value, | |
2804 | printer->port_monitor ? printer->port_monitor | |
2805 | : "none"); | |
2806 | ||
2807 | if (strcmp(ppdattr->value, "none")) | |
2808 | cupsdSetString(&printer->port_monitor, ppdattr->value); | |
2809 | else | |
2810 | cupsdClearString(&printer->port_monitor); | |
2811 | ||
2812 | break; | |
2813 | } | |
2814 | ||
2815 | ppdClose(ppd); | |
2816 | } | |
2817 | } | |
2818 | ||
2819 | /* | |
2820 | * Update the printer attributes and return... | |
2821 | */ | |
2822 | ||
2823 | cupsdSetPrinterAttrs(printer); | |
2824 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
2825 | ||
2826 | if (need_restart_job && printer->job) | |
2827 | { | |
2828 | cupsd_job_t *job; | |
2829 | ||
2830 | /* | |
2831 | * Stop the current job and then restart it below... | |
2832 | */ | |
2833 | ||
2834 | job = (cupsd_job_t *)printer->job; | |
2835 | ||
2836 | cupsdStopJob(job, 1); | |
2837 | ||
2838 | job->state->values[0].integer = IPP_JOB_PENDING; | |
2839 | job->state_value = IPP_JOB_PENDING; | |
2840 | } | |
2841 | ||
2842 | if (need_restart_job) | |
2843 | cupsdCheckJobs(); | |
2844 | ||
2845 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); | |
2846 | ||
2847 | if (modify) | |
2848 | { | |
2849 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, | |
2850 | "Printer \"%s\" modified by \"%s\".", printer->name, | |
2851 | get_username(con)); | |
2852 | ||
2853 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", | |
2854 | printer->name, get_username(con)); | |
2855 | } | |
2856 | else | |
2857 | { | |
2858 | cupsdAddPrinterHistory(printer); | |
2859 | ||
2860 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL, | |
2861 | "New printer \"%s\" added by \"%s\".", printer->name, | |
2862 | get_username(con)); | |
2863 | ||
2864 | cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".", | |
2865 | printer->name, get_username(con)); | |
2866 | } | |
2867 | ||
2868 | con->response->request.status.status_code = IPP_OK; | |
2869 | } | |
2870 | ||
2871 | ||
2872 | /* | |
2873 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute | |
2874 | * based upon the printer state... | |
2875 | */ | |
2876 | ||
2877 | static void | |
2878 | add_printer_state_reasons( | |
2879 | cupsd_client_t *con, /* I - Client connection */ | |
2880 | cupsd_printer_t *p) /* I - Printer info */ | |
2881 | { | |
2882 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2883 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
2884 | con, con->http.fd, p, p->name); | |
2885 | ||
2886 | if (p->num_reasons == 0) | |
2887 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2888 | "printer-state-reasons", NULL, | |
2889 | p->state == IPP_PRINTER_STOPPED ? "paused" : "none"); | |
2890 | else | |
2891 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2892 | "printer-state-reasons", p->num_reasons, NULL, | |
2893 | (const char * const *)p->reasons); | |
2894 | } | |
2895 | ||
2896 | ||
2897 | /* | |
2898 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2899 | * the specified printer or class. | |
2900 | */ | |
2901 | ||
2902 | static void | |
2903 | add_queued_job_count( | |
2904 | cupsd_client_t *con, /* I - Client connection */ | |
2905 | cupsd_printer_t *p) /* I - Printer or class */ | |
2906 | { | |
2907 | int count; /* Number of jobs on destination */ | |
2908 | ||
2909 | ||
2910 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", | |
2911 | con, con->http.fd, p, p->name); | |
2912 | ||
2913 | count = cupsdGetPrinterJobCount(p->name); | |
2914 | ||
2915 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
2916 | "queued-job-count", count); | |
2917 | } | |
2918 | ||
2919 | ||
2920 | #ifdef __APPLE__ | |
2921 | /* | |
2922 | * 'apple_init_profile()' - Initialize a color profile. | |
2923 | */ | |
2924 | ||
2925 | static void | |
2926 | apple_init_profile( | |
2927 | ppd_file_t *ppd, /* I - PPD file */ | |
2928 | cups_array_t *languages, /* I - Languages in the PPD file */ | |
2929 | CMDeviceProfileInfo *profile, /* I - Profile record */ | |
2930 | unsigned id, /* I - Profile ID */ | |
2931 | const char *name, /* I - Profile name */ | |
2932 | const char *text, /* I - Profile UI text */ | |
2933 | const char *iccfile) /* I - ICC filename */ | |
2934 | { | |
2935 | char url[1024]; /* URL for profile filename */ | |
2936 | CFMutableDictionaryRef dict; /* Dictionary for name */ | |
2937 | char *language; /* Current language */ | |
2938 | ppd_attr_t *attr; /* Profile attribute */ | |
2939 | CFStringRef cflang, /* Language string */ | |
2940 | cftext; /* Localized text */ | |
2941 | ||
2942 | ||
2943 | /* | |
2944 | * Build the profile name dictionary... | |
2945 | */ | |
2946 | ||
2947 | dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
2948 | &kCFTypeDictionaryKeyCallBacks, | |
2949 | &kCFTypeDictionaryValueCallBacks); | |
2950 | ||
2951 | cftext = CFStringCreateWithCString(kCFAllocatorDefault, text, | |
2952 | kCFStringEncodingUTF8); | |
2953 | ||
2954 | if (cftext) | |
2955 | { | |
2956 | CFDictionarySetValue(dict, CFSTR("en"), cftext); | |
2957 | CFRelease(cftext); | |
2958 | } | |
2959 | ||
2960 | if (languages) | |
2961 | { | |
2962 | /* | |
2963 | * Find localized names for the color profiles... | |
2964 | */ | |
2965 | ||
2966 | cupsArraySave(ppd->sorted_attrs); | |
2967 | ||
2968 | for (language = (char *)cupsArrayFirst(languages); | |
2969 | language; | |
2970 | language = (char *)cupsArrayNext(languages)) | |
2971 | { | |
2972 | if (iccfile) | |
2973 | { | |
2974 | if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name, | |
2975 | language)) == NULL) | |
2976 | attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language); | |
2977 | } | |
2978 | else | |
2979 | attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language); | |
2980 | ||
2981 | if (attr && attr->text[0]) | |
2982 | { | |
2983 | cflang = CFStringCreateWithCString(kCFAllocatorDefault, language, | |
2984 | kCFStringEncodingUTF8); | |
2985 | cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text, | |
2986 | kCFStringEncodingUTF8); | |
2987 | ||
2988 | if (cflang && cftext) | |
2989 | CFDictionarySetValue(dict, cflang, cftext); | |
2990 | ||
2991 | if (cflang) | |
2992 | CFRelease(cflang); | |
2993 | ||
2994 | if (cftext) | |
2995 | CFRelease(cftext); | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | cupsArrayRestore(ppd->sorted_attrs); | |
3000 | } | |
3001 | ||
3002 | /* | |
3003 | * Fill in the profile data... | |
3004 | */ | |
3005 | ||
3006 | if (iccfile) | |
3007 | httpAssembleURI(HTTP_URI_CODING_ALL, url, sizeof(url), "file", NULL, "", 0, | |
3008 | iccfile); | |
3009 | ||
3010 | profile->dataVersion = cmDeviceProfileInfoVersion1; | |
3011 | profile->profileID = id; | |
3012 | profile->profileLoc.locType = iccfile ? cmPathBasedProfile : cmNoProfileBase; | |
3013 | profile->profileName = dict; | |
3014 | ||
3015 | if (iccfile) | |
3016 | strlcpy(profile->profileLoc.u.pathLoc.path, iccfile, | |
3017 | sizeof(profile->profileLoc.u.pathLoc.path)); | |
3018 | } | |
3019 | ||
3020 | ||
3021 | /* | |
3022 | * 'apple_register_profiles()' - Register color profiles for a printer. | |
3023 | */ | |
3024 | ||
3025 | static void | |
3026 | apple_register_profiles( | |
3027 | cupsd_printer_t *p) /* I - Printer */ | |
3028 | { | |
3029 | int i; /* Looping var */ | |
3030 | char ppdfile[1024], /* PPD filename */ | |
3031 | iccfile[1024], /* ICC filename */ | |
3032 | selector[PPD_MAX_NAME]; | |
3033 | /* Profile selection string */ | |
3034 | ppd_file_t *ppd; /* PPD file */ | |
3035 | ppd_attr_t *attr, /* Profile attributes */ | |
3036 | *profileid_attr,/* cupsProfileID attribute */ | |
3037 | *q1_attr, /* ColorModel (or other) qualifier */ | |
3038 | *q2_attr, /* MediaType (or other) qualifier */ | |
3039 | *q3_attr; /* Resolution (or other) qualifier */ | |
3040 | char q_keyword[PPD_MAX_NAME]; | |
3041 | /* Qualifier keyword */ | |
3042 | const char *q1_choice, /* ColorModel (or other) choice */ | |
3043 | *q2_choice, /* MediaType (or other) choice */ | |
3044 | *q3_choice; /* Resolution (or other) choice */ | |
3045 | const char *profile_key; /* Profile keyword */ | |
3046 | ppd_option_t *cm_option; /* Color model option */ | |
3047 | ppd_choice_t *cm_choice; /* Color model choice */ | |
3048 | int num_profiles; /* Number of profiles */ | |
3049 | CMError error; /* Last error */ | |
3050 | unsigned device_id, /* Printer device ID */ | |
3051 | profile_id, /* Profile ID */ | |
3052 | default_profile_id = 0; | |
3053 | /* Default profile ID */ | |
3054 | CFMutableDictionaryRef device_name; /* Printer device name dictionary */ | |
3055 | CFStringRef printer_name; /* Printer name string */ | |
3056 | CMDeviceScope scope = /* Scope of the registration */ | |
3057 | { | |
3058 | kCFPreferencesAnyUser, | |
3059 | kCFPreferencesCurrentHost | |
3060 | }; | |
3061 | CMDeviceProfileArrayPtr profiles; /* Profiles */ | |
3062 | CMDeviceProfileInfo *profile; /* Current profile */ | |
3063 | cups_array_t *languages; /* Languages array */ | |
3064 | ||
3065 | ||
3066 | /* | |
3067 | * Make sure ColorSync is available... | |
3068 | */ | |
3069 | ||
3070 | if (CMRegisterColorDevice == NULL) | |
3071 | return; | |
3072 | ||
3073 | /* | |
3074 | * Try opening the PPD file for this printer... | |
3075 | */ | |
3076 | ||
3077 | snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name); | |
3078 | if ((ppd = ppdOpenFile(ppdfile)) == NULL) | |
3079 | return; | |
3080 | ||
3081 | /* | |
3082 | * See if we have any profiles... | |
3083 | */ | |
3084 | ||
3085 | if ((attr = ppdFindAttr(ppd, "APTiogaProfile", NULL)) != NULL) | |
3086 | profile_key = "APTiogaProfile"; | |
3087 | else | |
3088 | { | |
3089 | attr = ppdFindAttr(ppd, "cupsICCProfile", NULL); | |
3090 | profile_key = "cupsICCProfile"; | |
3091 | } | |
3092 | ||
3093 | for (num_profiles = 0; attr; attr = ppdFindNextAttr(ppd, profile_key, NULL)) | |
3094 | if (attr->spec[0] && attr->value && attr->value[0]) | |
3095 | { | |
3096 | if (attr->value[0] != '/') | |
3097 | snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir, | |
3098 | attr->value); | |
3099 | else | |
3100 | strlcpy(iccfile, attr->value, sizeof(iccfile)); | |
3101 | ||
3102 | if (access(iccfile, 0)) | |
3103 | continue; | |
3104 | ||
3105 | num_profiles ++; | |
3106 | } | |
3107 | ||
3108 | ||
3109 | /* | |
3110 | * If we have profiles, add them... | |
3111 | */ | |
3112 | ||
3113 | if (num_profiles > 0) | |
3114 | { | |
3115 | if (profile_key[0] == 'A') | |
3116 | { | |
3117 | /* | |
3118 | * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile | |
3119 | * attribute... | |
3120 | */ | |
3121 | ||
3122 | if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL && | |
3123 | attr->value) | |
3124 | default_profile_id = atoi(attr->value); | |
3125 | ||
3126 | q1_choice = q2_choice = q3_choice = NULL; | |
3127 | } | |
3128 | else | |
3129 | { | |
3130 | /* | |
3131 | * For CUPS PPDs, figure out the default profile selector values... | |
3132 | */ | |
3133 | ||
3134 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL && | |
3135 | attr->value && attr->value[0]) | |
3136 | { | |
3137 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3138 | q1_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3139 | } | |
3140 | else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL) | |
3141 | q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL); | |
3142 | ||
3143 | if (q1_attr && q1_attr->value && q1_attr->value[0]) | |
3144 | q1_choice = q1_attr->value; | |
3145 | else | |
3146 | q1_choice = ""; | |
3147 | ||
3148 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL && | |
3149 | attr->value && attr->value[0]) | |
3150 | { | |
3151 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3152 | q2_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3153 | } | |
3154 | else | |
3155 | q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL); | |
3156 | ||
3157 | if (q2_attr && q2_attr->value && q2_attr->value[0]) | |
3158 | q2_choice = q2_attr->value; | |
3159 | else | |
3160 | q2_choice = NULL; | |
3161 | ||
3162 | if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL && | |
3163 | attr->value && attr->value[0]) | |
3164 | { | |
3165 | snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value); | |
3166 | q3_attr = ppdFindAttr(ppd, q_keyword, NULL); | |
3167 | } | |
3168 | else | |
3169 | q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL); | |
3170 | ||
3171 | if (q3_attr && q3_attr->value && q3_attr->value[0]) | |
3172 | q3_choice = q3_attr->value; | |
3173 | else | |
3174 | q3_choice = NULL; | |
3175 | } | |
3176 | ||
3177 | /* | |
3178 | * Build the array of profiles... | |
3179 | * | |
3180 | * Note: This calloc actually requests slightly more memory than needed. | |
3181 | */ | |
3182 | ||
3183 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) | |
3184 | { | |
3185 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3186 | "Unable to allocate memory for %d profiles!", | |
3187 | num_profiles); | |
3188 | ppdClose(ppd); | |
3189 | return; | |
3190 | } | |
3191 | ||
3192 | profiles->profileCount = num_profiles; | |
3193 | languages = _ppdGetLanguages(ppd); | |
3194 | ||
3195 | for (profile = profiles->profiles, | |
3196 | attr = ppdFindAttr(ppd, profile_key, NULL); | |
3197 | attr; | |
3198 | attr = ppdFindNextAttr(ppd, profile_key, NULL)) | |
3199 | if (attr->spec[0] && attr->value && attr->value[0]) | |
3200 | { | |
3201 | /* | |
3202 | * Add this profile... | |
3203 | */ | |
3204 | ||
3205 | if (attr->value[0] != '/') | |
3206 | snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir, | |
3207 | attr->value); | |
3208 | else | |
3209 | strlcpy(iccfile, attr->value, sizeof(iccfile)); | |
3210 | ||
3211 | if (access(iccfile, 0)) | |
3212 | continue; | |
3213 | ||
3214 | if (profile_key[0] == 'c') | |
3215 | { | |
3216 | cupsArraySave(ppd->sorted_attrs); | |
3217 | ||
3218 | if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID", | |
3219 | attr->spec)) != NULL && | |
3220 | profileid_attr->value && isdigit(profileid_attr->value[0] & 255)) | |
3221 | profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10); | |
3222 | else | |
3223 | profile_id = _ppdHashName(attr->spec); | |
3224 | ||
3225 | cupsArrayRestore(ppd->sorted_attrs); | |
3226 | } | |
3227 | else | |
3228 | profile_id = atoi(attr->spec); | |
3229 | ||
3230 | apple_init_profile(ppd, languages, profile, profile_id, attr->spec, | |
3231 | attr->text[0] ? attr->text : attr->spec, iccfile); | |
3232 | ||
3233 | profile ++; | |
3234 | ||
3235 | /* | |
3236 | * See if this is the default profile... | |
3237 | */ | |
3238 | ||
3239 | if (!default_profile_id) | |
3240 | { | |
3241 | if (q2_choice) | |
3242 | { | |
3243 | if (q3_choice) | |
3244 | { | |
3245 | snprintf(selector, sizeof(selector), "%s.%s.%s", | |
3246 | q1_choice, q2_choice, q3_choice); | |
3247 | if (!strcmp(selector, attr->spec)) | |
3248 | default_profile_id = profile_id; | |
3249 | } | |
3250 | ||
3251 | if (!default_profile_id) | |
3252 | { | |
3253 | snprintf(selector, sizeof(selector), "%s.%s.", q1_choice, | |
3254 | q2_choice); | |
3255 | if (!strcmp(selector, attr->spec)) | |
3256 | default_profile_id = profile_id; | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | if (!default_profile_id && q3_choice) | |
3261 | { | |
3262 | snprintf(selector, sizeof(selector), "%s..%s", q1_choice, | |
3263 | q3_choice); | |
3264 | if (!strcmp(selector, attr->spec)) | |
3265 | default_profile_id = profile_id; | |
3266 | } | |
3267 | ||
3268 | if (!default_profile_id) | |
3269 | { | |
3270 | snprintf(selector, sizeof(selector), "%s..", q1_choice); | |
3271 | if (!strcmp(selector, attr->spec)) | |
3272 | default_profile_id = profile_id; | |
3273 | } | |
3274 | } | |
3275 | } | |
3276 | ||
3277 | _ppdFreeLanguages(languages); | |
3278 | } | |
3279 | else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL) | |
3280 | { | |
3281 | /* | |
3282 | * Extract profiles from ColorModel option... | |
3283 | */ | |
3284 | ||
3285 | const char *profile_name; /* Name of generic profile */ | |
3286 | ||
3287 | ||
3288 | num_profiles = cm_option->num_choices; | |
3289 | ||
3290 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) | |
3291 | { | |
3292 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3293 | "Unable to allocate memory for %d profiles!", | |
3294 | num_profiles); | |
3295 | ppdClose(ppd); | |
3296 | return; | |
3297 | } | |
3298 | ||
3299 | profiles->profileCount = num_profiles; | |
3300 | ||
3301 | for (profile = profiles->profiles, i = cm_option->num_choices, | |
3302 | cm_choice = cm_option->choices; | |
3303 | i > 0; | |
3304 | i --, cm_choice ++, profile ++) | |
3305 | { | |
3306 | if (!strcmp(cm_choice->choice, "Gray") || | |
3307 | !strcmp(cm_choice->choice, "Black")) | |
3308 | profile_name = "Gray"; | |
3309 | else if (!strcmp(cm_choice->choice, "RGB") || | |
3310 | !strcmp(cm_choice->choice, "CMY")) | |
3311 | profile_name = "RGB"; | |
3312 | else if (!strcmp(cm_choice->choice, "CMYK") || | |
3313 | !strcmp(cm_choice->choice, "KCMY")) | |
3314 | profile_name = "CMYK"; | |
3315 | else | |
3316 | profile_name = "DeviceN"; | |
3317 | ||
3318 | snprintf(selector, sizeof(selector), "%s..", profile_name); | |
3319 | profile_id = _ppdHashName(selector); | |
3320 | ||
3321 | apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice, | |
3322 | cm_choice->text, NULL); | |
3323 | ||
3324 | if (cm_choice->marked) | |
3325 | default_profile_id = profile_id; | |
3326 | } | |
3327 | } | |
3328 | else | |
3329 | { | |
3330 | /* | |
3331 | * Use the default colorspace... | |
3332 | */ | |
3333 | ||
3334 | num_profiles = 1 + ppd->colorspace != PPD_CS_GRAY; | |
3335 | ||
3336 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) | |
3337 | { | |
3338 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3339 | "Unable to allocate memory for %d profiles!", | |
3340 | num_profiles); | |
3341 | ppdClose(ppd); | |
3342 | return; | |
3343 | } | |
3344 | ||
3345 | profiles->profileCount = num_profiles; | |
3346 | ||
3347 | apple_init_profile(ppd, NULL, profiles->profiles, _ppdHashName("Gray.."), | |
3348 | "Gray", "Gray", NULL); | |
3349 | ||
3350 | switch (ppd->colorspace) | |
3351 | { | |
3352 | case PPD_CS_RGB : | |
3353 | case PPD_CS_CMY : | |
3354 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
3355 | _ppdHashName("RGB.."), "RGB", "RGB", NULL); | |
3356 | break; | |
3357 | case PPD_CS_RGBK : | |
3358 | case PPD_CS_CMYK : | |
3359 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
3360 | _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL); | |
3361 | break; | |
3362 | ||
3363 | case PPD_CS_N : | |
3364 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
3365 | _ppdHashName("DeviceN.."), "DeviceN", "DeviceN", | |
3366 | NULL); | |
3367 | break; | |
3368 | ||
3369 | default : | |
3370 | break; | |
3371 | } | |
3372 | } | |
3373 | ||
3374 | if (num_profiles > 0) | |
3375 | { | |
3376 | /* | |
3377 | * Make sure we have a default profile ID... | |
3378 | */ | |
3379 | ||
3380 | if (!default_profile_id) | |
3381 | default_profile_id = profiles->profiles[num_profiles - 1].profileID; | |
3382 | ||
3383 | /* | |
3384 | * Get the device ID hash and pathelogical name dictionary. | |
3385 | */ | |
3386 | ||
3387 | cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"", | |
3388 | p->name); | |
3389 | ||
3390 | device_id = _ppdHashName(p->name); | |
3391 | device_name = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
3392 | &kCFTypeDictionaryKeyCallBacks, | |
3393 | &kCFTypeDictionaryValueCallBacks); | |
3394 | printer_name = CFStringCreateWithCString(kCFAllocatorDefault, p->name, | |
3395 | kCFStringEncodingUTF8); | |
3396 | ||
3397 | if (device_name && printer_name) | |
3398 | { | |
3399 | CFDictionarySetValue(device_name, CFSTR("en"), printer_name); | |
3400 | ||
3401 | /* | |
3402 | * Register the device with ColorSync... | |
3403 | */ | |
3404 | ||
3405 | error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id, | |
3406 | device_name, &scope); | |
3407 | ||
3408 | /* | |
3409 | * Register the profiles... | |
3410 | */ | |
3411 | ||
3412 | if (error == noErr) | |
3413 | error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id, | |
3414 | default_profile_id, profiles); | |
3415 | } | |
3416 | else | |
3417 | error = 1000; | |
3418 | ||
3419 | /* | |
3420 | * Clean up... | |
3421 | */ | |
3422 | ||
3423 | if (error != noErr) | |
3424 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3425 | "Unable to register ICC color profiles for \"%s\" - %d", | |
3426 | p->name, (int)error); | |
3427 | ||
3428 | for (profile = profiles->profiles; | |
3429 | num_profiles > 0; | |
3430 | profile ++, num_profiles --) | |
3431 | CFRelease(profile->profileName); | |
3432 | ||
3433 | free(profiles); | |
3434 | ||
3435 | if (printer_name) | |
3436 | CFRelease(printer_name); | |
3437 | ||
3438 | if (device_name) | |
3439 | CFRelease(device_name); | |
3440 | } | |
3441 | ||
3442 | ppdClose(ppd); | |
3443 | } | |
3444 | ||
3445 | ||
3446 | /* | |
3447 | * 'apple_unregister_profiles()' - Remove color profiles for the specified | |
3448 | * printer. | |
3449 | */ | |
3450 | ||
3451 | static void | |
3452 | apple_unregister_profiles( | |
3453 | cupsd_printer_t *p) /* I - Printer */ | |
3454 | { | |
3455 | /* | |
3456 | * Make sure ColorSync is available... | |
3457 | */ | |
3458 | ||
3459 | if (CMUnregisterColorDevice != NULL) | |
3460 | CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name)); | |
3461 | } | |
3462 | #endif /* __APPLE__ */ | |
3463 | ||
3464 | /* | |
3465 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
3466 | */ | |
3467 | ||
3468 | static void | |
3469 | apply_printer_defaults( | |
3470 | cupsd_printer_t *printer, /* I - Printer */ | |
3471 | cupsd_job_t *job) /* I - Job */ | |
3472 | { | |
3473 | int i, /* Looping var */ | |
3474 | num_options; /* Number of default options */ | |
3475 | cups_option_t *options, /* Default options */ | |
3476 | *option; /* Current option */ | |
3477 | ||
3478 | ||
3479 | /* | |
3480 | * Collect all of the default options and add the missing ones to the | |
3481 | * job object... | |
3482 | */ | |
3483 | ||
3484 | for (i = printer->num_options, num_options = 0, option = printer->options; | |
3485 | i > 0; | |
3486 | i --, option ++) | |
3487 | if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO)) | |
3488 | { | |
3489 | num_options = cupsAddOption(option->name, option->value, num_options, | |
3490 | &options); | |
3491 | } | |
3492 | ||
3493 | /* | |
3494 | * Encode these options as attributes in the job object... | |
3495 | */ | |
3496 | ||
3497 | cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB); | |
3498 | cupsFreeOptions(num_options, options); | |
3499 | } | |
3500 | ||
3501 | ||
3502 | /* | |
3503 | * 'authenticate_job()' - Set job authentication info. | |
3504 | */ | |
3505 | ||
3506 | static void | |
3507 | authenticate_job(cupsd_client_t *con, /* I - Client connection */ | |
3508 | ipp_attribute_t *uri) /* I - Job URI */ | |
3509 | { | |
3510 | ipp_attribute_t *attr, /* job-id attribute */ | |
3511 | *auth_info; /* auth-info attribute */ | |
3512 | int jobid; /* Job ID */ | |
3513 | cupsd_job_t *job; /* Current job */ | |
3514 | char method[HTTP_MAX_URI], | |
3515 | /* Method portion of URI */ | |
3516 | username[HTTP_MAX_URI], | |
3517 | /* Username portion of URI */ | |
3518 | host[HTTP_MAX_URI], | |
3519 | /* Host portion of URI */ | |
3520 | resource[HTTP_MAX_URI]; | |
3521 | /* Resource portion of URI */ | |
3522 | int port; /* Port portion of URI */ | |
3523 | ||
3524 | ||
3525 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", | |
3526 | con, con->http.fd, uri->values[0].string.text); | |
3527 | ||
3528 | /* | |
3529 | * Start with "everything is OK" status... | |
3530 | */ | |
3531 | ||
3532 | con->response->request.status.status_code = IPP_OK; | |
3533 | ||
3534 | /* | |
3535 | * See if we have a job URI or a printer URI... | |
3536 | */ | |
3537 | ||
3538 | if (!strcmp(uri->name, "printer-uri")) | |
3539 | { | |
3540 | /* | |
3541 | * Got a printer URI; see if we also have a job-id attribute... | |
3542 | */ | |
3543 | ||
3544 | if ((attr = ippFindAttribute(con->request, "job-id", | |
3545 | IPP_TAG_INTEGER)) == NULL) | |
3546 | { | |
3547 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3548 | _("Got a printer-uri attribute but no job-id!")); | |
3549 | return; | |
3550 | } | |
3551 | ||
3552 | jobid = attr->values[0].integer; | |
3553 | } | |
3554 | else | |
3555 | { | |
3556 | /* | |
3557 | * Got a job URI; parse it to get the job ID... | |
3558 | */ | |
3559 | ||
3560 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
3561 | sizeof(method), username, sizeof(username), host, | |
3562 | sizeof(host), &port, resource, sizeof(resource)); | |
3563 | ||
3564 | if (strncmp(resource, "/jobs/", 6)) | |
3565 | { | |
3566 | /* | |
3567 | * Not a valid URI! | |
3568 | */ | |
3569 | ||
3570 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"), | |
3571 | uri->values[0].string.text); | |
3572 | return; | |
3573 | } | |
3574 | ||
3575 | jobid = atoi(resource + 6); | |
3576 | } | |
3577 | ||
3578 | /* | |
3579 | * See if the job exists... | |
3580 | */ | |
3581 | ||
3582 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3583 | { | |
3584 | /* | |
3585 | * Nope - return a "not found" error... | |
3586 | */ | |
3587 | ||
3588 | send_ipp_status(con, IPP_NOT_FOUND, | |
3589 | _("Job #%d does not exist!"), jobid); | |
3590 | return; | |
3591 | } | |
3592 | ||
3593 | /* | |
3594 | * See if the job has been completed... | |
3595 | */ | |
3596 | ||
3597 | if (job->state_value != IPP_JOB_HELD) | |
3598 | { | |
3599 | /* | |
3600 | * Return a "not-possible" error... | |
3601 | */ | |
3602 | ||
3603 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3604 | _("Job #%d is not held for authentication!"), | |
3605 | jobid); | |
3606 | return; | |
3607 | } | |
3608 | ||
3609 | /* | |
3610 | * See if we have already authenticated... | |
3611 | */ | |
3612 | ||
3613 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); | |
3614 | ||
3615 | if (!con->username[0] && !auth_info) | |
3616 | { | |
3617 | cupsd_printer_t *printer; /* Job destination */ | |
3618 | ||
3619 | ||
3620 | /* | |
3621 | * No auth data. If we need to authenticate via Kerberos, send a | |
3622 | * HTTP auth challenge, otherwise just return an IPP error... | |
3623 | */ | |
3624 | ||
3625 | printer = cupsdFindDest(job->dest); | |
3626 | ||
3627 | if (printer && printer->num_auth_info_required > 0 && | |
3628 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
3629 | send_http_error(con, HTTP_UNAUTHORIZED, printer); | |
3630 | else | |
3631 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
3632 | _("No authentication information provided!")); | |
3633 | return; | |
3634 | } | |
3635 | ||
3636 | /* | |
3637 | * See if the job is owned by the requesting user... | |
3638 | */ | |
3639 | ||
3640 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3641 | { | |
3642 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
3643 | return; | |
3644 | } | |
3645 | ||
3646 | /* | |
3647 | * Save the authentication information for this job... | |
3648 | */ | |
3649 | ||
3650 | save_auth_info(con, job, auth_info); | |
3651 | ||
3652 | /* | |
3653 | * Reset the job-hold-until value to "no-hold"... | |
3654 | */ | |
3655 | ||
3656 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
3657 | IPP_TAG_KEYWORD)) == NULL) | |
3658 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
3659 | ||
3660 | if (attr) | |
3661 | { | |
3662 | attr->value_tag = IPP_TAG_KEYWORD; | |
3663 | cupsdSetString(&(attr->values[0].string.text), "no-hold"); | |
3664 | } | |
3665 | ||
3666 | /* | |
3667 | * Release the job and return... | |
3668 | */ | |
3669 | ||
3670 | cupsdReleaseJob(job); | |
3671 | ||
3672 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Authenticated by \"%s\".", jobid, | |
3673 | con->username); | |
3674 | } | |
3675 | ||
3676 | ||
3677 | /* | |
3678 | * 'cancel_all_jobs()' - Cancel all print jobs. | |
3679 | */ | |
3680 | ||
3681 | static void | |
3682 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
3683 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
3684 | { | |
3685 | http_status_t status; /* Policy status */ | |
3686 | cups_ptype_t dtype; /* Destination type */ | |
3687 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
3688 | userpass[HTTP_MAX_URI], /* Username portion of URI */ | |
3689 | hostname[HTTP_MAX_URI], /* Host portion of URI */ | |
3690 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
3691 | int port; /* Port portion of URI */ | |
3692 | ipp_attribute_t *attr; /* Attribute in request */ | |
3693 | const char *username; /* Username */ | |
3694 | int purge; /* Purge? */ | |
3695 | cupsd_printer_t *printer; /* Printer */ | |
3696 | ||
3697 | ||
3698 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, | |
3699 | con->http.fd, uri->values[0].string.text); | |
3700 | ||
3701 | /* | |
3702 | * See if we have a printer URI... | |
3703 | */ | |
3704 | ||
3705 | if (strcmp(uri->name, "printer-uri")) | |
3706 | { | |
3707 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3708 | _("The printer-uri attribute is required!")); | |
3709 | return; | |
3710 | } | |
3711 | ||
3712 | /* | |
3713 | * Get the username (if any) for the jobs we want to cancel (only if | |
3714 | * "my-jobs" is specified... | |
3715 | */ | |
3716 | ||
3717 | if ((attr = ippFindAttribute(con->request, "my-jobs", | |
3718 | IPP_TAG_BOOLEAN)) != NULL && | |
3719 | attr->values[0].boolean) | |
3720 | { | |
3721 | if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
3722 | IPP_TAG_NAME)) != NULL) | |
3723 | username = attr->values[0].string.text; | |
3724 | else | |
3725 | { | |
3726 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3727 | _("Missing requesting-user-name attribute!")); | |
3728 | return; | |
3729 | } | |
3730 | } | |
3731 | else | |
3732 | username = NULL; | |
3733 | ||
3734 | /* | |
3735 | * Look for the "purge-jobs" attribute... | |
3736 | */ | |
3737 | ||
3738 | if ((attr = ippFindAttribute(con->request, "purge-jobs", | |
3739 | IPP_TAG_BOOLEAN)) != NULL) | |
3740 | purge = attr->values[0].boolean; | |
3741 | else | |
3742 | purge = 1; | |
3743 | ||
3744 | /* | |
3745 | * And if the destination is valid... | |
3746 | */ | |
3747 | ||
3748 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
3749 | { | |
3750 | /* | |
3751 | * Bad URI? | |
3752 | */ | |
3753 | ||
3754 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, | |
3755 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
3756 | hostname, sizeof(hostname), &port, | |
3757 | resource, sizeof(resource)); | |
3758 | ||
3759 | if ((!strncmp(resource, "/printers/", 10) && resource[10]) || | |
3760 | (!strncmp(resource, "/classes/", 9) && resource[9])) | |
3761 | { | |
3762 | send_ipp_status(con, IPP_NOT_FOUND, | |
3763 | _("The printer or class was not found.")); | |
3764 | return; | |
3765 | } | |
3766 | ||
3767 | /* | |
3768 | * Check policy... | |
3769 | */ | |
3770 | ||
3771 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
3772 | { | |
3773 | send_http_error(con, status, NULL); | |
3774 | return; | |
3775 | } | |
3776 | ||
3777 | /* | |
3778 | * Cancel all jobs on all printers... | |
3779 | */ | |
3780 | ||
3781 | cupsdCancelJobs(NULL, username, purge); | |
3782 | ||
3783 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", | |
3784 | purge ? "purged" : "canceled", get_username(con)); | |
3785 | } | |
3786 | else | |
3787 | { | |
3788 | /* | |
3789 | * Check policy... | |
3790 | */ | |
3791 | ||
3792 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, | |
3793 | NULL)) != HTTP_OK) | |
3794 | { | |
3795 | send_http_error(con, status, printer); | |
3796 | return; | |
3797 | } | |
3798 | ||
3799 | /* | |
3800 | * Cancel all of the jobs on the named printer... | |
3801 | */ | |
3802 | ||
3803 | cupsdCancelJobs(printer->name, username, purge); | |
3804 | ||
3805 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
3806 | printer->name, purge ? "purged" : "canceled", | |
3807 | get_username(con)); | |
3808 | } | |
3809 | ||
3810 | con->response->request.status.status_code = IPP_OK; | |
3811 | } | |
3812 | ||
3813 | ||
3814 | /* | |
3815 | * 'cancel_job()' - Cancel a print job. | |
3816 | */ | |
3817 | ||
3818 | static void | |
3819 | cancel_job(cupsd_client_t *con, /* I - Client connection */ | |
3820 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
3821 | { | |
3822 | ipp_attribute_t *attr; /* Current attribute */ | |
3823 | int jobid; /* Job ID */ | |
3824 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
3825 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
3826 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
3827 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
3828 | int port; /* Port portion of URI */ | |
3829 | cupsd_job_t *job; /* Job information */ | |
3830 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
3831 | cupsd_printer_t *printer; /* Printer data */ | |
3832 | int purge; /* Purge the job? */ | |
3833 | ||
3834 | ||
3835 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
3836 | con->http.fd, uri->values[0].string.text); | |
3837 | ||
3838 | /* | |
3839 | * See if we have a job URI or a printer URI... | |
3840 | */ | |
3841 | ||
3842 | if (!strcmp(uri->name, "printer-uri")) | |
3843 | { | |
3844 | /* | |
3845 | * Got a printer URI; see if we also have a job-id attribute... | |
3846 | */ | |
3847 | ||
3848 | if ((attr = ippFindAttribute(con->request, "job-id", | |
3849 | IPP_TAG_INTEGER)) == NULL) | |
3850 | { | |
3851 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3852 | _("Got a printer-uri attribute but no job-id!")); | |
3853 | return; | |
3854 | } | |
3855 | ||
3856 | if ((jobid = attr->values[0].integer) == 0) | |
3857 | { | |
3858 | /* | |
3859 | * Find the current job on the specified printer... | |
3860 | */ | |
3861 | ||
3862 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
3863 | { | |
3864 | /* | |
3865 | * Bad URI... | |
3866 | */ | |
3867 | ||
3868 | send_ipp_status(con, IPP_NOT_FOUND, | |
3869 | _("The printer or class was not found.")); | |
3870 | return; | |
3871 | } | |
3872 | ||
3873 | /* | |
3874 | * See if the printer is currently printing a job... | |
3875 | */ | |
3876 | ||
3877 | if (printer->job) | |
3878 | jobid = ((cupsd_job_t *)printer->job)->id; | |
3879 | else | |
3880 | { | |
3881 | /* | |
3882 | * No, see if there are any pending jobs... | |
3883 | */ | |
3884 | ||
3885 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
3886 | job; | |
3887 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
3888 | if (job->state_value <= IPP_JOB_PROCESSING && | |
3889 | !strcasecmp(job->dest, printer->name)) | |
3890 | break; | |
3891 | ||
3892 | if (job) | |
3893 | jobid = job->id; | |
3894 | else | |
3895 | { | |
3896 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
3897 | job; | |
3898 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
3899 | if (job->state_value == IPP_JOB_STOPPED && | |
3900 | !strcasecmp(job->dest, printer->name)) | |
3901 | break; | |
3902 | ||
3903 | if (job) | |
3904 | jobid = job->id; | |
3905 | else | |
3906 | { | |
3907 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"), | |
3908 | printer->name); | |
3909 | return; | |
3910 | } | |
3911 | } | |
3912 | } | |
3913 | } | |
3914 | } | |
3915 | else | |
3916 | { | |
3917 | /* | |
3918 | * Got a job URI; parse it to get the job ID... | |
3919 | */ | |
3920 | ||
3921 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
3922 | sizeof(scheme), username, sizeof(username), host, | |
3923 | sizeof(host), &port, resource, sizeof(resource)); | |
3924 | ||
3925 | if (strncmp(resource, "/jobs/", 6)) | |
3926 | { | |
3927 | /* | |
3928 | * Not a valid URI! | |
3929 | */ | |
3930 | ||
3931 | send_ipp_status(con, IPP_BAD_REQUEST, | |
3932 | _("Bad job-uri attribute \"%s\"!"), | |
3933 | uri->values[0].string.text); | |
3934 | return; | |
3935 | } | |
3936 | ||
3937 | jobid = atoi(resource + 6); | |
3938 | } | |
3939 | ||
3940 | /* | |
3941 | * Look for the "purge-job" attribute... | |
3942 | */ | |
3943 | ||
3944 | if ((attr = ippFindAttribute(con->request, "purge-job", | |
3945 | IPP_TAG_BOOLEAN)) != NULL) | |
3946 | purge = attr->values[0].boolean; | |
3947 | else | |
3948 | purge = 0; | |
3949 | ||
3950 | /* | |
3951 | * See if the job exists... | |
3952 | */ | |
3953 | ||
3954 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3955 | { | |
3956 | /* | |
3957 | * Nope - return a "not found" error... | |
3958 | */ | |
3959 | ||
3960 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
3961 | return; | |
3962 | } | |
3963 | ||
3964 | /* | |
3965 | * See if the job is owned by the requesting user... | |
3966 | */ | |
3967 | ||
3968 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3969 | { | |
3970 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
3971 | return; | |
3972 | } | |
3973 | ||
3974 | /* | |
3975 | * See if the job is already completed, canceled, or aborted; if so, | |
3976 | * we can't cancel... | |
3977 | */ | |
3978 | ||
3979 | if (job->state_value >= IPP_JOB_CANCELED && !purge) | |
3980 | { | |
3981 | switch (job->state_value) | |
3982 | { | |
3983 | case IPP_JOB_CANCELED : | |
3984 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3985 | _("Job #%d is already canceled - can\'t cancel."), | |
3986 | jobid); | |
3987 | break; | |
3988 | ||
3989 | case IPP_JOB_ABORTED : | |
3990 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3991 | _("Job #%d is already aborted - can\'t cancel."), | |
3992 | jobid); | |
3993 | break; | |
3994 | ||
3995 | default : | |
3996 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3997 | _("Job #%d is already completed - can\'t cancel."), | |
3998 | jobid); | |
3999 | break; | |
4000 | } | |
4001 | ||
4002 | return; | |
4003 | } | |
4004 | ||
4005 | /* | |
4006 | * Cancel the job and return... | |
4007 | */ | |
4008 | ||
4009 | cupsdCancelJob(job, purge, IPP_JOB_CANCELED); | |
4010 | cupsdCheckJobs(); | |
4011 | ||
4012 | if (purge) | |
4013 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid, | |
4014 | username); | |
4015 | else | |
4016 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid, | |
4017 | username); | |
4018 | ||
4019 | con->response->request.status.status_code = IPP_OK; | |
4020 | } | |
4021 | ||
4022 | ||
4023 | /* | |
4024 | * 'cancel_subscription()' - Cancel a subscription. | |
4025 | */ | |
4026 | ||
4027 | static void | |
4028 | cancel_subscription( | |
4029 | cupsd_client_t *con, /* I - Client connection */ | |
4030 | int sub_id) /* I - Subscription ID */ | |
4031 | { | |
4032 | http_status_t status; /* Policy status */ | |
4033 | cupsd_subscription_t *sub; /* Subscription */ | |
4034 | ||
4035 | ||
4036 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4037 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
4038 | con, con->http.fd, sub_id); | |
4039 | ||
4040 | /* | |
4041 | * Is the subscription ID valid? | |
4042 | */ | |
4043 | ||
4044 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
4045 | { | |
4046 | /* | |
4047 | * Bad subscription ID... | |
4048 | */ | |
4049 | ||
4050 | send_ipp_status(con, IPP_NOT_FOUND, | |
4051 | _("notify-subscription-id %d no good!"), sub_id); | |
4052 | return; | |
4053 | } | |
4054 | ||
4055 | /* | |
4056 | * Check policy... | |
4057 | */ | |
4058 | ||
4059 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
4060 | DefaultPolicyPtr, | |
4061 | con, sub->owner)) != HTTP_OK) | |
4062 | { | |
4063 | send_http_error(con, status, sub->dest); | |
4064 | return; | |
4065 | } | |
4066 | ||
4067 | /* | |
4068 | * Cancel the subscription... | |
4069 | */ | |
4070 | ||
4071 | cupsdDeleteSubscription(sub, 1); | |
4072 | ||
4073 | con->response->request.status.status_code = IPP_OK; | |
4074 | } | |
4075 | ||
4076 | ||
4077 | /* | |
4078 | * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI. | |
4079 | */ | |
4080 | ||
4081 | static int /* O - 1 if OK, 0 if not */ | |
4082 | check_rss_recipient( | |
4083 | const char *recipient) /* I - Recipient URI */ | |
4084 | { | |
4085 | cupsd_subscription_t *sub; /* Current subscription */ | |
4086 | ||
4087 | ||
4088 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
4089 | sub; | |
4090 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
4091 | if (sub->recipient) | |
4092 | { | |
4093 | /* | |
4094 | * Compare the URIs up to the first ?... | |
4095 | */ | |
4096 | ||
4097 | const char *r1, *r2; | |
4098 | ||
4099 | for (r1 = recipient, r2 = sub->recipient; | |
4100 | *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?'; | |
4101 | r1 ++, r2 ++); | |
4102 | ||
4103 | if (*r1 == *r2) | |
4104 | return (0); | |
4105 | } | |
4106 | ||
4107 | return (1); | |
4108 | } | |
4109 | ||
4110 | ||
4111 | /* | |
4112 | * 'check_quotas()' - Check quotas for a printer and user. | |
4113 | */ | |
4114 | ||
4115 | static int /* O - 1 if OK, 0 if not */ | |
4116 | check_quotas(cupsd_client_t *con, /* I - Client connection */ | |
4117 | cupsd_printer_t *p) /* I - Printer or class */ | |
4118 | { | |
4119 | int i; /* Looping var */ | |
4120 | char username[33]; /* Username */ | |
4121 | cupsd_quota_t *q; /* Quota data */ | |
4122 | #ifdef HAVE_MBR_UID_TO_UUID | |
4123 | /* | |
4124 | * Use Apple membership APIs which require that all names represent | |
4125 | * valid user account or group records accessible by the server. | |
4126 | */ | |
4127 | ||
4128 | uuid_t usr_uuid; /* UUID for job requesting user */ | |
4129 | uuid_t usr2_uuid; /* UUID for ACL user name entry */ | |
4130 | uuid_t grp_uuid; /* UUID for ACL group name entry */ | |
4131 | int mbr_err; /* Error from membership function */ | |
4132 | int is_member; /* Is this user a member? */ | |
4133 | #else | |
4134 | /* | |
4135 | * Use standard POSIX APIs for checking users and groups... | |
4136 | */ | |
4137 | ||
4138 | struct passwd *pw; /* User password data */ | |
4139 | #endif /* HAVE_MBR_UID_TO_UUID */ | |
4140 | ||
4141 | ||
4142 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", | |
4143 | con, con->http.fd, p, p->name); | |
4144 | ||
4145 | /* | |
4146 | * Figure out who is printing... | |
4147 | */ | |
4148 | ||
4149 | strlcpy(username, get_username(con), sizeof(username)); | |
4150 | ||
4151 | /* | |
4152 | * Check global active job limits for printers and users... | |
4153 | */ | |
4154 | ||
4155 | if (MaxJobsPerPrinter) | |
4156 | { | |
4157 | /* | |
4158 | * Check if there are too many pending jobs on this printer... | |
4159 | */ | |
4160 | ||
4161 | if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter) | |
4162 | { | |
4163 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...", | |
4164 | p->name); | |
4165 | return (0); | |
4166 | } | |
4167 | } | |
4168 | ||
4169 | if (MaxJobsPerUser) | |
4170 | { | |
4171 | /* | |
4172 | * Check if there are too many pending jobs for this user... | |
4173 | */ | |
4174 | ||
4175 | if (cupsdGetUserJobCount(username) >= MaxJobsPerUser) | |
4176 | { | |
4177 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...", | |
4178 | username); | |
4179 | return (0); | |
4180 | } | |
4181 | } | |
4182 | ||
4183 | /* | |
4184 | * Check against users... | |
4185 | */ | |
4186 | ||
4187 | if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0) | |
4188 | return (1); | |
4189 | ||
4190 | if (p->num_users) | |
4191 | { | |
4192 | #ifdef HAVE_MBR_UID_TO_UUID | |
4193 | /* | |
4194 | * Get UUID for job requesting user... | |
4195 | */ | |
4196 | ||
4197 | if (mbr_user_name_to_uuid((char *)username, usr_uuid)) | |
4198 | { | |
4199 | /* | |
4200 | * Unknown user... | |
4201 | */ | |
4202 | ||
4203 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4204 | "check_quotas: UUID lookup failed for user \"%s\"", | |
4205 | username); | |
4206 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4207 | "Denying user \"%s\" access to printer \"%s\" " | |
4208 | "(unknown user)...", | |
4209 | username, p->name); | |
4210 | return (0); | |
4211 | } | |
4212 | #else | |
4213 | /* | |
4214 | * Get UID and GID of requesting user... | |
4215 | */ | |
4216 | ||
4217 | pw = getpwnam(username); | |
4218 | endpwent(); | |
4219 | #endif /* HAVE_MBR_UID_TO_UUID */ | |
4220 | ||
4221 | for (i = 0; i < p->num_users; i ++) | |
4222 | if (p->users[i][0] == '@') | |
4223 | { | |
4224 | /* | |
4225 | * Check group membership... | |
4226 | */ | |
4227 | ||
4228 | #ifdef HAVE_MBR_UID_TO_UUID | |
4229 | if (p->users[i][1] == '#') | |
4230 | { | |
4231 | if (uuid_parse((char *)p->users[i] + 2, grp_uuid)) | |
4232 | uuid_clear(grp_uuid); | |
4233 | } | |
4234 | else if ((mbr_err = mbr_group_name_to_uuid((char *)p->users[i] + 1, | |
4235 | grp_uuid)) != 0) | |
4236 | { | |
4237 | /* | |
4238 | * Invalid ACL entries are ignored for matching; just record a | |
4239 | * warning in the log... | |
4240 | */ | |
4241 | ||
4242 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4243 | "check_quotas: UUID lookup failed for ACL entry " | |
4244 | "\"%s\" (err=%d)", p->users[i], mbr_err); | |
4245 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4246 | "Access control entry \"%s\" not a valid group name; " | |
4247 | "entry ignored", p->users[i]); | |
4248 | } | |
4249 | ||
4250 | if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid, | |
4251 | &is_member)) != 0) | |
4252 | { | |
4253 | /* | |
4254 | * At this point, there should be no errors, but check anyways... | |
4255 | */ | |
4256 | ||
4257 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4258 | "check_quotas: group \"%s\" membership check " | |
4259 | "failed (err=%d)", p->users[i] + 1, mbr_err); | |
4260 | is_member = 0; | |
4261 | } | |
4262 | ||
4263 | /* | |
4264 | * Stop if we found a match... | |
4265 | */ | |
4266 | ||
4267 | if (is_member) | |
4268 | break; | |
4269 | ||
4270 | #else | |
4271 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
4272 | break; | |
4273 | #endif /* HAVE_MBR_UID_TO_UUID */ | |
4274 | } | |
4275 | #ifdef HAVE_MBR_UID_TO_UUID | |
4276 | else | |
4277 | { | |
4278 | if (p->users[i][0] == '#') | |
4279 | { | |
4280 | if (uuid_parse((char *)p->users[i] + 1, usr2_uuid)) | |
4281 | uuid_clear(usr2_uuid); | |
4282 | } | |
4283 | else if ((mbr_err = mbr_user_name_to_uuid((char *)p->users[i], | |
4284 | usr2_uuid)) != 0) | |
4285 | { | |
4286 | /* | |
4287 | * Invalid ACL entries are ignored for matching; just record a | |
4288 | * warning in the log... | |
4289 | */ | |
4290 | ||
4291 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4292 | "check_quotas: UUID lookup failed for ACL entry " | |
4293 | "\"%s\" (err=%d)", p->users[i], mbr_err); | |
4294 | cupsdLogMessage(CUPSD_LOG_WARN, | |
4295 | "Access control entry \"%s\" not a valid user name; " | |
4296 | "entry ignored", p->users[i]); | |
4297 | } | |
4298 | ||
4299 | if ((mbr_err = mbr_check_membership(usr_uuid, usr2_uuid, | |
4300 | &is_member)) != 0) | |
4301 | { | |
4302 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4303 | "check_quotas: User \"%s\" identity check failed " | |
4304 | "(err=%d)", p->users[i], mbr_err); | |
4305 | is_member = 0; | |
4306 | } | |
4307 | ||
4308 | if (is_member) | |
4309 | break; | |
4310 | } | |
4311 | #else | |
4312 | else if (!strcasecmp(username, p->users[i])) | |
4313 | break; | |
4314 | #endif /* HAVE_MBR_UID_TO_UUID */ | |
4315 | ||
4316 | if ((i < p->num_users) == p->deny_users) | |
4317 | { | |
4318 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4319 | "Denying user \"%s\" access to printer \"%s\"...", | |
4320 | username, p->name); | |
4321 | return (0); | |
4322 | } | |
4323 | } | |
4324 | ||
4325 | /* | |
4326 | * Check quotas... | |
4327 | */ | |
4328 | ||
4329 | #ifdef __APPLE__ | |
4330 | if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL) | |
4331 | { | |
4332 | /* | |
4333 | * TODO: Define these special page count values as constants! | |
4334 | */ | |
4335 | ||
4336 | if (q->page_count == -4) /* special case: unlimited user */ | |
4337 | { | |
4338 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4339 | "User \"%s\" request approved for printer %s (%s): " | |
4340 | "unlimited quota.", | |
4341 | username, p->name, p->info); | |
4342 | q->page_count = 0; /* allow user to print */ | |
4343 | return (1); | |
4344 | } | |
4345 | else if (q->page_count == -3) /* quota exceeded */ | |
4346 | { | |
4347 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4348 | "User \"%s\" request denied for printer %s (%s): " | |
4349 | "quota limit exceeded.", | |
4350 | username, p->name, p->info); | |
4351 | q->page_count = 2; /* force quota exceeded failure */ | |
4352 | return (-1); | |
4353 | } | |
4354 | else if (q->page_count == -2) /* quota disabled for user */ | |
4355 | { | |
4356 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4357 | "User \"%s\" request denied for printer %s (%s): " | |
4358 | "printing disabled for user.", | |
4359 | username, p->name, p->info); | |
4360 | q->page_count = 2; /* force quota exceeded failure */ | |
4361 | return (-1); | |
4362 | } | |
4363 | else if (q->page_count == -1) /* quota access error */ | |
4364 | { | |
4365 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4366 | "User \"%s\" request denied for printer %s (%s): " | |
4367 | "unable to determine quota limit.", | |
4368 | username, p->name, p->info); | |
4369 | q->page_count = 2; /* force quota exceeded failure */ | |
4370 | return (-1); | |
4371 | } | |
4372 | else if (q->page_count < 0) /* user not found or other error */ | |
4373 | { | |
4374 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4375 | "User \"%s\" request denied for printer %s (%s): " | |
4376 | "user disabled / missing quota.", | |
4377 | username, p->name, p->info); | |
4378 | q->page_count = 2; /* force quota exceeded failure */ | |
4379 | return (-1); | |
4380 | } | |
4381 | else /* page within user limits */ | |
4382 | { | |
4383 | q->page_count = 0; /* allow user to print */ | |
4384 | return (1); | |
4385 | } | |
4386 | } | |
4387 | else | |
4388 | #endif /* __APPLE__ */ | |
4389 | if (p->k_limit || p->page_limit) | |
4390 | { | |
4391 | if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL) | |
4392 | { | |
4393 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4394 | "Unable to allocate quota data for user \"%s\"!", | |
4395 | username); | |
4396 | return (-1); | |
4397 | } | |
4398 | ||
4399 | if ((q->k_count >= p->k_limit && p->k_limit) || | |
4400 | (q->page_count >= p->page_limit && p->page_limit)) | |
4401 | { | |
4402 | cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...", | |
4403 | username); | |
4404 | return (-1); | |
4405 | } | |
4406 | } | |
4407 | ||
4408 | /* | |
4409 | * If we have gotten this far, we're done! | |
4410 | */ | |
4411 | ||
4412 | return (1); | |
4413 | } | |
4414 | ||
4415 | ||
4416 | /* | |
4417 | * 'copy_attribute()' - Copy a single attribute. | |
4418 | */ | |
4419 | ||
4420 | static ipp_attribute_t * /* O - New attribute */ | |
4421 | copy_attribute( | |
4422 | ipp_t *to, /* O - Destination request/response */ | |
4423 | ipp_attribute_t *attr, /* I - Attribute to copy */ | |
4424 | int quickcopy) /* I - Do a quick copy? */ | |
4425 | { | |
4426 | int i; /* Looping var */ | |
4427 | ipp_attribute_t *toattr; /* Destination attribute */ | |
4428 | ||
4429 | ||
4430 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4431 | "copy_attribute(%p, %p[%s,%x,%x])", to, attr, | |
4432 | attr->name ? attr->name : "(null)", attr->group_tag, | |
4433 | attr->value_tag); | |
4434 | ||
4435 | switch (attr->value_tag & ~IPP_TAG_COPY) | |
4436 | { | |
4437 | case IPP_TAG_ZERO : | |
4438 | toattr = ippAddSeparator(to); | |
4439 | break; | |
4440 | ||
4441 | case IPP_TAG_INTEGER : | |
4442 | case IPP_TAG_ENUM : | |
4443 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
4444 | attr->name, attr->num_values, NULL); | |
4445 | ||
4446 | for (i = 0; i < attr->num_values; i ++) | |
4447 | toattr->values[i].integer = attr->values[i].integer; | |
4448 | break; | |
4449 | ||
4450 | case IPP_TAG_BOOLEAN : | |
4451 | toattr = ippAddBooleans(to, attr->group_tag, attr->name, | |
4452 | attr->num_values, NULL); | |
4453 | ||
4454 | for (i = 0; i < attr->num_values; i ++) | |
4455 | toattr->values[i].boolean = attr->values[i].boolean; | |
4456 | break; | |
4457 | ||
4458 | case IPP_TAG_STRING : | |
4459 | case IPP_TAG_TEXT : | |
4460 | case IPP_TAG_NAME : | |
4461 | case IPP_TAG_KEYWORD : | |
4462 | case IPP_TAG_URI : | |
4463 | case IPP_TAG_URISCHEME : | |
4464 | case IPP_TAG_CHARSET : | |
4465 | case IPP_TAG_LANGUAGE : | |
4466 | case IPP_TAG_MIMETYPE : | |
4467 | toattr = ippAddStrings(to, attr->group_tag, | |
4468 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
4469 | attr->name, attr->num_values, NULL, NULL); | |
4470 | ||
4471 | if (quickcopy) | |
4472 | { | |
4473 | for (i = 0; i < attr->num_values; i ++) | |
4474 | toattr->values[i].string.text = attr->values[i].string.text; | |
4475 | } | |
4476 | else | |
4477 | { | |
4478 | for (i = 0; i < attr->num_values; i ++) | |
4479 | toattr->values[i].string.text = | |
4480 | _cupsStrAlloc(attr->values[i].string.text); | |
4481 | } | |
4482 | break; | |
4483 | ||
4484 | case IPP_TAG_DATE : | |
4485 | toattr = ippAddDate(to, attr->group_tag, attr->name, | |
4486 | attr->values[0].date); | |
4487 | break; | |
4488 | ||
4489 | case IPP_TAG_RESOLUTION : | |
4490 | toattr = ippAddResolutions(to, attr->group_tag, attr->name, | |
4491 | attr->num_values, IPP_RES_PER_INCH, | |
4492 | NULL, NULL); | |
4493 | ||
4494 | for (i = 0; i < attr->num_values; i ++) | |
4495 | { | |
4496 | toattr->values[i].resolution.xres = attr->values[i].resolution.xres; | |
4497 | toattr->values[i].resolution.yres = attr->values[i].resolution.yres; | |
4498 | toattr->values[i].resolution.units = attr->values[i].resolution.units; | |
4499 | } | |
4500 | break; | |
4501 | ||
4502 | case IPP_TAG_RANGE : | |
4503 | toattr = ippAddRanges(to, attr->group_tag, attr->name, | |
4504 | attr->num_values, NULL, NULL); | |
4505 | ||
4506 | for (i = 0; i < attr->num_values; i ++) | |
4507 | { | |
4508 | toattr->values[i].range.lower = attr->values[i].range.lower; | |
4509 | toattr->values[i].range.upper = attr->values[i].range.upper; | |
4510 | } | |
4511 | break; | |
4512 | ||
4513 | case IPP_TAG_TEXTLANG : | |
4514 | case IPP_TAG_NAMELANG : | |
4515 | toattr = ippAddStrings(to, attr->group_tag, | |
4516 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
4517 | attr->name, attr->num_values, NULL, NULL); | |
4518 | ||
4519 | if (quickcopy) | |
4520 | { | |
4521 | for (i = 0; i < attr->num_values; i ++) | |
4522 | { | |
4523 | toattr->values[i].string.charset = attr->values[i].string.charset; | |
4524 | toattr->values[i].string.text = attr->values[i].string.text; | |
4525 | } | |
4526 | } | |
4527 | else | |
4528 | { | |
4529 | for (i = 0; i < attr->num_values; i ++) | |
4530 | { | |
4531 | if (!i) | |
4532 | toattr->values[i].string.charset = | |
4533 | _cupsStrAlloc(attr->values[i].string.charset); | |
4534 | else | |
4535 | toattr->values[i].string.charset = | |
4536 | toattr->values[0].string.charset; | |
4537 | ||
4538 | toattr->values[i].string.text = | |
4539 | _cupsStrAlloc(attr->values[i].string.text); | |
4540 | } | |
4541 | } | |
4542 | break; | |
4543 | ||
4544 | case IPP_TAG_BEGIN_COLLECTION : | |
4545 | toattr = ippAddCollections(to, attr->group_tag, attr->name, | |
4546 | attr->num_values, NULL); | |
4547 | ||
4548 | for (i = 0; i < attr->num_values; i ++) | |
4549 | { | |
4550 | toattr->values[i].collection = ippNew(); | |
4551 | copy_attrs(toattr->values[i].collection, attr->values[i].collection, | |
4552 | NULL, IPP_TAG_ZERO, 0); | |
4553 | } | |
4554 | break; | |
4555 | ||
4556 | default : | |
4557 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
4558 | attr->name, attr->num_values, NULL); | |
4559 | ||
4560 | for (i = 0; i < attr->num_values; i ++) | |
4561 | { | |
4562 | toattr->values[i].unknown.length = attr->values[i].unknown.length; | |
4563 | ||
4564 | if (toattr->values[i].unknown.length > 0) | |
4565 | { | |
4566 | if ((toattr->values[i].unknown.data = | |
4567 | malloc(toattr->values[i].unknown.length)) == NULL) | |
4568 | toattr->values[i].unknown.length = 0; | |
4569 | else | |
4570 | memcpy(toattr->values[i].unknown.data, | |
4571 | attr->values[i].unknown.data, | |
4572 | toattr->values[i].unknown.length); | |
4573 | } | |
4574 | } | |
4575 | break; /* anti-compiler-warning-code */ | |
4576 | } | |
4577 | ||
4578 | return (toattr); | |
4579 | } | |
4580 | ||
4581 | ||
4582 | /* | |
4583 | * 'copy_attrs()' - Copy attributes from one request to another. | |
4584 | */ | |
4585 | ||
4586 | static void | |
4587 | copy_attrs(ipp_t *to, /* I - Destination request */ | |
4588 | ipp_t *from, /* I - Source request */ | |
4589 | cups_array_t *ra, /* I - Requested attributes */ | |
4590 | ipp_tag_t group, /* I - Group to copy */ | |
4591 | int quickcopy) /* I - Do a quick copy? */ | |
4592 | { | |
4593 | ipp_attribute_t *fromattr; /* Source attribute */ | |
4594 | ||
4595 | ||
4596 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4597 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
4598 | to, from, ra, group, quickcopy); | |
4599 | ||
4600 | if (!to || !from) | |
4601 | return; | |
4602 | ||
4603 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) | |
4604 | { | |
4605 | /* | |
4606 | * Filter attributes as needed... | |
4607 | */ | |
4608 | ||
4609 | if ((group != IPP_TAG_ZERO && fromattr->group_tag != group && | |
4610 | fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name) | |
4611 | continue; | |
4612 | ||
4613 | if (!ra || cupsArrayFind(ra, fromattr->name)) | |
4614 | copy_attribute(to, fromattr, quickcopy); | |
4615 | } | |
4616 | } | |
4617 | ||
4618 | ||
4619 | /* | |
4620 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
4621 | * specified job. | |
4622 | */ | |
4623 | ||
4624 | static int /* O - Size of banner file in kbytes */ | |
4625 | copy_banner(cupsd_client_t *con, /* I - Client connection */ | |
4626 | cupsd_job_t *job, /* I - Job information */ | |
4627 | const char *name) /* I - Name of banner */ | |
4628 | { | |
4629 | int i; /* Looping var */ | |
4630 | int kbytes; /* Size of banner file in kbytes */ | |
4631 | char filename[1024]; /* Job filename */ | |
4632 | cupsd_banner_t *banner; /* Pointer to banner */ | |
4633 | cups_file_t *in; /* Input file */ | |
4634 | cups_file_t *out; /* Output file */ | |
4635 | int ch; /* Character from file */ | |
4636 | char attrname[255], /* Name of attribute */ | |
4637 | *s; /* Pointer into name */ | |
4638 | ipp_attribute_t *attr; /* Attribute */ | |
4639 | ||
4640 | ||
4641 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4642 | "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")", | |
4643 | con, con ? con->http.fd : -1, job, job->id, | |
4644 | name ? name : "(null)"); | |
4645 | ||
4646 | /* | |
4647 | * Find the banner; return if not found or "none"... | |
4648 | */ | |
4649 | ||
4650 | if (!name || !strcmp(name, "none") || | |
4651 | (banner = cupsdFindBanner(name)) == NULL) | |
4652 | return (0); | |
4653 | ||
4654 | /* | |
4655 | * Open the banner and job files... | |
4656 | */ | |
4657 | ||
4658 | if (add_file(con, job, banner->filetype, 0)) | |
4659 | return (-1); | |
4660 | ||
4661 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
4662 | job->num_files); | |
4663 | if ((out = cupsFileOpen(filename, "w")) == NULL) | |
4664 | { | |
4665 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4666 | "Unable to create banner job file %s - %s", | |
4667 | filename, strerror(errno)); | |
4668 | job->num_files --; | |
4669 | return (0); | |
4670 | } | |
4671 | ||
4672 | fchmod(cupsFileNumber(out), 0640); | |
4673 | fchown(cupsFileNumber(out), RunUser, Group); | |
4674 | ||
4675 | /* | |
4676 | * Try the localized banner file under the subdirectory... | |
4677 | */ | |
4678 | ||
4679 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, | |
4680 | sizeof(attrname)); | |
4681 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
4682 | { | |
4683 | /* | |
4684 | * Convert ll-cc to ll_CC... | |
4685 | */ | |
4686 | ||
4687 | attrname[2] = '_'; | |
4688 | attrname[3] = toupper(attrname[3] & 255); | |
4689 | attrname[4] = toupper(attrname[4] & 255); | |
4690 | } | |
4691 | ||
4692 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, | |
4693 | attrname, name); | |
4694 | ||
4695 | if (access(filename, 0) && strlen(attrname) > 2) | |
4696 | { | |
4697 | /* | |
4698 | * Wasn't able to find "ll_CC" locale file; try the non-national | |
4699 | * localization banner directory. | |
4700 | */ | |
4701 | ||
4702 | attrname[2] = '\0'; | |
4703 | ||
4704 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, | |
4705 | attrname, name); | |
4706 | } | |
4707 | ||
4708 | if (access(filename, 0)) | |
4709 | { | |
4710 | /* | |
4711 | * Use the non-localized banner file. | |
4712 | */ | |
4713 | ||
4714 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); | |
4715 | } | |
4716 | ||
4717 | if ((in = cupsFileOpen(filename, "r")) == NULL) | |
4718 | { | |
4719 | cupsFileClose(out); | |
4720 | unlink(filename); | |
4721 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
4722 | "Unable to open banner template file %s - %s", | |
4723 | filename, strerror(errno)); | |
4724 | job->num_files --; | |
4725 | return (0); | |
4726 | } | |
4727 | ||
4728 | /* | |
4729 | * Parse the file to the end... | |
4730 | */ | |
4731 | ||
4732 | while ((ch = cupsFileGetChar(in)) != EOF) | |
4733 | if (ch == '{') | |
4734 | { | |
4735 | /* | |
4736 | * Get an attribute name... | |
4737 | */ | |
4738 | ||
4739 | for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;) | |
4740 | if (!isalpha(ch & 255) && ch != '-' && ch != '?') | |
4741 | break; | |
4742 | else if (s < (attrname + sizeof(attrname) - 1)) | |
4743 | *s++ = ch; | |
4744 | else | |
4745 | break; | |
4746 | ||
4747 | *s = '\0'; | |
4748 | ||
4749 | if (ch != '}') | |
4750 | { | |
4751 | /* | |
4752 | * Ignore { followed by stuff that is not an attribute name... | |
4753 | */ | |
4754 | ||
4755 | cupsFilePrintf(out, "{%s%c", attrname, ch); | |
4756 | continue; | |
4757 | } | |
4758 | ||
4759 | /* | |
4760 | * See if it is defined... | |
4761 | */ | |
4762 | ||
4763 | if (attrname[0] == '?') | |
4764 | s = attrname + 1; | |
4765 | else | |
4766 | s = attrname; | |
4767 | ||
4768 | if (!strcmp(s, "printer-name")) | |
4769 | { | |
4770 | cupsFilePuts(out, job->dest); | |
4771 | continue; | |
4772 | } | |
4773 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) | |
4774 | { | |
4775 | /* | |
4776 | * See if we have a leading question mark... | |
4777 | */ | |
4778 | ||
4779 | if (attrname[0] != '?') | |
4780 | { | |
4781 | /* | |
4782 | * Nope, write to file as-is; probably a PostScript procedure... | |
4783 | */ | |
4784 | ||
4785 | cupsFilePrintf(out, "{%s}", attrname); | |
4786 | } | |
4787 | ||
4788 | continue; | |
4789 | } | |
4790 | ||
4791 | /* | |
4792 | * Output value(s)... | |
4793 | */ | |
4794 | ||
4795 | for (i = 0; i < attr->num_values; i ++) | |
4796 | { | |
4797 | if (i) | |
4798 | cupsFilePutChar(out, ','); | |
4799 | ||
4800 | switch (attr->value_tag) | |
4801 | { | |
4802 | case IPP_TAG_INTEGER : | |
4803 | case IPP_TAG_ENUM : | |
4804 | if (!strncmp(s, "time-at-", 8)) | |
4805 | cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer)); | |
4806 | else | |
4807 | cupsFilePrintf(out, "%d", attr->values[i].integer); | |
4808 | break; | |
4809 | ||
4810 | case IPP_TAG_BOOLEAN : | |
4811 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
4812 | break; | |
4813 | ||
4814 | case IPP_TAG_NOVALUE : | |
4815 | cupsFilePuts(out, "novalue"); | |
4816 | break; | |
4817 | ||
4818 | case IPP_TAG_RANGE : | |
4819 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
4820 | attr->values[i].range.upper); | |
4821 | break; | |
4822 | ||
4823 | case IPP_TAG_RESOLUTION : | |
4824 | cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres, | |
4825 | attr->values[i].resolution.yres, | |
4826 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
4827 | "dpi" : "dpc"); | |
4828 | break; | |
4829 | ||
4830 | case IPP_TAG_URI : | |
4831 | case IPP_TAG_STRING : | |
4832 | case IPP_TAG_TEXT : | |
4833 | case IPP_TAG_NAME : | |
4834 | case IPP_TAG_KEYWORD : | |
4835 | case IPP_TAG_CHARSET : | |
4836 | case IPP_TAG_LANGUAGE : | |
4837 | if (!strcasecmp(banner->filetype->type, "postscript")) | |
4838 | { | |
4839 | /* | |
4840 | * Need to quote strings for PS banners... | |
4841 | */ | |
4842 | ||
4843 | const char *p; | |
4844 | ||
4845 | for (p = attr->values[i].string.text; *p; p ++) | |
4846 | { | |
4847 | if (*p == '(' || *p == ')' || *p == '\\') | |
4848 | { | |
4849 | cupsFilePutChar(out, '\\'); | |
4850 | cupsFilePutChar(out, *p); | |
4851 | } | |
4852 | else if (*p < 32 || *p > 126) | |
4853 | cupsFilePrintf(out, "\\%03o", *p & 255); | |
4854 | else | |
4855 | cupsFilePutChar(out, *p); | |
4856 | } | |
4857 | } | |
4858 | else | |
4859 | cupsFilePuts(out, attr->values[i].string.text); | |
4860 | break; | |
4861 | ||
4862 | default : | |
4863 | break; /* anti-compiler-warning-code */ | |
4864 | } | |
4865 | } | |
4866 | } | |
4867 | else if (ch == '\\') /* Quoted char */ | |
4868 | { | |
4869 | ch = cupsFileGetChar(in); | |
4870 | ||
4871 | if (ch != '{') /* Only do special handling for \{ */ | |
4872 | cupsFilePutChar(out, '\\'); | |
4873 | ||
4874 | cupsFilePutChar(out, ch); | |
4875 | } | |
4876 | else | |
4877 | cupsFilePutChar(out, ch); | |
4878 | ||
4879 | cupsFileClose(in); | |
4880 | ||
4881 | kbytes = (cupsFileTell(out) + 1023) / 1024; | |
4882 | ||
4883 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
4884 | IPP_TAG_INTEGER)) != NULL) | |
4885 | attr->values[0].integer += kbytes; | |
4886 | ||
4887 | cupsFileClose(out); | |
4888 | ||
4889 | return (kbytes); | |
4890 | } | |
4891 | ||
4892 | ||
4893 | /* | |
4894 | * 'copy_file()' - Copy a PPD file or interface script... | |
4895 | */ | |
4896 | ||
4897 | static int /* O - 0 = success, -1 = error */ | |
4898 | copy_file(const char *from, /* I - Source file */ | |
4899 | const char *to) /* I - Destination file */ | |
4900 | { | |
4901 | cups_file_t *src, /* Source file */ | |
4902 | *dst; /* Destination file */ | |
4903 | int bytes; /* Bytes to read/write */ | |
4904 | char buffer[2048]; /* Copy buffer */ | |
4905 | ||
4906 | ||
4907 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); | |
4908 | ||
4909 | /* | |
4910 | * Open the source and destination file for a copy... | |
4911 | */ | |
4912 | ||
4913 | if ((src = cupsFileOpen(from, "rb")) == NULL) | |
4914 | return (-1); | |
4915 | ||
4916 | if ((dst = cupsFileOpen(to, "wb")) == NULL) | |
4917 | { | |
4918 | cupsFileClose(src); | |
4919 | return (-1); | |
4920 | } | |
4921 | ||
4922 | /* | |
4923 | * Copy the source file to the destination... | |
4924 | */ | |
4925 | ||
4926 | while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0) | |
4927 | if (cupsFileWrite(dst, buffer, bytes) < bytes) | |
4928 | { | |
4929 | cupsFileClose(src); | |
4930 | cupsFileClose(dst); | |
4931 | return (-1); | |
4932 | } | |
4933 | ||
4934 | /* | |
4935 | * Close both files and return... | |
4936 | */ | |
4937 | ||
4938 | cupsFileClose(src); | |
4939 | ||
4940 | return (cupsFileClose(dst)); | |
4941 | } | |
4942 | ||
4943 | ||
4944 | /* | |
4945 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
4946 | * as needed... | |
4947 | */ | |
4948 | ||
4949 | static int /* O - 0 = success, -1 = error */ | |
4950 | copy_model(cupsd_client_t *con, /* I - Client connection */ | |
4951 | const char *from, /* I - Source file */ | |
4952 | const char *to) /* I - Destination file */ | |
4953 | { | |
4954 | fd_set input; /* select() input set */ | |
4955 | struct timeval timeout; /* select() timeout */ | |
4956 | int maxfd; /* Max file descriptor for select() */ | |
4957 | char tempfile[1024]; /* Temporary PPD file */ | |
4958 | int tempfd; /* Temporary PPD file descriptor */ | |
4959 | int temppid; /* Process ID of cups-driverd */ | |
4960 | int temppipe[2]; /* Temporary pipes */ | |
4961 | char *argv[4], /* Command-line arguments */ | |
4962 | *envp[MAX_ENV]; /* Environment */ | |
4963 | cups_file_t *src, /* Source file */ | |
4964 | *dst; /* Destination file */ | |
4965 | ppd_file_t *ppd; /* PPD file */ | |
4966 | int bytes, /* Bytes from pipe */ | |
4967 | total; /* Total bytes from pipe */ | |
4968 | char buffer[2048]; /* Copy buffer */ | |
4969 | int i; /* Looping var */ | |
4970 | char option[PPD_MAX_NAME], /* Option name */ | |
4971 | choice[PPD_MAX_NAME]; /* Choice name */ | |
4972 | int num_defaults; /* Number of default options */ | |
4973 | cups_option_t *defaults; /* Default options */ | |
4974 | char cups_protocol[PPD_MAX_LINE]; | |
4975 | /* cupsProtocol attribute */ | |
4976 | int have_letter, /* Have Letter size */ | |
4977 | have_a4; /* Have A4 size */ | |
4978 | #ifdef HAVE_LIBPAPER | |
4979 | char *paper_result; /* Paper size name from libpaper */ | |
4980 | char system_paper[64]; /* Paper size name buffer */ | |
4981 | #endif /* HAVE_LIBPAPER */ | |
4982 | ||
4983 | ||
4984 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
4985 | "copy_model(con=%p, from=\"%s\", to=\"%s\")", | |
4986 | con, from, to); | |
4987 | ||
4988 | /* | |
4989 | * Run cups-driverd to get the PPD file... | |
4990 | */ | |
4991 | ||
4992 | argv[0] = "cups-driverd"; | |
4993 | argv[1] = "cat"; | |
4994 | argv[2] = (char *)from; | |
4995 | argv[3] = NULL; | |
4996 | ||
4997 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
4998 | ||
4999 | snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin); | |
5000 | snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd); | |
5001 | tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
5002 | if (tempfd < 0) | |
5003 | return (-1); | |
5004 | ||
5005 | cupsdOpenPipe(temppipe); | |
5006 | ||
5007 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
5008 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
5009 | ||
5010 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], | |
5011 | -1, -1, 0, DefaultProfile, &temppid)) | |
5012 | { | |
5013 | close(tempfd); | |
5014 | unlink(tempfile); | |
5015 | ||
5016 | return (-1); | |
5017 | } | |
5018 | ||
5019 | close(temppipe[1]); | |
5020 | ||
5021 | /* | |
5022 | * Wait up to 30 seconds for the PPD file to be copied... | |
5023 | */ | |
5024 | ||
5025 | total = 0; | |
5026 | ||
5027 | if (temppipe[0] > CGIPipes[0]) | |
5028 | maxfd = temppipe[0] + 1; | |
5029 | else | |
5030 | maxfd = CGIPipes[0] + 1; | |
5031 | ||
5032 | for (;;) | |
5033 | { | |
5034 | /* | |
5035 | * See if we have data ready... | |
5036 | */ | |
5037 | ||
5038 | bytes = 0; | |
5039 | ||
5040 | FD_ZERO(&input); | |
5041 | FD_SET(temppipe[0], &input); | |
5042 | FD_SET(CGIPipes[0], &input); | |
5043 | ||
5044 | timeout.tv_sec = 30; | |
5045 | timeout.tv_usec = 0; | |
5046 | ||
5047 | if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0) | |
5048 | { | |
5049 | if (errno == EINTR) | |
5050 | continue; | |
5051 | else | |
5052 | break; | |
5053 | } | |
5054 | else if (i == 0) | |
5055 | { | |
5056 | /* | |
5057 | * We have timed out... | |
5058 | */ | |
5059 | ||
5060 | break; | |
5061 | } | |
5062 | ||
5063 | if (FD_ISSET(temppipe[0], &input)) | |
5064 | { | |
5065 | /* | |
5066 | * Read the PPD file from the pipe, and write it to the PPD file. | |
5067 | */ | |
5068 | ||
5069 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) | |
5070 | { | |
5071 | if (write(tempfd, buffer, bytes) < bytes) | |
5072 | break; | |
5073 | ||
5074 | total += bytes; | |
5075 | } | |
5076 | else | |
5077 | break; | |
5078 | } | |
5079 | ||
5080 | if (FD_ISSET(CGIPipes[0], &input)) | |
5081 | cupsdUpdateCGI(); | |
5082 | } | |
5083 | ||
5084 | close(temppipe[0]); | |
5085 | close(tempfd); | |
5086 | ||
5087 | if (!total) | |
5088 | { | |
5089 | /* | |
5090 | * No data from cups-deviced... | |
5091 | */ | |
5092 | ||
5093 | cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!"); | |
5094 | unlink(tempfile); | |
5095 | return (-1); | |
5096 | } | |
5097 | ||
5098 | /* | |
5099 | * Read the source file and see what page sizes are supported... | |
5100 | */ | |
5101 | ||
5102 | if ((ppd = ppdOpenFile(tempfile)) == NULL) | |
5103 | { | |
5104 | unlink(tempfile); | |
5105 | return (-1); | |
5106 | } | |
5107 | ||
5108 | have_letter = ppdPageSize(ppd, "Letter") != NULL; | |
5109 | have_a4 = ppdPageSize(ppd, "A4") != NULL; | |
5110 | ||
5111 | /* | |
5112 | * Open the destination (if possible) and set the default options... | |
5113 | */ | |
5114 | ||
5115 | num_defaults = 0; | |
5116 | defaults = NULL; | |
5117 | cups_protocol[0] = '\0'; | |
5118 | ||
5119 | if ((dst = cupsFileOpen(to, "rb")) != NULL) | |
5120 | { | |
5121 | /* | |
5122 | * Read all of the default lines from the old PPD... | |
5123 | */ | |
5124 | ||
5125 | while (cupsFileGets(dst, buffer, sizeof(buffer))) | |
5126 | if (!strncmp(buffer, "*Default", 8)) | |
5127 | { | |
5128 | /* | |
5129 | * Add the default option... | |
5130 | */ | |
5131 | ||
5132 | if (!ppd_parse_line(buffer, option, sizeof(option), | |
5133 | choice, sizeof(choice))) | |
5134 | { | |
5135 | ppd_option_t *ppdo; /* PPD option */ | |
5136 | ||
5137 | ||
5138 | /* | |
5139 | * Only add the default if the default hasn't already been | |
5140 | * set and the choice exists in the new PPD... | |
5141 | */ | |
5142 | ||
5143 | if (!cupsGetOption(option, num_defaults, defaults) && | |
5144 | (ppdo = ppdFindOption(ppd, option)) != NULL && | |
5145 | ppdFindChoice(ppdo, choice)) | |
5146 | num_defaults = cupsAddOption(option, choice, num_defaults, | |
5147 | &defaults); | |
5148 | } | |
5149 | } | |
5150 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
5151 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
5152 | ||
5153 | cupsFileClose(dst); | |
5154 | } | |
5155 | #ifdef HAVE_LIBPAPER | |
5156 | else if ((paper_result = systempapername()) != NULL) | |
5157 | { | |
5158 | /* | |
5159 | * Set the default media sizes from the systemwide default... | |
5160 | */ | |
5161 | ||
5162 | strlcpy(system_paper, paper_result, sizeof(system_paper)); | |
5163 | system_paper[0] = toupper(system_paper[0] & 255); | |
5164 | ||
5165 | if ((!strcmp(system_paper, "Letter") && have_letter) || | |
5166 | (!strcmp(system_paper, "A4") && have_a4)) | |
5167 | { | |
5168 | num_defaults = cupsAddOption("PageSize", system_paper, | |
5169 | num_defaults, &defaults); | |
5170 | num_defaults = cupsAddOption("PageRegion", system_paper, | |
5171 | num_defaults, &defaults); | |
5172 | num_defaults = cupsAddOption("PaperDimension", system_paper, | |
5173 | num_defaults, &defaults); | |
5174 | num_defaults = cupsAddOption("ImageableArea", system_paper, | |
5175 | num_defaults, &defaults); | |
5176 | } | |
5177 | } | |
5178 | #endif /* HAVE_LIBPAPER */ | |
5179 | else | |
5180 | { | |
5181 | /* | |
5182 | * Add the default media sizes... | |
5183 | * | |
5184 | * Note: These values are generally not valid for large-format devices | |
5185 | * like plotters, however it is probably safe to say that those | |
5186 | * users will configure the media size after initially adding | |
5187 | * the device anyways... | |
5188 | */ | |
5189 | ||
5190 | if (!DefaultLanguage || | |
5191 | !strcasecmp(DefaultLanguage, "C") || | |
5192 | !strcasecmp(DefaultLanguage, "POSIX") || | |
5193 | !strcasecmp(DefaultLanguage, "en") || | |
5194 | !strncasecmp(DefaultLanguage, "en.", 3) || | |
5195 | !strncasecmp(DefaultLanguage, "en_US", 5) || | |
5196 | !strncasecmp(DefaultLanguage, "en_CA", 5) || | |
5197 | !strncasecmp(DefaultLanguage, "fr_CA", 5)) | |
5198 | { | |
5199 | /* | |
5200 | * These are the only locales that will default to "letter" size... | |
5201 | */ | |
5202 | ||
5203 | if (have_letter) | |
5204 | { | |
5205 | num_defaults = cupsAddOption("PageSize", "Letter", num_defaults, | |
5206 | &defaults); | |
5207 | num_defaults = cupsAddOption("PageRegion", "Letter", num_defaults, | |
5208 | &defaults); | |
5209 | num_defaults = cupsAddOption("PaperDimension", "Letter", num_defaults, | |
5210 | &defaults); | |
5211 | num_defaults = cupsAddOption("ImageableArea", "Letter", num_defaults, | |
5212 | &defaults); | |
5213 | } | |
5214 | } | |
5215 | else if (have_a4) | |
5216 | { | |
5217 | /* | |
5218 | * The rest default to "a4" size... | |
5219 | */ | |
5220 | ||
5221 | num_defaults = cupsAddOption("PageSize", "A4", num_defaults, | |
5222 | &defaults); | |
5223 | num_defaults = cupsAddOption("PageRegion", "A4", num_defaults, | |
5224 | &defaults); | |
5225 | num_defaults = cupsAddOption("PaperDimension", "A4", num_defaults, | |
5226 | &defaults); | |
5227 | num_defaults = cupsAddOption("ImageableArea", "A4", num_defaults, | |
5228 | &defaults); | |
5229 | } | |
5230 | } | |
5231 | ||
5232 | ppdClose(ppd); | |
5233 | ||
5234 | /* | |
5235 | * Open the source file for a copy... | |
5236 | */ | |
5237 | ||
5238 | if ((src = cupsFileOpen(tempfile, "rb")) == NULL) | |
5239 | { | |
5240 | cupsFreeOptions(num_defaults, defaults); | |
5241 | unlink(tempfile); | |
5242 | return (-1); | |
5243 | } | |
5244 | ||
5245 | /* | |
5246 | * Open the destination file for a copy... | |
5247 | */ | |
5248 | ||
5249 | if ((dst = cupsFileOpen(to, "wb")) == NULL) | |
5250 | { | |
5251 | cupsFreeOptions(num_defaults, defaults); | |
5252 | cupsFileClose(src); | |
5253 | unlink(tempfile); | |
5254 | return (-1); | |
5255 | } | |
5256 | ||
5257 | /* | |
5258 | * Copy the source file to the destination... | |
5259 | */ | |
5260 | ||
5261 | while (cupsFileGets(src, buffer, sizeof(buffer))) | |
5262 | { | |
5263 | if (!strncmp(buffer, "*Default", 8)) | |
5264 | { | |
5265 | /* | |
5266 | * Check for an previous default option choice... | |
5267 | */ | |
5268 | ||
5269 | if (!ppd_parse_line(buffer, option, sizeof(option), | |
5270 | choice, sizeof(choice))) | |
5271 | { | |
5272 | const char *val; /* Default option value */ | |
5273 | ||
5274 | ||
5275 | if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL) | |
5276 | { | |
5277 | /* | |
5278 | * Substitute the previous choice... | |
5279 | */ | |
5280 | ||
5281 | snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val); | |
5282 | } | |
5283 | } | |
5284 | } | |
5285 | ||
5286 | cupsFilePrintf(dst, "%s\n", buffer); | |
5287 | } | |
5288 | ||
5289 | if (cups_protocol[0]) | |
5290 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
5291 | ||
5292 | cupsFreeOptions(num_defaults, defaults); | |
5293 | ||
5294 | /* | |
5295 | * Close both files and return... | |
5296 | */ | |
5297 | ||
5298 | cupsFileClose(src); | |
5299 | ||
5300 | unlink(tempfile); | |
5301 | ||
5302 | return (cupsFileClose(dst)); | |
5303 | } | |
5304 | ||
5305 | ||
5306 | /* | |
5307 | * 'copy_job_attrs()' - Copy job attributes. | |
5308 | */ | |
5309 | ||
5310 | static void | |
5311 | copy_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
5312 | cupsd_job_t *job, /* I - Job */ | |
5313 | cups_array_t *ra) /* I - Requested attributes array */ | |
5314 | { | |
5315 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
5316 | ||
5317 | ||
5318 | /* | |
5319 | * Send the requested attributes for each job... | |
5320 | */ | |
5321 | ||
5322 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, | |
5323 | con->servername, con->serverport, "/jobs/%d", | |
5324 | job->id); | |
5325 | ||
5326 | if (!ra || cupsArrayFind(ra, "document-count")) | |
5327 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5328 | "document-count", job->num_files); | |
5329 | ||
5330 | if (!ra || cupsArrayFind(ra, "job-more-info")) | |
5331 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
5332 | "job-more-info", NULL, job_uri); | |
5333 | ||
5334 | if (job->state_value > IPP_JOB_PROCESSING && | |
5335 | (!ra || cupsArrayFind(ra, "job-preserved"))) | |
5336 | ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved", | |
5337 | job->num_files > 0); | |
5338 | ||
5339 | if (!ra || cupsArrayFind(ra, "job-printer-up-time")) | |
5340 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5341 | "job-printer-up-time", time(NULL)); | |
5342 | ||
5343 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) | |
5344 | add_job_state_reasons(con, job); | |
5345 | ||
5346 | if (!ra || cupsArrayFind(ra, "job-uri")) | |
5347 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
5348 | "job-uri", NULL, job_uri); | |
5349 | ||
5350 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0); | |
5351 | } | |
5352 | ||
5353 | ||
5354 | /* | |
5355 | * 'copy_printer_attrs()' - Copy printer attributes. | |
5356 | */ | |
5357 | ||
5358 | static void | |
5359 | copy_printer_attrs( | |
5360 | cupsd_client_t *con, /* I - Client connection */ | |
5361 | cupsd_printer_t *printer, /* I - Printer */ | |
5362 | cups_array_t *ra) /* I - Requested attributes array */ | |
5363 | { | |
5364 | char printer_uri[HTTP_MAX_URI]; | |
5365 | /* Printer URI */ | |
5366 | time_t curtime; /* Current time */ | |
5367 | int i; /* Looping var */ | |
5368 | ipp_attribute_t *history; /* History collection */ | |
5369 | ||
5370 | ||
5371 | /* | |
5372 | * Copy the printer attributes to the response using requested-attributes | |
5373 | * and document-format attributes that may be provided by the client. | |
5374 | */ | |
5375 | ||
5376 | curtime = time(NULL); | |
5377 | ||
5378 | #ifdef __APPLE__ | |
5379 | if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) && | |
5380 | printer->recoverable) | |
5381 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5382 | "com.apple.print.recoverable-message", NULL, | |
5383 | printer->recoverable); | |
5384 | #endif /* __APPLE__ */ | |
5385 | ||
5386 | if (!ra || cupsArrayFind(ra, "marker-change-time")) | |
5387 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5388 | "marker-change-time", printer->marker_time); | |
5389 | ||
5390 | if (printer->num_printers > 0 && | |
5391 | (!ra || cupsArrayFind(ra, "member-uris"))) | |
5392 | { | |
5393 | ipp_attribute_t *member_uris; /* member-uris attribute */ | |
5394 | cupsd_printer_t *p2; /* Printer in class */ | |
5395 | ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */ | |
5396 | ||
5397 | ||
5398 | if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER, | |
5399 | IPP_TAG_URI, "member-uris", | |
5400 | printer->num_printers, NULL, | |
5401 | NULL)) != NULL) | |
5402 | { | |
5403 | for (i = 0; i < printer->num_printers; i ++) | |
5404 | { | |
5405 | p2 = printer->printers[i]; | |
5406 | ||
5407 | if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported", | |
5408 | IPP_TAG_URI)) != NULL) | |
5409 | member_uris->values[i].string.text = | |
5410 | _cupsStrAlloc(p2_uri->values[0].string.text); | |
5411 | else | |
5412 | { | |
5413 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, | |
5414 | sizeof(printer_uri), "ipp", NULL, con->servername, | |
5415 | con->serverport, | |
5416 | (p2->type & CUPS_PRINTER_CLASS) ? | |
5417 | "/classes/%s" : "/printers/%s", p2->name); | |
5418 | member_uris->values[i].string.text = _cupsStrAlloc(printer_uri); | |
5419 | } | |
5420 | } | |
5421 | } | |
5422 | } | |
5423 | ||
5424 | if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert"))) | |
5425 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING, | |
5426 | "printer-alert", NULL, printer->alert); | |
5427 | ||
5428 | if (printer->alert_description && | |
5429 | (!ra || cupsArrayFind(ra, "printer-alert-description"))) | |
5430 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5431 | "printer-alert-description", NULL, | |
5432 | printer->alert_description); | |
5433 | ||
5434 | if (!ra || cupsArrayFind(ra, "printer-current-time")) | |
5435 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", | |
5436 | ippTimeToDate(curtime)); | |
5437 | ||
5438 | #ifdef HAVE_DNSSD | |
5439 | if (!ra || cupsArrayFind(ra, "printer-dns-sd-name")) | |
5440 | { | |
5441 | if (printer->reg_name) | |
5442 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5443 | "printer-dns-sd-name", NULL, printer->reg_name); | |
5444 | else | |
5445 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE, | |
5446 | "printer-dns-sd-name", 0); | |
5447 | } | |
5448 | #endif /* HAVE_DNSSD */ | |
5449 | ||
5450 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) | |
5451 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5452 | "printer-error-policy", NULL, printer->error_policy); | |
5453 | ||
5454 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) | |
5455 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", | |
5456 | printer->accepting); | |
5457 | ||
5458 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) | |
5459 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", | |
5460 | printer->shared); | |
5461 | ||
5462 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) | |
5463 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5464 | "printer-op-policy", NULL, printer->op_policy); | |
5465 | ||
5466 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
5467 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
5468 | printer->state); | |
5469 | ||
5470 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) | |
5471 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5472 | "printer-state-change-time", printer->state_time); | |
5473 | ||
5474 | if (MaxPrinterHistory > 0 && printer->num_history > 0 && | |
5475 | cupsArrayFind(ra, "printer-state-history")) | |
5476 | { | |
5477 | /* | |
5478 | * Printer history is only sent if specifically requested, so that | |
5479 | * older CUPS/IPP clients won't barf on the collection attributes. | |
5480 | */ | |
5481 | ||
5482 | history = ippAddCollections(con->response, IPP_TAG_PRINTER, | |
5483 | "printer-state-history", | |
5484 | printer->num_history, NULL); | |
5485 | ||
5486 | for (i = 0; i < printer->num_history; i ++) | |
5487 | copy_attrs(history->values[i].collection = ippNew(), printer->history[i], | |
5488 | NULL, IPP_TAG_ZERO, 0); | |
5489 | } | |
5490 | ||
5491 | if (!ra || cupsArrayFind(ra, "printer-state-message")) | |
5492 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5493 | "printer-state-message", NULL, printer->state_message); | |
5494 | ||
5495 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) | |
5496 | add_printer_state_reasons(con, printer); | |
5497 | ||
5498 | if (!ra || cupsArrayFind(ra, "printer-type")) | |
5499 | { | |
5500 | int type; /* printer-type value */ | |
5501 | ||
5502 | ||
5503 | /* | |
5504 | * Add the CUPS-specific printer-type attribute... | |
5505 | */ | |
5506 | ||
5507 | type = printer->type; | |
5508 | ||
5509 | if (printer == DefaultPrinter) | |
5510 | type |= CUPS_PRINTER_DEFAULT; | |
5511 | ||
5512 | if (!printer->accepting) | |
5513 | type |= CUPS_PRINTER_REJECTING; | |
5514 | ||
5515 | if (!printer->shared) | |
5516 | type |= CUPS_PRINTER_NOT_SHARED; | |
5517 | ||
5518 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", | |
5519 | type); | |
5520 | } | |
5521 | ||
5522 | if (!ra || cupsArrayFind(ra, "printer-up-time")) | |
5523 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5524 | "printer-up-time", curtime); | |
5525 | ||
5526 | if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) && | |
5527 | !ippFindAttribute(printer->attrs, "printer-uri-supported", | |
5528 | IPP_TAG_URI)) | |
5529 | { | |
5530 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
5531 | "ipp", NULL, con->servername, con->serverport, | |
5532 | (printer->type & CUPS_PRINTER_CLASS) ? | |
5533 | "/classes/%s" : "/printers/%s", printer->name); | |
5534 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, | |
5535 | "printer-uri-supported", NULL, printer_uri); | |
5536 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", | |
5537 | printer_uri); | |
5538 | } | |
5539 | ||
5540 | if (!ra || cupsArrayFind(ra, "queued-job-count")) | |
5541 | add_queued_job_count(con, printer); | |
5542 | ||
5543 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0); | |
5544 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY); | |
5545 | } | |
5546 | ||
5547 | ||
5548 | /* | |
5549 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
5550 | */ | |
5551 | ||
5552 | static void | |
5553 | copy_subscription_attrs( | |
5554 | cupsd_client_t *con, /* I - Client connection */ | |
5555 | cupsd_subscription_t *sub, /* I - Subscription */ | |
5556 | cups_array_t *ra) /* I - Requested attributes array */ | |
5557 | { | |
5558 | ipp_attribute_t *attr; /* Current attribute */ | |
5559 | char printer_uri[HTTP_MAX_URI]; | |
5560 | /* Printer URI */ | |
5561 | int count; /* Number of events */ | |
5562 | unsigned mask; /* Current event mask */ | |
5563 | const char *name; /* Current event name */ | |
5564 | ||
5565 | ||
5566 | /* | |
5567 | * Copy the subscription attributes to the response using the | |
5568 | * requested-attributes attribute that may be provided by the client. | |
5569 | */ | |
5570 | ||
5571 | if (!ra || cupsArrayFind(ra, "notify-events")) | |
5572 | { | |
5573 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
5574 | { | |
5575 | /* | |
5576 | * Simple event list... | |
5577 | */ | |
5578 | ||
5579 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, | |
5580 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), | |
5581 | "notify-events", NULL, name); | |
5582 | } | |
5583 | else | |
5584 | { | |
5585 | /* | |
5586 | * Complex event list... | |
5587 | */ | |
5588 | ||
5589 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) | |
5590 | if (sub->mask & mask) | |
5591 | count ++; | |
5592 | ||
5593 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, | |
5594 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), | |
5595 | "notify-events", count, NULL, NULL); | |
5596 | ||
5597 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) | |
5598 | if (sub->mask & mask) | |
5599 | { | |
5600 | attr->values[count].string.text = | |
5601 | (char *)cupsdEventName((cupsd_eventmask_t)mask); | |
5602 | ||
5603 | count ++; | |
5604 | } | |
5605 | } | |
5606 | } | |
5607 | ||
5608 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) | |
5609 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5610 | "notify-job-id", sub->job->id); | |
5611 | ||
5612 | if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))) | |
5613 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5614 | "notify-lease-duration", sub->lease); | |
5615 | ||
5616 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) | |
5617 | { | |
5618 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
5619 | "ipp", NULL, con->servername, con->serverport, | |
5620 | "/printers/%s", sub->dest->name); | |
5621 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5622 | "notify-printer-uri", NULL, printer_uri); | |
5623 | } | |
5624 | ||
5625 | if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))) | |
5626 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5627 | "notify-recipient-uri", NULL, sub->recipient); | |
5628 | else if (!ra || cupsArrayFind(ra, "notify-pull-method")) | |
5629 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
5630 | "notify-pull-method", NULL, "ippget"); | |
5631 | ||
5632 | if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")) | |
5633 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
5634 | "notify-subscriber-user-name", NULL, sub->owner); | |
5635 | ||
5636 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) | |
5637 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5638 | "notify-subscription-id", sub->id); | |
5639 | ||
5640 | if (!ra || cupsArrayFind(ra, "notify-time-interval")) | |
5641 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5642 | "notify-time-interval", sub->interval); | |
5643 | ||
5644 | if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data"))) | |
5645 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
5646 | sub->user_data, sub->user_data_len); | |
5647 | } | |
5648 | ||
5649 | ||
5650 | /* | |
5651 | * 'create_job()' - Print a file to a printer or class. | |
5652 | */ | |
5653 | ||
5654 | static void | |
5655 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
5656 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5657 | { | |
5658 | cupsd_printer_t *printer; /* Printer */ | |
5659 | cupsd_job_t *job; /* New job */ | |
5660 | ||
5661 | ||
5662 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, | |
5663 | con->http.fd, uri->values[0].string.text); | |
5664 | ||
5665 | /* | |
5666 | * Is the destination valid? | |
5667 | */ | |
5668 | ||
5669 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
5670 | { | |
5671 | /* | |
5672 | * Bad URI... | |
5673 | */ | |
5674 | ||
5675 | send_ipp_status(con, IPP_NOT_FOUND, | |
5676 | _("The printer or class was not found.")); | |
5677 | return; | |
5678 | } | |
5679 | ||
5680 | /* | |
5681 | * Create the job object... | |
5682 | */ | |
5683 | ||
5684 | if ((job = add_job(con, printer, NULL)) == NULL) | |
5685 | return; | |
5686 | ||
5687 | job->pending_timeout = 1; | |
5688 | ||
5689 | /* | |
5690 | * Save and log the job... | |
5691 | */ | |
5692 | ||
5693 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Queued on \"%s\" by \"%s\".", | |
5694 | job->id, job->dest, job->username); | |
5695 | } | |
5696 | ||
5697 | ||
5698 | /* | |
5699 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
5700 | */ | |
5701 | ||
5702 | static cups_array_t * /* O - Array of attributes or NULL */ | |
5703 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
5704 | { | |
5705 | int i; /* Looping var */ | |
5706 | ipp_attribute_t *requested; /* requested-attributes attribute */ | |
5707 | cups_array_t *ra; /* Requested attributes array */ | |
5708 | char *value; /* Current value */ | |
5709 | ||
5710 | ||
5711 | /* | |
5712 | * Get the requested-attributes attribute, and return NULL if we don't | |
5713 | * have one... | |
5714 | */ | |
5715 | ||
5716 | if ((requested = ippFindAttribute(request, "requested-attributes", | |
5717 | IPP_TAG_KEYWORD)) == NULL) | |
5718 | return (NULL); | |
5719 | ||
5720 | /* | |
5721 | * If the attribute contains a single "all" keyword, return NULL... | |
5722 | */ | |
5723 | ||
5724 | if (requested->num_values == 1 && | |
5725 | !strcmp(requested->values[0].string.text, "all")) | |
5726 | return (NULL); | |
5727 | ||
5728 | /* | |
5729 | * Create an array using "strcmp" as the comparison function... | |
5730 | */ | |
5731 | ||
5732 | ra = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
5733 | ||
5734 | for (i = 0; i < requested->num_values; i ++) | |
5735 | { | |
5736 | value = requested->values[i].string.text; | |
5737 | ||
5738 | if (!strcmp(value, "job-template")) | |
5739 | { | |
5740 | cupsArrayAdd(ra, "copies"); | |
5741 | cupsArrayAdd(ra, "copies-default"); | |
5742 | cupsArrayAdd(ra, "copies-supported"); | |
5743 | cupsArrayAdd(ra, "finishings"); | |
5744 | cupsArrayAdd(ra, "finishings-default"); | |
5745 | cupsArrayAdd(ra, "finishings-supported"); | |
5746 | cupsArrayAdd(ra, "job-hold-until"); | |
5747 | cupsArrayAdd(ra, "job-hold-until-default"); | |
5748 | cupsArrayAdd(ra, "job-hold-until-supported"); | |
5749 | cupsArrayAdd(ra, "job-priority"); | |
5750 | cupsArrayAdd(ra, "job-priority-default"); | |
5751 | cupsArrayAdd(ra, "job-priority-supported"); | |
5752 | cupsArrayAdd(ra, "job-sheets"); | |
5753 | cupsArrayAdd(ra, "job-sheets-default"); | |
5754 | cupsArrayAdd(ra, "job-sheets-supported"); | |
5755 | cupsArrayAdd(ra, "media"); | |
5756 | cupsArrayAdd(ra, "media-default"); | |
5757 | cupsArrayAdd(ra, "media-supported"); | |
5758 | cupsArrayAdd(ra, "multiple-document-handling"); | |
5759 | cupsArrayAdd(ra, "multiple-document-handling-default"); | |
5760 | cupsArrayAdd(ra, "multiple-document-handling-supported"); | |
5761 | cupsArrayAdd(ra, "number-up"); | |
5762 | cupsArrayAdd(ra, "number-up-default"); | |
5763 | cupsArrayAdd(ra, "number-up-supported"); | |
5764 | cupsArrayAdd(ra, "orientation-requested"); | |
5765 | cupsArrayAdd(ra, "orientation-requested-default"); | |
5766 | cupsArrayAdd(ra, "orientation-requested-supported"); | |
5767 | cupsArrayAdd(ra, "page-ranges"); | |
5768 | cupsArrayAdd(ra, "page-ranges-supported"); | |
5769 | cupsArrayAdd(ra, "printer-resolution"); | |
5770 | cupsArrayAdd(ra, "printer-resolution-default"); | |
5771 | cupsArrayAdd(ra, "printer-resolution-supported"); | |
5772 | cupsArrayAdd(ra, "print-quality"); | |
5773 | cupsArrayAdd(ra, "print-quality-default"); | |
5774 | cupsArrayAdd(ra, "print-quality-supported"); | |
5775 | cupsArrayAdd(ra, "sides"); | |
5776 | cupsArrayAdd(ra, "sides-default"); | |
5777 | cupsArrayAdd(ra, "sides-supported"); | |
5778 | } | |
5779 | else if (!strcmp(value, "job-description")) | |
5780 | { | |
5781 | cupsArrayAdd(ra, "date-time-at-completed"); | |
5782 | cupsArrayAdd(ra, "date-time-at-creation"); | |
5783 | cupsArrayAdd(ra, "date-time-at-processing"); | |
5784 | cupsArrayAdd(ra, "job-detailed-status-message"); | |
5785 | cupsArrayAdd(ra, "job-document-access-errors"); | |
5786 | cupsArrayAdd(ra, "job-id"); | |
5787 | cupsArrayAdd(ra, "job-impressions"); | |
5788 | cupsArrayAdd(ra, "job-impressions-completed"); | |
5789 | cupsArrayAdd(ra, "job-k-octets"); | |
5790 | cupsArrayAdd(ra, "job-k-octets-processed"); | |
5791 | cupsArrayAdd(ra, "job-media-sheets"); | |
5792 | cupsArrayAdd(ra, "job-media-sheets-completed"); | |
5793 | cupsArrayAdd(ra, "job-message-from-operator"); | |
5794 | cupsArrayAdd(ra, "job-more-info"); | |
5795 | cupsArrayAdd(ra, "job-name"); | |
5796 | cupsArrayAdd(ra, "job-originating-user-name"); | |
5797 | cupsArrayAdd(ra, "job-printer-up-time"); | |
5798 | cupsArrayAdd(ra, "job-printer-uri"); | |
5799 | cupsArrayAdd(ra, "job-state"); | |
5800 | cupsArrayAdd(ra, "job-state-message"); | |
5801 | cupsArrayAdd(ra, "job-state-reasons"); | |
5802 | cupsArrayAdd(ra, "job-uri"); | |
5803 | cupsArrayAdd(ra, "number-of-documents"); | |
5804 | cupsArrayAdd(ra, "number-of-intervening-jobs"); | |
5805 | cupsArrayAdd(ra, "output-device-assigned"); | |
5806 | cupsArrayAdd(ra, "time-at-completed"); | |
5807 | cupsArrayAdd(ra, "time-at-creation"); | |
5808 | cupsArrayAdd(ra, "time-at-processing"); | |
5809 | } | |
5810 | else if (!strcmp(value, "printer-description")) | |
5811 | { | |
5812 | cupsArrayAdd(ra, "charset-configured"); | |
5813 | cupsArrayAdd(ra, "charset-supported"); | |
5814 | cupsArrayAdd(ra, "color-supported"); | |
5815 | cupsArrayAdd(ra, "compression-supported"); | |
5816 | cupsArrayAdd(ra, "document-format-default"); | |
5817 | cupsArrayAdd(ra, "document-format-supported"); | |
5818 | cupsArrayAdd(ra, "generated-natural-language-supported"); | |
5819 | cupsArrayAdd(ra, "ipp-versions-supported"); | |
5820 | cupsArrayAdd(ra, "job-impressions-supported"); | |
5821 | cupsArrayAdd(ra, "job-k-octets-supported"); | |
5822 | cupsArrayAdd(ra, "job-media-sheets-supported"); | |
5823 | cupsArrayAdd(ra, "multiple-document-jobs-supported"); | |
5824 | cupsArrayAdd(ra, "multiple-operation-time-out"); | |
5825 | cupsArrayAdd(ra, "natural-language-configured"); | |
5826 | cupsArrayAdd(ra, "notify-attributes-supported"); | |
5827 | cupsArrayAdd(ra, "notify-lease-duration-default"); | |
5828 | cupsArrayAdd(ra, "notify-lease-duration-supported"); | |
5829 | cupsArrayAdd(ra, "notify-max-events-supported"); | |
5830 | cupsArrayAdd(ra, "notify-events-default"); | |
5831 | cupsArrayAdd(ra, "notify-events-supported"); | |
5832 | cupsArrayAdd(ra, "notify-pull-method-supported"); | |
5833 | cupsArrayAdd(ra, "notify-schemes-supported"); | |
5834 | cupsArrayAdd(ra, "operations-supported"); | |
5835 | cupsArrayAdd(ra, "pages-per-minute"); | |
5836 | cupsArrayAdd(ra, "pages-per-minute-color"); | |
5837 | cupsArrayAdd(ra, "pdl-override-supported"); | |
5838 | cupsArrayAdd(ra, "printer-alert"); | |
5839 | cupsArrayAdd(ra, "printer-alert-description"); | |
5840 | cupsArrayAdd(ra, "printer-commands"); | |
5841 | cupsArrayAdd(ra, "printer-current-time"); | |
5842 | cupsArrayAdd(ra, "printer-driver-installer"); | |
5843 | cupsArrayAdd(ra, "printer-dns-sd-name"); | |
5844 | cupsArrayAdd(ra, "printer-info"); | |
5845 | cupsArrayAdd(ra, "printer-is-accepting-jobs"); | |
5846 | cupsArrayAdd(ra, "printer-location"); | |
5847 | cupsArrayAdd(ra, "printer-make-and-model"); | |
5848 | cupsArrayAdd(ra, "printer-message-from-operator"); | |
5849 | cupsArrayAdd(ra, "printer-more-info"); | |
5850 | cupsArrayAdd(ra, "printer-more-info-manufacturer"); | |
5851 | cupsArrayAdd(ra, "printer-name"); | |
5852 | cupsArrayAdd(ra, "printer-state"); | |
5853 | cupsArrayAdd(ra, "printer-state-message"); | |
5854 | cupsArrayAdd(ra, "printer-state-reasons"); | |
5855 | cupsArrayAdd(ra, "printer-type"); | |
5856 | cupsArrayAdd(ra, "printer-up-time"); | |
5857 | cupsArrayAdd(ra, "printer-uri-supported"); | |
5858 | cupsArrayAdd(ra, "queued-job-count"); | |
5859 | cupsArrayAdd(ra, "reference-uri-schemes-supported"); | |
5860 | cupsArrayAdd(ra, "uri-authentication-supported"); | |
5861 | cupsArrayAdd(ra, "uri-security-supported"); | |
5862 | } | |
5863 | else if (!strcmp(value, "printer-defaults")) | |
5864 | { | |
5865 | char *name; /* Option name */ | |
5866 | ||
5867 | ||
5868 | for (name = (char *)cupsArrayFirst(CommonDefaults); | |
5869 | name; | |
5870 | name = (char *)cupsArrayNext(CommonDefaults)) | |
5871 | cupsArrayAdd(ra, name); | |
5872 | } | |
5873 | else if (!strcmp(value, "subscription-template")) | |
5874 | { | |
5875 | cupsArrayAdd(ra, "notify-attributes"); | |
5876 | cupsArrayAdd(ra, "notify-charset"); | |
5877 | cupsArrayAdd(ra, "notify-events"); | |
5878 | cupsArrayAdd(ra, "notify-lease-duration"); | |
5879 | cupsArrayAdd(ra, "notify-natural-language"); | |
5880 | cupsArrayAdd(ra, "notify-pull-method"); | |
5881 | cupsArrayAdd(ra, "notify-recipient-uri"); | |
5882 | cupsArrayAdd(ra, "notify-time-interval"); | |
5883 | cupsArrayAdd(ra, "notify-user-data"); | |
5884 | } | |
5885 | else | |
5886 | cupsArrayAdd(ra, value); | |
5887 | } | |
5888 | ||
5889 | return (ra); | |
5890 | } | |
5891 | ||
5892 | ||
5893 | /* | |
5894 | * 'create_subscription()' - Create a notification subscription. | |
5895 | */ | |
5896 | ||
5897 | static void | |
5898 | create_subscription( | |
5899 | cupsd_client_t *con, /* I - Client connection */ | |
5900 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5901 | { | |
5902 | http_status_t status; /* Policy status */ | |
5903 | int i; /* Looping var */ | |
5904 | ipp_attribute_t *attr; /* Current attribute */ | |
5905 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
5906 | char scheme[HTTP_MAX_URI], | |
5907 | /* Scheme portion of URI */ | |
5908 | userpass[HTTP_MAX_URI], | |
5909 | /* Username portion of URI */ | |
5910 | host[HTTP_MAX_URI], | |
5911 | /* Host portion of URI */ | |
5912 | resource[HTTP_MAX_URI]; | |
5913 | /* Resource portion of URI */ | |
5914 | int port; /* Port portion of URI */ | |
5915 | cupsd_printer_t *printer; /* Printer/class */ | |
5916 | cupsd_job_t *job; /* Job */ | |
5917 | int jobid; /* Job ID */ | |
5918 | cupsd_subscription_t *sub; /* Subscription object */ | |
5919 | const char *username, /* requesting-user-name or | |
5920 | authenticated username */ | |
5921 | *recipient, /* notify-recipient-uri */ | |
5922 | *pullmethod; /* notify-pull-method */ | |
5923 | ipp_attribute_t *user_data; /* notify-user-data */ | |
5924 | int interval, /* notify-time-interval */ | |
5925 | lease; /* notify-lease-duration */ | |
5926 | unsigned mask; /* notify-events */ | |
5927 | ipp_attribute_t *notify_events,/* notify-events(-default) */ | |
5928 | *notify_lease; /* notify-lease-duration(-default) */ | |
5929 | ||
5930 | ||
5931 | #ifdef DEBUG | |
5932 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5933 | { | |
5934 | if (attr->group_tag != IPP_TAG_ZERO) | |
5935 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag, | |
5936 | attr->value_tag, attr->name); | |
5937 | else | |
5938 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----"); | |
5939 | } | |
5940 | #endif /* DEBUG */ | |
5941 | ||
5942 | /* | |
5943 | * Is the destination valid? | |
5944 | */ | |
5945 | ||
5946 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
5947 | "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")", | |
5948 | con, con->http.fd, uri->values[0].string.text); | |
5949 | ||
5950 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
5951 | sizeof(scheme), userpass, sizeof(userpass), host, | |
5952 | sizeof(host), &port, resource, sizeof(resource)); | |
5953 | ||
5954 | if (!strcmp(resource, "/")) | |
5955 | { | |
5956 | dtype = (cups_ptype_t)0; | |
5957 | printer = NULL; | |
5958 | } | |
5959 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
5960 | { | |
5961 | dtype = (cups_ptype_t)0; | |
5962 | printer = NULL; | |
5963 | } | |
5964 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
5965 | { | |
5966 | dtype = CUPS_PRINTER_CLASS; | |
5967 | printer = NULL; | |
5968 | } | |
5969 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
5970 | { | |
5971 | /* | |
5972 | * Bad URI... | |
5973 | */ | |
5974 | ||
5975 | send_ipp_status(con, IPP_NOT_FOUND, | |
5976 | _("The printer or class was not found.")); | |
5977 | return; | |
5978 | } | |
5979 | ||
5980 | /* | |
5981 | * Check policy... | |
5982 | */ | |
5983 | ||
5984 | if (printer) | |
5985 | { | |
5986 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, | |
5987 | NULL)) != HTTP_OK) | |
5988 | { | |
5989 | send_http_error(con, status, printer); | |
5990 | return; | |
5991 | } | |
5992 | } | |
5993 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5994 | { | |
5995 | send_http_error(con, status, NULL); | |
5996 | return; | |
5997 | } | |
5998 | ||
5999 | /* | |
6000 | * Get the user that is requesting the subscription... | |
6001 | */ | |
6002 | ||
6003 | username = get_username(con); | |
6004 | ||
6005 | /* | |
6006 | * Find the first subscription group attribute; return if we have | |
6007 | * none... | |
6008 | */ | |
6009 | ||
6010 | for (attr = con->request->attrs; attr; attr = attr->next) | |
6011 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
6012 | break; | |
6013 | ||
6014 | if (!attr) | |
6015 | { | |
6016 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6017 | _("No subscription attributes in request!")); | |
6018 | return; | |
6019 | } | |
6020 | ||
6021 | /* | |
6022 | * Process the subscription attributes in the request... | |
6023 | */ | |
6024 | ||
6025 | con->response->request.status.status_code = IPP_BAD_REQUEST; | |
6026 | ||
6027 | while (attr) | |
6028 | { | |
6029 | recipient = NULL; | |
6030 | pullmethod = NULL; | |
6031 | user_data = NULL; | |
6032 | interval = 0; | |
6033 | lease = DefaultLeaseDuration; | |
6034 | jobid = 0; | |
6035 | mask = CUPSD_EVENT_NONE; | |
6036 | ||
6037 | if (printer) | |
6038 | { | |
6039 | notify_events = ippFindAttribute(printer->attrs, "notify-events-default", | |
6040 | IPP_TAG_KEYWORD); | |
6041 | notify_lease = ippFindAttribute(printer->attrs, | |
6042 | "notify-lease-duration-default", | |
6043 | IPP_TAG_INTEGER); | |
6044 | ||
6045 | if (notify_lease) | |
6046 | lease = notify_lease->values[0].integer; | |
6047 | } | |
6048 | else | |
6049 | { | |
6050 | notify_events = NULL; | |
6051 | notify_lease = NULL; | |
6052 | } | |
6053 | ||
6054 | while (attr && attr->group_tag != IPP_TAG_ZERO) | |
6055 | { | |
6056 | if (!strcmp(attr->name, "notify-recipient-uri") && | |
6057 | attr->value_tag == IPP_TAG_URI) | |
6058 | { | |
6059 | /* | |
6060 | * Validate the recipient scheme against the ServerBin/notifier | |
6061 | * directory... | |
6062 | */ | |
6063 | ||
6064 | char notifier[1024]; /* Notifier filename */ | |
6065 | ||
6066 | ||
6067 | recipient = attr->values[0].string.text; | |
6068 | ||
6069 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, | |
6070 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
6071 | host, sizeof(host), &port, | |
6072 | resource, sizeof(resource)) < HTTP_URI_OK) | |
6073 | { | |
6074 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6075 | _("Bad notify-recipient-uri URI \"%s\"!"), recipient); | |
6076 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6077 | "notify-status-code", IPP_URI_SCHEME); | |
6078 | return; | |
6079 | } | |
6080 | ||
6081 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
6082 | scheme); | |
6083 | if (access(notifier, X_OK)) | |
6084 | { | |
6085 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6086 | _("notify-recipient-uri URI \"%s\" uses unknown " | |
6087 | "scheme!"), recipient); | |
6088 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6089 | "notify-status-code", IPP_URI_SCHEME); | |
6090 | return; | |
6091 | } | |
6092 | ||
6093 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
6094 | { | |
6095 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6096 | _("notify-recipient-uri URI \"%s\" is already used!"), | |
6097 | recipient); | |
6098 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6099 | "notify-status-code", IPP_ATTRIBUTES); | |
6100 | return; | |
6101 | } | |
6102 | } | |
6103 | else if (!strcmp(attr->name, "notify-pull-method") && | |
6104 | attr->value_tag == IPP_TAG_KEYWORD) | |
6105 | { | |
6106 | pullmethod = attr->values[0].string.text; | |
6107 | ||
6108 | if (strcmp(pullmethod, "ippget")) | |
6109 | { | |
6110 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6111 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
6112 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6113 | "notify-status-code", IPP_ATTRIBUTES); | |
6114 | return; | |
6115 | } | |
6116 | } | |
6117 | else if (!strcmp(attr->name, "notify-charset") && | |
6118 | attr->value_tag == IPP_TAG_CHARSET && | |
6119 | strcmp(attr->values[0].string.text, "us-ascii") && | |
6120 | strcmp(attr->values[0].string.text, "utf-8")) | |
6121 | { | |
6122 | send_ipp_status(con, IPP_CHARSET, | |
6123 | _("Character set \"%s\" not supported!"), | |
6124 | attr->values[0].string.text); | |
6125 | return; | |
6126 | } | |
6127 | else if (!strcmp(attr->name, "notify-natural-language") && | |
6128 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
6129 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
6130 | { | |
6131 | send_ipp_status(con, IPP_CHARSET, | |
6132 | _("Language \"%s\" not supported!"), | |
6133 | attr->values[0].string.text); | |
6134 | return; | |
6135 | } | |
6136 | else if (!strcmp(attr->name, "notify-user-data") && | |
6137 | attr->value_tag == IPP_TAG_STRING) | |
6138 | { | |
6139 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
6140 | { | |
6141 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
6142 | _("The notify-user-data value is too large " | |
6143 | "(%d > 63 octets)!"), | |
6144 | attr->values[0].unknown.length); | |
6145 | return; | |
6146 | } | |
6147 | ||
6148 | user_data = attr; | |
6149 | } | |
6150 | else if (!strcmp(attr->name, "notify-events") && | |
6151 | attr->value_tag == IPP_TAG_KEYWORD) | |
6152 | notify_events = attr; | |
6153 | else if (!strcmp(attr->name, "notify-lease-duration") && | |
6154 | attr->value_tag == IPP_TAG_INTEGER) | |
6155 | lease = attr->values[0].integer; | |
6156 | else if (!strcmp(attr->name, "notify-time-interval") && | |
6157 | attr->value_tag == IPP_TAG_INTEGER) | |
6158 | interval = attr->values[0].integer; | |
6159 | else if (!strcmp(attr->name, "notify-job-id") && | |
6160 | attr->value_tag == IPP_TAG_INTEGER) | |
6161 | jobid = attr->values[0].integer; | |
6162 | ||
6163 | attr = attr->next; | |
6164 | } | |
6165 | ||
6166 | if (notify_events) | |
6167 | { | |
6168 | for (i = 0; i < notify_events->num_values; i ++) | |
6169 | mask |= cupsdEventValue(notify_events->values[i].string.text); | |
6170 | } | |
6171 | ||
6172 | if (recipient) | |
6173 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); | |
6174 | if (pullmethod) | |
6175 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
6176 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
6177 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
6178 | ||
6179 | if (!recipient && !pullmethod) | |
6180 | break; | |
6181 | ||
6182 | if (mask == CUPSD_EVENT_NONE) | |
6183 | { | |
6184 | if (jobid) | |
6185 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
6186 | else if (printer) | |
6187 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
6188 | else | |
6189 | { | |
6190 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6191 | _("notify-events not specified!")); | |
6192 | return; | |
6193 | } | |
6194 | } | |
6195 | ||
6196 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) | |
6197 | { | |
6198 | cupsdLogMessage(CUPSD_LOG_INFO, | |
6199 | "create_subscription: Limiting notify-lease-duration to " | |
6200 | "%d seconds.", | |
6201 | MaxLeaseDuration); | |
6202 | lease = MaxLeaseDuration; | |
6203 | } | |
6204 | ||
6205 | if (jobid) | |
6206 | { | |
6207 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6208 | { | |
6209 | send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid); | |
6210 | return; | |
6211 | } | |
6212 | } | |
6213 | else | |
6214 | job = NULL; | |
6215 | ||
6216 | sub = cupsdAddSubscription(mask, printer, job, recipient, 0); | |
6217 | ||
6218 | if (job) | |
6219 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
6220 | sub->id, job->id); | |
6221 | else if (printer) | |
6222 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
6223 | "Added subscription %d for printer \"%s\"", | |
6224 | sub->id, printer->name); | |
6225 | else | |
6226 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server", | |
6227 | sub->id); | |
6228 | ||
6229 | sub->interval = interval; | |
6230 | sub->lease = lease; | |
6231 | sub->expire = lease ? time(NULL) + lease : 0; | |
6232 | ||
6233 | cupsdSetString(&sub->owner, username); | |
6234 | ||
6235 | if (user_data) | |
6236 | { | |
6237 | sub->user_data_len = user_data->values[0].unknown.length; | |
6238 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
6239 | sub->user_data_len); | |
6240 | } | |
6241 | ||
6242 | ippAddSeparator(con->response); | |
6243 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
6244 | "notify-subscription-id", sub->id); | |
6245 | ||
6246 | con->response->request.status.status_code = IPP_OK; | |
6247 | ||
6248 | if (attr) | |
6249 | attr = attr->next; | |
6250 | } | |
6251 | ||
6252 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); | |
6253 | } | |
6254 | ||
6255 | ||
6256 | /* | |
6257 | * 'delete_printer()' - Remove a printer or class from the system. | |
6258 | */ | |
6259 | ||
6260 | static void | |
6261 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
6262 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
6263 | { | |
6264 | http_status_t status; /* Policy status */ | |
6265 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
6266 | cupsd_printer_t *printer; /* Printer/class */ | |
6267 | char filename[1024]; /* Script/PPD filename */ | |
6268 | ||
6269 | ||
6270 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
6271 | con->http.fd, uri->values[0].string.text); | |
6272 | ||
6273 | /* | |
6274 | * Do we have a valid URI? | |
6275 | */ | |
6276 | ||
6277 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
6278 | { | |
6279 | /* | |
6280 | * Bad URI... | |
6281 | */ | |
6282 | ||
6283 | send_ipp_status(con, IPP_NOT_FOUND, | |
6284 | _("The printer or class was not found.")); | |
6285 | return; | |
6286 | } | |
6287 | ||
6288 | /* | |
6289 | * Check policy... | |
6290 | */ | |
6291 | ||
6292 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6293 | { | |
6294 | send_http_error(con, status, NULL); | |
6295 | return; | |
6296 | } | |
6297 | ||
6298 | /* | |
6299 | * Remove old jobs... | |
6300 | */ | |
6301 | ||
6302 | cupsdCancelJobs(printer->name, NULL, 1); | |
6303 | ||
6304 | /* | |
6305 | * Remove old subscriptions and send a "deleted printer" event... | |
6306 | */ | |
6307 | ||
6308 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
6309 | "%s \"%s\" deleted by \"%s\".", | |
6310 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
6311 | printer->name, get_username(con)); | |
6312 | ||
6313 | cupsdExpireSubscriptions(printer, NULL); | |
6314 | ||
6315 | /* | |
6316 | * Remove any old PPD or script files... | |
6317 | */ | |
6318 | ||
6319 | snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, | |
6320 | printer->name); | |
6321 | unlink(filename); | |
6322 | ||
6323 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, | |
6324 | printer->name); | |
6325 | unlink(filename); | |
6326 | ||
6327 | #ifdef __APPLE__ | |
6328 | /* | |
6329 | * Unregister color profiles... | |
6330 | */ | |
6331 | ||
6332 | apple_unregister_profiles(printer); | |
6333 | #endif /* __APPLE__ */ | |
6334 | ||
6335 | if (dtype & CUPS_PRINTER_CLASS) | |
6336 | { | |
6337 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", | |
6338 | printer->name, get_username(con)); | |
6339 | ||
6340 | cupsdDeletePrinter(printer, 0); | |
6341 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); | |
6342 | } | |
6343 | else | |
6344 | { | |
6345 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", | |
6346 | printer->name, get_username(con)); | |
6347 | ||
6348 | cupsdDeletePrinter(printer, 0); | |
6349 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
6350 | } | |
6351 | ||
6352 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); | |
6353 | ||
6354 | /* | |
6355 | * Return with no errors... | |
6356 | */ | |
6357 | ||
6358 | con->response->request.status.status_code = IPP_OK; | |
6359 | } | |
6360 | ||
6361 | ||
6362 | /* | |
6363 | * 'get_default()' - Get the default destination. | |
6364 | */ | |
6365 | ||
6366 | static void | |
6367 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
6368 | { | |
6369 | http_status_t status; /* Policy status */ | |
6370 | cups_array_t *ra; /* Requested attributes array */ | |
6371 | ||
6372 | ||
6373 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd); | |
6374 | ||
6375 | /* | |
6376 | * Check policy... | |
6377 | */ | |
6378 | ||
6379 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6380 | { | |
6381 | send_http_error(con, status, NULL); | |
6382 | return; | |
6383 | } | |
6384 | ||
6385 | if (DefaultPrinter) | |
6386 | { | |
6387 | ra = create_requested_array(con->request); | |
6388 | ||
6389 | copy_printer_attrs(con, DefaultPrinter, ra); | |
6390 | ||
6391 | cupsArrayDelete(ra); | |
6392 | ||
6393 | con->response->request.status.status_code = IPP_OK; | |
6394 | } | |
6395 | else | |
6396 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer")); | |
6397 | } | |
6398 | ||
6399 | ||
6400 | /* | |
6401 | * 'get_devices()' - Get the list of available devices on the local system. | |
6402 | */ | |
6403 | ||
6404 | static void | |
6405 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
6406 | { | |
6407 | http_status_t status; /* Policy status */ | |
6408 | ipp_attribute_t *limit, /* limit attribute */ | |
6409 | *timeout, /* timeout attribute */ | |
6410 | *requested, /* requested-attributes attribute */ | |
6411 | *exclude; /* exclude-schemes attribute */ | |
6412 | char command[1024], /* cups-deviced command */ | |
6413 | options[1024], /* Options to pass to command */ | |
6414 | requested_str[256], | |
6415 | /* String for requested attributes */ | |
6416 | exclude_str[512]; | |
6417 | /* String for excluded attributes */ | |
6418 | ||
6419 | ||
6420 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd); | |
6421 | ||
6422 | /* | |
6423 | * Check policy... | |
6424 | */ | |
6425 | ||
6426 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6427 | { | |
6428 | send_http_error(con, status, NULL); | |
6429 | return; | |
6430 | } | |
6431 | ||
6432 | /* | |
6433 | * Run cups-deviced command with the given options... | |
6434 | */ | |
6435 | ||
6436 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
6437 | timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER); | |
6438 | requested = ippFindAttribute(con->request, "requested-attributes", | |
6439 | IPP_TAG_KEYWORD); | |
6440 | exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME); | |
6441 | ||
6442 | if (requested) | |
6443 | url_encode_attr(requested, requested_str, sizeof(requested_str)); | |
6444 | else | |
6445 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); | |
6446 | ||
6447 | if (exclude) | |
6448 | url_encode_attr(exclude, exclude_str, sizeof(exclude_str)); | |
6449 | else | |
6450 | exclude_str[0] = '\0'; | |
6451 | ||
6452 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); | |
6453 | snprintf(options, sizeof(options), | |
6454 | "%d+%d+%d+%d+%s%s%s", | |
6455 | con->request->request.op.request_id, | |
6456 | limit ? limit->values[0].integer : 0, | |
6457 | timeout ? timeout->values[0].integer : 10, | |
6458 | (int)User, | |
6459 | requested_str, | |
6460 | exclude_str[0] ? "%20" : "", exclude_str); | |
6461 | ||
6462 | if (cupsdSendCommand(con, command, options, 1)) | |
6463 | { | |
6464 | /* | |
6465 | * Command started successfully, don't send an IPP response here... | |
6466 | */ | |
6467 | ||
6468 | ippDelete(con->response); | |
6469 | con->response = NULL; | |
6470 | } | |
6471 | else | |
6472 | { | |
6473 | /* | |
6474 | * Command failed, return "internal error" so the user knows something | |
6475 | * went wrong... | |
6476 | */ | |
6477 | ||
6478 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
6479 | _("cups-deviced failed to execute.")); | |
6480 | } | |
6481 | } | |
6482 | ||
6483 | ||
6484 | /* | |
6485 | * 'get_document()' - Get a copy of a job file. | |
6486 | */ | |
6487 | ||
6488 | static void | |
6489 | get_document(cupsd_client_t *con, /* I - Client connection */ | |
6490 | ipp_attribute_t *uri) /* I - Job URI */ | |
6491 | { | |
6492 | http_status_t status; /* Policy status */ | |
6493 | ipp_attribute_t *attr; /* Current attribute */ | |
6494 | int jobid; /* Job ID */ | |
6495 | int docnum; /* Document number */ | |
6496 | cupsd_job_t *job; /* Current job */ | |
6497 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6498 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6499 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6500 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6501 | int port; /* Port portion of URI */ | |
6502 | char filename[1024], /* Filename for document */ | |
6503 | format[1024]; /* Format for document */ | |
6504 | ||
6505 | ||
6506 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con, | |
6507 | con->http.fd, uri->values[0].string.text); | |
6508 | ||
6509 | /* | |
6510 | * See if we have a job URI or a printer URI... | |
6511 | */ | |
6512 | ||
6513 | if (!strcmp(uri->name, "printer-uri")) | |
6514 | { | |
6515 | /* | |
6516 | * Got a printer URI; see if we also have a job-id attribute... | |
6517 | */ | |
6518 | ||
6519 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6520 | IPP_TAG_INTEGER)) == NULL) | |
6521 | { | |
6522 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6523 | _("Got a printer-uri attribute but no job-id!")); | |
6524 | return; | |
6525 | } | |
6526 | ||
6527 | jobid = attr->values[0].integer; | |
6528 | } | |
6529 | else | |
6530 | { | |
6531 | /* | |
6532 | * Got a job URI; parse it to get the job ID... | |
6533 | */ | |
6534 | ||
6535 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
6536 | sizeof(method), username, sizeof(username), host, | |
6537 | sizeof(host), &port, resource, sizeof(resource)); | |
6538 | ||
6539 | if (strncmp(resource, "/jobs/", 6)) | |
6540 | { | |
6541 | /* | |
6542 | * Not a valid URI! | |
6543 | */ | |
6544 | ||
6545 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6546 | _("Bad job-uri attribute \"%s\"!"), | |
6547 | uri->values[0].string.text); | |
6548 | return; | |
6549 | } | |
6550 | ||
6551 | jobid = atoi(resource + 6); | |
6552 | } | |
6553 | ||
6554 | /* | |
6555 | * See if the job exists... | |
6556 | */ | |
6557 | ||
6558 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6559 | { | |
6560 | /* | |
6561 | * Nope - return a "not found" error... | |
6562 | */ | |
6563 | ||
6564 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6565 | return; | |
6566 | } | |
6567 | ||
6568 | /* | |
6569 | * Check policy... | |
6570 | */ | |
6571 | ||
6572 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6573 | { | |
6574 | send_http_error(con, status, NULL); | |
6575 | return; | |
6576 | } | |
6577 | ||
6578 | /* | |
6579 | * Get the document number... | |
6580 | */ | |
6581 | ||
6582 | if ((attr = ippFindAttribute(con->request, "document-number", | |
6583 | IPP_TAG_INTEGER)) == NULL) | |
6584 | { | |
6585 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6586 | _("Missing document-number attribute!")); | |
6587 | return; | |
6588 | } | |
6589 | ||
6590 | if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files || | |
6591 | attr->num_values > 1) | |
6592 | { | |
6593 | send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."), | |
6594 | docnum, jobid); | |
6595 | return; | |
6596 | } | |
6597 | ||
6598 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid, | |
6599 | docnum); | |
6600 | if ((con->file = open(filename, O_RDONLY)) == -1) | |
6601 | { | |
6602 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6603 | "Unable to open document %d in job %d - %s", docnum, jobid, | |
6604 | strerror(errno)); | |
6605 | send_ipp_status(con, IPP_NOT_FOUND, | |
6606 | _("Unable to open document %d in job %d!"), docnum, jobid); | |
6607 | return; | |
6608 | } | |
6609 | ||
6610 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
6611 | ||
6612 | cupsdLoadJob(job); | |
6613 | ||
6614 | snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super, | |
6615 | job->filetypes[docnum - 1]->type); | |
6616 | ||
6617 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
6618 | NULL, format); | |
6619 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number", | |
6620 | docnum); | |
6621 | if ((attr = ippFindAttribute(job->attrs, "document-name", | |
6622 | IPP_TAG_NAME)) != NULL) | |
6623 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name", | |
6624 | NULL, attr->values[0].string.text); | |
6625 | } | |
6626 | ||
6627 | ||
6628 | /* | |
6629 | * 'get_job_attrs()' - Get job attributes. | |
6630 | */ | |
6631 | ||
6632 | static void | |
6633 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
6634 | ipp_attribute_t *uri) /* I - Job URI */ | |
6635 | { | |
6636 | http_status_t status; /* Policy status */ | |
6637 | ipp_attribute_t *attr; /* Current attribute */ | |
6638 | int jobid; /* Job ID */ | |
6639 | cupsd_job_t *job; /* Current job */ | |
6640 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6641 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6642 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6643 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6644 | int port; /* Port portion of URI */ | |
6645 | cups_array_t *ra; /* Requested attributes array */ | |
6646 | ||
6647 | ||
6648 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, | |
6649 | con->http.fd, uri->values[0].string.text); | |
6650 | ||
6651 | /* | |
6652 | * See if we have a job URI or a printer URI... | |
6653 | */ | |
6654 | ||
6655 | if (!strcmp(uri->name, "printer-uri")) | |
6656 | { | |
6657 | /* | |
6658 | * Got a printer URI; see if we also have a job-id attribute... | |
6659 | */ | |
6660 | ||
6661 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6662 | IPP_TAG_INTEGER)) == NULL) | |
6663 | { | |
6664 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6665 | _("Got a printer-uri attribute but no job-id!")); | |
6666 | return; | |
6667 | } | |
6668 | ||
6669 | jobid = attr->values[0].integer; | |
6670 | } | |
6671 | else | |
6672 | { | |
6673 | /* | |
6674 | * Got a job URI; parse it to get the job ID... | |
6675 | */ | |
6676 | ||
6677 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
6678 | sizeof(method), username, sizeof(username), host, | |
6679 | sizeof(host), &port, resource, sizeof(resource)); | |
6680 | ||
6681 | if (strncmp(resource, "/jobs/", 6)) | |
6682 | { | |
6683 | /* | |
6684 | * Not a valid URI! | |
6685 | */ | |
6686 | ||
6687 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6688 | _("Bad job-uri attribute \"%s\"!"), | |
6689 | uri->values[0].string.text); | |
6690 | return; | |
6691 | } | |
6692 | ||
6693 | jobid = atoi(resource + 6); | |
6694 | } | |
6695 | ||
6696 | /* | |
6697 | * See if the job exists... | |
6698 | */ | |
6699 | ||
6700 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6701 | { | |
6702 | /* | |
6703 | * Nope - return a "not found" error... | |
6704 | */ | |
6705 | ||
6706 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6707 | return; | |
6708 | } | |
6709 | ||
6710 | /* | |
6711 | * Check policy... | |
6712 | */ | |
6713 | ||
6714 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6715 | { | |
6716 | send_http_error(con, status, NULL); | |
6717 | return; | |
6718 | } | |
6719 | ||
6720 | /* | |
6721 | * Copy attributes... | |
6722 | */ | |
6723 | ||
6724 | cupsdLoadJob(job); | |
6725 | ||
6726 | ra = create_requested_array(con->request); | |
6727 | copy_job_attrs(con, job, ra); | |
6728 | cupsArrayDelete(ra); | |
6729 | ||
6730 | con->response->request.status.status_code = IPP_OK; | |
6731 | } | |
6732 | ||
6733 | ||
6734 | /* | |
6735 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
6736 | */ | |
6737 | ||
6738 | static void | |
6739 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
6740 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6741 | { | |
6742 | http_status_t status; /* Policy status */ | |
6743 | ipp_attribute_t *attr; /* Current attribute */ | |
6744 | const char *dest; /* Destination */ | |
6745 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
6746 | cups_ptype_t dmask; /* Destination type mask */ | |
6747 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
6748 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6749 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6750 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6751 | int port; /* Port portion of URI */ | |
6752 | int completed; /* Completed jobs? */ | |
6753 | int first_job_id; /* First job ID */ | |
6754 | int limit; /* Maximum number of jobs to return */ | |
6755 | int count; /* Number of jobs that match */ | |
6756 | cupsd_job_t *job; /* Current job pointer */ | |
6757 | cupsd_printer_t *printer; /* Printer */ | |
6758 | cups_array_t *list; /* Which job list... */ | |
6759 | cups_array_t *ra; /* Requested attributes array */ | |
6760 | ||
6761 | ||
6762 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd, | |
6763 | uri->values[0].string.text); | |
6764 | ||
6765 | /* | |
6766 | * Is the destination valid? | |
6767 | */ | |
6768 | ||
6769 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
6770 | sizeof(scheme), username, sizeof(username), host, | |
6771 | sizeof(host), &port, resource, sizeof(resource)); | |
6772 | ||
6773 | if (!strcmp(resource, "/") || | |
6774 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6)) | |
6775 | { | |
6776 | dest = NULL; | |
6777 | dtype = (cups_ptype_t)0; | |
6778 | dmask = (cups_ptype_t)0; | |
6779 | printer = NULL; | |
6780 | } | |
6781 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
6782 | { | |
6783 | dest = NULL; | |
6784 | dtype = (cups_ptype_t)0; | |
6785 | dmask = CUPS_PRINTER_CLASS; | |
6786 | printer = NULL; | |
6787 | } | |
6788 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
6789 | { | |
6790 | dest = NULL; | |
6791 | dtype = CUPS_PRINTER_CLASS; | |
6792 | dmask = CUPS_PRINTER_CLASS; | |
6793 | printer = NULL; | |
6794 | } | |
6795 | else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype, | |
6796 | &printer)) == NULL) | |
6797 | { | |
6798 | /* | |
6799 | * Bad URI... | |
6800 | */ | |
6801 | ||
6802 | send_ipp_status(con, IPP_NOT_FOUND, | |
6803 | _("The printer or class was not found.")); | |
6804 | return; | |
6805 | } | |
6806 | else | |
6807 | { | |
6808 | dtype &= CUPS_PRINTER_CLASS; | |
6809 | dmask = CUPS_PRINTER_CLASS; | |
6810 | } | |
6811 | ||
6812 | /* | |
6813 | * Check policy... | |
6814 | */ | |
6815 | ||
6816 | if (printer) | |
6817 | { | |
6818 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, | |
6819 | NULL)) != HTTP_OK) | |
6820 | { | |
6821 | send_http_error(con, status, printer); | |
6822 | return; | |
6823 | } | |
6824 | } | |
6825 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6826 | { | |
6827 | send_http_error(con, status, NULL); | |
6828 | return; | |
6829 | } | |
6830 | ||
6831 | /* | |
6832 | * See if the "which-jobs" attribute have been specified... | |
6833 | */ | |
6834 | ||
6835 | if ((attr = ippFindAttribute(con->request, "which-jobs", | |
6836 | IPP_TAG_KEYWORD)) != NULL && | |
6837 | !strcmp(attr->values[0].string.text, "completed")) | |
6838 | { | |
6839 | completed = 1; | |
6840 | list = Jobs; | |
6841 | } | |
6842 | else if (attr && !strcmp(attr->values[0].string.text, "all")) | |
6843 | { | |
6844 | completed = 0; | |
6845 | list = Jobs; | |
6846 | } | |
6847 | else if (attr && !strcmp(attr->values[0].string.text, "printing")) | |
6848 | { | |
6849 | completed = 0; | |
6850 | list = PrintingJobs; | |
6851 | } | |
6852 | else | |
6853 | { | |
6854 | completed = 0; | |
6855 | list = ActiveJobs; | |
6856 | } | |
6857 | ||
6858 | /* | |
6859 | * See if they want to limit the number of jobs reported... | |
6860 | */ | |
6861 | ||
6862 | if ((attr = ippFindAttribute(con->request, "limit", | |
6863 | IPP_TAG_INTEGER)) != NULL) | |
6864 | limit = attr->values[0].integer; | |
6865 | else | |
6866 | limit = 0; | |
6867 | ||
6868 | if ((attr = ippFindAttribute(con->request, "first-job-id", | |
6869 | IPP_TAG_INTEGER)) != NULL) | |
6870 | first_job_id = attr->values[0].integer; | |
6871 | else | |
6872 | first_job_id = 1; | |
6873 | ||
6874 | /* | |
6875 | * See if we only want to see jobs for a specific user... | |
6876 | */ | |
6877 | ||
6878 | if ((attr = ippFindAttribute(con->request, "my-jobs", | |
6879 | IPP_TAG_BOOLEAN)) != NULL && | |
6880 | attr->values[0].boolean) | |
6881 | strlcpy(username, get_username(con), sizeof(username)); | |
6882 | else | |
6883 | username[0] = '\0'; | |
6884 | ||
6885 | ra = create_requested_array(con->request); | |
6886 | ||
6887 | /* | |
6888 | * OK, build a list of jobs for this printer... | |
6889 | */ | |
6890 | ||
6891 | for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list); | |
6892 | (limit <= 0 || count < limit) && job; | |
6893 | job = (cupsd_job_t *)cupsArrayNext(list)) | |
6894 | { | |
6895 | /* | |
6896 | * Filter out jobs that don't match... | |
6897 | */ | |
6898 | ||
6899 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id); | |
6900 | ||
6901 | if (!job->dest || !job->username) | |
6902 | cupsdLoadJob(job); | |
6903 | ||
6904 | if (!job->dest || !job->username) | |
6905 | continue; | |
6906 | ||
6907 | if ((dest && strcmp(job->dest, dest)) && | |
6908 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
6909 | continue; | |
6910 | if ((job->dtype & dmask) != dtype && | |
6911 | (!job->printer || (job->printer->type & dmask) != dtype)) | |
6912 | continue; | |
6913 | if (completed && job->state_value <= IPP_JOB_STOPPED) | |
6914 | continue; | |
6915 | ||
6916 | if (job->id < first_job_id) | |
6917 | continue; | |
6918 | ||
6919 | cupsdLoadJob(job); | |
6920 | ||
6921 | if (!job->attrs) | |
6922 | continue; | |
6923 | ||
6924 | if (username[0] && strcasecmp(username, job->username)) | |
6925 | continue; | |
6926 | ||
6927 | if (count > 0) | |
6928 | ippAddSeparator(con->response); | |
6929 | ||
6930 | count ++; | |
6931 | ||
6932 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count); | |
6933 | ||
6934 | copy_job_attrs(con, job, ra); | |
6935 | } | |
6936 | ||
6937 | cupsArrayDelete(ra); | |
6938 | ||
6939 | con->response->request.status.status_code = IPP_OK; | |
6940 | } | |
6941 | ||
6942 | ||
6943 | /* | |
6944 | * 'get_notifications()' - Get events for a subscription. | |
6945 | */ | |
6946 | ||
6947 | static void | |
6948 | get_notifications(cupsd_client_t *con) /* I - Client connection */ | |
6949 | { | |
6950 | int i, j; /* Looping vars */ | |
6951 | http_status_t status; /* Policy status */ | |
6952 | cupsd_subscription_t *sub; /* Subscription */ | |
6953 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
6954 | *sequences; /* notify-sequence-numbers */ | |
6955 | int min_seq; /* Minimum sequence number */ | |
6956 | int interval; /* Poll interval */ | |
6957 | ||
6958 | ||
6959 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])", | |
6960 | con, con->http.fd); | |
6961 | ||
6962 | /* | |
6963 | * Get subscription attributes... | |
6964 | */ | |
6965 | ||
6966 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
6967 | IPP_TAG_INTEGER); | |
6968 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
6969 | IPP_TAG_INTEGER); | |
6970 | ||
6971 | if (!ids) | |
6972 | { | |
6973 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6974 | _("Missing notify-subscription-ids attribute!")); | |
6975 | return; | |
6976 | } | |
6977 | ||
6978 | /* | |
6979 | * Are the subscription IDs valid? | |
6980 | */ | |
6981 | ||
6982 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
6983 | { | |
6984 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
6985 | { | |
6986 | /* | |
6987 | * Bad subscription ID... | |
6988 | */ | |
6989 | ||
6990 | send_ipp_status(con, IPP_NOT_FOUND, | |
6991 | _("notify-subscription-id %d no good!"), | |
6992 | ids->values[i].integer); | |
6993 | return; | |
6994 | } | |
6995 | ||
6996 | /* | |
6997 | * Check policy... | |
6998 | */ | |
6999 | ||
7000 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
7001 | DefaultPolicyPtr, | |
7002 | con, sub->owner)) != HTTP_OK) | |
7003 | { | |
7004 | send_http_error(con, status, sub->dest); | |
7005 | return; | |
7006 | } | |
7007 | ||
7008 | /* | |
7009 | * Check the subscription type and update the interval accordingly. | |
7010 | */ | |
7011 | ||
7012 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
7013 | interval > 10) | |
7014 | interval = 10; | |
7015 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
7016 | interval = 0; | |
7017 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
7018 | interval > 30) | |
7019 | interval = 30; | |
7020 | } | |
7021 | ||
7022 | /* | |
7023 | * Tell the client to poll again in N seconds... | |
7024 | */ | |
7025 | ||
7026 | if (interval > 0) | |
7027 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
7028 | "notify-get-interval", interval); | |
7029 | ||
7030 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
7031 | "printer-up-time", time(NULL)); | |
7032 | ||
7033 | /* | |
7034 | * Copy the subscription event attributes to the response. | |
7035 | */ | |
7036 | ||
7037 | con->response->request.status.status_code = | |
7038 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
7039 | ||
7040 | for (i = 0; i < ids->num_values; i ++) | |
7041 | { | |
7042 | /* | |
7043 | * Get the subscription and sequence number... | |
7044 | */ | |
7045 | ||
7046 | sub = cupsdFindSubscription(ids->values[i].integer); | |
7047 | ||
7048 | if (sequences && i < sequences->num_values) | |
7049 | min_seq = sequences->values[i].integer; | |
7050 | else | |
7051 | min_seq = 1; | |
7052 | ||
7053 | /* | |
7054 | * If we don't have any new events, nothing to do here... | |
7055 | */ | |
7056 | ||
7057 | if (min_seq > (sub->first_event_id + sub->num_events)) | |
7058 | continue; | |
7059 | ||
7060 | /* | |
7061 | * Otherwise copy all of the new events... | |
7062 | */ | |
7063 | ||
7064 | if (sub->first_event_id > min_seq) | |
7065 | j = 0; | |
7066 | else | |
7067 | j = min_seq - sub->first_event_id; | |
7068 | ||
7069 | for (; j < sub->num_events; j ++) | |
7070 | { | |
7071 | ippAddSeparator(con->response); | |
7072 | ||
7073 | copy_attrs(con->response, sub->events[j]->attrs, NULL, | |
7074 | IPP_TAG_EVENT_NOTIFICATION, 0); | |
7075 | } | |
7076 | } | |
7077 | } | |
7078 | ||
7079 | ||
7080 | /* | |
7081 | * 'get_ppd()' - Get a named PPD from the local system. | |
7082 | */ | |
7083 | ||
7084 | static void | |
7085 | get_ppd(cupsd_client_t *con, /* I - Client connection */ | |
7086 | ipp_attribute_t *uri) /* I - Printer URI or PPD name */ | |
7087 | { | |
7088 | http_status_t status; /* Policy status */ | |
7089 | cupsd_printer_t *dest; /* Destination */ | |
7090 | cups_ptype_t dtype; /* Destination type */ | |
7091 | ||
7092 | ||
7093 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con, | |
7094 | con->http.fd, uri, uri->name, uri->values[0].string.text); | |
7095 | ||
7096 | if (!strcmp(uri->name, "ppd-name")) | |
7097 | { | |
7098 | /* | |
7099 | * Return a PPD file from cups-driverd... | |
7100 | */ | |
7101 | ||
7102 | char command[1024], /* cups-driverd command */ | |
7103 | options[1024], /* Options to pass to command */ | |
7104 | ppd_name[1024]; /* ppd-name */ | |
7105 | ||
7106 | ||
7107 | /* | |
7108 | * Check policy... | |
7109 | */ | |
7110 | ||
7111 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7112 | { | |
7113 | send_http_error(con, status, NULL); | |
7114 | return; | |
7115 | } | |
7116 | ||
7117 | /* | |
7118 | * Run cups-driverd command with the given options... | |
7119 | */ | |
7120 | ||
7121 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); | |
7122 | url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name)); | |
7123 | snprintf(options, sizeof(options), "get+%d+%s", | |
7124 | con->request->request.op.request_id, ppd_name); | |
7125 | ||
7126 | if (cupsdSendCommand(con, command, options, 0)) | |
7127 | { | |
7128 | /* | |
7129 | * Command started successfully, don't send an IPP response here... | |
7130 | */ | |
7131 | ||
7132 | ippDelete(con->response); | |
7133 | con->response = NULL; | |
7134 | } | |
7135 | else | |
7136 | { | |
7137 | /* | |
7138 | * Command failed, return "internal error" so the user knows something | |
7139 | * went wrong... | |
7140 | */ | |
7141 | ||
7142 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7143 | _("cups-driverd failed to execute.")); | |
7144 | } | |
7145 | } | |
7146 | else if (!strcmp(uri->name, "printer-uri") && | |
7147 | cupsdValidateDest(uri->values[0].string.text, &dtype, &dest)) | |
7148 | { | |
7149 | int i; /* Looping var */ | |
7150 | char filename[1024]; /* PPD filename */ | |
7151 | ||
7152 | ||
7153 | /* | |
7154 | * Check policy... | |
7155 | */ | |
7156 | ||
7157 | if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7158 | { | |
7159 | send_http_error(con, status, dest); | |
7160 | return; | |
7161 | } | |
7162 | ||
7163 | /* | |
7164 | * See if we need the PPD for a class or remote printer... | |
7165 | */ | |
7166 | ||
7167 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, | |
7168 | dest->name); | |
7169 | ||
7170 | if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0)) | |
7171 | { | |
7172 | con->response->request.status.status_code = CUPS_SEE_OTHER; | |
7173 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, | |
7174 | "printer-uri", NULL, dest->uri); | |
7175 | return; | |
7176 | } | |
7177 | else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) | |
7178 | { | |
7179 | for (i = 0; i < dest->num_printers; i ++) | |
7180 | if (!(dest->printers[i]->type & | |
7181 | (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))) | |
7182 | { | |
7183 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, | |
7184 | dest->printers[i]->name); | |
7185 | ||
7186 | if (!access(filename, 0)) | |
7187 | break; | |
7188 | } | |
7189 | ||
7190 | if (i < dest->num_printers) | |
7191 | dest = dest->printers[i]; | |
7192 | else | |
7193 | { | |
7194 | con->response->request.status.status_code = CUPS_SEE_OTHER; | |
7195 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, | |
7196 | "printer-uri", NULL, dest->printers[0]->uri); | |
7197 | return; | |
7198 | } | |
7199 | } | |
7200 | ||
7201 | /* | |
7202 | * Found the printer with the PPD file, now see if there is one... | |
7203 | */ | |
7204 | ||
7205 | if ((con->file = open(filename, O_RDONLY)) < 0) | |
7206 | { | |
7207 | send_ipp_status(con, IPP_NOT_FOUND, | |
7208 | _("The PPD file \"%s\" could not be opened: %s"), | |
7209 | uri->values[0].string.text, strerror(errno)); | |
7210 | return; | |
7211 | } | |
7212 | ||
7213 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
7214 | ||
7215 | con->pipe_pid = 0; | |
7216 | ||
7217 | con->response->request.status.status_code = IPP_OK; | |
7218 | } | |
7219 | else | |
7220 | send_ipp_status(con, IPP_NOT_FOUND, | |
7221 | _("The PPD file \"%s\" could not be found."), | |
7222 | uri->values[0].string.text); | |
7223 | } | |
7224 | ||
7225 | ||
7226 | /* | |
7227 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
7228 | */ | |
7229 | ||
7230 | static void | |
7231 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
7232 | { | |
7233 | http_status_t status; /* Policy status */ | |
7234 | ipp_attribute_t *limit, /* Limit attribute */ | |
7235 | *device, /* ppd-device-id attribute */ | |
7236 | *language, /* ppd-natural-language attribute */ | |
7237 | *make, /* ppd-make attribute */ | |
7238 | *model, /* ppd-make-and-model attribute */ | |
7239 | *model_number, /* ppd-model-number attribute */ | |
7240 | *product, /* ppd-product attribute */ | |
7241 | *psversion, /* ppd-psverion attribute */ | |
7242 | *type, /* ppd-type attribute */ | |
7243 | *requested; /* requested-attributes attribute */ | |
7244 | char command[1024], /* cups-driverd command */ | |
7245 | options[1024], /* Options to pass to command */ | |
7246 | device_str[256],/* Escaped ppd-device-id string */ | |
7247 | language_str[256], | |
7248 | /* Escaped ppd-natural-language */ | |
7249 | make_str[256], /* Escaped ppd-make string */ | |
7250 | model_str[256], /* Escaped ppd-make-and-model string */ | |
7251 | model_number_str[256], | |
7252 | /* ppd-model-number string */ | |
7253 | product_str[256], | |
7254 | /* Escaped ppd-product string */ | |
7255 | psversion_str[256], | |
7256 | /* Escaped ppd-psversion string */ | |
7257 | type_str[256], /* Escaped ppd-type string */ | |
7258 | requested_str[256]; | |
7259 | /* String for requested attributes */ | |
7260 | ||
7261 | ||
7262 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd); | |
7263 | ||
7264 | /* | |
7265 | * Check policy... | |
7266 | */ | |
7267 | ||
7268 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7269 | { | |
7270 | send_http_error(con, status, NULL); | |
7271 | return; | |
7272 | } | |
7273 | ||
7274 | /* | |
7275 | * Run cups-driverd command with the given options... | |
7276 | */ | |
7277 | ||
7278 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
7279 | device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT); | |
7280 | language = ippFindAttribute(con->request, "ppd-natural-language", | |
7281 | IPP_TAG_LANGUAGE); | |
7282 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
7283 | model = ippFindAttribute(con->request, "ppd-make-and-model", | |
7284 | IPP_TAG_TEXT); | |
7285 | model_number = ippFindAttribute(con->request, "ppd-model-number", | |
7286 | IPP_TAG_INTEGER); | |
7287 | product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT); | |
7288 | psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT); | |
7289 | type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD); | |
7290 | requested = ippFindAttribute(con->request, "requested-attributes", | |
7291 | IPP_TAG_KEYWORD); | |
7292 | ||
7293 | if (requested) | |
7294 | url_encode_attr(requested, requested_str, sizeof(requested_str)); | |
7295 | else | |
7296 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); | |
7297 | ||
7298 | if (device) | |
7299 | url_encode_attr(device, device_str, sizeof(device_str)); | |
7300 | else | |
7301 | device_str[0] = '\0'; | |
7302 | ||
7303 | if (language) | |
7304 | url_encode_attr(language, language_str, sizeof(language_str)); | |
7305 | else | |
7306 | language_str[0] = '\0'; | |
7307 | ||
7308 | if (make) | |
7309 | url_encode_attr(make, make_str, sizeof(make_str)); | |
7310 | else | |
7311 | make_str[0] = '\0'; | |
7312 | ||
7313 | if (model) | |
7314 | url_encode_attr(model, model_str, sizeof(model_str)); | |
7315 | else | |
7316 | model_str[0] = '\0'; | |
7317 | ||
7318 | if (model_number) | |
7319 | snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d", | |
7320 | model_number->values[0].integer); | |
7321 | else | |
7322 | model_number_str[0] = '\0'; | |
7323 | ||
7324 | if (product) | |
7325 | url_encode_attr(product, product_str, sizeof(product_str)); | |
7326 | else | |
7327 | product_str[0] = '\0'; | |
7328 | ||
7329 | if (psversion) | |
7330 | url_encode_attr(psversion, psversion_str, sizeof(psversion_str)); | |
7331 | else | |
7332 | psversion_str[0] = '\0'; | |
7333 | ||
7334 | if (type) | |
7335 | url_encode_attr(type, type_str, sizeof(type_str)); | |
7336 | else | |
7337 | type_str[0] = '\0'; | |
7338 | ||
7339 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); | |
7340 | snprintf(options, sizeof(options), | |
7341 | "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", | |
7342 | con->request->request.op.request_id, | |
7343 | limit ? limit->values[0].integer : 0, | |
7344 | requested_str, | |
7345 | device ? "%20" : "", device_str, | |
7346 | language ? "%20" : "", language_str, | |
7347 | make ? "%20" : "", make_str, | |
7348 | model ? "%20" : "", model_str, | |
7349 | model_number ? "%20" : "", model_number_str, | |
7350 | product ? "%20" : "", product_str, | |
7351 | psversion ? "%20" : "", psversion_str, | |
7352 | type ? "%20" : "", type_str); | |
7353 | ||
7354 | if (cupsdSendCommand(con, command, options, 0)) | |
7355 | { | |
7356 | /* | |
7357 | * Command started successfully, don't send an IPP response here... | |
7358 | */ | |
7359 | ||
7360 | ippDelete(con->response); | |
7361 | con->response = NULL; | |
7362 | } | |
7363 | else | |
7364 | { | |
7365 | /* | |
7366 | * Command failed, return "internal error" so the user knows something | |
7367 | * went wrong... | |
7368 | */ | |
7369 | ||
7370 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7371 | _("cups-driverd failed to execute.")); | |
7372 | } | |
7373 | } | |
7374 | ||
7375 | ||
7376 | /* | |
7377 | * 'get_printer_attrs()' - Get printer attributes. | |
7378 | */ | |
7379 | ||
7380 | static void | |
7381 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ | |
7382 | ipp_attribute_t *uri) /* I - Printer URI */ | |
7383 | { | |
7384 | http_status_t status; /* Policy status */ | |
7385 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
7386 | cupsd_printer_t *printer; /* Printer/class */ | |
7387 | cups_array_t *ra; /* Requested attributes array */ | |
7388 | ||
7389 | ||
7390 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, | |
7391 | con->http.fd, uri->values[0].string.text); | |
7392 | ||
7393 | /* | |
7394 | * Is the destination valid? | |
7395 | */ | |
7396 | ||
7397 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
7398 | { | |
7399 | /* | |
7400 | * Bad URI... | |
7401 | */ | |
7402 | ||
7403 | send_ipp_status(con, IPP_NOT_FOUND, | |
7404 | _("The printer or class was not found.")); | |
7405 | return; | |
7406 | } | |
7407 | ||
7408 | /* | |
7409 | * Check policy... | |
7410 | */ | |
7411 | ||
7412 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7413 | { | |
7414 | send_http_error(con, status, printer); | |
7415 | return; | |
7416 | } | |
7417 | ||
7418 | /* | |
7419 | * Send the attributes... | |
7420 | */ | |
7421 | ||
7422 | ra = create_requested_array(con->request); | |
7423 | ||
7424 | copy_printer_attrs(con, printer, ra); | |
7425 | ||
7426 | cupsArrayDelete(ra); | |
7427 | ||
7428 | con->response->request.status.status_code = IPP_OK; | |
7429 | } | |
7430 | ||
7431 | ||
7432 | /* | |
7433 | * 'get_printers()' - Get a list of printers or classes. | |
7434 | */ | |
7435 | ||
7436 | static void | |
7437 | get_printers(cupsd_client_t *con, /* I - Client connection */ | |
7438 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
7439 | { | |
7440 | http_status_t status; /* Policy status */ | |
7441 | ipp_attribute_t *attr; /* Current attribute */ | |
7442 | int limit; /* Max number of printers to return */ | |
7443 | int count; /* Number of printers that match */ | |
7444 | cupsd_printer_t *printer; /* Current printer pointer */ | |
7445 | int printer_type, /* printer-type attribute */ | |
7446 | printer_mask; /* printer-type-mask attribute */ | |
7447 | char *location; /* Location string */ | |
7448 | const char *username; /* Current user */ | |
7449 | char *first_printer_name; /* first-printer-name attribute */ | |
7450 | cups_array_t *ra; /* Requested attributes array */ | |
7451 | ||
7452 | ||
7453 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, | |
7454 | con->http.fd, type); | |
7455 | ||
7456 | /* | |
7457 | * Check policy... | |
7458 | */ | |
7459 | ||
7460 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7461 | { | |
7462 | send_http_error(con, status, NULL); | |
7463 | return; | |
7464 | } | |
7465 | ||
7466 | /* | |
7467 | * Check for printers... | |
7468 | */ | |
7469 | ||
7470 | if (!Printers || !cupsArrayCount(Printers)) | |
7471 | { | |
7472 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
7473 | return; | |
7474 | } | |
7475 | ||
7476 | /* | |
7477 | * See if they want to limit the number of printers reported... | |
7478 | */ | |
7479 | ||
7480 | if ((attr = ippFindAttribute(con->request, "limit", | |
7481 | IPP_TAG_INTEGER)) != NULL) | |
7482 | limit = attr->values[0].integer; | |
7483 | else | |
7484 | limit = 10000000; | |
7485 | ||
7486 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
7487 | IPP_TAG_NAME)) != NULL) | |
7488 | first_printer_name = attr->values[0].string.text; | |
7489 | else | |
7490 | first_printer_name = NULL; | |
7491 | ||
7492 | /* | |
7493 | * Support filtering... | |
7494 | */ | |
7495 | ||
7496 | if ((attr = ippFindAttribute(con->request, "printer-type", | |
7497 | IPP_TAG_ENUM)) != NULL) | |
7498 | printer_type = attr->values[0].integer; | |
7499 | else | |
7500 | printer_type = 0; | |
7501 | ||
7502 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", | |
7503 | IPP_TAG_ENUM)) != NULL) | |
7504 | printer_mask = attr->values[0].integer; | |
7505 | else | |
7506 | printer_mask = 0; | |
7507 | ||
7508 | if ((attr = ippFindAttribute(con->request, "printer-location", | |
7509 | IPP_TAG_TEXT)) != NULL) | |
7510 | location = attr->values[0].string.text; | |
7511 | else | |
7512 | location = NULL; | |
7513 | ||
7514 | if (con->username[0]) | |
7515 | username = con->username; | |
7516 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
7517 | IPP_TAG_NAME)) != NULL) | |
7518 | username = attr->values[0].string.text; | |
7519 | else | |
7520 | username = NULL; | |
7521 | ||
7522 | ra = create_requested_array(con->request); | |
7523 | ||
7524 | /* | |
7525 | * OK, build a list of printers for this printer... | |
7526 | */ | |
7527 | ||
7528 | if (first_printer_name) | |
7529 | { | |
7530 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) | |
7531 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
7532 | } | |
7533 | else | |
7534 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
7535 | ||
7536 | for (count = 0; | |
7537 | count < limit && printer; | |
7538 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
7539 | { | |
7540 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && | |
7541 | (printer->type & printer_mask) == printer_type && | |
7542 | (!location || !printer->location || | |
7543 | !strcasecmp(printer->location, location))) | |
7544 | { | |
7545 | /* | |
7546 | * If HideImplicitMembers is enabled, see if this printer or class | |
7547 | * is a member of an implicit class... | |
7548 | */ | |
7549 | ||
7550 | if (ImplicitClasses && HideImplicitMembers && | |
7551 | printer->in_implicit_class) | |
7552 | continue; | |
7553 | ||
7554 | /* | |
7555 | * If a username is specified, see if it is allowed or denied | |
7556 | * access... | |
7557 | */ | |
7558 | ||
7559 | if (!(printer->type & CUPS_PRINTER_AUTHENTICATED) && | |
7560 | printer->num_users && username && !user_allowed(printer, username)) | |
7561 | continue; | |
7562 | ||
7563 | /* | |
7564 | * Add the group separator as needed... | |
7565 | */ | |
7566 | ||
7567 | if (count > 0) | |
7568 | ippAddSeparator(con->response); | |
7569 | ||
7570 | count ++; | |
7571 | ||
7572 | /* | |
7573 | * Send the attributes... | |
7574 | */ | |
7575 | ||
7576 | copy_printer_attrs(con, printer, ra); | |
7577 | } | |
7578 | } | |
7579 | ||
7580 | cupsArrayDelete(ra); | |
7581 | ||
7582 | con->response->request.status.status_code = IPP_OK; | |
7583 | } | |
7584 | ||
7585 | ||
7586 | /* | |
7587 | * 'get_subscription_attrs()' - Get subscription attributes. | |
7588 | */ | |
7589 | ||
7590 | static void | |
7591 | get_subscription_attrs( | |
7592 | cupsd_client_t *con, /* I - Client connection */ | |
7593 | int sub_id) /* I - Subscription ID */ | |
7594 | { | |
7595 | http_status_t status; /* Policy status */ | |
7596 | cupsd_subscription_t *sub; /* Subscription */ | |
7597 | cups_array_t *ra; /* Requested attributes array */ | |
7598 | ||
7599 | ||
7600 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
7601 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
7602 | con, con->http.fd, sub_id); | |
7603 | ||
7604 | /* | |
7605 | * Is the subscription ID valid? | |
7606 | */ | |
7607 | ||
7608 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
7609 | { | |
7610 | /* | |
7611 | * Bad subscription ID... | |
7612 | */ | |
7613 | ||
7614 | send_ipp_status(con, IPP_NOT_FOUND, | |
7615 | _("notify-subscription-id %d no good!"), sub_id); | |
7616 | return; | |
7617 | } | |
7618 | ||
7619 | /* | |
7620 | * Check policy... | |
7621 | */ | |
7622 | ||
7623 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
7624 | DefaultPolicyPtr, | |
7625 | con, sub->owner)) != HTTP_OK) | |
7626 | { | |
7627 | send_http_error(con, status, sub->dest); | |
7628 | return; | |
7629 | } | |
7630 | ||
7631 | /* | |
7632 | * Copy the subscription attributes to the response using the | |
7633 | * requested-attributes attribute that may be provided by the client. | |
7634 | */ | |
7635 | ||
7636 | ra = create_requested_array(con->request); | |
7637 | ||
7638 | copy_subscription_attrs(con, sub, ra); | |
7639 | ||
7640 | cupsArrayDelete(ra); | |
7641 | ||
7642 | con->response->request.status.status_code = IPP_OK; | |
7643 | } | |
7644 | ||
7645 | ||
7646 | /* | |
7647 | * 'get_subscriptions()' - Get subscriptions. | |
7648 | */ | |
7649 | ||
7650 | static void | |
7651 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
7652 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
7653 | { | |
7654 | http_status_t status; /* Policy status */ | |
7655 | int count; /* Number of subscriptions */ | |
7656 | int limit; /* Limit */ | |
7657 | cupsd_subscription_t *sub; /* Subscription */ | |
7658 | cups_array_t *ra; /* Requested attributes array */ | |
7659 | ipp_attribute_t *attr; /* Attribute */ | |
7660 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
7661 | char scheme[HTTP_MAX_URI], | |
7662 | /* Scheme portion of URI */ | |
7663 | username[HTTP_MAX_URI], | |
7664 | /* Username portion of URI */ | |
7665 | host[HTTP_MAX_URI], | |
7666 | /* Host portion of URI */ | |
7667 | resource[HTTP_MAX_URI]; | |
7668 | /* Resource portion of URI */ | |
7669 | int port; /* Port portion of URI */ | |
7670 | cupsd_job_t *job; /* Job pointer */ | |
7671 | cupsd_printer_t *printer; /* Printer */ | |
7672 | ||
7673 | ||
7674 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
7675 | "get_subscriptions(con=%p[%d], uri=%s)", | |
7676 | con, con->http.fd, uri->values[0].string.text); | |
7677 | ||
7678 | /* | |
7679 | * Is the destination valid? | |
7680 | */ | |
7681 | ||
7682 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
7683 | sizeof(scheme), username, sizeof(username), host, | |
7684 | sizeof(host), &port, resource, sizeof(resource)); | |
7685 | ||
7686 | if (!strcmp(resource, "/") || | |
7687 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
7688 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
7689 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
7690 | { | |
7691 | printer = NULL; | |
7692 | job = NULL; | |
7693 | } | |
7694 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) | |
7695 | { | |
7696 | printer = NULL; | |
7697 | job = cupsdFindJob(atoi(resource + 6)); | |
7698 | ||
7699 | if (!job) | |
7700 | { | |
7701 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"), | |
7702 | resource + 6); | |
7703 | return; | |
7704 | } | |
7705 | } | |
7706 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
7707 | { | |
7708 | /* | |
7709 | * Bad URI... | |
7710 | */ | |
7711 | ||
7712 | send_ipp_status(con, IPP_NOT_FOUND, | |
7713 | _("The printer or class was not found.")); | |
7714 | return; | |
7715 | } | |
7716 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
7717 | IPP_TAG_INTEGER)) != NULL) | |
7718 | { | |
7719 | job = cupsdFindJob(attr->values[0].integer); | |
7720 | ||
7721 | if (!job) | |
7722 | { | |
7723 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), | |
7724 | attr->values[0].integer); | |
7725 | return; | |
7726 | } | |
7727 | } | |
7728 | else | |
7729 | job = NULL; | |
7730 | ||
7731 | /* | |
7732 | * Check policy... | |
7733 | */ | |
7734 | ||
7735 | if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr : | |
7736 | DefaultPolicyPtr, | |
7737 | con, NULL)) != HTTP_OK) | |
7738 | { | |
7739 | send_http_error(con, status, printer); | |
7740 | return; | |
7741 | } | |
7742 | ||
7743 | /* | |
7744 | * Copy the subscription attributes to the response using the | |
7745 | * requested-attributes attribute that may be provided by the client. | |
7746 | */ | |
7747 | ||
7748 | ra = create_requested_array(con->request); | |
7749 | ||
7750 | if ((attr = ippFindAttribute(con->request, "limit", | |
7751 | IPP_TAG_INTEGER)) != NULL) | |
7752 | limit = attr->values[0].integer; | |
7753 | else | |
7754 | limit = 0; | |
7755 | ||
7756 | /* | |
7757 | * See if we only want to see subscriptions for a specific user... | |
7758 | */ | |
7759 | ||
7760 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", | |
7761 | IPP_TAG_BOOLEAN)) != NULL && | |
7762 | attr->values[0].boolean) | |
7763 | strlcpy(username, get_username(con), sizeof(username)); | |
7764 | else | |
7765 | username[0] = '\0'; | |
7766 | ||
7767 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; | |
7768 | sub; | |
7769 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
7770 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
7771 | (!username[0] || !strcasecmp(username, sub->owner))) | |
7772 | { | |
7773 | ippAddSeparator(con->response); | |
7774 | copy_subscription_attrs(con, sub, ra); | |
7775 | ||
7776 | count ++; | |
7777 | if (limit && count >= limit) | |
7778 | break; | |
7779 | } | |
7780 | ||
7781 | cupsArrayDelete(ra); | |
7782 | ||
7783 | if (count) | |
7784 | con->response->request.status.status_code = IPP_OK; | |
7785 | else | |
7786 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
7787 | } | |
7788 | ||
7789 | ||
7790 | /* | |
7791 | * 'get_username()' - Get the username associated with a request. | |
7792 | */ | |
7793 | ||
7794 | static const char * /* O - Username */ | |
7795 | get_username(cupsd_client_t *con) /* I - Connection */ | |
7796 | { | |
7797 | ipp_attribute_t *attr; /* Attribute */ | |
7798 | ||
7799 | ||
7800 | if (con->username[0]) | |
7801 | return (con->username); | |
7802 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
7803 | IPP_TAG_NAME)) != NULL) | |
7804 | return (attr->values[0].string.text); | |
7805 | else | |
7806 | return ("anonymous"); | |
7807 | } | |
7808 | ||
7809 | ||
7810 | /* | |
7811 | * 'hold_job()' - Hold a print job. | |
7812 | */ | |
7813 | ||
7814 | static void | |
7815 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
7816 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
7817 | { | |
7818 | ipp_attribute_t *attr, /* Current job-hold-until */ | |
7819 | *newattr; /* New job-hold-until */ | |
7820 | int jobid; /* Job ID */ | |
7821 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7822 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7823 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7824 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7825 | int port; /* Port portion of URI */ | |
7826 | cupsd_job_t *job; /* Job information */ | |
7827 | ||
7828 | ||
7829 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd, | |
7830 | uri->values[0].string.text); | |
7831 | ||
7832 | /* | |
7833 | * See if we have a job URI or a printer URI... | |
7834 | */ | |
7835 | ||
7836 | if (!strcmp(uri->name, "printer-uri")) | |
7837 | { | |
7838 | /* | |
7839 | * Got a printer URI; see if we also have a job-id attribute... | |
7840 | */ | |
7841 | ||
7842 | if ((attr = ippFindAttribute(con->request, "job-id", | |
7843 | IPP_TAG_INTEGER)) == NULL) | |
7844 | { | |
7845 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7846 | _("Got a printer-uri attribute but no job-id!")); | |
7847 | return; | |
7848 | } | |
7849 | ||
7850 | jobid = attr->values[0].integer; | |
7851 | } | |
7852 | else | |
7853 | { | |
7854 | /* | |
7855 | * Got a job URI; parse it to get the job ID... | |
7856 | */ | |
7857 | ||
7858 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
7859 | sizeof(method), username, sizeof(username), host, | |
7860 | sizeof(host), &port, resource, sizeof(resource)); | |
7861 | ||
7862 | if (strncmp(resource, "/jobs/", 6)) | |
7863 | { | |
7864 | /* | |
7865 | * Not a valid URI! | |
7866 | */ | |
7867 | ||
7868 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7869 | _("Bad job-uri attribute \"%s\"!"), | |
7870 | uri->values[0].string.text); | |
7871 | return; | |
7872 | } | |
7873 | ||
7874 | jobid = atoi(resource + 6); | |
7875 | } | |
7876 | ||
7877 | /* | |
7878 | * See if the job exists... | |
7879 | */ | |
7880 | ||
7881 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7882 | { | |
7883 | /* | |
7884 | * Nope - return a "not found" error... | |
7885 | */ | |
7886 | ||
7887 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7888 | return; | |
7889 | } | |
7890 | ||
7891 | /* | |
7892 | * See if the job is owned by the requesting user... | |
7893 | */ | |
7894 | ||
7895 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7896 | { | |
7897 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
7898 | return; | |
7899 | } | |
7900 | ||
7901 | /* | |
7902 | * Hold the job and return... | |
7903 | */ | |
7904 | ||
7905 | cupsdHoldJob(job); | |
7906 | ||
7907 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, | |
7908 | "Job held by user."); | |
7909 | ||
7910 | if ((newattr = ippFindAttribute(con->request, "job-hold-until", | |
7911 | IPP_TAG_KEYWORD)) == NULL) | |
7912 | newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
7913 | ||
7914 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
7915 | IPP_TAG_KEYWORD)) == NULL) | |
7916 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
7917 | ||
7918 | if (attr) | |
7919 | { | |
7920 | /* | |
7921 | * Free the old hold value and copy the new one over... | |
7922 | */ | |
7923 | ||
7924 | _cupsStrFree(attr->values[0].string.text); | |
7925 | ||
7926 | if (newattr) | |
7927 | { | |
7928 | attr->value_tag = newattr->value_tag; | |
7929 | attr->values[0].string.text = | |
7930 | _cupsStrAlloc(newattr->values[0].string.text); | |
7931 | } | |
7932 | else | |
7933 | { | |
7934 | attr->value_tag = IPP_TAG_KEYWORD; | |
7935 | attr->values[0].string.text = _cupsStrAlloc("indefinite"); | |
7936 | } | |
7937 | ||
7938 | /* | |
7939 | * Hold job until specified time... | |
7940 | */ | |
7941 | ||
7942 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
7943 | ||
7944 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, | |
7945 | "Job job-hold-until value changed by user."); | |
7946 | } | |
7947 | ||
7948 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Held by \"%s\".", jobid, | |
7949 | username); | |
7950 | ||
7951 | con->response->request.status.status_code = IPP_OK; | |
7952 | } | |
7953 | ||
7954 | ||
7955 | /* | |
7956 | * 'move_job()' - Move a job to a new destination. | |
7957 | */ | |
7958 | ||
7959 | static void | |
7960 | move_job(cupsd_client_t *con, /* I - Client connection */ | |
7961 | ipp_attribute_t *uri) /* I - Job URI */ | |
7962 | { | |
7963 | http_status_t status; /* Policy status */ | |
7964 | ipp_attribute_t *attr; /* Current attribute */ | |
7965 | int jobid; /* Job ID */ | |
7966 | cupsd_job_t *job; /* Current job */ | |
7967 | const char *src; /* Source printer/class */ | |
7968 | cups_ptype_t stype, /* Source type (printer or class) */ | |
7969 | dtype; /* Destination type (printer/class) */ | |
7970 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ | |
7971 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7972 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7973 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7974 | int port; /* Port portion of URI */ | |
7975 | cupsd_printer_t *sprinter, /* Source printer */ | |
7976 | *dprinter; /* Destination printer */ | |
7977 | ||
7978 | ||
7979 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd, | |
7980 | uri->values[0].string.text); | |
7981 | ||
7982 | /* | |
7983 | * Get the new printer or class... | |
7984 | */ | |
7985 | ||
7986 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", | |
7987 | IPP_TAG_URI)) == NULL) | |
7988 | { | |
7989 | /* | |
7990 | * Need job-printer-uri... | |
7991 | */ | |
7992 | ||
7993 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7994 | _("job-printer-uri attribute missing!")); | |
7995 | return; | |
7996 | } | |
7997 | ||
7998 | if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter)) | |
7999 | { | |
8000 | /* | |
8001 | * Bad URI... | |
8002 | */ | |
8003 | ||
8004 | send_ipp_status(con, IPP_NOT_FOUND, | |
8005 | _("The printer or class was not found.")); | |
8006 | return; | |
8007 | } | |
8008 | ||
8009 | /* | |
8010 | * See if we have a job URI or a printer URI... | |
8011 | */ | |
8012 | ||
8013 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, | |
8014 | sizeof(scheme), username, sizeof(username), host, | |
8015 | sizeof(host), &port, resource, sizeof(resource)); | |
8016 | ||
8017 | if (!strcmp(uri->name, "printer-uri")) | |
8018 | { | |
8019 | /* | |
8020 | * Got a printer URI; see if we also have a job-id attribute... | |
8021 | */ | |
8022 | ||
8023 | if ((attr = ippFindAttribute(con->request, "job-id", | |
8024 | IPP_TAG_INTEGER)) == NULL) | |
8025 | { | |
8026 | /* | |
8027 | * Move all jobs... | |
8028 | */ | |
8029 | ||
8030 | if ((src = cupsdValidateDest(uri->values[0].string.text, &stype, | |
8031 | &sprinter)) == NULL) | |
8032 | { | |
8033 | /* | |
8034 | * Bad URI... | |
8035 | */ | |
8036 | ||
8037 | send_ipp_status(con, IPP_NOT_FOUND, | |
8038 | _("The printer or class was not found.")); | |
8039 | return; | |
8040 | } | |
8041 | ||
8042 | job = NULL; | |
8043 | } | |
8044 | else | |
8045 | { | |
8046 | /* | |
8047 | * Otherwise, just move a single job... | |
8048 | */ | |
8049 | ||
8050 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
8051 | { | |
8052 | /* | |
8053 | * Nope - return a "not found" error... | |
8054 | */ | |
8055 | ||
8056 | send_ipp_status(con, IPP_NOT_FOUND, | |
8057 | _("Job #%d does not exist!"), attr->values[0].integer); | |
8058 | return; | |
8059 | } | |
8060 | else | |
8061 | { | |
8062 | /* | |
8063 | * Job found, initialize source pointers... | |
8064 | */ | |
8065 | ||
8066 | src = NULL; | |
8067 | sprinter = NULL; | |
8068 | } | |
8069 | } | |
8070 | } | |
8071 | else | |
8072 | { | |
8073 | /* | |
8074 | * Got a job URI; parse it to get the job ID... | |
8075 | */ | |
8076 | ||
8077 | if (strncmp(resource, "/jobs/", 6)) | |
8078 | { | |
8079 | /* | |
8080 | * Not a valid URI! | |
8081 | */ | |
8082 | ||
8083 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8084 | _("Bad job-uri attribute \"%s\"!"), | |
8085 | uri->values[0].string.text); | |
8086 | return; | |
8087 | } | |
8088 | ||
8089 | /* | |
8090 | * See if the job exists... | |
8091 | */ | |
8092 | ||
8093 | jobid = atoi(resource + 6); | |
8094 | ||
8095 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8096 | { | |
8097 | /* | |
8098 | * Nope - return a "not found" error... | |
8099 | */ | |
8100 | ||
8101 | send_ipp_status(con, IPP_NOT_FOUND, | |
8102 | _("Job #%d does not exist!"), jobid); | |
8103 | return; | |
8104 | } | |
8105 | else | |
8106 | { | |
8107 | /* | |
8108 | * Job found, initialize source pointers... | |
8109 | */ | |
8110 | ||
8111 | src = NULL; | |
8112 | sprinter = NULL; | |
8113 | } | |
8114 | } | |
8115 | ||
8116 | /* | |
8117 | * Check the policy of the destination printer... | |
8118 | */ | |
8119 | ||
8120 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, | |
8121 | job ? job->username : NULL)) != HTTP_OK) | |
8122 | { | |
8123 | send_http_error(con, status, dprinter); | |
8124 | return; | |
8125 | } | |
8126 | ||
8127 | /* | |
8128 | * Now move the job or jobs... | |
8129 | */ | |
8130 | ||
8131 | if (job) | |
8132 | { | |
8133 | /* | |
8134 | * See if the job has been completed... | |
8135 | */ | |
8136 | ||
8137 | if (job->state_value > IPP_JOB_STOPPED) | |
8138 | { | |
8139 | /* | |
8140 | * Return a "not-possible" error... | |
8141 | */ | |
8142 | ||
8143 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8144 | _("Job #%d is finished and cannot be altered!"), | |
8145 | job->id); | |
8146 | return; | |
8147 | } | |
8148 | ||
8149 | /* | |
8150 | * See if the job is owned by the requesting user... | |
8151 | */ | |
8152 | ||
8153 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8154 | { | |
8155 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
8156 | return; | |
8157 | } | |
8158 | ||
8159 | /* | |
8160 | * Move the job to a different printer or class... | |
8161 | */ | |
8162 | ||
8163 | cupsdMoveJob(job, dprinter); | |
8164 | } | |
8165 | else | |
8166 | { | |
8167 | /* | |
8168 | * Got the source printer, now look through the jobs... | |
8169 | */ | |
8170 | ||
8171 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
8172 | job; | |
8173 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
8174 | { | |
8175 | /* | |
8176 | * See if the job is pointing at the source printer or has not been | |
8177 | * completed... | |
8178 | */ | |
8179 | ||
8180 | if (strcasecmp(job->dest, src) || | |
8181 | job->state_value > IPP_JOB_STOPPED) | |
8182 | continue; | |
8183 | ||
8184 | /* | |
8185 | * See if the job can be moved by the requesting user... | |
8186 | */ | |
8187 | ||
8188 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8189 | continue; | |
8190 | ||
8191 | /* | |
8192 | * Move the job to a different printer or class... | |
8193 | */ | |
8194 | ||
8195 | cupsdMoveJob(job, dprinter); | |
8196 | } | |
8197 | } | |
8198 | ||
8199 | /* | |
8200 | * Start jobs if possible... | |
8201 | */ | |
8202 | ||
8203 | cupsdCheckJobs(); | |
8204 | ||
8205 | /* | |
8206 | * Return with "everything is OK" status... | |
8207 | */ | |
8208 | ||
8209 | con->response->request.status.status_code = IPP_OK; | |
8210 | } | |
8211 | ||
8212 | ||
8213 | /* | |
8214 | * 'ppd_parse_line()' - Parse a PPD default line. | |
8215 | */ | |
8216 | ||
8217 | static int /* O - 0 on success, -1 on failure */ | |
8218 | ppd_parse_line(const char *line, /* I - Line */ | |
8219 | char *option, /* O - Option name */ | |
8220 | int olen, /* I - Size of option name */ | |
8221 | char *choice, /* O - Choice name */ | |
8222 | int clen) /* I - Size of choice name */ | |
8223 | { | |
8224 | /* | |
8225 | * Verify this is a default option line... | |
8226 | */ | |
8227 | ||
8228 | if (strncmp(line, "*Default", 8)) | |
8229 | return (-1); | |
8230 | ||
8231 | /* | |
8232 | * Read the option name... | |
8233 | */ | |
8234 | ||
8235 | for (line += 8, olen --; isalnum(*line & 255); line ++) | |
8236 | if (olen > 0) | |
8237 | { | |
8238 | *option++ = *line; | |
8239 | olen --; | |
8240 | } | |
8241 | ||
8242 | *option = '\0'; | |
8243 | ||
8244 | /* | |
8245 | * Skip everything else up to the colon (:)... | |
8246 | */ | |
8247 | ||
8248 | while (*line && *line != ':') | |
8249 | line ++; | |
8250 | ||
8251 | if (!*line) | |
8252 | return (-1); | |
8253 | ||
8254 | line ++; | |
8255 | ||
8256 | /* | |
8257 | * Now grab the option choice, skipping leading whitespace... | |
8258 | */ | |
8259 | ||
8260 | while (isspace(*line & 255)) | |
8261 | line ++; | |
8262 | ||
8263 | for (clen --; isalnum(*line & 255); line ++) | |
8264 | if (clen > 0) | |
8265 | { | |
8266 | *choice++ = *line; | |
8267 | clen --; | |
8268 | } | |
8269 | ||
8270 | *choice = '\0'; | |
8271 | ||
8272 | /* | |
8273 | * Return with no errors... | |
8274 | */ | |
8275 | ||
8276 | return (0); | |
8277 | } | |
8278 | ||
8279 | ||
8280 | /* | |
8281 | * 'print_job()' - Print a file to a printer or class. | |
8282 | */ | |
8283 | ||
8284 | static void | |
8285 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
8286 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8287 | { | |
8288 | ipp_attribute_t *attr; /* Current attribute */ | |
8289 | ipp_attribute_t *format; /* Document-format attribute */ | |
8290 | const char *default_format; /* document-format-default value */ | |
8291 | cupsd_job_t *job; /* New job */ | |
8292 | char filename[1024]; /* Job filename */ | |
8293 | mime_type_t *filetype; /* Type of file */ | |
8294 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
8295 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
8296 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
8297 | /* Textual name of mime type */ | |
8298 | cupsd_printer_t *printer; /* Printer data */ | |
8299 | struct stat fileinfo; /* File information */ | |
8300 | int kbytes; /* Size of file */ | |
8301 | int compression; /* Document compression */ | |
8302 | ||
8303 | ||
8304 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd, | |
8305 | uri->values[0].string.text); | |
8306 | ||
8307 | /* | |
8308 | * Validate print file attributes, for now just document-format and | |
8309 | * compression (CUPS only supports "none" and "gzip")... | |
8310 | */ | |
8311 | ||
8312 | compression = CUPS_FILE_NONE; | |
8313 | ||
8314 | if ((attr = ippFindAttribute(con->request, "compression", | |
8315 | IPP_TAG_KEYWORD)) != NULL) | |
8316 | { | |
8317 | if (strcmp(attr->values[0].string.text, "none") | |
8318 | #ifdef HAVE_LIBZ | |
8319 | && strcmp(attr->values[0].string.text, "gzip") | |
8320 | #endif /* HAVE_LIBZ */ | |
8321 | ) | |
8322 | { | |
8323 | send_ipp_status(con, IPP_ATTRIBUTES, | |
8324 | _("Unsupported compression \"%s\"!"), | |
8325 | attr->values[0].string.text); | |
8326 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
8327 | "compression", NULL, attr->values[0].string.text); | |
8328 | return; | |
8329 | } | |
8330 | ||
8331 | #ifdef HAVE_LIBZ | |
8332 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
8333 | compression = CUPS_FILE_GZIP; | |
8334 | #endif /* HAVE_LIBZ */ | |
8335 | } | |
8336 | ||
8337 | /* | |
8338 | * Do we have a file to print? | |
8339 | */ | |
8340 | ||
8341 | if (!con->filename) | |
8342 | { | |
8343 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
8344 | return; | |
8345 | } | |
8346 | ||
8347 | /* | |
8348 | * Is the destination valid? | |
8349 | */ | |
8350 | ||
8351 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
8352 | { | |
8353 | /* | |
8354 | * Bad URI... | |
8355 | */ | |
8356 | ||
8357 | send_ipp_status(con, IPP_NOT_FOUND, | |
8358 | _("The printer or class was not found.")); | |
8359 | return; | |
8360 | } | |
8361 | ||
8362 | /* | |
8363 | * Is it a format we support? | |
8364 | */ | |
8365 | ||
8366 | if ((format = ippFindAttribute(con->request, "document-format", | |
8367 | IPP_TAG_MIMETYPE)) != NULL) | |
8368 | { | |
8369 | /* | |
8370 | * Grab format from client... | |
8371 | */ | |
8372 | ||
8373 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, | |
8374 | type) != 2) | |
8375 | { | |
8376 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8377 | _("Could not scan type \"%s\"!"), | |
8378 | format->values[0].string.text); | |
8379 | return; | |
8380 | } | |
8381 | } | |
8382 | else if ((default_format = cupsGetOption("document-format", | |
8383 | printer->num_options, | |
8384 | printer->options)) != NULL) | |
8385 | { | |
8386 | /* | |
8387 | * Use default document format... | |
8388 | */ | |
8389 | ||
8390 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
8391 | { | |
8392 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8393 | _("Could not scan type \"%s\"!"), | |
8394 | default_format); | |
8395 | return; | |
8396 | } | |
8397 | } | |
8398 | else | |
8399 | { | |
8400 | /* | |
8401 | * Auto-type it! | |
8402 | */ | |
8403 | ||
8404 | strcpy(super, "application"); | |
8405 | strcpy(type, "octet-stream"); | |
8406 | } | |
8407 | ||
8408 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) | |
8409 | { | |
8410 | /* | |
8411 | * Auto-type the file... | |
8412 | */ | |
8413 | ||
8414 | ipp_attribute_t *doc_name; /* document-name attribute */ | |
8415 | ||
8416 | ||
8417 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file..."); | |
8418 | ||
8419 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
8420 | filetype = mimeFileType(MimeDatabase, con->filename, | |
8421 | doc_name ? doc_name->values[0].string.text : NULL, | |
8422 | &compression); | |
8423 | ||
8424 | if (!filetype) | |
8425 | filetype = mimeType(MimeDatabase, super, type); | |
8426 | ||
8427 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.", | |
8428 | filetype->super, filetype->type); | |
8429 | } | |
8430 | else | |
8431 | filetype = mimeType(MimeDatabase, super, type); | |
8432 | ||
8433 | if (filetype && | |
8434 | (!format || | |
8435 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) | |
8436 | { | |
8437 | /* | |
8438 | * Replace the document-format attribute value with the auto-typed or | |
8439 | * default one. | |
8440 | */ | |
8441 | ||
8442 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
8443 | filetype->type); | |
8444 | ||
8445 | if (format) | |
8446 | { | |
8447 | _cupsStrFree(format->values[0].string.text); | |
8448 | ||
8449 | format->values[0].string.text = _cupsStrAlloc(mimetype); | |
8450 | } | |
8451 | else | |
8452 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
8453 | "document-format", NULL, mimetype); | |
8454 | } | |
8455 | else if (!filetype) | |
8456 | { | |
8457 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
8458 | _("Unsupported format \'%s/%s\'!"), super, type); | |
8459 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8460 | "Hint: Do you have the raw file printing rules enabled?"); | |
8461 | ||
8462 | if (format) | |
8463 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
8464 | "document-format", NULL, format->values[0].string.text); | |
8465 | ||
8466 | return; | |
8467 | } | |
8468 | ||
8469 | /* | |
8470 | * Read any embedded job ticket info from PS files... | |
8471 | */ | |
8472 | ||
8473 | if (!strcasecmp(filetype->super, "application") && | |
8474 | !strcasecmp(filetype->type, "postscript")) | |
8475 | read_ps_job_ticket(con); | |
8476 | ||
8477 | /* | |
8478 | * Create the job object... | |
8479 | */ | |
8480 | ||
8481 | if ((job = add_job(con, printer, filetype)) == NULL) | |
8482 | return; | |
8483 | ||
8484 | /* | |
8485 | * Update quota data... | |
8486 | */ | |
8487 | ||
8488 | if (stat(con->filename, &fileinfo)) | |
8489 | kbytes = 0; | |
8490 | else | |
8491 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
8492 | ||
8493 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
8494 | ||
8495 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
8496 | IPP_TAG_INTEGER)) != NULL) | |
8497 | attr->values[0].integer += kbytes; | |
8498 | ||
8499 | /* | |
8500 | * Add the job file... | |
8501 | */ | |
8502 | ||
8503 | if (add_file(con, job, filetype, compression)) | |
8504 | return; | |
8505 | ||
8506 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
8507 | job->num_files); | |
8508 | rename(con->filename, filename); | |
8509 | cupsdClearString(&con->filename); | |
8510 | ||
8511 | /* | |
8512 | * See if we need to add the ending sheet... | |
8513 | */ | |
8514 | ||
8515 | if (cupsdTimeoutJob(job)) | |
8516 | return; | |
8517 | ||
8518 | /* | |
8519 | * Log and save the job... | |
8520 | */ | |
8521 | ||
8522 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8523 | "[Job %d] File of type %s/%s queued by \"%s\".", job->id, | |
8524 | filetype->super, filetype->type, job->username); | |
8525 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] hold_until=%d", job->id, | |
8526 | (int)job->hold_until); | |
8527 | ||
8528 | /* | |
8529 | * Start the job if possible... | |
8530 | */ | |
8531 | ||
8532 | cupsdCheckJobs(); | |
8533 | } | |
8534 | ||
8535 | ||
8536 | /* | |
8537 | * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file. | |
8538 | * | |
8539 | * This function only gets called when printing a single PostScript | |
8540 | * file using the Print-Job operation. It doesn't work for Create-Job + | |
8541 | * Send-File, since the job attributes need to be set at job creation | |
8542 | * time for banners to work. The embedded PS job ticket stuff is here | |
8543 | * only to allow the Windows printer driver for CUPS to pass in JCL | |
8544 | * options and IPP attributes which otherwise would be lost. | |
8545 | * | |
8546 | * The format of a PS job ticket is simple: | |
8547 | * | |
8548 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
8549 | * | |
8550 | * %cupsJobTicket: attr1=value1 | |
8551 | * %cupsJobTicket: attr2=value2 | |
8552 | * ... | |
8553 | * %cupsJobTicket: attrN=valueN | |
8554 | * | |
8555 | * Job ticket lines must appear immediately after the first line that | |
8556 | * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop | |
8557 | * looking for job ticket info when it finds a line that does not begin | |
8558 | * with "%cupsJobTicket:". | |
8559 | * | |
8560 | * The maximum length of a job ticket line, including the prefix, is | |
8561 | * 255 characters to conform with the Adobe DSC. | |
8562 | * | |
8563 | * Read-only attributes are rejected with a notice to the error log in | |
8564 | * case a malicious user tries anything. Since the job ticket is read | |
8565 | * prior to attribute validation in print_job(), job ticket attributes | |
8566 | * will go through the same validation as IPP attributes... | |
8567 | */ | |
8568 | ||
8569 | static void | |
8570 | read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */ | |
8571 | { | |
8572 | cups_file_t *fp; /* File to read from */ | |
8573 | char line[256]; /* Line data */ | |
8574 | int num_options; /* Number of options */ | |
8575 | cups_option_t *options; /* Options */ | |
8576 | ipp_t *ticket; /* New attributes */ | |
8577 | ipp_attribute_t *attr, /* Current attribute */ | |
8578 | *attr2, /* Job attribute */ | |
8579 | *prev2; /* Previous job attribute */ | |
8580 | ||
8581 | ||
8582 | /* | |
8583 | * First open the print file... | |
8584 | */ | |
8585 | ||
8586 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
8587 | { | |
8588 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8589 | "read_ps_job_ticket: Unable to open PostScript print file " | |
8590 | "- %s", | |
8591 | strerror(errno)); | |
8592 | return; | |
8593 | } | |
8594 | ||
8595 | /* | |
8596 | * Skip the first line... | |
8597 | */ | |
8598 | ||
8599 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
8600 | { | |
8601 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8602 | "read_ps_job_ticket: Unable to read from PostScript print " | |
8603 | "file - %s", | |
8604 | strerror(errno)); | |
8605 | cupsFileClose(fp); | |
8606 | return; | |
8607 | } | |
8608 | ||
8609 | if (strncmp(line, "%!PS-Adobe-", 11)) | |
8610 | { | |
8611 | /* | |
8612 | * Not a DSC-compliant file, so no job ticket info will be available... | |
8613 | */ | |
8614 | ||
8615 | cupsFileClose(fp); | |
8616 | return; | |
8617 | } | |
8618 | ||
8619 | /* | |
8620 | * Read job ticket info from the file... | |
8621 | */ | |
8622 | ||
8623 | num_options = 0; | |
8624 | options = NULL; | |
8625 | ||
8626 | while (cupsFileGets(fp, line, sizeof(line))) | |
8627 | { | |
8628 | /* | |
8629 | * Stop at the first non-ticket line... | |
8630 | */ | |
8631 | ||
8632 | if (strncmp(line, "%cupsJobTicket:", 15)) | |
8633 | break; | |
8634 | ||
8635 | /* | |
8636 | * Add the options to the option array... | |
8637 | */ | |
8638 | ||
8639 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
8640 | } | |
8641 | ||
8642 | /* | |
8643 | * Done with the file; see if we have any options... | |
8644 | */ | |
8645 | ||
8646 | cupsFileClose(fp); | |
8647 | ||
8648 | if (num_options == 0) | |
8649 | return; | |
8650 | ||
8651 | /* | |
8652 | * OK, convert the options to an attribute list, and apply them to | |
8653 | * the request... | |
8654 | */ | |
8655 | ||
8656 | ticket = ippNew(); | |
8657 | cupsEncodeOptions(ticket, num_options, options); | |
8658 | ||
8659 | /* | |
8660 | * See what the user wants to change. | |
8661 | */ | |
8662 | ||
8663 | for (attr = ticket->attrs; attr; attr = attr->next) | |
8664 | { | |
8665 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8666 | continue; | |
8667 | ||
8668 | if (!strcmp(attr->name, "job-originating-host-name") || | |
8669 | !strcmp(attr->name, "job-originating-user-name") || | |
8670 | !strcmp(attr->name, "job-media-sheets-completed") || | |
8671 | !strcmp(attr->name, "job-k-octets") || | |
8672 | !strcmp(attr->name, "job-id") || | |
8673 | !strncmp(attr->name, "job-state", 9) || | |
8674 | !strncmp(attr->name, "time-at-", 8)) | |
8675 | continue; /* Read-only attrs */ | |
8676 | ||
8677 | if ((attr2 = ippFindAttribute(con->request, attr->name, | |
8678 | IPP_TAG_ZERO)) != NULL) | |
8679 | { | |
8680 | /* | |
8681 | * Some other value; first free the old value... | |
8682 | */ | |
8683 | ||
8684 | if (con->request->attrs == attr2) | |
8685 | { | |
8686 | con->request->attrs = attr2->next; | |
8687 | prev2 = NULL; | |
8688 | } | |
8689 | else | |
8690 | { | |
8691 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) | |
8692 | if (prev2->next == attr2) | |
8693 | { | |
8694 | prev2->next = attr2->next; | |
8695 | break; | |
8696 | } | |
8697 | } | |
8698 | ||
8699 | if (con->request->last == attr2) | |
8700 | con->request->last = prev2; | |
8701 | ||
8702 | _ippFreeAttr(attr2); | |
8703 | } | |
8704 | ||
8705 | /* | |
8706 | * Add new option by copying it... | |
8707 | */ | |
8708 | ||
8709 | copy_attribute(con->request, attr, 0); | |
8710 | } | |
8711 | ||
8712 | /* | |
8713 | * Then free the attribute list and option array... | |
8714 | */ | |
8715 | ||
8716 | ippDelete(ticket); | |
8717 | cupsFreeOptions(num_options, options); | |
8718 | } | |
8719 | ||
8720 | ||
8721 | /* | |
8722 | * 'reject_jobs()' - Reject print jobs to a printer. | |
8723 | */ | |
8724 | ||
8725 | static void | |
8726 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
8727 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
8728 | { | |
8729 | http_status_t status; /* Policy status */ | |
8730 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
8731 | cupsd_printer_t *printer; /* Printer data */ | |
8732 | ipp_attribute_t *attr; /* printer-state-message text */ | |
8733 | ||
8734 | ||
8735 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
8736 | con->http.fd, uri->values[0].string.text); | |
8737 | ||
8738 | /* | |
8739 | * Is the destination valid? | |
8740 | */ | |
8741 | ||
8742 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
8743 | { | |
8744 | /* | |
8745 | * Bad URI... | |
8746 | */ | |
8747 | ||
8748 | send_ipp_status(con, IPP_NOT_FOUND, | |
8749 | _("The printer or class was not found.")); | |
8750 | return; | |
8751 | } | |
8752 | ||
8753 | /* | |
8754 | * Check policy... | |
8755 | */ | |
8756 | ||
8757 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8758 | { | |
8759 | send_http_error(con, status, printer); | |
8760 | return; | |
8761 | } | |
8762 | ||
8763 | /* | |
8764 | * Reject jobs sent to the printer... | |
8765 | */ | |
8766 | ||
8767 | printer->accepting = 0; | |
8768 | ||
8769 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8770 | IPP_TAG_TEXT)) == NULL) | |
8771 | strcpy(printer->state_message, "Rejecting Jobs"); | |
8772 | else | |
8773 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8774 | sizeof(printer->state_message)); | |
8775 | ||
8776 | cupsdAddPrinterHistory(printer); | |
8777 | ||
8778 | if (dtype & CUPS_PRINTER_CLASS) | |
8779 | { | |
8780 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); | |
8781 | ||
8782 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
8783 | printer->name, get_username(con)); | |
8784 | } | |
8785 | else | |
8786 | { | |
8787 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); | |
8788 | ||
8789 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
8790 | printer->name, get_username(con)); | |
8791 | } | |
8792 | ||
8793 | /* | |
8794 | * Everything was ok, so return OK status... | |
8795 | */ | |
8796 | ||
8797 | con->response->request.status.status_code = IPP_OK; | |
8798 | } | |
8799 | ||
8800 | ||
8801 | /* | |
8802 | * 'release_job()' - Release a held print job. | |
8803 | */ | |
8804 | ||
8805 | static void | |
8806 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
8807 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
8808 | { | |
8809 | ipp_attribute_t *attr; /* Current attribute */ | |
8810 | int jobid; /* Job ID */ | |
8811 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
8812 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
8813 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
8814 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
8815 | int port; /* Port portion of URI */ | |
8816 | cupsd_job_t *job; /* Job information */ | |
8817 | ||
8818 | ||
8819 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
8820 | con->http.fd, uri->values[0].string.text); | |
8821 | ||
8822 | /* | |
8823 | * See if we have a job URI or a printer URI... | |
8824 | */ | |
8825 | ||
8826 | if (!strcmp(uri->name, "printer-uri")) | |
8827 | { | |
8828 | /* | |
8829 | * Got a printer URI; see if we also have a job-id attribute... | |
8830 | */ | |
8831 | ||
8832 | if ((attr = ippFindAttribute(con->request, "job-id", | |
8833 | IPP_TAG_INTEGER)) == NULL) | |
8834 | { | |
8835 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8836 | _("Got a printer-uri attribute but no job-id!")); | |
8837 | return; | |
8838 | } | |
8839 | ||
8840 | jobid = attr->values[0].integer; | |
8841 | } | |
8842 | else | |
8843 | { | |
8844 | /* | |
8845 | * Got a job URI; parse it to get the job ID... | |
8846 | */ | |
8847 | ||
8848 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
8849 | sizeof(method), username, sizeof(username), host, | |
8850 | sizeof(host), &port, resource, sizeof(resource)); | |
8851 | ||
8852 | if (strncmp(resource, "/jobs/", 6)) | |
8853 | { | |
8854 | /* | |
8855 | * Not a valid URI! | |
8856 | */ | |
8857 | ||
8858 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8859 | _("Bad job-uri attribute \"%s\"!"), | |
8860 | uri->values[0].string.text); | |
8861 | return; | |
8862 | } | |
8863 | ||
8864 | jobid = atoi(resource + 6); | |
8865 | } | |
8866 | ||
8867 | /* | |
8868 | * See if the job exists... | |
8869 | */ | |
8870 | ||
8871 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8872 | { | |
8873 | /* | |
8874 | * Nope - return a "not found" error... | |
8875 | */ | |
8876 | ||
8877 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
8878 | return; | |
8879 | } | |
8880 | ||
8881 | /* | |
8882 | * See if job is "held"... | |
8883 | */ | |
8884 | ||
8885 | if (job->state_value != IPP_JOB_HELD) | |
8886 | { | |
8887 | /* | |
8888 | * Nope - return a "not possible" error... | |
8889 | */ | |
8890 | ||
8891 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid); | |
8892 | return; | |
8893 | } | |
8894 | ||
8895 | /* | |
8896 | * See if the job is owned by the requesting user... | |
8897 | */ | |
8898 | ||
8899 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8900 | { | |
8901 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
8902 | return; | |
8903 | } | |
8904 | ||
8905 | /* | |
8906 | * Reset the job-hold-until value to "no-hold"... | |
8907 | */ | |
8908 | ||
8909 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
8910 | IPP_TAG_KEYWORD)) == NULL) | |
8911 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
8912 | ||
8913 | if (attr) | |
8914 | { | |
8915 | _cupsStrFree(attr->values[0].string.text); | |
8916 | ||
8917 | attr->value_tag = IPP_TAG_KEYWORD; | |
8918 | attr->values[0].string.text = _cupsStrAlloc("no-hold"); | |
8919 | ||
8920 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, | |
8921 | "Job job-hold-until value changed by user."); | |
8922 | } | |
8923 | ||
8924 | /* | |
8925 | * Release the job and return... | |
8926 | */ | |
8927 | ||
8928 | cupsdReleaseJob(job); | |
8929 | ||
8930 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, | |
8931 | "Job released by user."); | |
8932 | ||
8933 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Released by \"%s\".", jobid, | |
8934 | username); | |
8935 | ||
8936 | con->response->request.status.status_code = IPP_OK; | |
8937 | } | |
8938 | ||
8939 | ||
8940 | /* | |
8941 | * 'renew_subscription()' - Renew an existing subscription... | |
8942 | */ | |
8943 | ||
8944 | static void | |
8945 | renew_subscription( | |
8946 | cupsd_client_t *con, /* I - Client connection */ | |
8947 | int sub_id) /* I - Subscription ID */ | |
8948 | { | |
8949 | http_status_t status; /* Policy status */ | |
8950 | cupsd_subscription_t *sub; /* Subscription */ | |
8951 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
8952 | ||
8953 | ||
8954 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
8955 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
8956 | con, con->http.fd, sub_id); | |
8957 | ||
8958 | /* | |
8959 | * Is the subscription ID valid? | |
8960 | */ | |
8961 | ||
8962 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
8963 | { | |
8964 | /* | |
8965 | * Bad subscription ID... | |
8966 | */ | |
8967 | ||
8968 | send_ipp_status(con, IPP_NOT_FOUND, | |
8969 | _("notify-subscription-id %d no good!"), sub_id); | |
8970 | return; | |
8971 | } | |
8972 | ||
8973 | if (sub->job) | |
8974 | { | |
8975 | /* | |
8976 | * Job subscriptions cannot be renewed... | |
8977 | */ | |
8978 | ||
8979 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8980 | _("Job subscriptions cannot be renewed!")); | |
8981 | return; | |
8982 | } | |
8983 | ||
8984 | /* | |
8985 | * Check policy... | |
8986 | */ | |
8987 | ||
8988 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
8989 | DefaultPolicyPtr, | |
8990 | con, sub->owner)) != HTTP_OK) | |
8991 | { | |
8992 | send_http_error(con, status, sub->dest); | |
8993 | return; | |
8994 | } | |
8995 | ||
8996 | /* | |
8997 | * Renew the subscription... | |
8998 | */ | |
8999 | ||
9000 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
9001 | IPP_TAG_INTEGER); | |
9002 | ||
9003 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
9004 | ||
9005 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
9006 | { | |
9007 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9008 | "renew_subscription: Limiting notify-lease-duration to " | |
9009 | "%d seconds.", | |
9010 | MaxLeaseDuration); | |
9011 | sub->lease = MaxLeaseDuration; | |
9012 | } | |
9013 | ||
9014 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
9015 | ||
9016 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); | |
9017 | ||
9018 | con->response->request.status.status_code = IPP_OK; | |
9019 | ||
9020 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
9021 | "notify-lease-duration", sub->lease); | |
9022 | } | |
9023 | ||
9024 | ||
9025 | /* | |
9026 | * 'restart_job()' - Restart an old print job. | |
9027 | */ | |
9028 | ||
9029 | static void | |
9030 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
9031 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
9032 | { | |
9033 | ipp_attribute_t *attr; /* Current attribute */ | |
9034 | int jobid; /* Job ID */ | |
9035 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
9036 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
9037 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
9038 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9039 | int port; /* Port portion of URI */ | |
9040 | cupsd_job_t *job; /* Job information */ | |
9041 | ||
9042 | ||
9043 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
9044 | con->http.fd, uri->values[0].string.text); | |
9045 | ||
9046 | /* | |
9047 | * See if we have a job URI or a printer URI... | |
9048 | */ | |
9049 | ||
9050 | if (!strcmp(uri->name, "printer-uri")) | |
9051 | { | |
9052 | /* | |
9053 | * Got a printer URI; see if we also have a job-id attribute... | |
9054 | */ | |
9055 | ||
9056 | if ((attr = ippFindAttribute(con->request, "job-id", | |
9057 | IPP_TAG_INTEGER)) == NULL) | |
9058 | { | |
9059 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9060 | _("Got a printer-uri attribute but no job-id!")); | |
9061 | return; | |
9062 | } | |
9063 | ||
9064 | jobid = attr->values[0].integer; | |
9065 | } | |
9066 | else | |
9067 | { | |
9068 | /* | |
9069 | * Got a job URI; parse it to get the job ID... | |
9070 | */ | |
9071 | ||
9072 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
9073 | sizeof(method), username, sizeof(username), host, | |
9074 | sizeof(host), &port, resource, sizeof(resource)); | |
9075 | ||
9076 | if (strncmp(resource, "/jobs/", 6)) | |
9077 | { | |
9078 | /* | |
9079 | * Not a valid URI! | |
9080 | */ | |
9081 | ||
9082 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9083 | _("Bad job-uri attribute \"%s\"!"), | |
9084 | uri->values[0].string.text); | |
9085 | return; | |
9086 | } | |
9087 | ||
9088 | jobid = atoi(resource + 6); | |
9089 | } | |
9090 | ||
9091 | /* | |
9092 | * See if the job exists... | |
9093 | */ | |
9094 | ||
9095 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9096 | { | |
9097 | /* | |
9098 | * Nope - return a "not found" error... | |
9099 | */ | |
9100 | ||
9101 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9102 | return; | |
9103 | } | |
9104 | ||
9105 | /* | |
9106 | * See if job is in any of the "completed" states... | |
9107 | */ | |
9108 | ||
9109 | if (job->state_value <= IPP_JOB_PROCESSING) | |
9110 | { | |
9111 | /* | |
9112 | * Nope - return a "not possible" error... | |
9113 | */ | |
9114 | ||
9115 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"), | |
9116 | jobid); | |
9117 | return; | |
9118 | } | |
9119 | ||
9120 | /* | |
9121 | * See if we have retained the job files... | |
9122 | */ | |
9123 | ||
9124 | cupsdLoadJob(job); | |
9125 | ||
9126 | if (!job->attrs || job->num_files == 0) | |
9127 | { | |
9128 | /* | |
9129 | * Nope - return a "not possible" error... | |
9130 | */ | |
9131 | ||
9132 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
9133 | _("Job #%d cannot be restarted - no files!"), jobid); | |
9134 | return; | |
9135 | } | |
9136 | ||
9137 | /* | |
9138 | * See if the job is owned by the requesting user... | |
9139 | */ | |
9140 | ||
9141 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9142 | { | |
9143 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
9144 | return; | |
9145 | } | |
9146 | ||
9147 | /* | |
9148 | * Restart the job and return... | |
9149 | */ | |
9150 | ||
9151 | cupsdRestartJob(job); | |
9152 | ||
9153 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Restarted by \"%s\".", jobid, | |
9154 | username); | |
9155 | ||
9156 | con->response->request.status.status_code = IPP_OK; | |
9157 | } | |
9158 | ||
9159 | ||
9160 | /* | |
9161 | * 'save_auth_info()' - Save authentication information for a job. | |
9162 | */ | |
9163 | ||
9164 | static void | |
9165 | save_auth_info( | |
9166 | cupsd_client_t *con, /* I - Client connection */ | |
9167 | cupsd_job_t *job, /* I - Job */ | |
9168 | ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */ | |
9169 | { | |
9170 | int i; /* Looping var */ | |
9171 | char filename[1024]; /* Job authentication filename */ | |
9172 | cups_file_t *fp; /* Job authentication file */ | |
9173 | char line[2048]; /* Line for file */ | |
9174 | cupsd_printer_t *dest; /* Destination printer/class */ | |
9175 | ||
9176 | ||
9177 | /* | |
9178 | * This function saves the in-memory authentication information for | |
9179 | * a job so that it can be used to authenticate with a remote host. | |
9180 | * The information is stored in a file that is readable only by the | |
9181 | * root user. The fields are Base-64 encoded, each on a separate line, | |
9182 | * followed by random number (up to 1024) of newlines to limit the | |
9183 | * amount of information that is exposed. | |
9184 | * | |
9185 | * Because of the potential for exposing of authentication information, | |
9186 | * this functionality is only enabled when running cupsd as root. | |
9187 | * | |
9188 | * This caching only works for the Basic and BasicDigest authentication | |
9189 | * types. Digest authentication cannot be cached this way, and in | |
9190 | * the future Kerberos authentication may make all of this obsolete. | |
9191 | * | |
9192 | * Authentication information is saved whenever an authenticated | |
9193 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
9194 | * performed. | |
9195 | * | |
9196 | * This information is deleted after a job is completed or canceled, | |
9197 | * so reprints may require subsequent re-authentication. | |
9198 | */ | |
9199 | ||
9200 | if (RunUser) | |
9201 | return; | |
9202 | ||
9203 | if ((dest = cupsdFindDest(job->dest)) == NULL) | |
9204 | return; | |
9205 | ||
9206 | /* | |
9207 | * Create the authentication file and change permissions... | |
9208 | */ | |
9209 | ||
9210 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
9211 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
9212 | { | |
9213 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9214 | "Unable to save authentication info to \"%s\" - %s", | |
9215 | filename, strerror(errno)); | |
9216 | return; | |
9217 | } | |
9218 | ||
9219 | fchown(cupsFileNumber(fp), 0, 0); | |
9220 | fchmod(cupsFileNumber(fp), 0400); | |
9221 | ||
9222 | if (auth_info && auth_info->num_values == dest->num_auth_info_required) | |
9223 | { | |
9224 | /* | |
9225 | * Write 1 to 3 auth values... | |
9226 | */ | |
9227 | ||
9228 | cupsdClearString(&job->auth_username); | |
9229 | cupsdClearString(&job->auth_domain); | |
9230 | cupsdClearString(&job->auth_password); | |
9231 | ||
9232 | for (i = 0; i < auth_info->num_values; i ++) | |
9233 | { | |
9234 | httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, | |
9235 | strlen(auth_info->values[i].string.text)); | |
9236 | cupsFilePrintf(fp, "%s\n", line); | |
9237 | ||
9238 | if (!strcmp(dest->auth_info_required[i], "username")) | |
9239 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", | |
9240 | auth_info->values[i].string.text); | |
9241 | else if (!strcmp(dest->auth_info_required[i], "domain")) | |
9242 | cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", | |
9243 | auth_info->values[i].string.text); | |
9244 | else if (!strcmp(dest->auth_info_required[i], "password")) | |
9245 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", | |
9246 | auth_info->values[i].string.text); | |
9247 | } | |
9248 | } | |
9249 | else if (con->username[0]) | |
9250 | { | |
9251 | /* | |
9252 | * Write the authenticated username... | |
9253 | */ | |
9254 | ||
9255 | httpEncode64_2(line, sizeof(line), con->username, strlen(con->username)); | |
9256 | cupsFilePrintf(fp, "%s\n", line); | |
9257 | ||
9258 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username); | |
9259 | cupsdClearString(&job->auth_domain); | |
9260 | ||
9261 | /* | |
9262 | * Write the authenticated password... | |
9263 | */ | |
9264 | ||
9265 | httpEncode64_2(line, sizeof(line), con->password, strlen(con->password)); | |
9266 | cupsFilePrintf(fp, "%s\n", line); | |
9267 | ||
9268 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password); | |
9269 | } | |
9270 | ||
9271 | /* | |
9272 | * Write a random number of newlines to the end of the file... | |
9273 | */ | |
9274 | ||
9275 | for (i = (rand() % 1024); i >= 0; i --) | |
9276 | cupsFilePutChar(fp, '\n'); | |
9277 | ||
9278 | /* | |
9279 | * Close the file and return... | |
9280 | */ | |
9281 | ||
9282 | cupsFileClose(fp); | |
9283 | ||
9284 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
9285 | if (con->gss_have_creds) | |
9286 | save_krb5_creds(con, job); | |
9287 | else if (job->ccname) | |
9288 | cupsdClearString(&(job->ccname)); | |
9289 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ | |
9290 | } | |
9291 | ||
9292 | ||
9293 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
9294 | /* | |
9295 | * 'save_krb5_creds()' - Save Kerberos credentials for the job. | |
9296 | */ | |
9297 | ||
9298 | static void | |
9299 | save_krb5_creds(cupsd_client_t *con, /* I - Client connection */ | |
9300 | cupsd_job_t *job) /* I - Job */ | |
9301 | { | |
9302 | # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL) | |
9303 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9304 | "Sorry, your version of Kerberos does not support delegated " | |
9305 | "credentials!"); | |
9306 | return; | |
9307 | ||
9308 | # else | |
9309 | krb5_error_code error; /* Kerberos error code */ | |
9310 | OM_uint32 major_status, /* Major status code */ | |
9311 | minor_status; /* Minor status code */ | |
9312 | krb5_principal principal; /* Kerberos principal */ | |
9313 | ||
9314 | ||
9315 | # ifdef __APPLE__ | |
9316 | /* | |
9317 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
9318 | * to use it... | |
9319 | */ | |
9320 | ||
9321 | if (krb5_init_context == NULL) | |
9322 | return; | |
9323 | # endif /* __APPLE__ */ | |
9324 | ||
9325 | /* | |
9326 | * We MUST create a file-based cache because memory-based caches are | |
9327 | * only valid for the current process/address space. | |
9328 | * | |
9329 | * Due to various bugs/features in different versions of Kerberos, we | |
9330 | * need either the krb5_cc_new_unique() function or Heimdal's version | |
9331 | * of krb5_cc_gen_new() to create a new FILE: credential cache that | |
9332 | * can be passed to the backend. These functions create a temporary | |
9333 | * file (typically in /tmp) containing the cached credentials, which | |
9334 | * are removed when we have successfully printed a job. | |
9335 | */ | |
9336 | ||
9337 | # ifdef HAVE_KRB5_CC_NEW_UNIQUE | |
9338 | if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL, | |
9339 | &(job->ccache))) != 0) | |
9340 | # else /* HAVE_HEIMDAL */ | |
9341 | if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops, | |
9342 | &(job->ccache))) != 0) | |
9343 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE */ | |
9344 | { | |
9345 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9346 | "Unable to create new credentials cache (%d/%s)", | |
9347 | error, strerror(errno)); | |
9348 | job->ccache = NULL; | |
9349 | return; | |
9350 | } | |
9351 | ||
9352 | if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0) | |
9353 | { | |
9354 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)", | |
9355 | error, strerror(errno)); | |
9356 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9357 | job->ccache = NULL; | |
9358 | return; | |
9359 | } | |
9360 | ||
9361 | if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal))) | |
9362 | { | |
9363 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9364 | "Unable to initialize credentials cache (%d/%s)", error, | |
9365 | strerror(errno)); | |
9366 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9367 | krb5_free_principal(KerberosContext, principal); | |
9368 | job->ccache = NULL; | |
9369 | return; | |
9370 | } | |
9371 | ||
9372 | krb5_free_principal(KerberosContext, principal); | |
9373 | ||
9374 | /* | |
9375 | * Copy the user's credentials to the new cache file... | |
9376 | */ | |
9377 | ||
9378 | major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred, | |
9379 | job->ccache); | |
9380 | ||
9381 | if (GSS_ERROR(major_status)) | |
9382 | { | |
9383 | cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status, | |
9384 | "Unable to import client credentials cache"); | |
9385 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9386 | job->ccache = NULL; | |
9387 | return; | |
9388 | } | |
9389 | ||
9390 | /* | |
9391 | * Add the KRB5CCNAME environment variable to the job so that the | |
9392 | * backend can use the credentials when printing. | |
9393 | */ | |
9394 | ||
9395 | cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s", | |
9396 | krb5_cc_get_name(KerberosContext, job->ccache)); | |
9397 | ||
9398 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] save_krb5_creds: %s", job->id, | |
9399 | job->ccname); | |
9400 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */ | |
9401 | } | |
9402 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ | |
9403 | ||
9404 | ||
9405 | /* | |
9406 | * 'send_document()' - Send a file to a printer or class. | |
9407 | */ | |
9408 | ||
9409 | static void | |
9410 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
9411 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9412 | { | |
9413 | ipp_attribute_t *attr; /* Current attribute */ | |
9414 | ipp_attribute_t *format; /* Request's document-format attribute */ | |
9415 | ipp_attribute_t *jformat; /* Job's document-format attribute */ | |
9416 | const char *default_format;/* document-format-default value */ | |
9417 | int jobid; /* Job ID number */ | |
9418 | cupsd_job_t *job; /* Current job */ | |
9419 | char job_uri[HTTP_MAX_URI], | |
9420 | /* Job URI */ | |
9421 | method[HTTP_MAX_URI], | |
9422 | /* Method portion of URI */ | |
9423 | username[HTTP_MAX_URI], | |
9424 | /* Username portion of URI */ | |
9425 | host[HTTP_MAX_URI], | |
9426 | /* Host portion of URI */ | |
9427 | resource[HTTP_MAX_URI]; | |
9428 | /* Resource portion of URI */ | |
9429 | int port; /* Port portion of URI */ | |
9430 | mime_type_t *filetype; /* Type of file */ | |
9431 | char super[MIME_MAX_SUPER], | |
9432 | /* Supertype of file */ | |
9433 | type[MIME_MAX_TYPE], | |
9434 | /* Subtype of file */ | |
9435 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
9436 | /* Textual name of mime type */ | |
9437 | char filename[1024]; /* Job filename */ | |
9438 | cupsd_printer_t *printer; /* Current printer */ | |
9439 | struct stat fileinfo; /* File information */ | |
9440 | int kbytes; /* Size of file */ | |
9441 | int compression; /* Type of compression */ | |
9442 | ||
9443 | ||
9444 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
9445 | con->http.fd, uri->values[0].string.text); | |
9446 | ||
9447 | /* | |
9448 | * See if we have a job URI or a printer URI... | |
9449 | */ | |
9450 | ||
9451 | if (!strcmp(uri->name, "printer-uri")) | |
9452 | { | |
9453 | /* | |
9454 | * Got a printer URI; see if we also have a job-id attribute... | |
9455 | */ | |
9456 | ||
9457 | if ((attr = ippFindAttribute(con->request, "job-id", | |
9458 | IPP_TAG_INTEGER)) == NULL) | |
9459 | { | |
9460 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9461 | _("Got a printer-uri attribute but no job-id!")); | |
9462 | return; | |
9463 | } | |
9464 | ||
9465 | jobid = attr->values[0].integer; | |
9466 | } | |
9467 | else | |
9468 | { | |
9469 | /* | |
9470 | * Got a job URI; parse it to get the job ID... | |
9471 | */ | |
9472 | ||
9473 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
9474 | sizeof(method), username, sizeof(username), host, | |
9475 | sizeof(host), &port, resource, sizeof(resource)); | |
9476 | ||
9477 | if (strncmp(resource, "/jobs/", 6)) | |
9478 | { | |
9479 | /* | |
9480 | * Not a valid URI! | |
9481 | */ | |
9482 | ||
9483 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9484 | _("Bad job-uri attribute \"%s\"!"), | |
9485 | uri->values[0].string.text); | |
9486 | return; | |
9487 | } | |
9488 | ||
9489 | jobid = atoi(resource + 6); | |
9490 | } | |
9491 | ||
9492 | /* | |
9493 | * See if the job exists... | |
9494 | */ | |
9495 | ||
9496 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9497 | { | |
9498 | /* | |
9499 | * Nope - return a "not found" error... | |
9500 | */ | |
9501 | ||
9502 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9503 | return; | |
9504 | } | |
9505 | ||
9506 | printer = cupsdFindDest(job->dest); | |
9507 | ||
9508 | /* | |
9509 | * See if the job is owned by the requesting user... | |
9510 | */ | |
9511 | ||
9512 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9513 | { | |
9514 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
9515 | return; | |
9516 | } | |
9517 | ||
9518 | /* | |
9519 | * OK, see if the client is sending the document compressed - CUPS | |
9520 | * only supports "none" and "gzip". | |
9521 | */ | |
9522 | ||
9523 | compression = CUPS_FILE_NONE; | |
9524 | ||
9525 | if ((attr = ippFindAttribute(con->request, "compression", | |
9526 | IPP_TAG_KEYWORD)) != NULL) | |
9527 | { | |
9528 | if (strcmp(attr->values[0].string.text, "none") | |
9529 | #ifdef HAVE_LIBZ | |
9530 | && strcmp(attr->values[0].string.text, "gzip") | |
9531 | #endif /* HAVE_LIBZ */ | |
9532 | ) | |
9533 | { | |
9534 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"), | |
9535 | attr->values[0].string.text); | |
9536 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9537 | "compression", NULL, attr->values[0].string.text); | |
9538 | return; | |
9539 | } | |
9540 | ||
9541 | #ifdef HAVE_LIBZ | |
9542 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
9543 | compression = CUPS_FILE_GZIP; | |
9544 | #endif /* HAVE_LIBZ */ | |
9545 | } | |
9546 | ||
9547 | /* | |
9548 | * Do we have a file to print? | |
9549 | */ | |
9550 | ||
9551 | if (!con->filename) | |
9552 | { | |
9553 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
9554 | return; | |
9555 | } | |
9556 | ||
9557 | /* | |
9558 | * Is it a format we support? | |
9559 | */ | |
9560 | ||
9561 | if ((format = ippFindAttribute(con->request, "document-format", | |
9562 | IPP_TAG_MIMETYPE)) != NULL) | |
9563 | { | |
9564 | /* | |
9565 | * Grab format from client... | |
9566 | */ | |
9567 | ||
9568 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", | |
9569 | super, type) != 2) | |
9570 | { | |
9571 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
9572 | format->values[0].string.text); | |
9573 | return; | |
9574 | } | |
9575 | } | |
9576 | else if ((default_format = cupsGetOption("document-format", | |
9577 | printer->num_options, | |
9578 | printer->options)) != NULL) | |
9579 | { | |
9580 | /* | |
9581 | * Use default document format... | |
9582 | */ | |
9583 | ||
9584 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
9585 | { | |
9586 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9587 | _("Could not scan type \"%s\"!"), | |
9588 | default_format); | |
9589 | return; | |
9590 | } | |
9591 | } | |
9592 | else | |
9593 | { | |
9594 | /* | |
9595 | * No document format attribute? Auto-type it! | |
9596 | */ | |
9597 | ||
9598 | strcpy(super, "application"); | |
9599 | strcpy(type, "octet-stream"); | |
9600 | } | |
9601 | ||
9602 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) | |
9603 | { | |
9604 | /* | |
9605 | * Auto-type the file... | |
9606 | */ | |
9607 | ||
9608 | ipp_attribute_t *doc_name; /* document-name attribute */ | |
9609 | ||
9610 | ||
9611 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Auto-typing file...", job->id); | |
9612 | ||
9613 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
9614 | filetype = mimeFileType(MimeDatabase, con->filename, | |
9615 | doc_name ? doc_name->values[0].string.text : NULL, | |
9616 | &compression); | |
9617 | ||
9618 | if (!filetype) | |
9619 | filetype = mimeType(MimeDatabase, super, type); | |
9620 | ||
9621 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
9622 | "[Job %d] Request file type is %s/%s.", job->id, | |
9623 | filetype->super, filetype->type); | |
9624 | } | |
9625 | else | |
9626 | filetype = mimeType(MimeDatabase, super, type); | |
9627 | ||
9628 | jformat = ippFindAttribute(job->attrs, "document-format", IPP_TAG_MIMETYPE); | |
9629 | ||
9630 | if (filetype && | |
9631 | (!jformat || | |
9632 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) | |
9633 | { | |
9634 | /* | |
9635 | * Replace the document-format attribute value with the auto-typed or | |
9636 | * default one. | |
9637 | */ | |
9638 | ||
9639 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9640 | filetype->type); | |
9641 | ||
9642 | if (jformat) | |
9643 | { | |
9644 | _cupsStrFree(jformat->values[0].string.text); | |
9645 | ||
9646 | jformat->values[0].string.text = _cupsStrAlloc(mimetype); | |
9647 | } | |
9648 | else | |
9649 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
9650 | "document-format", NULL, mimetype); | |
9651 | } | |
9652 | else if (!filetype) | |
9653 | { | |
9654 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9655 | _("Unsupported format \'%s/%s\'!"), super, type); | |
9656 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9657 | "Hint: Do you have the raw file printing rules enabled?"); | |
9658 | ||
9659 | if (format) | |
9660 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9661 | "document-format", NULL, format->values[0].string.text); | |
9662 | ||
9663 | return; | |
9664 | } | |
9665 | ||
9666 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) | |
9667 | { | |
9668 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9669 | filetype->type); | |
9670 | ||
9671 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9672 | _("Unsupported format \'%s\'!"), mimetype); | |
9673 | ||
9674 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9675 | "document-format", NULL, mimetype); | |
9676 | ||
9677 | return; | |
9678 | } | |
9679 | ||
9680 | /* | |
9681 | * Add the file to the job... | |
9682 | */ | |
9683 | ||
9684 | cupsdLoadJob(job); | |
9685 | ||
9686 | if (add_file(con, job, filetype, compression)) | |
9687 | return; | |
9688 | ||
9689 | if (stat(con->filename, &fileinfo)) | |
9690 | kbytes = 0; | |
9691 | else | |
9692 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
9693 | ||
9694 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
9695 | ||
9696 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
9697 | IPP_TAG_INTEGER)) != NULL) | |
9698 | attr->values[0].integer += kbytes; | |
9699 | ||
9700 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
9701 | job->num_files); | |
9702 | rename(con->filename, filename); | |
9703 | ||
9704 | cupsdClearString(&con->filename); | |
9705 | ||
9706 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9707 | "[Job %d] File of type %s/%s queued by \"%s\".", job->id, | |
9708 | filetype->super, filetype->type, job->username); | |
9709 | ||
9710 | /* | |
9711 | * Start the job if this is the last document... | |
9712 | */ | |
9713 | ||
9714 | if ((attr = ippFindAttribute(con->request, "last-document", | |
9715 | IPP_TAG_BOOLEAN)) != NULL && | |
9716 | attr->values[0].boolean) | |
9717 | { | |
9718 | /* | |
9719 | * See if we need to add the ending sheet... | |
9720 | */ | |
9721 | ||
9722 | if (cupsdTimeoutJob(job)) | |
9723 | return; | |
9724 | ||
9725 | if (job->state_value == IPP_JOB_STOPPED) | |
9726 | { | |
9727 | job->state->values[0].integer = IPP_JOB_PENDING; | |
9728 | job->state_value = IPP_JOB_PENDING; | |
9729 | } | |
9730 | else if (job->state_value == IPP_JOB_HELD) | |
9731 | { | |
9732 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
9733 | IPP_TAG_KEYWORD)) == NULL) | |
9734 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
9735 | ||
9736 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) | |
9737 | { | |
9738 | job->state->values[0].integer = IPP_JOB_PENDING; | |
9739 | job->state_value = IPP_JOB_PENDING; | |
9740 | } | |
9741 | } | |
9742 | ||
9743 | job->dirty = 1; | |
9744 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
9745 | ||
9746 | /* | |
9747 | * Start the job if possible... Since cupsdCheckJobs() can cancel a | |
9748 | * job if it doesn't print, we need to re-find the job afterwards... | |
9749 | */ | |
9750 | ||
9751 | jobid = job->id; | |
9752 | ||
9753 | cupsdCheckJobs(); | |
9754 | ||
9755 | job = cupsdFindJob(jobid); | |
9756 | } | |
9757 | else | |
9758 | { | |
9759 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
9760 | IPP_TAG_KEYWORD)) == NULL) | |
9761 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
9762 | ||
9763 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) | |
9764 | { | |
9765 | job->state->values[0].integer = IPP_JOB_HELD; | |
9766 | job->state_value = IPP_JOB_HELD; | |
9767 | job->hold_until = time(NULL) + 60; | |
9768 | job->dirty = 1; | |
9769 | ||
9770 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
9771 | } | |
9772 | } | |
9773 | ||
9774 | /* | |
9775 | * Fill in the response info... | |
9776 | */ | |
9777 | ||
9778 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
9779 | LocalPort, jobid); | |
9780 | ||
9781 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
9782 | job_uri); | |
9783 | ||
9784 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
9785 | ||
9786 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
9787 | job ? job->state_value : IPP_JOB_CANCELED); | |
9788 | add_job_state_reasons(con, job); | |
9789 | ||
9790 | con->response->request.status.status_code = IPP_OK; | |
9791 | } | |
9792 | ||
9793 | ||
9794 | /* | |
9795 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
9796 | */ | |
9797 | ||
9798 | static void | |
9799 | send_http_error( | |
9800 | cupsd_client_t *con, /* I - Client connection */ | |
9801 | http_status_t status, /* I - HTTP status code */ | |
9802 | cupsd_printer_t *printer) /* I - Printer, if any */ | |
9803 | { | |
9804 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s", | |
9805 | ippOpString(con->request->request.op.operation_id), | |
9806 | httpStatus(status)); | |
9807 | ||
9808 | if (status == HTTP_UNAUTHORIZED && | |
9809 | printer && printer->num_auth_info_required > 0 && | |
9810 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
9811 | cupsdSendError(con, status, CUPSD_AUTH_NEGOTIATE); | |
9812 | else if (printer) | |
9813 | { | |
9814 | char resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9815 | cupsd_location_t *auth; /* Pointer to authentication element */ | |
9816 | int auth_type; /* Type of authentication required */ | |
9817 | ||
9818 | ||
9819 | if (printer->type & CUPS_PRINTER_CLASS) | |
9820 | snprintf(resource, sizeof(resource), "/classes/%s", printer->name); | |
9821 | else | |
9822 | snprintf(resource, sizeof(resource), "/printers/%s", printer->name); | |
9823 | ||
9824 | if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL || | |
9825 | auth->type == CUPSD_AUTH_NONE) | |
9826 | auth = cupsdFindPolicyOp(printer->op_policy_ptr, | |
9827 | con->request ? | |
9828 | con->request->request.op.operation_id : | |
9829 | IPP_PRINT_JOB); | |
9830 | ||
9831 | if (!auth) | |
9832 | auth_type = CUPSD_AUTH_NONE; | |
9833 | else if (auth->type == CUPSD_AUTH_DEFAULT) | |
9834 | auth_type = DefaultAuthType; | |
9835 | else | |
9836 | auth_type = auth->type; | |
9837 | ||
9838 | cupsdSendError(con, status, auth_type); | |
9839 | } | |
9840 | else | |
9841 | cupsdSendError(con, status, CUPSD_AUTH_NONE); | |
9842 | ||
9843 | ippDelete(con->response); | |
9844 | con->response = NULL; | |
9845 | ||
9846 | return; | |
9847 | } | |
9848 | ||
9849 | ||
9850 | /* | |
9851 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
9852 | */ | |
9853 | ||
9854 | static void | |
9855 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
9856 | ipp_status_t status, /* I - IPP status code */ | |
9857 | const char *message, /* I - Status message */ | |
9858 | ...) /* I - Additional args as needed */ | |
9859 | { | |
9860 | va_list ap; /* Pointer to additional args */ | |
9861 | char formatted[1024]; /* Formatted errror message */ | |
9862 | ||
9863 | ||
9864 | va_start(ap, message); | |
9865 | vsnprintf(formatted, sizeof(formatted), | |
9866 | _cupsLangString(con->language, message), ap); | |
9867 | va_end(ap); | |
9868 | ||
9869 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s", | |
9870 | ippOpString(con->request->request.op.operation_id), | |
9871 | ippErrorString(status), formatted); | |
9872 | ||
9873 | con->response->request.status.status_code = status; | |
9874 | ||
9875 | if (ippFindAttribute(con->response, "attributes-charset", | |
9876 | IPP_TAG_ZERO) == NULL) | |
9877 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
9878 | "attributes-charset", NULL, DefaultCharset); | |
9879 | ||
9880 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
9881 | IPP_TAG_ZERO) == NULL) | |
9882 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
9883 | "attributes-natural-language", NULL, DefaultLanguage); | |
9884 | ||
9885 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
9886 | "status-message", NULL, formatted); | |
9887 | } | |
9888 | ||
9889 | ||
9890 | /* | |
9891 | * 'set_default()' - Set the default destination... | |
9892 | */ | |
9893 | ||
9894 | static void | |
9895 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
9896 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9897 | { | |
9898 | http_status_t status; /* Policy status */ | |
9899 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
9900 | cupsd_printer_t *printer, /* Printer */ | |
9901 | *oldprinter; /* Old default printer */ | |
9902 | ||
9903 | ||
9904 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
9905 | con->http.fd, uri->values[0].string.text); | |
9906 | ||
9907 | /* | |
9908 | * Is the destination valid? | |
9909 | */ | |
9910 | ||
9911 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
9912 | { | |
9913 | /* | |
9914 | * Bad URI... | |
9915 | */ | |
9916 | ||
9917 | send_ipp_status(con, IPP_NOT_FOUND, | |
9918 | _("The printer or class was not found.")); | |
9919 | return; | |
9920 | } | |
9921 | ||
9922 | /* | |
9923 | * Check policy... | |
9924 | */ | |
9925 | ||
9926 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
9927 | { | |
9928 | send_http_error(con, status, NULL); | |
9929 | return; | |
9930 | } | |
9931 | ||
9932 | /* | |
9933 | * Set it as the default... | |
9934 | */ | |
9935 | ||
9936 | oldprinter = DefaultPrinter; | |
9937 | DefaultPrinter = printer; | |
9938 | ||
9939 | if (oldprinter) | |
9940 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL, | |
9941 | "%s is no longer the default printer.", oldprinter->name); | |
9942 | ||
9943 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, | |
9944 | "%s is now the default printer.", printer->name); | |
9945 | ||
9946 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES | | |
9947 | CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP); | |
9948 | ||
9949 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9950 | "Default destination set to \"%s\" by \"%s\".", | |
9951 | printer->name, get_username(con)); | |
9952 | ||
9953 | /* | |
9954 | * Everything was ok, so return OK status... | |
9955 | */ | |
9956 | ||
9957 | con->response->request.status.status_code = IPP_OK; | |
9958 | } | |
9959 | ||
9960 | ||
9961 | /* | |
9962 | * 'set_job_attrs()' - Set job attributes. | |
9963 | */ | |
9964 | ||
9965 | static void | |
9966 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
9967 | ipp_attribute_t *uri) /* I - Job URI */ | |
9968 | { | |
9969 | ipp_attribute_t *attr, /* Current attribute */ | |
9970 | *attr2; /* Job attribute */ | |
9971 | int jobid; /* Job ID */ | |
9972 | cupsd_job_t *job; /* Current job */ | |
9973 | char method[HTTP_MAX_URI], | |
9974 | /* Method portion of URI */ | |
9975 | username[HTTP_MAX_URI], | |
9976 | /* Username portion of URI */ | |
9977 | host[HTTP_MAX_URI], | |
9978 | /* Host portion of URI */ | |
9979 | resource[HTTP_MAX_URI]; | |
9980 | /* Resource portion of URI */ | |
9981 | int port; /* Port portion of URI */ | |
9982 | int event; /* Events? */ | |
9983 | ||
9984 | ||
9985 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
9986 | con->http.fd, uri->values[0].string.text); | |
9987 | ||
9988 | /* | |
9989 | * Start with "everything is OK" status... | |
9990 | */ | |
9991 | ||
9992 | con->response->request.status.status_code = IPP_OK; | |
9993 | ||
9994 | /* | |
9995 | * See if we have a job URI or a printer URI... | |
9996 | */ | |
9997 | ||
9998 | if (!strcmp(uri->name, "printer-uri")) | |
9999 | { | |
10000 | /* | |
10001 | * Got a printer URI; see if we also have a job-id attribute... | |
10002 | */ | |
10003 | ||
10004 | if ((attr = ippFindAttribute(con->request, "job-id", | |
10005 | IPP_TAG_INTEGER)) == NULL) | |
10006 | { | |
10007 | send_ipp_status(con, IPP_BAD_REQUEST, | |
10008 | _("Got a printer-uri attribute but no job-id!")); | |
10009 | return; | |
10010 | } | |
10011 | ||
10012 | jobid = attr->values[0].integer; | |
10013 | } | |
10014 | else | |
10015 | { | |
10016 | /* | |
10017 | * Got a job URI; parse it to get the job ID... | |
10018 | */ | |
10019 | ||
10020 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
10021 | sizeof(method), username, sizeof(username), host, | |
10022 | sizeof(host), &port, resource, sizeof(resource)); | |
10023 | ||
10024 | if (strncmp(resource, "/jobs/", 6)) | |
10025 | { | |
10026 | /* | |
10027 | * Not a valid URI! | |
10028 | */ | |
10029 | ||
10030 | send_ipp_status(con, IPP_BAD_REQUEST, | |
10031 | _("Bad job-uri attribute \"%s\"!"), | |
10032 | uri->values[0].string.text); | |
10033 | return; | |
10034 | } | |
10035 | ||
10036 | jobid = atoi(resource + 6); | |
10037 | } | |
10038 | ||
10039 | /* | |
10040 | * See if the job exists... | |
10041 | */ | |
10042 | ||
10043 | if ((job = cupsdFindJob(jobid)) == NULL) | |
10044 | { | |
10045 | /* | |
10046 | * Nope - return a "not found" error... | |
10047 | */ | |
10048 | ||
10049 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
10050 | return; | |
10051 | } | |
10052 | ||
10053 | /* | |
10054 | * See if the job has been completed... | |
10055 | */ | |
10056 | ||
10057 | if (job->state_value > IPP_JOB_STOPPED) | |
10058 | { | |
10059 | /* | |
10060 | * Return a "not-possible" error... | |
10061 | */ | |
10062 | ||
10063 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10064 | _("Job #%d is finished and cannot be altered!"), jobid); | |
10065 | return; | |
10066 | } | |
10067 | ||
10068 | /* | |
10069 | * See if the job is owned by the requesting user... | |
10070 | */ | |
10071 | ||
10072 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
10073 | { | |
10074 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); | |
10075 | return; | |
10076 | } | |
10077 | ||
10078 | /* | |
10079 | * See what the user wants to change. | |
10080 | */ | |
10081 | ||
10082 | cupsdLoadJob(job); | |
10083 | ||
10084 | event = 0; | |
10085 | ||
10086 | for (attr = con->request->attrs; attr; attr = attr->next) | |
10087 | { | |
10088 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
10089 | continue; | |
10090 | ||
10091 | if (!strcmp(attr->name, "attributes-charset") || | |
10092 | !strcmp(attr->name, "attributes-natural-language") || | |
10093 | !strcmp(attr->name, "document-compression") || | |
10094 | !strcmp(attr->name, "document-format") || | |
10095 | !strcmp(attr->name, "job-detailed-status-messages") || | |
10096 | !strcmp(attr->name, "job-document-access-errors") || | |
10097 | !strcmp(attr->name, "job-id") || | |
10098 | !strcmp(attr->name, "job-k-octets") || | |
10099 | !strcmp(attr->name, "job-originating-host-name") || | |
10100 | !strcmp(attr->name, "job-originating-user-name") || | |
10101 | !strcmp(attr->name, "job-printer-up-time") || | |
10102 | !strcmp(attr->name, "job-printer-uri") || | |
10103 | !strcmp(attr->name, "job-sheets") || | |
10104 | !strcmp(attr->name, "job-state-message") || | |
10105 | !strcmp(attr->name, "job-state-reasons") || | |
10106 | !strcmp(attr->name, "job-uri") || | |
10107 | !strcmp(attr->name, "number-of-documents") || | |
10108 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
10109 | !strcmp(attr->name, "output-device-assigned") || | |
10110 | !strncmp(attr->name, "date-time-at-", 13) || | |
10111 | !strncmp(attr->name, "job-impressions", 15) || | |
10112 | !strncmp(attr->name, "job-k-octets", 12) || | |
10113 | !strncmp(attr->name, "job-media-sheets", 16) || | |
10114 | !strncmp(attr->name, "time-at-", 8)) | |
10115 | { | |
10116 | /* | |
10117 | * Read-only attrs! | |
10118 | */ | |
10119 | ||
10120 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
10121 | _("%s cannot be changed."), attr->name); | |
10122 | ||
10123 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10124 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10125 | ||
10126 | continue; | |
10127 | } | |
10128 | ||
10129 | if (!strcmp(attr->name, "job-priority")) | |
10130 | { | |
10131 | /* | |
10132 | * Change the job priority... | |
10133 | */ | |
10134 | ||
10135 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10136 | { | |
10137 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!")); | |
10138 | ||
10139 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10140 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10141 | } | |
10142 | else if (job->state_value >= IPP_JOB_PROCESSING) | |
10143 | { | |
10144 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10145 | _("Job is completed and cannot be changed.")); | |
10146 | return; | |
10147 | } | |
10148 | else if (con->response->request.status.status_code == IPP_OK) | |
10149 | { | |
10150 | cupsdSetJobPriority(job, attr->values[0].integer); | |
10151 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
10152 | CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED; | |
10153 | } | |
10154 | } | |
10155 | else if (!strcmp(attr->name, "job-state")) | |
10156 | { | |
10157 | /* | |
10158 | * Change the job state... | |
10159 | */ | |
10160 | ||
10161 | if (attr->value_tag != IPP_TAG_ENUM) | |
10162 | { | |
10163 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!")); | |
10164 | ||
10165 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10166 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10167 | } | |
10168 | else | |
10169 | { | |
10170 | switch (attr->values[0].integer) | |
10171 | { | |
10172 | case IPP_JOB_PENDING : | |
10173 | case IPP_JOB_HELD : | |
10174 | if (job->state_value > IPP_JOB_HELD) | |
10175 | { | |
10176 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10177 | _("Job state cannot be changed.")); | |
10178 | return; | |
10179 | } | |
10180 | else if (con->response->request.status.status_code == IPP_OK) | |
10181 | { | |
10182 | job->state->values[0].integer = attr->values[0].integer; | |
10183 | job->state_value = (ipp_jstate_t)attr->values[0].integer; | |
10184 | ||
10185 | event |= CUPSD_EVENT_JOB_STATE; | |
10186 | } | |
10187 | break; | |
10188 | ||
10189 | case IPP_JOB_PROCESSING : | |
10190 | case IPP_JOB_STOPPED : | |
10191 | if (job->state_value != attr->values[0].integer) | |
10192 | { | |
10193 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10194 | _("Job state cannot be changed.")); | |
10195 | return; | |
10196 | } | |
10197 | break; | |
10198 | ||
10199 | case IPP_JOB_CANCELED : | |
10200 | case IPP_JOB_ABORTED : | |
10201 | case IPP_JOB_COMPLETED : | |
10202 | if (job->state_value > IPP_JOB_PROCESSING) | |
10203 | { | |
10204 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10205 | _("Job state cannot be changed.")); | |
10206 | return; | |
10207 | } | |
10208 | else if (con->response->request.status.status_code == IPP_OK) | |
10209 | cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer); | |
10210 | break; | |
10211 | } | |
10212 | } | |
10213 | } | |
10214 | else if (con->response->request.status.status_code != IPP_OK) | |
10215 | continue; | |
10216 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
10217 | IPP_TAG_ZERO)) != NULL) | |
10218 | { | |
10219 | /* | |
10220 | * Some other value; first free the old value... | |
10221 | */ | |
10222 | ||
10223 | if (job->attrs->prev) | |
10224 | job->attrs->prev->next = attr2->next; | |
10225 | else | |
10226 | job->attrs->attrs = attr2->next; | |
10227 | ||
10228 | if (job->attrs->last == attr2) | |
10229 | job->attrs->last = job->attrs->prev; | |
10230 | ||
10231 | _ippFreeAttr(attr2); | |
10232 | ||
10233 | /* | |
10234 | * Then copy the attribute... | |
10235 | */ | |
10236 | ||
10237 | copy_attribute(job->attrs, attr, 0); | |
10238 | ||
10239 | /* | |
10240 | * See if the job-name or job-hold-until is being changed. | |
10241 | */ | |
10242 | ||
10243 | if (!strcmp(attr->name, "job-hold-until")) | |
10244 | { | |
10245 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
10246 | ||
10247 | if (!strcmp(attr->values[0].string.text, "no-hold")) | |
10248 | cupsdReleaseJob(job); | |
10249 | else | |
10250 | cupsdHoldJob(job); | |
10251 | ||
10252 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE; | |
10253 | } | |
10254 | } | |
10255 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
10256 | { | |
10257 | /* | |
10258 | * Delete the attribute... | |
10259 | */ | |
10260 | ||
10261 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
10262 | IPP_TAG_ZERO)) != NULL) | |
10263 | { | |
10264 | if (job->attrs->prev) | |
10265 | job->attrs->prev->next = attr2->next; | |
10266 | else | |
10267 | job->attrs->attrs = attr2->next; | |
10268 | ||
10269 | if (attr2 == job->attrs->last) | |
10270 | job->attrs->last = job->attrs->prev; | |
10271 | ||
10272 | _ippFreeAttr(attr2); | |
10273 | ||
10274 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
10275 | } | |
10276 | } | |
10277 | else | |
10278 | { | |
10279 | /* | |
10280 | * Add new option by copying it... | |
10281 | */ | |
10282 | ||
10283 | copy_attribute(job->attrs, attr, 0); | |
10284 | ||
10285 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
10286 | } | |
10287 | } | |
10288 | ||
10289 | /* | |
10290 | * Save the job... | |
10291 | */ | |
10292 | ||
10293 | job->dirty = 1; | |
10294 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
10295 | ||
10296 | /* | |
10297 | * Send events as needed... | |
10298 | */ | |
10299 | ||
10300 | if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED) | |
10301 | cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED, | |
10302 | cupsdFindDest(job->dest), job, | |
10303 | "Job priority changed by user."); | |
10304 | ||
10305 | if (event & CUPSD_EVENT_JOB_STATE) | |
10306 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, | |
10307 | job->state_value == IPP_JOB_HELD ? | |
10308 | "Job held by user." : "Job restarted by user."); | |
10309 | ||
10310 | if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED) | |
10311 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, | |
10312 | "Job options changed by user."); | |
10313 | ||
10314 | /* | |
10315 | * Start jobs if possible... | |
10316 | */ | |
10317 | ||
10318 | cupsdCheckJobs(); | |
10319 | } | |
10320 | ||
10321 | ||
10322 | /* | |
10323 | * 'set_printer_defaults()' - Set printer default options from a request. | |
10324 | */ | |
10325 | ||
10326 | static void | |
10327 | set_printer_defaults( | |
10328 | cupsd_client_t *con, /* I - Client connection */ | |
10329 | cupsd_printer_t *printer) /* I - Printer */ | |
10330 | { | |
10331 | int i; /* Looping var */ | |
10332 | ipp_attribute_t *attr; /* Current attribute */ | |
10333 | int namelen; /* Length of attribute name */ | |
10334 | char name[256], /* New attribute name */ | |
10335 | value[256]; /* String version of integer attrs */ | |
10336 | ||
10337 | ||
10338 | for (attr = con->request->attrs; attr; attr = attr->next) | |
10339 | { | |
10340 | /* | |
10341 | * Skip non-printer attributes... | |
10342 | */ | |
10343 | ||
10344 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
10345 | continue; | |
10346 | ||
10347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
10348 | ||
10349 | if (!strcmp(attr->name, "job-sheets-default")) | |
10350 | { | |
10351 | /* | |
10352 | * Only allow keywords and names... | |
10353 | */ | |
10354 | ||
10355 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10356 | continue; | |
10357 | ||
10358 | /* | |
10359 | * Only allow job-sheets-default to be set when running without a | |
10360 | * system high classification level... | |
10361 | */ | |
10362 | ||
10363 | if (Classification) | |
10364 | continue; | |
10365 | ||
10366 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
10367 | ||
10368 | if (attr->num_values > 1) | |
10369 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
10370 | else | |
10371 | cupsdSetString(&printer->job_sheets[1], "none"); | |
10372 | } | |
10373 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
10374 | { | |
10375 | cupsdFreePrinterUsers(printer); | |
10376 | ||
10377 | printer->deny_users = 0; | |
10378 | ||
10379 | if (attr->value_tag == IPP_TAG_NAME && | |
10380 | (attr->num_values > 1 || | |
10381 | strcmp(attr->values[0].string.text, "all"))) | |
10382 | { | |
10383 | for (i = 0; i < attr->num_values; i ++) | |
10384 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10385 | } | |
10386 | } | |
10387 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
10388 | { | |
10389 | cupsdFreePrinterUsers(printer); | |
10390 | ||
10391 | printer->deny_users = 1; | |
10392 | ||
10393 | if (attr->value_tag == IPP_TAG_NAME && | |
10394 | (attr->num_values > 1 || | |
10395 | strcmp(attr->values[0].string.text, "none"))) | |
10396 | { | |
10397 | for (i = 0; i < attr->num_values; i ++) | |
10398 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10399 | } | |
10400 | } | |
10401 | else if (!strcmp(attr->name, "job-quota-period")) | |
10402 | { | |
10403 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10404 | continue; | |
10405 | ||
10406 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
10407 | attr->values[0].integer); | |
10408 | cupsdFreeQuotas(printer); | |
10409 | ||
10410 | printer->quota_period = attr->values[0].integer; | |
10411 | } | |
10412 | else if (!strcmp(attr->name, "job-k-limit")) | |
10413 | { | |
10414 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10415 | continue; | |
10416 | ||
10417 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
10418 | attr->values[0].integer); | |
10419 | cupsdFreeQuotas(printer); | |
10420 | ||
10421 | printer->k_limit = attr->values[0].integer; | |
10422 | } | |
10423 | else if (!strcmp(attr->name, "job-page-limit")) | |
10424 | { | |
10425 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10426 | continue; | |
10427 | ||
10428 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
10429 | attr->values[0].integer); | |
10430 | cupsdFreeQuotas(printer); | |
10431 | ||
10432 | printer->page_limit = attr->values[0].integer; | |
10433 | } | |
10434 | else if (!strcmp(attr->name, "printer-op-policy")) | |
10435 | { | |
10436 | cupsd_policy_t *p; /* Policy */ | |
10437 | ||
10438 | ||
10439 | if (attr->value_tag != IPP_TAG_NAME) | |
10440 | continue; | |
10441 | ||
10442 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
10443 | { | |
10444 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10445 | "Setting printer-op-policy to \"%s\"...", | |
10446 | attr->values[0].string.text); | |
10447 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
10448 | printer->op_policy_ptr = p; | |
10449 | } | |
10450 | else | |
10451 | { | |
10452 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10453 | _("Unknown printer-op-policy \"%s\"."), | |
10454 | attr->values[0].string.text); | |
10455 | return; | |
10456 | } | |
10457 | } | |
10458 | else if (!strcmp(attr->name, "printer-error-policy")) | |
10459 | { | |
10460 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10461 | continue; | |
10462 | ||
10463 | if (strcmp(attr->values[0].string.text, "abort-job") && | |
10464 | strcmp(attr->values[0].string.text, "retry-job") && | |
10465 | strcmp(attr->values[0].string.text, "stop-printer")) | |
10466 | { | |
10467 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10468 | _("Unknown printer-error-policy \"%s\"."), | |
10469 | attr->values[0].string.text); | |
10470 | return; | |
10471 | } | |
10472 | ||
10473 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10474 | "Setting printer-error-policy to \"%s\"...", | |
10475 | attr->values[0].string.text); | |
10476 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
10477 | } | |
10478 | ||
10479 | /* | |
10480 | * Skip any other non-default attributes... | |
10481 | */ | |
10482 | ||
10483 | namelen = strlen(attr->name); | |
10484 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
10485 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
10486 | continue; | |
10487 | ||
10488 | /* | |
10489 | * OK, anything else must be a user-defined default... | |
10490 | */ | |
10491 | ||
10492 | strlcpy(name, attr->name, sizeof(name)); | |
10493 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
10494 | ||
10495 | switch (attr->value_tag) | |
10496 | { | |
10497 | case IPP_TAG_DELETEATTR : | |
10498 | printer->num_options = cupsRemoveOption(name, | |
10499 | printer->num_options, | |
10500 | &(printer->options)); | |
10501 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10502 | "Deleting %s", attr->name); | |
10503 | break; | |
10504 | ||
10505 | case IPP_TAG_NAME : | |
10506 | case IPP_TAG_KEYWORD : | |
10507 | case IPP_TAG_URI : | |
10508 | printer->num_options = cupsAddOption(name, | |
10509 | attr->values[0].string.text, | |
10510 | printer->num_options, | |
10511 | &(printer->options)); | |
10512 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10513 | "Setting %s to \"%s\"...", attr->name, | |
10514 | attr->values[0].string.text); | |
10515 | break; | |
10516 | ||
10517 | case IPP_TAG_BOOLEAN : | |
10518 | printer->num_options = cupsAddOption(name, | |
10519 | attr->values[0].boolean ? | |
10520 | "true" : "false", | |
10521 | printer->num_options, | |
10522 | &(printer->options)); | |
10523 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10524 | "Setting %s to %s...", attr->name, | |
10525 | attr->values[0].boolean ? "true" : "false"); | |
10526 | break; | |
10527 | ||
10528 | case IPP_TAG_INTEGER : | |
10529 | case IPP_TAG_ENUM : | |
10530 | sprintf(value, "%d", attr->values[0].integer); | |
10531 | printer->num_options = cupsAddOption(name, value, | |
10532 | printer->num_options, | |
10533 | &(printer->options)); | |
10534 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10535 | "Setting %s to %s...", attr->name, value); | |
10536 | break; | |
10537 | ||
10538 | case IPP_TAG_RANGE : | |
10539 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
10540 | attr->values[0].range.upper); | |
10541 | printer->num_options = cupsAddOption(name, value, | |
10542 | printer->num_options, | |
10543 | &(printer->options)); | |
10544 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10545 | "Setting %s to %s...", attr->name, value); | |
10546 | break; | |
10547 | ||
10548 | case IPP_TAG_RESOLUTION : | |
10549 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
10550 | attr->values[0].resolution.yres, | |
10551 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
10552 | "dpi" : "dpc"); | |
10553 | printer->num_options = cupsAddOption(name, value, | |
10554 | printer->num_options, | |
10555 | &(printer->options)); | |
10556 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10557 | "Setting %s to %s...", attr->name, value); | |
10558 | break; | |
10559 | ||
10560 | default : | |
10561 | /* Do nothing for other values */ | |
10562 | break; | |
10563 | } | |
10564 | } | |
10565 | } | |
10566 | ||
10567 | ||
10568 | /* | |
10569 | * 'start_printer()' - Start a printer. | |
10570 | */ | |
10571 | ||
10572 | static void | |
10573 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
10574 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10575 | { | |
10576 | http_status_t status; /* Policy status */ | |
10577 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
10578 | cupsd_printer_t *printer; /* Printer data */ | |
10579 | ||
10580 | ||
10581 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
10582 | con->http.fd, uri->values[0].string.text); | |
10583 | ||
10584 | /* | |
10585 | * Is the destination valid? | |
10586 | */ | |
10587 | ||
10588 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
10589 | { | |
10590 | /* | |
10591 | * Bad URI... | |
10592 | */ | |
10593 | ||
10594 | send_ipp_status(con, IPP_NOT_FOUND, | |
10595 | _("The printer or class was not found.")); | |
10596 | return; | |
10597 | } | |
10598 | ||
10599 | /* | |
10600 | * Check policy... | |
10601 | */ | |
10602 | ||
10603 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10604 | { | |
10605 | send_http_error(con, status, printer); | |
10606 | return; | |
10607 | } | |
10608 | ||
10609 | /* | |
10610 | * Start the printer... | |
10611 | */ | |
10612 | ||
10613 | printer->state_message[0] = '\0'; | |
10614 | ||
10615 | cupsdStartPrinter(printer, 1); | |
10616 | ||
10617 | if (dtype & CUPS_PRINTER_CLASS) | |
10618 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", | |
10619 | printer->name, get_username(con)); | |
10620 | else | |
10621 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", | |
10622 | printer->name, get_username(con)); | |
10623 | ||
10624 | cupsdCheckJobs(); | |
10625 | ||
10626 | /* | |
10627 | * Everything was ok, so return OK status... | |
10628 | */ | |
10629 | ||
10630 | con->response->request.status.status_code = IPP_OK; | |
10631 | } | |
10632 | ||
10633 | ||
10634 | /* | |
10635 | * 'stop_printer()' - Stop a printer. | |
10636 | */ | |
10637 | ||
10638 | static void | |
10639 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
10640 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10641 | { | |
10642 | http_status_t status; /* Policy status */ | |
10643 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
10644 | cupsd_printer_t *printer; /* Printer data */ | |
10645 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
10646 | ||
10647 | ||
10648 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
10649 | con->http.fd, uri->values[0].string.text); | |
10650 | ||
10651 | /* | |
10652 | * Is the destination valid? | |
10653 | */ | |
10654 | ||
10655 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
10656 | { | |
10657 | /* | |
10658 | * Bad URI... | |
10659 | */ | |
10660 | ||
10661 | send_ipp_status(con, IPP_NOT_FOUND, | |
10662 | _("The printer or class was not found.")); | |
10663 | return; | |
10664 | } | |
10665 | ||
10666 | /* | |
10667 | * Check policy... | |
10668 | */ | |
10669 | ||
10670 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10671 | { | |
10672 | send_http_error(con, status, printer); | |
10673 | return; | |
10674 | } | |
10675 | ||
10676 | /* | |
10677 | * Stop the printer... | |
10678 | */ | |
10679 | ||
10680 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
10681 | IPP_TAG_TEXT)) == NULL) | |
10682 | strcpy(printer->state_message, "Paused"); | |
10683 | else | |
10684 | { | |
10685 | strlcpy(printer->state_message, attr->values[0].string.text, | |
10686 | sizeof(printer->state_message)); | |
10687 | } | |
10688 | ||
10689 | cupsdStopPrinter(printer, 1); | |
10690 | ||
10691 | if (dtype & CUPS_PRINTER_CLASS) | |
10692 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", | |
10693 | printer->name, get_username(con)); | |
10694 | else | |
10695 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", | |
10696 | printer->name, get_username(con)); | |
10697 | ||
10698 | /* | |
10699 | * Everything was ok, so return OK status... | |
10700 | */ | |
10701 | ||
10702 | con->response->request.status.status_code = IPP_OK; | |
10703 | } | |
10704 | ||
10705 | ||
10706 | /* | |
10707 | * 'url_encode_attr()' - URL-encode a string attribute. | |
10708 | */ | |
10709 | ||
10710 | static void | |
10711 | url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */ | |
10712 | char *buffer,/* I - String buffer */ | |
10713 | int bufsize)/* I - Size of buffer */ | |
10714 | { | |
10715 | int i; /* Looping var */ | |
10716 | char *bufptr, /* Pointer into buffer */ | |
10717 | *bufend; /* End of buffer */ | |
10718 | ||
10719 | ||
10720 | strlcpy(buffer, attr->name, bufsize); | |
10721 | bufptr = buffer + strlen(buffer); | |
10722 | bufend = buffer + bufsize - 1; | |
10723 | ||
10724 | for (i = 0; i < attr->num_values; i ++) | |
10725 | { | |
10726 | if (bufptr >= bufend) | |
10727 | break; | |
10728 | ||
10729 | if (i) | |
10730 | *bufptr++ = ','; | |
10731 | else | |
10732 | *bufptr++ = '='; | |
10733 | ||
10734 | if (bufptr >= bufend) | |
10735 | break; | |
10736 | ||
10737 | *bufptr++ = '\''; | |
10738 | ||
10739 | bufptr = url_encode_string(attr->values[i].string.text, | |
10740 | bufptr, bufend - bufptr + 1); | |
10741 | ||
10742 | if (bufptr >= bufend) | |
10743 | break; | |
10744 | ||
10745 | *bufptr++ = '\''; | |
10746 | } | |
10747 | ||
10748 | *bufptr = '\0'; | |
10749 | } | |
10750 | ||
10751 | ||
10752 | /* | |
10753 | * 'url_encode_string()' - URL-encode a string. | |
10754 | */ | |
10755 | ||
10756 | static char * /* O - End of string */ | |
10757 | url_encode_string(const char *s, /* I - String */ | |
10758 | char *buffer, /* I - String buffer */ | |
10759 | int bufsize) /* I - Size of buffer */ | |
10760 | { | |
10761 | char *bufptr, /* Pointer into buffer */ | |
10762 | *bufend; /* End of buffer */ | |
10763 | static const char *hex = "0123456789ABCDEF"; | |
10764 | /* Hex digits */ | |
10765 | ||
10766 | ||
10767 | bufptr = buffer; | |
10768 | bufend = buffer + bufsize - 1; | |
10769 | ||
10770 | while (*s && bufptr < bufend) | |
10771 | { | |
10772 | if (*s == ' ' || *s == '%') | |
10773 | { | |
10774 | if (bufptr >= (bufend - 2)) | |
10775 | break; | |
10776 | ||
10777 | *bufptr++ = '%'; | |
10778 | *bufptr++ = hex[(*s >> 4) & 15]; | |
10779 | *bufptr++ = hex[*s & 15]; | |
10780 | ||
10781 | s ++; | |
10782 | } | |
10783 | else if (*s == '\'' || *s == '\\') | |
10784 | { | |
10785 | if (bufptr >= (bufend - 1)) | |
10786 | break; | |
10787 | ||
10788 | *bufptr++ = '\\'; | |
10789 | *bufptr++ = *s++; | |
10790 | } | |
10791 | else | |
10792 | *bufptr++ = *s++; | |
10793 | } | |
10794 | ||
10795 | *bufptr = '\0'; | |
10796 | ||
10797 | return (bufptr); | |
10798 | } | |
10799 | ||
10800 | ||
10801 | /* | |
10802 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
10803 | */ | |
10804 | ||
10805 | static int /* O - 0 if not allowed, 1 if allowed */ | |
10806 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
10807 | const char *username) /* I - Username */ | |
10808 | { | |
10809 | int i; /* Looping var */ | |
10810 | struct passwd *pw; /* User password data */ | |
10811 | char baseuser[256], /* Base username */ | |
10812 | *baseptr; /* Pointer to "@" in base username */ | |
10813 | ||
10814 | ||
10815 | if (p->num_users == 0) | |
10816 | return (1); | |
10817 | ||
10818 | if (!strcmp(username, "root")) | |
10819 | return (1); | |
10820 | ||
10821 | if (strchr(username, '@')) | |
10822 | { | |
10823 | /* | |
10824 | * Strip @REALM for username check... | |
10825 | */ | |
10826 | ||
10827 | strlcpy(baseuser, username, sizeof(baseuser)); | |
10828 | ||
10829 | if ((baseptr = strchr(baseuser, '@')) != NULL) | |
10830 | *baseptr = '\0'; | |
10831 | ||
10832 | username = baseuser; | |
10833 | } | |
10834 | ||
10835 | pw = getpwnam(username); | |
10836 | endpwent(); | |
10837 | ||
10838 | for (i = 0; i < p->num_users; i ++) | |
10839 | { | |
10840 | if (p->users[i][0] == '@') | |
10841 | { | |
10842 | /* | |
10843 | * Check group membership... | |
10844 | */ | |
10845 | ||
10846 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
10847 | break; | |
10848 | } | |
10849 | else if (p->users[i][0] == '#') | |
10850 | { | |
10851 | /* | |
10852 | * Check UUID... | |
10853 | */ | |
10854 | ||
10855 | if (cupsdCheckGroup(username, pw, p->users[i])) | |
10856 | break; | |
10857 | } | |
10858 | else if (!strcasecmp(username, p->users[i])) | |
10859 | break; | |
10860 | } | |
10861 | ||
10862 | return ((i < p->num_users) != p->deny_users); | |
10863 | } | |
10864 | ||
10865 | ||
10866 | /* | |
10867 | * 'validate_job()' - Validate printer options and destination. | |
10868 | */ | |
10869 | ||
10870 | static void | |
10871 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
10872 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10873 | { | |
10874 | http_status_t status; /* Policy status */ | |
10875 | ipp_attribute_t *attr; /* Current attribute */ | |
10876 | ipp_attribute_t *format; /* Document-format attribute */ | |
10877 | cups_ptype_t dtype; /* Destination type (printer/class) */ | |
10878 | char super[MIME_MAX_SUPER], | |
10879 | /* Supertype of file */ | |
10880 | type[MIME_MAX_TYPE]; | |
10881 | /* Subtype of file */ | |
10882 | cupsd_printer_t *printer; /* Printer */ | |
10883 | ||
10884 | ||
10885 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
10886 | con->http.fd, uri->values[0].string.text); | |
10887 | ||
10888 | /* | |
10889 | * OK, see if the client is sending the document compressed - CUPS | |
10890 | * doesn't support compression yet... | |
10891 | */ | |
10892 | ||
10893 | if ((attr = ippFindAttribute(con->request, "compression", | |
10894 | IPP_TAG_KEYWORD)) != NULL && | |
10895 | !strcmp(attr->values[0].string.text, "none")) | |
10896 | { | |
10897 | send_ipp_status(con, IPP_ATTRIBUTES, | |
10898 | _("Unsupported compression attribute %s!"), | |
10899 | attr->values[0].string.text); | |
10900 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
10901 | "compression", NULL, attr->values[0].string.text); | |
10902 | return; | |
10903 | } | |
10904 | ||
10905 | /* | |
10906 | * Is it a format we support? | |
10907 | */ | |
10908 | ||
10909 | if ((format = ippFindAttribute(con->request, "document-format", | |
10910 | IPP_TAG_MIMETYPE)) != NULL) | |
10911 | { | |
10912 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", | |
10913 | super, type) != 2) | |
10914 | { | |
10915 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
10916 | format->values[0].string.text); | |
10917 | return; | |
10918 | } | |
10919 | ||
10920 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && | |
10921 | !mimeType(MimeDatabase, super, type)) | |
10922 | { | |
10923 | cupsdLogMessage(CUPSD_LOG_INFO, | |
10924 | "Hint: Do you have the raw file printing rules enabled?"); | |
10925 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
10926 | _("Unsupported format \"%s\"!"), | |
10927 | format->values[0].string.text); | |
10928 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
10929 | "document-format", NULL, format->values[0].string.text); | |
10930 | return; | |
10931 | } | |
10932 | } | |
10933 | ||
10934 | /* | |
10935 | * Is the destination valid? | |
10936 | */ | |
10937 | ||
10938 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) | |
10939 | { | |
10940 | /* | |
10941 | * Bad URI... | |
10942 | */ | |
10943 | ||
10944 | send_ipp_status(con, IPP_NOT_FOUND, | |
10945 | _("The printer or class was not found.")); | |
10946 | return; | |
10947 | } | |
10948 | ||
10949 | /* | |
10950 | * Check policy... | |
10951 | */ | |
10952 | ||
10953 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10954 | { | |
10955 | send_http_error(con, status, printer); | |
10956 | return; | |
10957 | } | |
10958 | ||
10959 | /* | |
10960 | * Everything was ok, so return OK status... | |
10961 | */ | |
10962 | ||
10963 | con->response->request.status.status_code = IPP_OK; | |
10964 | } | |
10965 | ||
10966 | ||
10967 | /* | |
10968 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
10969 | */ | |
10970 | ||
10971 | static int /* O - 0 if name is no good, 1 if good */ | |
10972 | validate_name(const char *name) /* I - Name to check */ | |
10973 | { | |
10974 | const char *ptr; /* Pointer into name */ | |
10975 | ||
10976 | ||
10977 | /* | |
10978 | * Scan the whole name... | |
10979 | */ | |
10980 | ||
10981 | for (ptr = name; *ptr; ptr ++) | |
10982 | if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') | |
10983 | return (0); | |
10984 | ||
10985 | /* | |
10986 | * All the characters are good; validate the length, too... | |
10987 | */ | |
10988 | ||
10989 | return ((ptr - name) < 128); | |
10990 | } | |
10991 | ||
10992 | ||
10993 | /* | |
10994 | * 'validate_user()' - Validate the user for the request. | |
10995 | */ | |
10996 | ||
10997 | static int /* O - 1 if permitted, 0 otherwise */ | |
10998 | validate_user(cupsd_job_t *job, /* I - Job */ | |
10999 | cupsd_client_t *con, /* I - Client connection */ | |
11000 | const char *owner, /* I - Owner of job/resource */ | |
11001 | char *username, /* O - Authenticated username */ | |
11002 | int userlen) /* I - Length of username */ | |
11003 | { | |
11004 | cupsd_printer_t *printer; /* Printer for job */ | |
11005 | ||
11006 | ||
11007 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
11008 | "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, " | |
11009 | "userlen=%d)", | |
11010 | job->id, con ? con->http.fd : 0, | |
11011 | owner ? owner : "(null)", username, userlen); | |
11012 | ||
11013 | /* | |
11014 | * Validate input... | |
11015 | */ | |
11016 | ||
11017 | if (!con || !owner || !username || userlen <= 0) | |
11018 | return (0); | |
11019 | ||
11020 | /* | |
11021 | * Get the best authenticated username that is available. | |
11022 | */ | |
11023 | ||
11024 | strlcpy(username, get_username(con), userlen); | |
11025 | ||
11026 | /* | |
11027 | * Check the username against the owner... | |
11028 | */ | |
11029 | ||
11030 | printer = cupsdFindDest(job->dest); | |
11031 | ||
11032 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
11033 | con, owner) == HTTP_OK); | |
11034 | } | |
11035 | ||
11036 | ||
11037 | /* | |
11038 | * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $". | |
11039 | */ |