]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $" |
ef416fc2 | 3 | * |
4 | * IPP routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
8 | * | |
9 | * This file contains Kerberos support code, copyright 2006 by | |
10 | * Jelmer Vernooij. | |
ef416fc2 | 11 | * |
12 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 13 | * property of Apple Inc. and are protected by Federal copyright |
14 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
15 | * which should have been included with this file. If this file is | |
16 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 17 | * |
18 | * Contents: | |
19 | * | |
a603edef MS |
20 | * cupsdProcessIPPRequest() - Process an incoming IPP request. |
21 | * cupsdTimeoutJob() - Timeout a job waiting on job files. | |
22 | * accept_jobs() - Accept print jobs to a printer. | |
23 | * add_class() - Add a class to the system. | |
24 | * add_file() - Add a file to a job. | |
25 | * add_job() - Add a job to a print queue. | |
26 | * add_job_state_reasons() - Add the "job-state-reasons" attribute based | |
27 | * upon the job and printer state... | |
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. | |
3dfe78b3 MS |
45 | * check_rss_recipient() - Check that we do not have a duplicate RSS |
46 | * feed URI. | |
a603edef MS |
47 | * copy_attribute() - Copy a single attribute. |
48 | * copy_attrs() - Copy attributes from one request to another. | |
49 | * copy_banner() - Copy a banner file to the requests directory | |
50 | * for the specified job. | |
51 | * copy_file() - Copy a PPD file or interface script... | |
52 | * copy_model() - Copy a PPD model file, substituting default | |
53 | * values as needed... | |
54 | * copy_job_attrs() - Copy job attributes. | |
55 | * copy_printer_attrs() - Copy printer attributes. | |
56 | * copy_subscription_attrs() - Copy subscription attributes. | |
57 | * create_job() - Print a file to a printer or class. | |
58 | * create_requested_array() - Create an array for the requested-attributes. | |
59 | * create_subscription() - Create a notification subscription. | |
60 | * delete_printer() - Remove a printer or class from the system. | |
61 | * get_default() - Get the default destination. | |
62 | * get_devices() - Get the list of available devices on the | |
63 | * local system. | |
64 | * get_document() - Get a copy of a job file. | |
65 | * get_job_attrs() - Get job attributes. | |
66 | * get_jobs() - Get a list of jobs for the specified printer. | |
67 | * get_notifications() - Get events for a subscription. | |
68 | * get_ppd() - Get a named PPD from the local system. | |
69 | * get_ppds() - Get the list of PPD files on the local | |
70 | * system. | |
71 | * get_printer_attrs() - Get printer attributes. | |
72 | * get_printers() - Get a list of printers or classes. | |
73 | * get_subscription_attrs() - Get subscription attributes. | |
74 | * get_subscriptions() - Get subscriptions. | |
75 | * get_username() - Get the username associated with a request. | |
76 | * hold_job() - Hold a print job. | |
77 | * move_job() - Move a job to a new destination. | |
78 | * ppd_parse_line() - Parse a PPD default line. | |
79 | * print_job() - Print a file to a printer or class. | |
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. | |
ef416fc2 | 102 | */ |
103 | ||
104 | /* | |
105 | * Include necessary headers... | |
106 | */ | |
107 | ||
108 | #include "cupsd.h" | |
a603edef | 109 | #include <cups/ppd-private.h> |
ef416fc2 | 110 | |
111 | #ifdef HAVE_LIBPAPER | |
112 | # include <paper.h> | |
113 | #endif /* HAVE_LIBPAPER */ | |
114 | ||
f899b121 | 115 | #ifdef __APPLE__ |
568fa3fa MS |
116 | # include <ApplicationServices/ApplicationServices.h> |
117 | # include <CoreFoundation/CoreFoundation.h> | |
f899b121 | 118 | # ifdef HAVE_MEMBERSHIP_H |
119 | # include <membership.h> | |
120 | # endif /* HAVE_MEMBERSHIP_H */ | |
121 | # ifdef HAVE_MEMBERSHIPPRIV_H | |
122 | # include <membershipPriv.h> | |
123 | # else | |
7594b224 | 124 | extern int mbr_user_name_to_uuid(const char* name, uuid_t uu); |
125 | extern int mbr_group_name_to_uuid(const char* name, uuid_t uu); | |
126 | extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember); | |
f899b121 | 127 | # endif /* HAVE_MEMBERSHIPPRIV_H */ |
128 | #endif /* __APPLE__ */ | |
7594b224 | 129 | |
ef416fc2 | 130 | |
ef416fc2 | 131 | /* |
132 | * Local functions... | |
133 | */ | |
134 | ||
135 | static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
136 | static void add_class(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 137 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, |
138 | mime_type_t *filetype, int compression); | |
f7deaa1a | 139 | static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer, |
80ca4592 | 140 | mime_type_t *filetype); |
ef416fc2 | 141 | static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job); |
142 | static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job); | |
bd7854cb | 143 | static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job); |
ef416fc2 | 144 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
b423cd4c | 145 | static void add_printer_state_reasons(cupsd_client_t *con, |
146 | cupsd_printer_t *p); | |
ef416fc2 | 147 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); |
568fa3fa | 148 | #ifdef __APPLE__ |
a603edef MS |
149 | static void apple_init_profile(ppd_file_t *ppd, cups_array_t *languages, |
150 | CMDeviceProfileInfo *profile, unsigned id, | |
151 | const char *name, const char *text, | |
152 | const char *iccfile); | |
568fa3fa MS |
153 | static void apple_register_profiles(cupsd_printer_t *p); |
154 | static void apple_unregister_profiles(cupsd_printer_t *p); | |
155 | #endif /* __APPLE__ */ | |
b423cd4c | 156 | static void apply_printer_defaults(cupsd_printer_t *printer, |
157 | cupsd_job_t *job); | |
ef416fc2 | 158 | static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri); |
159 | static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
160 | static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
161 | static void cancel_subscription(cupsd_client_t *con, int id); | |
3dfe78b3 | 162 | static int check_rss_recipient(const char *recipient); |
ef416fc2 | 163 | static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p); |
164 | static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr, | |
165 | int quickcopy); | |
fa73b229 | 166 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, |
ef416fc2 | 167 | ipp_tag_t group, int quickcopy); |
b423cd4c | 168 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, |
169 | const char *name); | |
ef416fc2 | 170 | static int copy_file(const char *from, const char *to); |
b423cd4c | 171 | static int copy_model(cupsd_client_t *con, const char *from, |
172 | const char *to); | |
fa73b229 | 173 | static void copy_job_attrs(cupsd_client_t *con, |
174 | cupsd_job_t *job, | |
175 | cups_array_t *ra); | |
176 | static void copy_printer_attrs(cupsd_client_t *con, | |
177 | cupsd_printer_t *printer, | |
178 | cups_array_t *ra); | |
ef416fc2 | 179 | static void copy_subscription_attrs(cupsd_client_t *con, |
180 | cupsd_subscription_t *sub, | |
181 | cups_array_t *ra); | |
182 | static void create_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
183 | static cups_array_t *create_requested_array(ipp_t *request); | |
184 | static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri); | |
185 | static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
186 | static void get_default(cupsd_client_t *con); | |
187 | static void get_devices(cupsd_client_t *con); | |
2e4ff8af | 188 | static void get_document(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 189 | static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri); |
190 | static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
bd7854cb | 191 | static void get_notifications(cupsd_client_t *con); |
b94498cf | 192 | static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri); |
ef416fc2 | 193 | static void get_ppds(cupsd_client_t *con); |
194 | static void get_printers(cupsd_client_t *con, int type); | |
195 | static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
196 | static void get_subscription_attrs(cupsd_client_t *con, int sub_id); | |
197 | static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); | |
e00b005a | 198 | static const char *get_username(cupsd_client_t *con); |
ef416fc2 | 199 | static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri); |
200 | static void move_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
ef416fc2 | 201 | static int ppd_parse_line(const char *line, char *option, int olen, |
202 | char *choice, int clen); | |
203 | static void print_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
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); | |
f7deaa1a | 209 | static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job, |
210 | ipp_attribute_t *auth_info); | |
211 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
212 | static void save_krb5_creds(cupsd_client_t *con, cupsd_job_t *job); | |
213 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ | |
ef416fc2 | 214 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); |
f899b121 | 215 | static void send_http_error(cupsd_client_t *con, http_status_t status, |
216 | cupsd_printer_t *printer); | |
ef416fc2 | 217 | static void send_ipp_status(cupsd_client_t *con, ipp_status_t status, |
218 | const char *message, ...) | |
219 | # ifdef __GNUC__ | |
220 | __attribute__ ((__format__ (__printf__, 3, 4))) | |
221 | # endif /* __GNUC__ */ | |
222 | ; | |
223 | static void set_default(cupsd_client_t *con, ipp_attribute_t *uri); | |
224 | static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 225 | static void set_printer_defaults(cupsd_client_t *con, |
226 | cupsd_printer_t *printer); | |
ef416fc2 | 227 | static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
228 | static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
89d46774 | 229 | static void url_encode_attr(ipp_attribute_t *attr, char *buffer, |
230 | int bufsize); | |
b94498cf | 231 | static char *url_encode_string(const char *s, char *buffer, int bufsize); |
ef416fc2 | 232 | static int user_allowed(cupsd_printer_t *p, const char *username); |
233 | static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
234 | static int validate_name(const char *name); | |
235 | static int validate_user(cupsd_job_t *job, cupsd_client_t *con, | |
236 | const char *owner, char *username, | |
237 | int userlen); | |
238 | ||
239 | ||
240 | /* | |
09a101d6 | 241 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request. |
ef416fc2 | 242 | */ |
243 | ||
244 | int /* O - 1 on success, 0 on failure */ | |
245 | cupsdProcessIPPRequest( | |
246 | cupsd_client_t *con) /* I - Client connection */ | |
247 | { | |
248 | ipp_tag_t group; /* Current group tag */ | |
249 | ipp_attribute_t *attr; /* Current attribute */ | |
250 | ipp_attribute_t *charset; /* Character set attribute */ | |
251 | ipp_attribute_t *language; /* Language attribute */ | |
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 | ||
bc44d920 | 267 | con->response->request.status.version[0] = |
268 | con->request->request.op.version[0]; | |
269 | con->response->request.status.version[1] = | |
270 | con->request->request.op.version[1]; | |
271 | con->response->request.status.request_id = | |
272 | con->request->request.op.request_id; | |
ef416fc2 | 273 | |
274 | /* | |
275 | * Then validate the request header and required attributes... | |
276 | */ | |
f7deaa1a | 277 | |
ef416fc2 | 278 | if (con->request->request.any.version[0] != 1) |
279 | { | |
280 | /* | |
281 | * Return an error, since we only support IPP 1.x. | |
282 | */ | |
283 | ||
284 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
285 | "%04X %s Bad request version number %d.%d", | |
286 | IPP_VERSION_NOT_SUPPORTED, con->http.hostname, | |
287 | con->request->request.any.version[0], | |
288 | con->request->request.any.version[1]); | |
289 | ||
290 | send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED, | |
291 | _("Bad request version number %d.%d!"), | |
292 | con->request->request.any.version[0], | |
293 | con->request->request.any.version[1]); | |
f7deaa1a | 294 | } |
fa73b229 | 295 | else if (!con->request->attrs) |
ef416fc2 | 296 | { |
297 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
298 | "%04X %s No attributes in request", | |
299 | IPP_BAD_REQUEST, con->http.hostname); | |
300 | ||
301 | send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!")); | |
302 | } | |
303 | else | |
304 | { | |
305 | /* | |
306 | * Make sure that the attributes are provided in the correct order and | |
307 | * don't repeat groups... | |
308 | */ | |
309 | ||
310 | for (attr = con->request->attrs, group = attr->group_tag; | |
fa73b229 | 311 | attr; |
ef416fc2 | 312 | attr = attr->next) |
313 | if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO) | |
314 | { | |
315 | /* | |
316 | * Out of order; return an error... | |
317 | */ | |
318 | ||
319 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
320 | "%04X %s Attribute groups are out of order", | |
321 | IPP_BAD_REQUEST, con->http.hostname); | |
322 | ||
323 | send_ipp_status(con, IPP_BAD_REQUEST, | |
324 | _("Attribute groups are out of order (%x < %x)!"), | |
325 | attr->group_tag, group); | |
326 | break; | |
327 | } | |
328 | else | |
329 | group = attr->group_tag; | |
330 | ||
fa73b229 | 331 | if (!attr) |
ef416fc2 | 332 | { |
333 | /* | |
334 | * Then make sure that the first three attributes are: | |
335 | * | |
336 | * attributes-charset | |
337 | * attributes-natural-language | |
338 | * printer-uri/job-uri | |
339 | */ | |
340 | ||
341 | attr = con->request->attrs; | |
342 | if (attr && !strcmp(attr->name, "attributes-charset") && | |
fa73b229 | 343 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET) |
ef416fc2 | 344 | charset = attr; |
345 | else | |
346 | charset = NULL; | |
347 | ||
348 | if (attr) | |
349 | attr = attr->next; | |
350 | ||
351 | if (attr && !strcmp(attr->name, "attributes-natural-language") && | |
fa73b229 | 352 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE) |
ef416fc2 | 353 | language = attr; |
354 | else | |
355 | language = NULL; | |
356 | ||
fa73b229 | 357 | if ((attr = ippFindAttribute(con->request, "printer-uri", |
358 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 359 | uri = attr; |
fa73b229 | 360 | else if ((attr = ippFindAttribute(con->request, "job-uri", |
361 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 362 | uri = attr; |
b94498cf | 363 | else if (con->request->request.op.operation_id == CUPS_GET_PPD) |
364 | uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME); | |
ef416fc2 | 365 | else |
366 | uri = NULL; | |
367 | ||
368 | if (charset) | |
369 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
bc44d920 | 370 | "attributes-charset", NULL, |
371 | charset->values[0].string.text); | |
ef416fc2 | 372 | else |
373 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
374 | "attributes-charset", NULL, DefaultCharset); | |
375 | ||
376 | if (language) | |
377 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
378 | "attributes-natural-language", NULL, | |
379 | language->values[0].string.text); | |
380 | else | |
381 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
382 | "attributes-natural-language", NULL, DefaultLanguage); | |
383 | ||
2e4ff8af MS |
384 | if (charset && |
385 | strcasecmp(charset->values[0].string.text, "us-ascii") && | |
386 | strcasecmp(charset->values[0].string.text, "utf-8")) | |
387 | { | |
388 | /* | |
389 | * Bad character set... | |
390 | */ | |
391 | ||
392 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"!", | |
393 | charset->values[0].string.text); | |
394 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
395 | "%04X %s Unsupported attributes-charset value \"%s\"", | |
396 | IPP_CHARSET, con->http.hostname, | |
397 | charset->values[0].string.text); | |
398 | send_ipp_status(con, IPP_BAD_REQUEST, | |
399 | _("Unsupported character set \"%s\"!"), | |
400 | charset->values[0].string.text); | |
401 | } | |
402 | else if (!charset || !language || | |
403 | (!uri && | |
404 | con->request->request.op.operation_id != CUPS_GET_DEFAULT && | |
405 | con->request->request.op.operation_id != CUPS_GET_PRINTERS && | |
406 | con->request->request.op.operation_id != CUPS_GET_CLASSES && | |
407 | con->request->request.op.operation_id != CUPS_GET_DEVICES && | |
408 | con->request->request.op.operation_id != CUPS_GET_PPDS)) | |
ef416fc2 | 409 | { |
410 | /* | |
411 | * Return an error, since attributes-charset, | |
412 | * attributes-natural-language, and printer-uri/job-uri are required | |
413 | * for all operations. | |
414 | */ | |
415 | ||
416 | if (!charset) | |
417 | { | |
418 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
419 | "Missing attributes-charset attribute!"); | |
420 | ||
421 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
422 | "%04X %s Missing attributes-charset attribute", | |
423 | IPP_BAD_REQUEST, con->http.hostname); | |
424 | } | |
425 | ||
426 | if (!language) | |
427 | { | |
428 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
429 | "Missing attributes-natural-language attribute!"); | |
430 | ||
431 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
432 | "%04X %s Missing attributes-natural-language attribute", | |
433 | IPP_BAD_REQUEST, con->http.hostname); | |
434 | } | |
435 | ||
436 | if (!uri) | |
437 | { | |
438 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
b94498cf | 439 | "Missing printer-uri, job-uri, or ppd-name " |
440 | "attribute!"); | |
ef416fc2 | 441 | |
442 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
b94498cf | 443 | "%04X %s Missing printer-uri, job-uri, or ppd-name " |
444 | "attribute", IPP_BAD_REQUEST, con->http.hostname); | |
ef416fc2 | 445 | } |
446 | ||
447 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
448 | ||
fa73b229 | 449 | for (attr = con->request->attrs; attr; attr = attr->next) |
f7deaa1a | 450 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
ef416fc2 | 451 | "attr \"%s\": group_tag = %x, value_tag = %x", |
452 | attr->name ? attr->name : "(null)", attr->group_tag, | |
453 | attr->value_tag); | |
454 | ||
455 | cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes..."); | |
456 | ||
457 | send_ipp_status(con, IPP_BAD_REQUEST, | |
458 | _("Missing required attributes!")); | |
459 | } | |
460 | else | |
461 | { | |
462 | /* | |
463 | * OK, all the checks pass so far; make sure requesting-user-name is | |
464 | * not "root" from a remote host... | |
465 | */ | |
466 | ||
467 | if ((username = ippFindAttribute(con->request, "requesting-user-name", | |
468 | IPP_TAG_NAME)) != NULL) | |
469 | { | |
470 | /* | |
471 | * Check for root user... | |
472 | */ | |
473 | ||
474 | if (!strcmp(username->values[0].string.text, "root") && | |
475 | strcasecmp(con->http.hostname, "localhost") && | |
476 | strcmp(con->username, "root")) | |
477 | { | |
478 | /* | |
479 | * Remote unauthenticated user masquerading as local root... | |
480 | */ | |
481 | ||
757d2cad | 482 | _cupsStrFree(username->values[0].string.text); |
483 | username->values[0].string.text = _cupsStrAlloc(RemoteRoot); | |
ef416fc2 | 484 | } |
485 | } | |
486 | ||
487 | if ((attr = ippFindAttribute(con->request, "notify-subscription-id", | |
488 | IPP_TAG_INTEGER)) != NULL) | |
489 | sub_id = attr->values[0].integer; | |
490 | else | |
491 | sub_id = 0; | |
492 | ||
493 | /* | |
494 | * Then try processing the operation... | |
495 | */ | |
496 | ||
497 | if (uri) | |
bd7854cb | 498 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", |
499 | ippOpString(con->request->request.op.operation_id), | |
500 | uri->values[0].string.text); | |
501 | else | |
502 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", | |
503 | ippOpString(con->request->request.op.operation_id)); | |
ef416fc2 | 504 | |
505 | switch (con->request->request.op.operation_id) | |
506 | { | |
507 | case IPP_PRINT_JOB : | |
508 | print_job(con, uri); | |
509 | break; | |
510 | ||
511 | case IPP_VALIDATE_JOB : | |
512 | validate_job(con, uri); | |
513 | break; | |
514 | ||
515 | case IPP_CREATE_JOB : | |
516 | create_job(con, uri); | |
517 | break; | |
518 | ||
519 | case IPP_SEND_DOCUMENT : | |
520 | send_document(con, uri); | |
521 | break; | |
522 | ||
523 | case IPP_CANCEL_JOB : | |
524 | cancel_job(con, uri); | |
525 | break; | |
526 | ||
527 | case IPP_GET_JOB_ATTRIBUTES : | |
528 | get_job_attrs(con, uri); | |
529 | break; | |
530 | ||
531 | case IPP_GET_JOBS : | |
532 | get_jobs(con, uri); | |
533 | break; | |
534 | ||
535 | case IPP_GET_PRINTER_ATTRIBUTES : | |
536 | get_printer_attrs(con, uri); | |
537 | break; | |
538 | ||
539 | case IPP_HOLD_JOB : | |
540 | hold_job(con, uri); | |
541 | break; | |
542 | ||
543 | case IPP_RELEASE_JOB : | |
544 | release_job(con, uri); | |
545 | break; | |
546 | ||
547 | case IPP_RESTART_JOB : | |
548 | restart_job(con, uri); | |
549 | break; | |
550 | ||
551 | case IPP_PAUSE_PRINTER : | |
552 | stop_printer(con, uri); | |
553 | break; | |
554 | ||
555 | case IPP_RESUME_PRINTER : | |
556 | start_printer(con, uri); | |
557 | break; | |
558 | ||
559 | case IPP_PURGE_JOBS : | |
560 | cancel_all_jobs(con, uri); | |
561 | break; | |
562 | ||
563 | case IPP_SET_JOB_ATTRIBUTES : | |
564 | set_job_attrs(con, uri); | |
565 | break; | |
566 | ||
567 | case CUPS_GET_DEFAULT : | |
568 | get_default(con); | |
569 | break; | |
570 | ||
571 | case CUPS_GET_PRINTERS : | |
572 | get_printers(con, 0); | |
573 | break; | |
574 | ||
575 | case CUPS_GET_CLASSES : | |
576 | get_printers(con, CUPS_PRINTER_CLASS); | |
577 | break; | |
578 | ||
579 | case CUPS_ADD_PRINTER : | |
580 | add_printer(con, uri); | |
581 | break; | |
582 | ||
583 | case CUPS_DELETE_PRINTER : | |
584 | delete_printer(con, uri); | |
585 | break; | |
586 | ||
587 | case CUPS_ADD_CLASS : | |
588 | add_class(con, uri); | |
589 | break; | |
590 | ||
591 | case CUPS_DELETE_CLASS : | |
592 | delete_printer(con, uri); | |
593 | break; | |
594 | ||
595 | case CUPS_ACCEPT_JOBS : | |
596 | case IPP_ENABLE_PRINTER : | |
597 | accept_jobs(con, uri); | |
598 | break; | |
599 | ||
600 | case CUPS_REJECT_JOBS : | |
601 | case IPP_DISABLE_PRINTER : | |
602 | reject_jobs(con, uri); | |
603 | break; | |
604 | ||
605 | case CUPS_SET_DEFAULT : | |
606 | set_default(con, uri); | |
607 | break; | |
608 | ||
609 | case CUPS_GET_DEVICES : | |
610 | get_devices(con); | |
611 | break; | |
612 | ||
2e4ff8af MS |
613 | case CUPS_GET_DOCUMENT : |
614 | get_document(con, uri); | |
615 | break; | |
616 | ||
b94498cf | 617 | case CUPS_GET_PPD : |
618 | get_ppd(con, uri); | |
619 | break; | |
620 | ||
ef416fc2 | 621 | case CUPS_GET_PPDS : |
622 | get_ppds(con); | |
623 | break; | |
624 | ||
625 | case CUPS_MOVE_JOB : | |
626 | move_job(con, uri); | |
627 | break; | |
628 | ||
629 | case CUPS_AUTHENTICATE_JOB : | |
630 | authenticate_job(con, uri); | |
631 | break; | |
632 | ||
633 | case IPP_CREATE_PRINTER_SUBSCRIPTION : | |
634 | case IPP_CREATE_JOB_SUBSCRIPTION : | |
635 | create_subscription(con, uri); | |
636 | break; | |
637 | ||
638 | case IPP_GET_SUBSCRIPTION_ATTRIBUTES : | |
639 | get_subscription_attrs(con, sub_id); | |
640 | break; | |
641 | ||
642 | case IPP_GET_SUBSCRIPTIONS : | |
643 | get_subscriptions(con, uri); | |
644 | break; | |
645 | ||
646 | case IPP_RENEW_SUBSCRIPTION : | |
647 | renew_subscription(con, sub_id); | |
648 | break; | |
649 | ||
650 | case IPP_CANCEL_SUBSCRIPTION : | |
651 | cancel_subscription(con, sub_id); | |
652 | break; | |
653 | ||
654 | case IPP_GET_NOTIFICATIONS : | |
bd7854cb | 655 | get_notifications(con); |
ef416fc2 | 656 | break; |
657 | ||
658 | default : | |
659 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
660 | "%04X %s Operation %04X (%s) not supported", | |
661 | IPP_OPERATION_NOT_SUPPORTED, con->http.hostname, | |
662 | con->request->request.op.operation_id, | |
663 | ippOpString(con->request->request.op.operation_id)); | |
664 | ||
665 | send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED, | |
666 | _("%s not supported!"), | |
bc44d920 | 667 | ippOpString( |
668 | con->request->request.op.operation_id)); | |
ef416fc2 | 669 | break; |
670 | } | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | if (con->response) | |
676 | { | |
677 | /* | |
678 | * Sending data from the scheduler... | |
679 | */ | |
680 | ||
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 | ||
5bd77a73 | 686 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE)) |
ef416fc2 | 687 | { |
f301802f | 688 | #ifdef CUPSD_USE_CHUNKING |
689 | /* | |
690 | * Because older versions of CUPS (1.1.17 and older) and some IPP | |
d09495fa | 691 | * clients do not implement chunking properly, we cannot use |
f301802f | 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 | ||
ef416fc2 | 697 | if (con->http.version == HTTP_1_1) |
698 | { | |
07725fee | 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); | |
d09495fa | 704 | |
705 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
ef416fc2 | 706 | } |
707 | else | |
f301802f | 708 | #endif /* CUPSD_USE_CHUNKING */ |
ef416fc2 | 709 | { |
d09495fa | 710 | size_t length; /* Length of response */ |
ef416fc2 | 711 | |
d09495fa | 712 | |
713 | length = ippLength(con->response); | |
ef416fc2 | 714 | |
b94498cf | 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 | ||
07725fee | 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); | |
d09495fa | 730 | |
731 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
732 | con->http.data_remaining = length; | |
b94498cf | 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; | |
ef416fc2 | 738 | } |
739 | ||
f7deaa1a | 740 | cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient, |
741 | (cupsd_selfunc_t)cupsdWriteClient, con); | |
ef416fc2 | 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 | ||
09a101d6 | 770 | /* |
771 | * 'cupsdTimeoutJob()' - Timeout a job waiting on job files. | |
772 | */ | |
773 | ||
91c84a35 | 774 | int /* O - 0 on success, -1 on error */ |
09a101d6 | 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 | ||
91c84a35 MS |
802 | if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0) |
803 | return (-1); | |
09a101d6 | 804 | |
805 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
806 | } | |
91c84a35 MS |
807 | |
808 | return (0); | |
09a101d6 | 809 | } |
810 | ||
811 | ||
ef416fc2 | 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 */ | |
bc44d920 | 821 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 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 | ||
f7deaa1a | 832 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 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 | { | |
f899b121 | 849 | send_http_error(con, status, printer); |
ef416fc2 | 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) | |
f7deaa1a | 863 | { |
3dfe78b3 | 864 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
f7deaa1a | 865 | |
866 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").", | |
867 | printer->name, get_username(con)); | |
868 | } | |
ef416fc2 | 869 | else |
f7deaa1a | 870 | { |
3dfe78b3 | 871 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 872 | |
bc44d920 | 873 | cupsdLogMessage(CUPSD_LOG_INFO, |
874 | "Printer \"%s\" now accepting jobs (\"%s\").", | |
f7deaa1a | 875 | printer->name, get_username(con)); |
876 | } | |
ef416fc2 | 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 */ | |
ef416fc2 | 904 | ipp_attribute_t *attr; /* Printer attribute */ |
905 | int modify; /* Non-zero if we just modified */ | |
b423cd4c | 906 | char newname[IPP_MAX_NAME]; /* New class name */ |
e00b005a | 907 | int need_restart_job; /* Need to restart job? */ |
ef416fc2 | 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 | ||
a4d04587 | 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)); | |
ef416fc2 | 920 | |
921 | ||
fa73b229 | 922 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) |
ef416fc2 | 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 | ||
ef416fc2 | 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 && | |
bc44d920 | 961 | !(pclass->type & CUPS_PRINTER_DISCOVERED)) |
ef416fc2 | 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 | /* | |
2e4ff8af | 974 | * No, check the default policy and then add the class... |
ef416fc2 | 975 | */ |
976 | ||
2e4ff8af MS |
977 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
978 | { | |
979 | send_http_error(con, status, NULL); | |
980 | return; | |
981 | } | |
982 | ||
ef416fc2 | 983 | pclass = cupsdAddClass(resource + 9); |
984 | modify = 0; | |
985 | } | |
986 | else if (pclass->type & CUPS_PRINTER_IMPLICIT) | |
987 | { | |
988 | /* | |
2fb76298 | 989 | * Check the default policy, then rename the implicit class to "AnyClass" |
2e4ff8af | 990 | * or remove it... |
ef416fc2 | 991 | */ |
992 | ||
2e4ff8af MS |
993 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
994 | { | |
995 | send_http_error(con, status, NULL); | |
996 | return; | |
997 | } | |
998 | ||
ef416fc2 | 999 | if (ImplicitAnyClasses) |
1000 | { | |
b423cd4c | 1001 | snprintf(newname, sizeof(newname), "Any%s", resource + 9); |
1002 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 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 | } | |
bc44d920 | 1014 | else if (pclass->type & CUPS_PRINTER_DISCOVERED) |
ef416fc2 | 1015 | { |
1016 | /* | |
2e4ff8af | 1017 | * Check the default policy, then rename the remote class to "Class"... |
ef416fc2 | 1018 | */ |
1019 | ||
2e4ff8af MS |
1020 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
1021 | { | |
1022 | send_http_error(con, status, NULL); | |
1023 | return; | |
1024 | } | |
1025 | ||
b423cd4c | 1026 | snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname); |
1027 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 1028 | |
1029 | /* | |
1030 | * Add the class as a new local class... | |
1031 | */ | |
1032 | ||
1033 | pclass = cupsdAddClass(resource + 9); | |
1034 | modify = 0; | |
1035 | } | |
2e4ff8af MS |
1036 | else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con, |
1037 | NULL)) != HTTP_OK) | |
1038 | { | |
2fb76298 | 1039 | send_http_error(con, status, pclass); |
2e4ff8af MS |
1040 | return; |
1041 | } | |
ef416fc2 | 1042 | else |
1043 | modify = 1; | |
1044 | ||
1045 | /* | |
1046 | * Look for attributes and copy them over as needed... | |
1047 | */ | |
1048 | ||
e00b005a | 1049 | need_restart_job = 0; |
1050 | ||
fa73b229 | 1051 | if ((attr = ippFindAttribute(con->request, "printer-location", |
1052 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1053 | cupsdSetString(&pclass->location, attr->values[0].string.text); |
1054 | ||
fa73b229 | 1055 | if ((attr = ippFindAttribute(con->request, "printer-info", |
1056 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1057 | cupsdSetString(&pclass->info, attr->values[0].string.text); |
1058 | ||
fa73b229 | 1059 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
1060 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 1061 | { |
bc44d920 | 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); | |
ef416fc2 | 1065 | |
1066 | pclass->accepting = attr->values[0].boolean; | |
1067 | cupsdAddPrinterHistory(pclass); | |
1068 | } | |
1069 | ||
fa73b229 | 1070 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
1071 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 1072 | { |
1073 | if (pclass->shared && !attr->values[0].boolean) | |
f7deaa1a | 1074 | cupsdDeregisterPrinter(pclass, 1); |
ef416fc2 | 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 | ||
fa73b229 | 1083 | if ((attr = ippFindAttribute(con->request, "printer-state", |
1084 | IPP_TAG_ENUM)) != NULL) | |
ef416fc2 | 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 | ||
bc44d920 | 1095 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
1096 | pclass->name, attr->values[0].integer, pclass->state); | |
ef416fc2 | 1097 | |
1098 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
1099 | cupsdStopPrinter(pclass, 0); | |
1100 | else | |
e00b005a | 1101 | { |
ef416fc2 | 1102 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); |
e00b005a | 1103 | need_restart_job = 1; |
1104 | } | |
ef416fc2 | 1105 | } |
fa73b229 | 1106 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
1107 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 1108 | { |
1109 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
1110 | sizeof(pclass->state_message)); | |
1111 | cupsdAddPrinterHistory(pclass); | |
1112 | } | |
fa73b229 | 1113 | if ((attr = ippFindAttribute(con->request, "member-uris", |
1114 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 1115 | { |
1116 | /* | |
1117 | * Clear the printer array as needed... | |
1118 | */ | |
1119 | ||
e00b005a | 1120 | need_restart_job = 1; |
1121 | ||
ef416fc2 | 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 | ||
f7deaa1a | 1138 | if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member)) |
ef416fc2 | 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 | ||
b423cd4c | 1157 | set_printer_defaults(con, pclass); |
1158 | ||
09a101d6 | 1159 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
1160 | IPP_TAG_KEYWORD)) != NULL) | |
1161 | cupsdSetAuthInfoRequired(pclass, NULL, attr); | |
1162 | ||
ef416fc2 | 1163 | /* |
1164 | * Update the printer class attributes and return... | |
1165 | */ | |
1166 | ||
1167 | cupsdSetPrinterAttrs(pclass); | |
3dfe78b3 | 1168 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
e00b005a | 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); | |
bd7854cb | 1181 | |
e00b005a | 1182 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 1183 | job->state_value = IPP_JOB_PENDING; |
e00b005a | 1184 | } |
1185 | ||
1186 | if (need_restart_job) | |
1187 | cupsdCheckJobs(); | |
ef416fc2 | 1188 | |
3dfe78b3 | 1189 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 1190 | |
1191 | if (modify) | |
1192 | { | |
1193 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL, | |
1194 | "Class \"%s\" modified by \"%s\".", pclass->name, | |
e00b005a | 1195 | get_username(con)); |
ef416fc2 | 1196 | |
1197 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
e00b005a | 1198 | pclass->name, get_username(con)); |
ef416fc2 | 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, | |
e00b005a | 1206 | get_username(con)); |
ef416fc2 | 1207 | |
1208 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
e00b005a | 1209 | pclass->name, get_username(con)); |
ef416fc2 | 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, | |
bc44d920 | 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); | |
ef416fc2 | 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 | ||
fa73b229 | 1253 | if (!compressions || !filetypes) |
ef416fc2 | 1254 | { |
07725fee | 1255 | cupsdCancelJob(job, 1, IPP_JOB_ABORTED); |
ef416fc2 | 1256 | |
09a101d6 | 1257 | if (con) |
1258 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1259 | _("Unable to allocate memory for file types!")); | |
1260 | ||
ef416fc2 | 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 | ||
3dfe78b3 MS |
1271 | job->dirty = 1; |
1272 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
1273 | ||
ef416fc2 | 1274 | return (0); |
1275 | } | |
1276 | ||
1277 | ||
1278 | /* | |
b423cd4c | 1279 | * 'add_job()' - Add a job to a print queue. |
ef416fc2 | 1280 | */ |
1281 | ||
b423cd4c | 1282 | static cupsd_job_t * /* O - Job object */ |
1283 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
f7deaa1a | 1284 | cupsd_printer_t *printer, /* I - Destination printer */ |
80ca4592 | 1285 | mime_type_t *filetype) /* I - First print file type, if any */ |
ef416fc2 | 1286 | { |
b423cd4c | 1287 | http_status_t status; /* Policy status */ |
f7deaa1a | 1288 | ipp_attribute_t *attr, /* Current attribute */ |
1289 | *auth_info; /* auth-info attribute */ | |
b423cd4c | 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 */ | |
f7deaa1a | 1294 | char job_uri[HTTP_MAX_URI]; /* Job URI */ |
b423cd4c | 1295 | int kbytes; /* Size of print file */ |
1296 | int i; /* Looping var */ | |
1297 | int lowerpagerange; /* Page range bound */ | |
ef416fc2 | 1298 | |
1299 | ||
f7deaa1a | 1300 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))", |
1301 | con, con->http.fd, printer, printer->name, | |
0a682745 MS |
1302 | filetype, filetype ? filetype->super : "none", |
1303 | filetype ? filetype->type : "none"); | |
ef416fc2 | 1304 | |
b423cd4c | 1305 | /* |
1306 | * Check remote printing to non-shared printer... | |
1307 | */ | |
ef416fc2 | 1308 | |
b423cd4c | 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 | } | |
ef416fc2 | 1317 | |
b423cd4c | 1318 | /* |
1319 | * Check policy... | |
1320 | */ | |
ef416fc2 | 1321 | |
f899b121 | 1322 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
7594b224 | 1323 | |
b423cd4c | 1324 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
1325 | { | |
f899b121 | 1326 | send_http_error(con, status, printer); |
b423cd4c | 1327 | return (NULL); |
1328 | } | |
3dfe78b3 MS |
1329 | else if (printer->num_auth_info_required > 0 && |
1330 | strcmp(printer->auth_info_required[0], "none") && | |
7594b224 | 1331 | !con->username[0] && !auth_info) |
b423cd4c | 1332 | { |
f899b121 | 1333 | send_http_error(con, HTTP_UNAUTHORIZED, printer); |
b423cd4c | 1334 | return (NULL); |
1335 | } | |
7594b224 | 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 | ||
f899b121 | 1344 | send_http_error(con, HTTP_UPGRADE_REQUIRED, printer); |
7594b224 | 1345 | return (NULL); |
1346 | } | |
1347 | #endif /* HAVE_SSL */ | |
ef416fc2 | 1348 | |
b423cd4c | 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."), | |
f7deaa1a | 1357 | printer->name); |
b423cd4c | 1358 | return (NULL); |
ef416fc2 | 1359 | } |
ef416fc2 | 1360 | |
b423cd4c | 1361 | /* |
80ca4592 | 1362 | * Validate job template attributes; for now just document-format, |
f7deaa1a | 1363 | * copies, number-up, and page-ranges... |
b423cd4c | 1364 | */ |
ef416fc2 | 1365 | |
80ca4592 | 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 | ||
b423cd4c | 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 | } | |
ef416fc2 | 1397 | |
3dfe78b3 MS |
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 | ||
f7deaa1a | 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 | ||
b423cd4c | 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 | { | |
f7deaa1a | 1449 | if (attr->values[i].range.lower < lowerpagerange || |
b423cd4c | 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 | } | |
ef416fc2 | 1458 | |
b423cd4c | 1459 | lowerpagerange = attr->values[i].range.upper + 1; |
1460 | } | |
1461 | } | |
ef416fc2 | 1462 | |
1463 | /* | |
b423cd4c | 1464 | * Make sure we aren't over our limit... |
ef416fc2 | 1465 | */ |
1466 | ||
b423cd4c | 1467 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
1468 | cupsdCleanJobs(); | |
ef416fc2 | 1469 | |
f7deaa1a | 1470 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
b423cd4c | 1471 | { |
1472 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
1473 | _("Too many active jobs.")); | |
1474 | return (NULL); | |
1475 | } | |
1476 | ||
bc44d920 | 1477 | if ((i = check_quotas(con, printer)) < 0) |
b423cd4c | 1478 | { |
1479 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1480 | return (NULL); | |
1481 | } | |
bc44d920 | 1482 | else if (i == 0) |
1483 | { | |
1484 | send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print.")); | |
1485 | return (NULL); | |
1486 | } | |
ef416fc2 | 1487 | |
1488 | /* | |
b423cd4c | 1489 | * Create the job and set things up... |
ef416fc2 | 1490 | */ |
1491 | ||
b423cd4c | 1492 | if ((attr = ippFindAttribute(con->request, "job-priority", |
1493 | IPP_TAG_INTEGER)) != NULL) | |
1494 | priority = attr->values[0].integer; | |
1495 | else | |
ef416fc2 | 1496 | { |
b423cd4c | 1497 | if ((val = cupsGetOption("job-priority", printer->num_options, |
1498 | printer->options)) != NULL) | |
1499 | priority = atoi(val); | |
1500 | else | |
1501 | priority = 50; | |
ef416fc2 | 1502 | |
b423cd4c | 1503 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", |
1504 | priority); | |
1505 | } | |
ef416fc2 | 1506 | |
b423cd4c | 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"); | |
ef416fc2 | 1513 | |
b423cd4c | 1514 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) |
1515 | { | |
1516 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
f7deaa1a | 1517 | _("Unable to add job for destination \"%s\"!"), |
1518 | printer->name); | |
b423cd4c | 1519 | return (NULL); |
1520 | } | |
ef416fc2 | 1521 | |
f7deaa1a | 1522 | job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT | |
1523 | CUPS_PRINTER_REMOTE); | |
b423cd4c | 1524 | job->attrs = con->request; |
3dfe78b3 | 1525 | job->dirty = 1; |
2abf387c | 1526 | con->request = ippNewRequest(job->attrs->request.op.operation_id); |
ef416fc2 | 1527 | |
3dfe78b3 MS |
1528 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); |
1529 | ||
b423cd4c | 1530 | add_job_uuid(con, job); |
1531 | apply_printer_defaults(printer, job); | |
ef416fc2 | 1532 | |
b423cd4c | 1533 | attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME); |
ef416fc2 | 1534 | |
b423cd4c | 1535 | if (con->username[0]) |
1536 | { | |
1537 | cupsdSetString(&job->username, con->username); | |
ef416fc2 | 1538 | |
1539 | if (attr) | |
b423cd4c | 1540 | cupsdSetString(&attr->values[0].string.text, con->username); |
ef416fc2 | 1541 | } |
b423cd4c | 1542 | else if (attr) |
1543 | { | |
1544 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1545 | "add_job: requesting-user-name=\"%s\"", | |
1546 | attr->values[0].string.text); | |
ef416fc2 | 1547 | |
b423cd4c | 1548 | cupsdSetString(&job->username, attr->values[0].string.text); |
1549 | } | |
1550 | else | |
1551 | cupsdSetString(&job->username, "anonymous"); | |
ef416fc2 | 1552 | |
b423cd4c | 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; | |
757d2cad | 1559 | _cupsStrFree(attr->name); |
1560 | attr->name = _cupsStrAlloc("job-originating-user-name"); | |
b423cd4c | 1561 | } |
ef416fc2 | 1562 | |
f7deaa1a | 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) | |
a4924f6c | 1572 | ippDeleteAttribute(job->attrs, auth_info); |
f7deaa1a | 1573 | } |
1574 | ||
b423cd4c | 1575 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", |
1576 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 1577 | { |
b423cd4c | 1578 | /* |
1579 | * Request contains a job-originating-host-name attribute; validate it... | |
1580 | */ | |
ef416fc2 | 1581 | |
b423cd4c | 1582 | if (attr->value_tag != IPP_TAG_NAME || |
1583 | attr->num_values != 1 || | |
1584 | strcmp(con->http.hostname, "localhost")) | |
ef416fc2 | 1585 | { |
1586 | /* | |
b423cd4c | 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. | |
ef416fc2 | 1589 | */ |
1590 | ||
b423cd4c | 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 | */ | |
ef416fc2 | 1607 | |
b423cd4c | 1608 | for (i = 0; i < attr->num_values; i ++) |
1609 | { | |
757d2cad | 1610 | _cupsStrFree(attr->values[i].string.text); |
b423cd4c | 1611 | attr->values[i].string.text = NULL; |
1612 | if (attr->values[i].string.charset) | |
1613 | { | |
757d2cad | 1614 | _cupsStrFree(attr->values[i].string.charset); |
b423cd4c | 1615 | attr->values[i].string.charset = NULL; |
1616 | } | |
1617 | } | |
ef416fc2 | 1618 | |
b423cd4c | 1619 | default : |
1620 | break; | |
1621 | } | |
ef416fc2 | 1622 | |
b423cd4c | 1623 | /* |
1624 | * Use the default connection hostname instead... | |
1625 | */ | |
bd7854cb | 1626 | |
b423cd4c | 1627 | attr->value_tag = IPP_TAG_NAME; |
1628 | attr->num_values = 1; | |
757d2cad | 1629 | attr->values[0].string.text = _cupsStrAlloc(con->http.hostname); |
b423cd4c | 1630 | } |
bd7854cb | 1631 | |
b423cd4c | 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 | */ | |
bd7854cb | 1640 | |
f7deaa1a | 1641 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, |
b423cd4c | 1642 | "job-originating-host-name", NULL, con->http.hostname); |
1643 | } | |
bd7854cb | 1644 | |
b423cd4c | 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; | |
bd7854cb | 1653 | |
1654 | /* | |
b423cd4c | 1655 | * Add remaining job attributes... |
bd7854cb | 1656 | */ |
1657 | ||
b423cd4c | 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); | |
d09495fa | 1661 | job->state_value = (ipp_jstate_t)job->state->values[0].integer; |
b423cd4c | 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); | |
bd7854cb | 1668 | |
b423cd4c | 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); | |
bd7854cb | 1675 | |
b423cd4c | 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) | |
ef416fc2 | 1680 | { |
b423cd4c | 1681 | if ((val = cupsGetOption("job-hold-until", printer->num_options, |
1682 | printer->options)) == NULL) | |
1683 | val = "no-hold"; | |
ef416fc2 | 1684 | |
b423cd4c | 1685 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1686 | "job-hold-until", NULL, val); | |
ef416fc2 | 1687 | } |
b423cd4c | 1688 | if (attr && strcmp(attr->values[0].string.text, "no-hold") && |
1689 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 1690 | { |
1691 | /* | |
b423cd4c | 1692 | * Hold job until specified time... |
ef416fc2 | 1693 | */ |
1694 | ||
b423cd4c | 1695 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
e53920b9 | 1696 | |
1697 | job->state->values[0].integer = IPP_JOB_HELD; | |
1698 | job->state_value = IPP_JOB_HELD; | |
ef416fc2 | 1699 | } |
b423cd4c | 1700 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) |
ef416fc2 | 1701 | { |
e53920b9 | 1702 | job->hold_until = time(NULL) + 60; |
b423cd4c | 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; | |
ef416fc2 | 1710 | } |
1711 | ||
b423cd4c | 1712 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
1713 | Classification) | |
ef416fc2 | 1714 | { |
1715 | /* | |
b423cd4c | 1716 | * Add job sheets options... |
ef416fc2 | 1717 | */ |
1718 | ||
b423cd4c | 1719 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1720 | IPP_TAG_ZERO)) == NULL) | |
ef416fc2 | 1721 | { |
b423cd4c | 1722 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1723 | "Adding default job-sheets values \"%s,%s\"...", | |
1724 | printer->job_sheets[0], printer->job_sheets[1]); | |
ef416fc2 | 1725 | |
b423cd4c | 1726 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", |
1727 | 2, NULL, NULL); | |
757d2cad | 1728 | attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]); |
1729 | attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]); | |
ef416fc2 | 1730 | } |
1731 | ||
b423cd4c | 1732 | job->job_sheets = attr; |
ef416fc2 | 1733 | |
ef416fc2 | 1734 | /* |
b423cd4c | 1735 | * Enforce classification level if set... |
ef416fc2 | 1736 | */ |
1737 | ||
b423cd4c | 1738 | if (Classification) |
ef416fc2 | 1739 | { |
b423cd4c | 1740 | cupsdLogMessage(CUPSD_LOG_INFO, |
1741 | "Classification=\"%s\", ClassifyOverride=%d", | |
1742 | Classification ? Classification : "(null)", | |
1743 | ClassifyOverride); | |
ef416fc2 | 1744 | |
b423cd4c | 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 | */ | |
ef416fc2 | 1754 | |
b423cd4c | 1755 | cupsdSetString(&attr->values[0].string.text, Classification); |
e00b005a | 1756 | |
b423cd4c | 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 | */ | |
ef416fc2 | 1771 | |
b423cd4c | 1772 | cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text); |
ef416fc2 | 1773 | |
b423cd4c | 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))) | |
ef416fc2 | 1804 | { |
1805 | /* | |
b423cd4c | 1806 | * Force the banner to have the classification on it... |
ef416fc2 | 1807 | */ |
1808 | ||
b423cd4c | 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); | |
ef416fc2 | 1820 | |
b423cd4c | 1821 | if (attr->num_values > 1 && |
1822 | strcmp(attr->values[1].string.text, "none")) | |
1823 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1824 | } | |
ef416fc2 | 1825 | |
b423cd4c | 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); | |
ef416fc2 | 1839 | } |
1840 | } | |
1841 | ||
b423cd4c | 1842 | /* |
1843 | * See if we need to add the starting sheet... | |
1844 | */ | |
ef416fc2 | 1845 | |
b423cd4c | 1846 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
ef416fc2 | 1847 | { |
b423cd4c | 1848 | cupsdLogMessage(CUPSD_LOG_INFO, |
09a101d6 | 1849 | "[Job %d] Adding start banner page \"%s\".", |
1850 | job->id, attr->values[0].string.text); | |
ef416fc2 | 1851 | |
91c84a35 | 1852 | if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0) |
3dfe78b3 MS |
1853 | { |
1854 | cupsdDeleteJob(job); | |
91c84a35 | 1855 | return (NULL); |
3dfe78b3 | 1856 | } |
ef416fc2 | 1857 | |
b423cd4c | 1858 | cupsdUpdateQuota(printer, job->username, 0, kbytes); |
1859 | } | |
ef416fc2 | 1860 | } |
b423cd4c | 1861 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1862 | IPP_TAG_ZERO)) != NULL) | |
1863 | job->sheets = attr; | |
ef416fc2 | 1864 | |
b423cd4c | 1865 | /* |
1866 | * Fill in the response info... | |
1867 | */ | |
ef416fc2 | 1868 | |
b423cd4c | 1869 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, |
1870 | LocalPort, job->id); | |
ef416fc2 | 1871 | |
b423cd4c | 1872 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
1873 | job_uri); | |
ef416fc2 | 1874 | |
b423cd4c | 1875 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
ef416fc2 | 1876 | |
b423cd4c | 1877 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", |
1878 | job->state_value); | |
1879 | add_job_state_reasons(con, job); | |
ef416fc2 | 1880 | |
b423cd4c | 1881 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 1882 | |
b423cd4c | 1883 | /* |
1884 | * Add any job subscriptions... | |
1885 | */ | |
ef416fc2 | 1886 | |
b423cd4c | 1887 | add_job_subscriptions(con, job); |
ef416fc2 | 1888 | |
b423cd4c | 1889 | /* |
1890 | * Set all but the first two attributes to the job attributes group... | |
1891 | */ | |
ef416fc2 | 1892 | |
b423cd4c | 1893 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) |
1894 | attr->group_tag = IPP_TAG_JOB; | |
ef416fc2 | 1895 | |
1896 | /* | |
b423cd4c | 1897 | * Fire the "job created" event... |
ef416fc2 | 1898 | */ |
1899 | ||
b423cd4c | 1900 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); |
ef416fc2 | 1901 | |
1902 | /* | |
b423cd4c | 1903 | * Return the new job... |
ef416fc2 | 1904 | */ |
1905 | ||
b423cd4c | 1906 | return (job); |
1907 | } | |
ef416fc2 | 1908 | |
ef416fc2 | 1909 | |
b423cd4c | 1910 | /* |
1911 | * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based | |
1912 | * upon the job and printer state... | |
1913 | */ | |
ef416fc2 | 1914 | |
b423cd4c | 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 */ | |
ef416fc2 | 1921 | |
ef416fc2 | 1922 | |
b423cd4c | 1923 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)", |
1924 | con, con->http.fd, job ? job->id : 0); | |
ef416fc2 | 1925 | |
d09495fa | 1926 | switch (job ? job->state_value : IPP_JOB_CANCELED) |
ef416fc2 | 1927 | { |
b423cd4c | 1928 | case IPP_JOB_PENDING : |
80ca4592 | 1929 | dest = cupsdFindDest(job->dest); |
bd7854cb | 1930 | |
b423cd4c | 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; | |
ef416fc2 | 1938 | |
b423cd4c | 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; | |
ef416fc2 | 1950 | |
b423cd4c | 1951 | case IPP_JOB_PROCESSING : |
1952 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1953 | "job-state-reasons", NULL, "job-printing"); | |
1954 | break; | |
ef416fc2 | 1955 | |
b423cd4c | 1956 | case IPP_JOB_STOPPED : |
1957 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1958 | "job-state-reasons", NULL, "job-stopped"); | |
1959 | break; | |
ef416fc2 | 1960 | |
d09495fa | 1961 | case IPP_JOB_CANCELED : |
b423cd4c | 1962 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1963 | "job-state-reasons", NULL, "job-canceled-by-user"); | |
1964 | break; | |
ef416fc2 | 1965 | |
b423cd4c | 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; | |
ef416fc2 | 1970 | |
b423cd4c | 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; | |
ef416fc2 | 1975 | } |
ef416fc2 | 1976 | } |
1977 | ||
1978 | ||
1979 | /* | |
b423cd4c | 1980 | * 'add_job_subscriptions()' - Add any subcriptions for a job. |
ef416fc2 | 1981 | */ |
1982 | ||
1983 | static void | |
b423cd4c | 1984 | add_job_subscriptions( |
1985 | cupsd_client_t *con, /* I - Client connection */ | |
1986 | cupsd_job_t *job) /* I - Newly created job */ | |
ef416fc2 | 1987 | { |
b423cd4c | 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 */ | |
ef416fc2 | 1998 | |
ef416fc2 | 1999 | |
b423cd4c | 2000 | /* |
2001 | * Find the first subscription group attribute; return if we have | |
2002 | * none... | |
2003 | */ | |
ef416fc2 | 2004 | |
b423cd4c | 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; | |
ef416fc2 | 2010 | |
b423cd4c | 2011 | if (!attr) |
2012 | return; | |
ef416fc2 | 2013 | |
b423cd4c | 2014 | /* |
2015 | * Process the subscription attributes in the request... | |
2016 | */ | |
ef416fc2 | 2017 | |
b423cd4c | 2018 | while (attr) |
2019 | { | |
2020 | recipient = NULL; | |
2021 | pullmethod = NULL; | |
2022 | user_data = NULL; | |
2023 | interval = 0; | |
2024 | mask = CUPSD_EVENT_NONE; | |
ef416fc2 | 2025 | |
b423cd4c | 2026 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
2027 | { | |
f899b121 | 2028 | if (!strcmp(attr->name, "notify-recipient-uri") && |
b423cd4c | 2029 | attr->value_tag == IPP_TAG_URI) |
3dfe78b3 MS |
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 | ||
b423cd4c | 2044 | recipient = attr->values[0].string.text; |
3dfe78b3 MS |
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 | } | |
b423cd4c | 2080 | else if (!strcmp(attr->name, "notify-pull-method") && |
2081 | attr->value_tag == IPP_TAG_KEYWORD) | |
3dfe78b3 | 2082 | { |
b423cd4c | 2083 | pullmethod = attr->values[0].string.text; |
3dfe78b3 MS |
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 | } | |
b423cd4c | 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 | } | |
ef416fc2 | 2124 | |
b423cd4c | 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; | |
ef416fc2 | 2143 | |
b423cd4c | 2144 | attr = attr->next; |
2145 | } | |
ef416fc2 | 2146 | |
b423cd4c | 2147 | if (!recipient && !pullmethod) |
2148 | break; | |
ef416fc2 | 2149 | |
b423cd4c | 2150 | if (mask == CUPSD_EVENT_NONE) |
2151 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
ef416fc2 | 2152 | |
b423cd4c | 2153 | sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient, |
2154 | 0); | |
ef416fc2 | 2155 | |
b423cd4c | 2156 | sub->interval = interval; |
ef416fc2 | 2157 | |
b423cd4c | 2158 | cupsdSetString(&sub->owner, job->username); |
ef416fc2 | 2159 | |
b423cd4c | 2160 | if (user_data) |
ef416fc2 | 2161 | { |
b423cd4c | 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); | |
ef416fc2 | 2165 | } |
2166 | ||
b423cd4c | 2167 | ippAddSeparator(con->response); |
2168 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
2169 | "notify-subscription-id", sub->id); | |
ef416fc2 | 2170 | |
b423cd4c | 2171 | if (attr) |
2172 | attr = attr->next; | |
ef416fc2 | 2173 | } |
2174 | ||
3dfe78b3 | 2175 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
b423cd4c | 2176 | |
ef416fc2 | 2177 | /* |
b423cd4c | 2178 | * Remove all of the subscription attributes from the job request... |
ef416fc2 | 2179 | */ |
2180 | ||
b423cd4c | 2181 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) |
ef416fc2 | 2182 | { |
b423cd4c | 2183 | next = attr->next; |
ef416fc2 | 2184 | |
b423cd4c | 2185 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || |
2186 | attr->group_tag == IPP_TAG_ZERO) | |
2187 | { | |
2188 | /* | |
2189 | * Free and remove this attribute... | |
2190 | */ | |
ef416fc2 | 2191 | |
757d2cad | 2192 | _ippFreeAttr(attr); |
ef416fc2 | 2193 | |
b423cd4c | 2194 | if (prev) |
2195 | prev->next = next; | |
2196 | else | |
2197 | job->attrs->attrs = next; | |
2198 | } | |
2199 | else | |
2200 | prev = attr; | |
ef416fc2 | 2201 | } |
2202 | ||
b423cd4c | 2203 | job->attrs->last = prev; |
2204 | job->attrs->current = prev; | |
2205 | } | |
ef416fc2 | 2206 | |
ef416fc2 | 2207 | |
b423cd4c | 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 */ | |
b423cd4c | 2219 | _cups_md5_state_t md5state; /* MD5 state */ |
2220 | unsigned char md5sum[16]; /* MD5 digest/sum */ | |
ef416fc2 | 2221 | |
ef416fc2 | 2222 | |
2223 | /* | |
b423cd4c | 2224 | * First see if the job already has a job-uuid attribute; if so, return... |
ef416fc2 | 2225 | */ |
2226 | ||
d09495fa | 2227 | if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) |
b423cd4c | 2228 | return; |
ef416fc2 | 2229 | |
2230 | /* | |
b423cd4c | 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... | |
ef416fc2 | 2235 | */ |
2236 | ||
b423cd4c | 2237 | snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername, |
2238 | con->serverport, job->id); | |
ef416fc2 | 2239 | |
757d2cad | 2240 | _cupsMD5Init(&md5state); |
2241 | _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid)); | |
2242 | _cupsMD5Finish(&md5state, md5sum); | |
ef416fc2 | 2243 | |
2244 | /* | |
b423cd4c | 2245 | * Format the UUID URI using the MD5 sum and job ID. |
ef416fc2 | 2246 | */ |
2247 | ||
b423cd4c | 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]); | |
ef416fc2 | 2255 | |
b423cd4c | 2256 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid); |
ef416fc2 | 2257 | } |
2258 | ||
2259 | ||
2260 | /* | |
b423cd4c | 2261 | * 'add_printer()' - Add a printer to the system. |
ef416fc2 | 2262 | */ |
2263 | ||
2264 | static void | |
b423cd4c | 2265 | add_printer(cupsd_client_t *con, /* I - Client connection */ |
2266 | ipp_attribute_t *uri) /* I - URI of printer */ | |
ef416fc2 | 2267 | { |
2268 | http_status_t status; /* Policy status */ | |
b423cd4c | 2269 | int i; /* Looping var */ |
09a101d6 | 2270 | char scheme[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2271 | username[HTTP_MAX_URI], /* Username portion of URI */ |
ef416fc2 | 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 */ | |
b423cd4c | 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? */ | |
09a101d6 | 2284 | int set_device_uri, /* Did we set the device URI? */ |
2285 | set_port_monitor; /* Did we set the port monitor? */ | |
ef416fc2 | 2286 | |
2287 | ||
b423cd4c | 2288 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, |
ef416fc2 | 2289 | con->http.fd, uri->values[0].string.text); |
2290 | ||
2291 | /* | |
b423cd4c | 2292 | * Do we have a valid URI? |
ef416fc2 | 2293 | */ |
2294 | ||
09a101d6 | 2295 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
2296 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 2297 | sizeof(host), &port, resource, sizeof(resource)); |
2298 | ||
2299 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
ef416fc2 | 2300 | { |
b423cd4c | 2301 | /* |
2302 | * No, return an error... | |
2303 | */ | |
2304 | ||
ef416fc2 | 2305 | send_ipp_status(con, IPP_BAD_REQUEST, |
b423cd4c | 2306 | _("The printer-uri must be of the form " |
2307 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
ef416fc2 | 2308 | return; |
2309 | } | |
2310 | ||
2311 | /* | |
b423cd4c | 2312 | * Do we have a valid printer name? |
ef416fc2 | 2313 | */ |
2314 | ||
b423cd4c | 2315 | if (!validate_name(resource + 10)) |
ef416fc2 | 2316 | { |
b423cd4c | 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; | |
ef416fc2 | 2325 | } |
ef416fc2 | 2326 | |
ef416fc2 | 2327 | /* |
b423cd4c | 2328 | * See if the printer already exists; if not, create a new printer... |
2329 | */ | |
ef416fc2 | 2330 | |
b423cd4c | 2331 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) |
ef416fc2 | 2332 | { |
2333 | /* | |
b423cd4c | 2334 | * Printer doesn't exist; see if we have a class of the same name... |
ef416fc2 | 2335 | */ |
2336 | ||
b423cd4c | 2337 | if ((printer = cupsdFindClass(resource + 10)) != NULL && |
bc44d920 | 2338 | !(printer->type & CUPS_PRINTER_DISCOVERED)) |
ef416fc2 | 2339 | { |
b423cd4c | 2340 | /* |
2341 | * Yes, return an error... | |
2342 | */ | |
ef416fc2 | 2343 | |
b423cd4c | 2344 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2345 | _("A class named \"%s\" already exists!"), | |
2346 | resource + 10); | |
ef416fc2 | 2347 | return; |
2348 | } | |
2349 | ||
2350 | /* | |
2e4ff8af | 2351 | * No, check the default policy then add the printer... |
ef416fc2 | 2352 | */ |
2353 | ||
2e4ff8af MS |
2354 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2355 | { | |
2356 | send_http_error(con, status, NULL); | |
2357 | return; | |
2358 | } | |
2359 | ||
b423cd4c | 2360 | printer = cupsdAddPrinter(resource + 10); |
2361 | modify = 0; | |
ef416fc2 | 2362 | } |
b423cd4c | 2363 | else if (printer->type & CUPS_PRINTER_IMPLICIT) |
ef416fc2 | 2364 | { |
2365 | /* | |
2e4ff8af MS |
2366 | * Check the default policy, then rename the implicit printer to |
2367 | * "AnyPrinter" or delete it... | |
ef416fc2 | 2368 | */ |
2369 | ||
2e4ff8af MS |
2370 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2371 | { | |
2372 | send_http_error(con, status, NULL); | |
2373 | return; | |
2374 | } | |
2375 | ||
b423cd4c | 2376 | if (ImplicitAnyClasses) |
ef416fc2 | 2377 | { |
b423cd4c | 2378 | snprintf(newname, sizeof(newname), "Any%s", resource + 10); |
2379 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2380 | } |
b423cd4c | 2381 | else |
2382 | cupsdDeletePrinter(printer, 1); | |
ef416fc2 | 2383 | |
2384 | /* | |
b423cd4c | 2385 | * Add the printer as a new local printer... |
ef416fc2 | 2386 | */ |
2387 | ||
b423cd4c | 2388 | printer = cupsdAddPrinter(resource + 10); |
2389 | modify = 0; | |
ef416fc2 | 2390 | } |
bc44d920 | 2391 | else if (printer->type & CUPS_PRINTER_DISCOVERED) |
b423cd4c | 2392 | { |
2393 | /* | |
2e4ff8af MS |
2394 | * Check the default policy, then rename the remote printer to |
2395 | * "Printer@server"... | |
b423cd4c | 2396 | */ |
ef416fc2 | 2397 | |
2e4ff8af MS |
2398 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2399 | { | |
2400 | send_http_error(con, status, NULL); | |
2401 | return; | |
2402 | } | |
2403 | ||
b423cd4c | 2404 | snprintf(newname, sizeof(newname), "%s@%s", resource + 10, |
2405 | printer->hostname); | |
2406 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2407 | |
b423cd4c | 2408 | /* |
2409 | * Add the printer as a new local printer... | |
2410 | */ | |
ef416fc2 | 2411 | |
b423cd4c | 2412 | printer = cupsdAddPrinter(resource + 10); |
2413 | modify = 0; | |
2414 | } | |
2e4ff8af MS |
2415 | else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
2416 | NULL)) != HTTP_OK) | |
2417 | { | |
2fb76298 | 2418 | send_http_error(con, status, printer); |
2e4ff8af MS |
2419 | return; |
2420 | } | |
b423cd4c | 2421 | else |
2422 | modify = 1; | |
ef416fc2 | 2423 | |
b423cd4c | 2424 | /* |
2425 | * Look for attributes and copy them over as needed... | |
2426 | */ | |
ef416fc2 | 2427 | |
b423cd4c | 2428 | need_restart_job = 0; |
ef416fc2 | 2429 | |
b423cd4c | 2430 | if ((attr = ippFindAttribute(con->request, "printer-location", |
2431 | IPP_TAG_TEXT)) != NULL) | |
2432 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
ef416fc2 | 2433 | |
b423cd4c | 2434 | if ((attr = ippFindAttribute(con->request, "printer-info", |
2435 | IPP_TAG_TEXT)) != NULL) | |
2436 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
ef416fc2 | 2437 | |
09a101d6 | 2438 | set_device_uri = 0; |
2439 | ||
b423cd4c | 2440 | if ((attr = ippFindAttribute(con->request, "device-uri", |
2441 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 2442 | { |
2443 | /* | |
b423cd4c | 2444 | * Do we have a valid device URI? |
ef416fc2 | 2445 | */ |
2446 | ||
2fb76298 MS |
2447 | http_uri_status_t uri_status; /* URI separation status */ |
2448 | ||
2449 | ||
b423cd4c | 2450 | need_restart_job = 1; |
ef416fc2 | 2451 | |
2fb76298 MS |
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 | } | |
b423cd4c | 2467 | |
09a101d6 | 2468 | if (!strcmp(scheme, "file")) |
ef416fc2 | 2469 | { |
2470 | /* | |
b423cd4c | 2471 | * See if the administrator has enabled file devices... |
ef416fc2 | 2472 | */ |
2473 | ||
b423cd4c | 2474 | if (!FileDevice && strcmp(resource, "/dev/null")) |
ef416fc2 | 2475 | { |
2476 | /* | |
b423cd4c | 2477 | * File devices are disabled and the URL is not file:/dev/null... |
ef416fc2 | 2478 | */ |
2479 | ||
b423cd4c | 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); | |
ef416fc2 | 2485 | return; |
2486 | } | |
b423cd4c | 2487 | } |
2488 | else | |
2489 | { | |
ef416fc2 | 2490 | /* |
b423cd4c | 2491 | * See if the backend exists and is executable... |
ef416fc2 | 2492 | */ |
2493 | ||
09a101d6 | 2494 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme); |
b423cd4c | 2495 | if (access(srcfile, X_OK)) |
ef416fc2 | 2496 | { |
2497 | /* | |
b423cd4c | 2498 | * Could not find device in list! |
ef416fc2 | 2499 | */ |
ef416fc2 | 2500 | |
b423cd4c | 2501 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), |
2502 | attr->values[0].string.text); | |
2503 | return; | |
ef416fc2 | 2504 | } |
2505 | } | |
ef416fc2 | 2506 | |
b423cd4c | 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))); | |
ef416fc2 | 2514 | |
b423cd4c | 2515 | cupsdSetString(&printer->device_uri, attr->values[0].string.text); |
09a101d6 | 2516 | set_device_uri = 1; |
ef416fc2 | 2517 | } |
2518 | ||
09a101d6 | 2519 | set_port_monitor = 0; |
2520 | ||
b423cd4c | 2521 | if ((attr = ippFindAttribute(con->request, "port-monitor", |
09a101d6 | 2522 | IPP_TAG_NAME)) != NULL) |
ef416fc2 | 2523 | { |
b423cd4c | 2524 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ |
ef416fc2 | 2525 | |
ef416fc2 | 2526 | |
b423cd4c | 2527 | need_restart_job = 1; |
ef416fc2 | 2528 | |
b423cd4c | 2529 | supported = ippFindAttribute(printer->attrs, "port-monitor-supported", |
a41f09e2 | 2530 | IPP_TAG_NAME); |
080811b1 MS |
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 | } | |
ef416fc2 | 2538 | |
080811b1 | 2539 | if (!supported || i >= supported->num_values) |
b423cd4c | 2540 | { |
2541 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"), | |
2542 | attr->values[0].string.text); | |
2543 | return; | |
2544 | } | |
ef416fc2 | 2545 | |
b423cd4c | 2546 | cupsdLogMessage(CUPSD_LOG_INFO, |
2547 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2548 | printer->name, attr->values[0].string.text, | |
09a101d6 | 2549 | printer->port_monitor ? printer->port_monitor : "none"); |
ef416fc2 | 2550 | |
b423cd4c | 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); | |
09a101d6 | 2555 | |
2556 | set_port_monitor = 1; | |
b423cd4c | 2557 | } |
ef416fc2 | 2558 | |
b423cd4c | 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); | |
ef416fc2 | 2565 | |
b423cd4c | 2566 | printer->accepting = attr->values[0].boolean; |
2567 | cupsdAddPrinterHistory(printer); | |
2568 | } | |
bd7854cb | 2569 | |
b423cd4c | 2570 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
2571 | IPP_TAG_BOOLEAN)) != NULL) | |
2572 | { | |
2573 | if (printer->shared && !attr->values[0].boolean) | |
f7deaa1a | 2574 | cupsdDeregisterPrinter(printer, 1); |
bd7854cb | 2575 | |
b423cd4c | 2576 | cupsdLogMessage(CUPSD_LOG_INFO, |
2577 | "Setting %s printer-is-shared to %d (was %d.)", | |
2578 | printer->name, attr->values[0].boolean, printer->shared); | |
bd7854cb | 2579 | |
b423cd4c | 2580 | printer->shared = attr->values[0].boolean; |
2581 | } | |
bd7854cb | 2582 | |
b423cd4c | 2583 | if ((attr = ippFindAttribute(con->request, "printer-state", |
2584 | IPP_TAG_ENUM)) != NULL) | |
bd7854cb | 2585 | { |
b423cd4c | 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 | } | |
bd7854cb | 2593 | |
bc44d920 | 2594 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", |
2595 | printer->name, attr->values[0].integer, printer->state); | |
b423cd4c | 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); | |
bd7854cb | 2611 | } |
2612 | ||
b423cd4c | 2613 | set_printer_defaults(con, printer); |
2614 | ||
09a101d6 | 2615 | if ((attr = ippFindAttribute(con->request, "auth-info-required", |
2616 | IPP_TAG_KEYWORD)) != NULL) | |
2617 | cupsdSetAuthInfoRequired(printer, NULL, attr); | |
2618 | ||
bd7854cb | 2619 | /* |
b423cd4c | 2620 | * See if we have all required attributes... |
bd7854cb | 2621 | */ |
2622 | ||
b423cd4c | 2623 | if (!printer->device_uri) |
2624 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
bd7854cb | 2625 | |
2626 | /* | |
b423cd4c | 2627 | * See if we have an interface script or PPD file attached to the request... |
bd7854cb | 2628 | */ |
2629 | ||
b423cd4c | 2630 | if (con->filename) |
2631 | { | |
2632 | need_restart_job = 1; | |
bd7854cb | 2633 | |
b423cd4c | 2634 | strlcpy(srcfile, con->filename, sizeof(srcfile)); |
ef416fc2 | 2635 | |
b423cd4c | 2636 | if ((fp = cupsFileOpen(srcfile, "rb"))) |
2637 | { | |
2638 | /* | |
2639 | * Yes; get the first line from it... | |
2640 | */ | |
ef416fc2 | 2641 | |
b423cd4c | 2642 | line[0] = '\0'; |
2643 | cupsFileGets(fp, line, sizeof(line)); | |
2644 | cupsFileClose(fp); | |
ef416fc2 | 2645 | |
b423cd4c | 2646 | /* |
2647 | * Then see what kind of file it is... | |
2648 | */ | |
ef416fc2 | 2649 | |
b423cd4c | 2650 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2651 | printer->name); | |
ef416fc2 | 2652 | |
b423cd4c | 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 | */ | |
ef416fc2 | 2659 | |
b423cd4c | 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 | */ | |
ef416fc2 | 2668 | |
b423cd4c | 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 | } | |
568fa3fa MS |
2676 | |
2677 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2678 | "Copied interface script successfully!"); | |
2679 | chmod(dstfile, 0755); | |
b423cd4c | 2680 | } |
ef416fc2 | 2681 | |
b423cd4c | 2682 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2683 | printer->name); | |
ef416fc2 | 2684 | |
b423cd4c | 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 | */ | |
ef416fc2 | 2691 | |
b423cd4c | 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 | } | |
568fa3fa MS |
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__ */ | |
b423cd4c | 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 | */ | |
ef416fc2 | 2719 | |
b423cd4c | 2720 | unlink(dstfile); |
2721 | } | |
2722 | } | |
2723 | } | |
2724 | else if ((attr = ippFindAttribute(con->request, "ppd-name", | |
2725 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 2726 | { |
b423cd4c | 2727 | need_restart_job = 1; |
ef416fc2 | 2728 | |
b423cd4c | 2729 | if (!strcmp(attr->values[0].string.text, "raw")) |
ef416fc2 | 2730 | { |
b423cd4c | 2731 | /* |
2732 | * Raw driver, remove any existing PPD or interface script files. | |
2733 | */ | |
ef416fc2 | 2734 | |
b423cd4c | 2735 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2736 | printer->name); | |
2737 | unlink(dstfile); | |
ef416fc2 | 2738 | |
b423cd4c | 2739 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2740 | printer->name); | |
2741 | unlink(dstfile); | |
2742 | } | |
2743 | else | |
ef416fc2 | 2744 | { |
b423cd4c | 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 | } | |
568fa3fa MS |
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__ */ | |
ef416fc2 | 2774 | } |
2775 | } | |
2776 | ||
09a101d6 | 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, | |
bc44d920 | 2804 | printer->port_monitor ? printer->port_monitor |
2805 | : "none"); | |
09a101d6 | 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 | ||
ef416fc2 | 2819 | /* |
b423cd4c | 2820 | * Update the printer attributes and return... |
ef416fc2 | 2821 | */ |
2822 | ||
b423cd4c | 2823 | cupsdSetPrinterAttrs(printer); |
3dfe78b3 | 2824 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 2825 | |
b423cd4c | 2826 | if (need_restart_job && printer->job) |
ef416fc2 | 2827 | { |
b423cd4c | 2828 | cupsd_job_t *job; |
ef416fc2 | 2829 | |
b423cd4c | 2830 | /* |
2831 | * Stop the current job and then restart it below... | |
2832 | */ | |
ef416fc2 | 2833 | |
b423cd4c | 2834 | job = (cupsd_job_t *)printer->job; |
ef416fc2 | 2835 | |
b423cd4c | 2836 | cupsdStopJob(job, 1); |
2837 | ||
2838 | job->state->values[0].integer = IPP_JOB_PENDING; | |
2839 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 2840 | } |
2841 | ||
b423cd4c | 2842 | if (need_restart_job) |
2843 | cupsdCheckJobs(); | |
ef416fc2 | 2844 | |
3dfe78b3 | 2845 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
b423cd4c | 2846 | |
2847 | if (modify) | |
ef416fc2 | 2848 | { |
b423cd4c | 2849 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
2850 | "Printer \"%s\" modified by \"%s\".", printer->name, | |
2851 | get_username(con)); | |
ef416fc2 | 2852 | |
b423cd4c | 2853 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", |
2854 | printer->name, get_username(con)); | |
ef416fc2 | 2855 | } |
b423cd4c | 2856 | else |
2857 | { | |
2858 | cupsdAddPrinterHistory(printer); | |
ef416fc2 | 2859 | |
b423cd4c | 2860 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL, |
2861 | "New printer \"%s\" added by \"%s\".", printer->name, | |
2862 | get_username(con)); | |
ef416fc2 | 2863 | |
b423cd4c | 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; | |
ef416fc2 | 2869 | } |
2870 | ||
2871 | ||
2872 | /* | |
b423cd4c | 2873 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute |
2874 | * based upon the printer state... | |
ef416fc2 | 2875 | */ |
2876 | ||
b423cd4c | 2877 | static void |
2878 | add_printer_state_reasons( | |
2879 | cupsd_client_t *con, /* I - Client connection */ | |
2880 | cupsd_printer_t *p) /* I - Printer info */ | |
ef416fc2 | 2881 | { |
b423cd4c | 2882 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2883 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
2884 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2885 | |
b423cd4c | 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 | } | |
ef416fc2 | 2895 | |
ef416fc2 | 2896 | |
b423cd4c | 2897 | /* |
2898 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2899 | * the specified printer or class. | |
2900 | */ | |
ef416fc2 | 2901 | |
b423cd4c | 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 */ | |
ef416fc2 | 2908 | |
ef416fc2 | 2909 | |
b423cd4c | 2910 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", |
2911 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2912 | |
b423cd4c | 2913 | count = cupsdGetPrinterJobCount(p->name); |
ef416fc2 | 2914 | |
b423cd4c | 2915 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, |
2916 | "queued-job-count", count); | |
2917 | } | |
ef416fc2 | 2918 | |
ef416fc2 | 2919 | |
568fa3fa | 2920 | #ifdef __APPLE__ |
568fa3fa MS |
2921 | /* |
2922 | * 'apple_init_profile()' - Initialize a color profile. | |
2923 | */ | |
2924 | ||
2925 | static void | |
2926 | apple_init_profile( | |
a603edef MS |
2927 | ppd_file_t *ppd, /* I - PPD file */ |
2928 | cups_array_t *languages, /* I - Languages in the PPD file */ | |
568fa3fa MS |
2929 | CMDeviceProfileInfo *profile, /* I - Profile record */ |
2930 | unsigned id, /* I - Profile ID */ | |
2931 | const char *name, /* I - Profile name */ | |
a603edef | 2932 | const char *text, /* I - Profile UI text */ |
568fa3fa MS |
2933 | const char *iccfile) /* I - ICC filename */ |
2934 | { | |
2935 | char url[1024]; /* URL for profile filename */ | |
2936 | CFMutableDictionaryRef dict; /* Dictionary for name */ | |
a603edef MS |
2937 | char *language; /* Current language */ |
2938 | ppd_attr_t *attr; /* Profile attribute */ | |
db0bd74a MS |
2939 | CFStringRef cflang, /* Language string */ |
2940 | cftext; /* Localized text */ | |
568fa3fa MS |
2941 | |
2942 | ||
a603edef MS |
2943 | /* |
2944 | * Build the profile name dictionary... | |
2945 | */ | |
568fa3fa MS |
2946 | |
2947 | dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
2948 | &kCFTypeDictionaryKeyCallBacks, | |
2949 | &kCFTypeDictionaryValueCallBacks); | |
2950 | ||
db0bd74a MS |
2951 | cftext = CFStringCreateWithCString(kCFAllocatorDefault, text, |
2952 | kCFStringEncodingUTF8); | |
a603edef | 2953 | |
db0bd74a MS |
2954 | if (cftext) |
2955 | { | |
2956 | CFDictionarySetValue(dict, CFSTR("en"), cftext); | |
2957 | CFRelease(cftext); | |
2958 | } | |
2959 | ||
2960 | if (languages) | |
a603edef MS |
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 | { | |
db0bd74a | 2972 | if (iccfile) |
c9fc04c6 MS |
2973 | { |
2974 | if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name, | |
2975 | language)) == NULL) | |
2976 | attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language); | |
2977 | } | |
db0bd74a MS |
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 | } | |
a603edef MS |
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 | ||
568fa3fa MS |
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)); | |
568fa3fa MS |
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 */ | |
db0bd74a MS |
3031 | iccfile[1024], /* ICC filename */ |
3032 | selector[PPD_MAX_NAME]; | |
3033 | /* Profile selection string */ | |
568fa3fa | 3034 | ppd_file_t *ppd; /* PPD file */ |
01ce6322 MS |
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 */ | |
c9fc04c6 | 3045 | const char *profile_key; /* Profile keyword */ |
568fa3fa | 3046 | ppd_option_t *cm_option; /* Color model option */ |
01ce6322 | 3047 | ppd_choice_t *cm_choice; /* Color model choice */ |
568fa3fa MS |
3048 | int num_profiles; /* Number of profiles */ |
3049 | CMError error; /* Last error */ | |
db0bd74a MS |
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 */ | |
568fa3fa MS |
3056 | CMDeviceScope scope = /* Scope of the registration */ |
3057 | { | |
3058 | kCFPreferencesAnyUser, | |
3059 | kCFPreferencesCurrentHost | |
3060 | }; | |
3061 | CMDeviceProfileArrayPtr profiles; /* Profiles */ | |
3062 | CMDeviceProfileInfo *profile; /* Current profile */ | |
a603edef | 3063 | cups_array_t *languages; /* Languages array */ |
568fa3fa MS |
3064 | |
3065 | ||
64a69576 MS |
3066 | /* |
3067 | * Make sure ColorSync is available... | |
3068 | */ | |
3069 | ||
3070 | if (CMRegisterColorDevice == NULL) | |
3071 | return; | |
3072 | ||
568fa3fa MS |
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 | ||
c9fc04c6 MS |
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)) | |
568fa3fa MS |
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 | ||
c9fc04c6 | 3108 | |
568fa3fa MS |
3109 | /* |
3110 | * If we have profiles, add them... | |
3111 | */ | |
3112 | ||
3113 | if (num_profiles > 0) | |
3114 | { | |
01ce6322 MS |
3115 | if (profile_key[0] == 'A') |
3116 | { | |
3117 | /* | |
3118 | * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile | |
3119 | * attribute... | |
3120 | */ | |
db0bd74a | 3121 | |
01ce6322 MS |
3122 | if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL && |
3123 | attr->value) | |
3124 | default_profile_id = atoi(attr->value); | |
db0bd74a | 3125 | |
01ce6322 MS |
3126 | q1_choice = q2_choice = q3_choice = NULL; |
3127 | } | |
db0bd74a | 3128 | else |
01ce6322 MS |
3129 | { |
3130 | /* | |
3131 | * For CUPS PPDs, figure out the default profile selector values... | |
3132 | */ | |
db0bd74a | 3133 | |
01ce6322 MS |
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 | } | |
db0bd74a | 3176 | |
568fa3fa MS |
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; | |
a603edef | 3193 | languages = _ppdGetLanguages(ppd); |
568fa3fa MS |
3194 | |
3195 | for (profile = profiles->profiles, | |
c9fc04c6 | 3196 | attr = ppdFindAttr(ppd, profile_key, NULL); |
568fa3fa | 3197 | attr; |
c9fc04c6 | 3198 | attr = ppdFindNextAttr(ppd, profile_key, NULL)) |
568fa3fa MS |
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 | ||
c9fc04c6 MS |
3214 | if (profile_key[0] == 'c') |
3215 | { | |
3216 | cupsArraySave(ppd->sorted_attrs); | |
db0bd74a | 3217 | |
c9fc04c6 MS |
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); | |
db0bd74a | 3224 | |
c9fc04c6 MS |
3225 | cupsArrayRestore(ppd->sorted_attrs); |
3226 | } | |
3227 | else | |
3228 | profile_id = atoi(attr->spec); | |
db0bd74a MS |
3229 | |
3230 | apple_init_profile(ppd, languages, profile, profile_id, attr->spec, | |
3231 | attr->text[0] ? attr->text : attr->spec, iccfile); | |
568fa3fa MS |
3232 | |
3233 | profile ++; | |
db0bd74a MS |
3234 | |
3235 | /* | |
3236 | * See if this is the default profile... | |
3237 | */ | |
3238 | ||
01ce6322 | 3239 | if (!default_profile_id) |
db0bd74a MS |
3240 | { |
3241 | if (q2_choice) | |
3242 | { | |
3243 | if (q3_choice) | |
3244 | { | |
3245 | snprintf(selector, sizeof(selector), "%s.%s.%s", | |
01ce6322 | 3246 | q1_choice, q2_choice, q3_choice); |
db0bd74a MS |
3247 | if (!strcmp(selector, attr->spec)) |
3248 | default_profile_id = profile_id; | |
3249 | } | |
3250 | ||
3251 | if (!default_profile_id) | |
3252 | { | |
01ce6322 MS |
3253 | snprintf(selector, sizeof(selector), "%s.%s.", q1_choice, |
3254 | q2_choice); | |
db0bd74a MS |
3255 | if (!strcmp(selector, attr->spec)) |
3256 | default_profile_id = profile_id; | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | if (!default_profile_id && q3_choice) | |
3261 | { | |
01ce6322 MS |
3262 | snprintf(selector, sizeof(selector), "%s..%s", q1_choice, |
3263 | q3_choice); | |
db0bd74a MS |
3264 | if (!strcmp(selector, attr->spec)) |
3265 | default_profile_id = profile_id; | |
3266 | } | |
3267 | ||
3268 | if (!default_profile_id) | |
3269 | { | |
01ce6322 | 3270 | snprintf(selector, sizeof(selector), "%s..", q1_choice); |
db0bd74a MS |
3271 | if (!strcmp(selector, attr->spec)) |
3272 | default_profile_id = profile_id; | |
3273 | } | |
3274 | } | |
568fa3fa | 3275 | } |
a603edef MS |
3276 | |
3277 | _ppdFreeLanguages(languages); | |
568fa3fa MS |
3278 | } |
3279 | else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL) | |
3280 | { | |
3281 | /* | |
db0bd74a | 3282 | * Extract profiles from ColorModel option... |
568fa3fa MS |
3283 | */ |
3284 | ||
db0bd74a | 3285 | const char *profile_name; /* Name of generic profile */ |
568fa3fa | 3286 | |
db0bd74a MS |
3287 | |
3288 | num_profiles = cm_option->num_choices; | |
568fa3fa MS |
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; | |
db0bd74a MS |
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 | } | |
568fa3fa MS |
3327 | } |
3328 | else | |
3329 | { | |
3330 | /* | |
3331 | * Use the default colorspace... | |
3332 | */ | |
3333 | ||
db0bd74a | 3334 | num_profiles = 1 + ppd->colorspace != PPD_CS_GRAY; |
568fa3fa | 3335 | |
db0bd74a | 3336 | if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL) |
568fa3fa MS |
3337 | { |
3338 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3339 | "Unable to allocate memory for %d profiles!", | |
3340 | num_profiles); | |
3341 | ppdClose(ppd); | |
3342 | return; | |
3343 | } | |
3344 | ||
db0bd74a MS |
3345 | profiles->profileCount = num_profiles; |
3346 | ||
3347 | apple_init_profile(ppd, NULL, profiles->profiles, _ppdHashName("Gray.."), | |
3348 | "Gray", "Gray", NULL); | |
568fa3fa MS |
3349 | |
3350 | switch (ppd->colorspace) | |
3351 | { | |
568fa3fa | 3352 | case PPD_CS_RGB : |
db0bd74a MS |
3353 | case PPD_CS_CMY : |
3354 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
a603edef | 3355 | _ppdHashName("RGB.."), "RGB", "RGB", NULL); |
568fa3fa | 3356 | break; |
db0bd74a | 3357 | case PPD_CS_RGBK : |
568fa3fa | 3358 | case PPD_CS_CMYK : |
db0bd74a | 3359 | apple_init_profile(ppd, NULL, profiles->profiles + 1, |
a603edef | 3360 | _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL); |
568fa3fa MS |
3361 | break; |
3362 | ||
db0bd74a MS |
3363 | case PPD_CS_N : |
3364 | apple_init_profile(ppd, NULL, profiles->profiles + 1, | |
3365 | _ppdHashName("DeviceN.."), "DeviceN", "DeviceN", | |
3366 | NULL); | |
3367 | break; | |
3368 | ||
568fa3fa | 3369 | default : |
568fa3fa MS |
3370 | break; |
3371 | } | |
3372 | } | |
3373 | ||
3374 | if (num_profiles > 0) | |
3375 | { | |
db0bd74a MS |
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 | ||
568fa3fa MS |
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 | ||
db0bd74a MS |
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); | |
568fa3fa | 3396 | |
db0bd74a MS |
3397 | if (device_name && printer_name) |
3398 | { | |
3399 | CFDictionarySetValue(device_name, CFSTR("en"), printer_name); | |
568fa3fa | 3400 | |
db0bd74a MS |
3401 | /* |
3402 | * Register the device with ColorSync... | |
3403 | */ | |
568fa3fa | 3404 | |
db0bd74a MS |
3405 | error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id, |
3406 | device_name, &scope); | |
568fa3fa | 3407 | |
db0bd74a MS |
3408 | /* |
3409 | * Register the profiles... | |
3410 | */ | |
568fa3fa | 3411 | |
db0bd74a MS |
3412 | if (error == noErr) |
3413 | error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id, | |
3414 | default_profile_id, profiles); | |
3415 | } | |
3416 | else | |
3417 | error = 1000; | |
568fa3fa MS |
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); | |
db0bd74a MS |
3434 | |
3435 | if (printer_name) | |
3436 | CFRelease(printer_name); | |
3437 | ||
3438 | if (device_name) | |
3439 | CFRelease(device_name); | |
568fa3fa MS |
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 | { | |
64a69576 MS |
3455 | /* |
3456 | * Make sure ColorSync is available... | |
3457 | */ | |
3458 | ||
3459 | if (CMUnregisterColorDevice != NULL) | |
3460 | CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name)); | |
568fa3fa MS |
3461 | } |
3462 | #endif /* __APPLE__ */ | |
3463 | ||
b423cd4c | 3464 | /* |
3465 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
3466 | */ | |
ef416fc2 | 3467 | |
b423cd4c | 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 */ | |
f7deaa1a | 3476 | *option; /* Current option */ |
ef416fc2 | 3477 | |
ef416fc2 | 3478 | |
b423cd4c | 3479 | /* |
3480 | * Collect all of the default options and add the missing ones to the | |
3481 | * job object... | |
3482 | */ | |
ef416fc2 | 3483 | |
b423cd4c | 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 | } | |
ef416fc2 | 3492 | |
b423cd4c | 3493 | /* |
3494 | * Encode these options as attributes in the job object... | |
3495 | */ | |
ef416fc2 | 3496 | |
b423cd4c | 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 | { | |
f7deaa1a | 3510 | ipp_attribute_t *attr, /* job-id attribute */ |
3511 | *auth_info; /* auth-info attribute */ | |
b423cd4c | 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 */ | |
ef416fc2 | 3523 | |
ef416fc2 | 3524 | |
b423cd4c | 3525 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", |
3526 | con, con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 3527 | |
b423cd4c | 3528 | /* |
3529 | * Start with "everything is OK" status... | |
3530 | */ | |
ef416fc2 | 3531 | |
b423cd4c | 3532 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 3533 | |
b423cd4c | 3534 | /* |
3535 | * See if we have a job URI or a printer URI... | |
3536 | */ | |
ef416fc2 | 3537 | |
b423cd4c | 3538 | if (!strcmp(uri->name, "printer-uri")) |
3539 | { | |
3540 | /* | |
3541 | * Got a printer URI; see if we also have a job-id attribute... | |
3542 | */ | |
ef416fc2 | 3543 | |
b423cd4c | 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 | */ | |
ef416fc2 | 3559 | |
b423cd4c | 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)); | |
f7deaa1a | 3563 | |
b423cd4c | 3564 | if (strncmp(resource, "/jobs/", 6)) |
3565 | { | |
3566 | /* | |
3567 | * Not a valid URI! | |
3568 | */ | |
ef416fc2 | 3569 | |
b423cd4c | 3570 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"), |
3571 | uri->values[0].string.text); | |
3572 | return; | |
3573 | } | |
ef416fc2 | 3574 | |
b423cd4c | 3575 | jobid = atoi(resource + 6); |
3576 | } | |
ef416fc2 | 3577 | |
b423cd4c | 3578 | /* |
3579 | * See if the job exists... | |
3580 | */ | |
ef416fc2 | 3581 | |
b423cd4c | 3582 | if ((job = cupsdFindJob(jobid)) == NULL) |
3583 | { | |
3584 | /* | |
3585 | * Nope - return a "not found" error... | |
3586 | */ | |
ef416fc2 | 3587 | |
b423cd4c | 3588 | send_ipp_status(con, IPP_NOT_FOUND, |
3589 | _("Job #%d does not exist!"), jobid); | |
ef416fc2 | 3590 | return; |
b423cd4c | 3591 | } |
ef416fc2 | 3592 | |
b423cd4c | 3593 | /* |
3594 | * See if the job has been completed... | |
3595 | */ | |
3596 | ||
3597 | if (job->state_value != IPP_JOB_HELD) | |
ef416fc2 | 3598 | { |
3599 | /* | |
b423cd4c | 3600 | * Return a "not-possible" error... |
ef416fc2 | 3601 | */ |
3602 | ||
b423cd4c | 3603 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
3604 | _("Job #%d is not held for authentication!"), | |
3605 | jobid); | |
3606 | return; | |
3607 | } | |
ef416fc2 | 3608 | |
b423cd4c | 3609 | /* |
3610 | * See if we have already authenticated... | |
3611 | */ | |
3612 | ||
f7deaa1a | 3613 | auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT); |
3614 | ||
3615 | if (!con->username[0] && !auth_info) | |
b423cd4c | 3616 | { |
db1f069b MS |
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!")); | |
b423cd4c | 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 | { | |
2fb76298 | 3642 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 3643 | return; |
3644 | } | |
3645 | ||
3646 | /* | |
3647 | * Save the authentication information for this job... | |
3648 | */ | |
3649 | ||
f7deaa1a | 3650 | save_auth_info(con, job, auth_info); |
b423cd4c | 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"); | |
ef416fc2 | 3664 | } |
b423cd4c | 3665 | |
3666 | /* | |
3667 | * Release the job and return... | |
3668 | */ | |
3669 | ||
3670 | cupsdReleaseJob(job); | |
3671 | ||
09a101d6 | 3672 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Authenticated by \"%s\".", jobid, |
b423cd4c | 3673 | con->username); |
ef416fc2 | 3674 | } |
3675 | ||
3676 | ||
3677 | /* | |
b423cd4c | 3678 | * 'cancel_all_jobs()' - Cancel all print jobs. |
ef416fc2 | 3679 | */ |
3680 | ||
b423cd4c | 3681 | static void |
3682 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
3683 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 3684 | { |
b423cd4c | 3685 | http_status_t status; /* Policy status */ |
b423cd4c | 3686 | cups_ptype_t dtype; /* Destination type */ |
f7deaa1a | 3687 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 3688 | userpass[HTTP_MAX_URI], /* Username portion of URI */ |
f7deaa1a | 3689 | hostname[HTTP_MAX_URI], /* Host portion of URI */ |
b423cd4c | 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 */ | |
ef416fc2 | 3696 | |
3697 | ||
b423cd4c | 3698 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, |
3699 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 3700 | |
3701 | /* | |
b423cd4c | 3702 | * See if we have a printer URI... |
ef416fc2 | 3703 | */ |
3704 | ||
b423cd4c | 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 | } | |
ef416fc2 | 3711 | |
3712 | /* | |
b423cd4c | 3713 | * Get the username (if any) for the jobs we want to cancel (only if |
3714 | * "my-jobs" is specified... | |
ef416fc2 | 3715 | */ |
3716 | ||
b423cd4c | 3717 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
3718 | IPP_TAG_BOOLEAN)) != NULL && | |
3719 | attr->values[0].boolean) | |
ef416fc2 | 3720 | { |
b423cd4c | 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 | } | |
ef416fc2 | 3730 | } |
b423cd4c | 3731 | else |
3732 | username = NULL; | |
ef416fc2 | 3733 | |
b423cd4c | 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; | |
ef416fc2 | 3743 | |
3744 | /* | |
b423cd4c | 3745 | * And if the destination is valid... |
ef416fc2 | 3746 | */ |
3747 | ||
f7deaa1a | 3748 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3749 | { |
3750 | /* | |
b423cd4c | 3751 | * Bad URI? |
ef416fc2 | 3752 | */ |
3753 | ||
f7deaa1a | 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 | ||
b423cd4c | 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 | } | |
ef416fc2 | 3766 | |
b423cd4c | 3767 | /* |
3768 | * Check policy... | |
3769 | */ | |
3770 | ||
3771 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
3772 | { | |
f899b121 | 3773 | send_http_error(con, status, NULL); |
b423cd4c | 3774 | return; |
3775 | } | |
ef416fc2 | 3776 | |
ef416fc2 | 3777 | /* |
b423cd4c | 3778 | * Cancel all jobs on all printers... |
ef416fc2 | 3779 | */ |
3780 | ||
b423cd4c | 3781 | cupsdCancelJobs(NULL, username, purge); |
ef416fc2 | 3782 | |
b423cd4c | 3783 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", |
d09495fa | 3784 | purge ? "purged" : "canceled", get_username(con)); |
ef416fc2 | 3785 | } |
b423cd4c | 3786 | else |
ef416fc2 | 3787 | { |
3788 | /* | |
b423cd4c | 3789 | * Check policy... |
ef416fc2 | 3790 | */ |
3791 | ||
f7deaa1a | 3792 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
3793 | NULL)) != HTTP_OK) | |
b423cd4c | 3794 | { |
f899b121 | 3795 | send_http_error(con, status, printer); |
b423cd4c | 3796 | return; |
3797 | } | |
ef416fc2 | 3798 | |
b423cd4c | 3799 | /* |
3800 | * Cancel all of the jobs on the named printer... | |
3801 | */ | |
3802 | ||
f7deaa1a | 3803 | cupsdCancelJobs(printer->name, username, purge); |
b423cd4c | 3804 | |
3805 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
f7deaa1a | 3806 | printer->name, purge ? "purged" : "canceled", |
3807 | get_username(con)); | |
ef416fc2 | 3808 | } |
3809 | ||
b423cd4c | 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 */ | |
f7deaa1a | 3824 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
b423cd4c | 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 */ | |
bc44d920 | 3830 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 3831 | cupsd_printer_t *printer; /* Printer data */ |
0a682745 | 3832 | int purge; /* Purge the job? */ |
b423cd4c | 3833 | |
3834 | ||
3835 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
3836 | con->http.fd, uri->values[0].string.text); | |
3837 | ||
ef416fc2 | 3838 | /* |
b423cd4c | 3839 | * See if we have a job URI or a printer URI... |
ef416fc2 | 3840 | */ |
3841 | ||
b423cd4c | 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) | |
ef416fc2 | 3857 | { |
3858 | /* | |
b423cd4c | 3859 | * Find the current job on the specified printer... |
ef416fc2 | 3860 | */ |
3861 | ||
f7deaa1a | 3862 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 3863 | { |
3864 | /* | |
b423cd4c | 3865 | * Bad URI... |
ef416fc2 | 3866 | */ |
3867 | ||
b423cd4c | 3868 | send_ipp_status(con, IPP_NOT_FOUND, |
3869 | _("The printer or class was not found.")); | |
3870 | return; | |
ef416fc2 | 3871 | } |
3872 | ||
3873 | /* | |
b423cd4c | 3874 | * See if the printer is currently printing a job... |
ef416fc2 | 3875 | */ |
3876 | ||
b423cd4c | 3877 | if (printer->job) |
3878 | jobid = ((cupsd_job_t *)printer->job)->id; | |
ef416fc2 | 3879 | else |
ef416fc2 | 3880 | { |
3881 | /* | |
b423cd4c | 3882 | * No, see if there are any pending jobs... |
ef416fc2 | 3883 | */ |
f7deaa1a | 3884 | |
b423cd4c | 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 && | |
f7deaa1a | 3889 | !strcasecmp(job->dest, printer->name)) |
b423cd4c | 3890 | break; |
ef416fc2 | 3891 | |
b423cd4c | 3892 | if (job) |
3893 | jobid = job->id; | |
3894 | else | |
ef416fc2 | 3895 | { |
355e94dc MS |
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 | } | |
b423cd4c | 3911 | } |
3912 | } | |
3913 | } | |
3914 | } | |
3915 | else | |
3916 | { | |
3917 | /* | |
3918 | * Got a job URI; parse it to get the job ID... | |
3919 | */ | |
ef416fc2 | 3920 | |
f7deaa1a | 3921 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
3922 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 3923 | sizeof(host), &port, resource, sizeof(resource)); |
f7deaa1a | 3924 | |
b423cd4c | 3925 | if (strncmp(resource, "/jobs/", 6)) |
3926 | { | |
3927 | /* | |
3928 | * Not a valid URI! | |
3929 | */ | |
ef416fc2 | 3930 | |
b423cd4c | 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 | ||
0a682745 MS |
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 | ||
b423cd4c | 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 | { | |
2fb76298 | 3970 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 3971 | return; |
3972 | } | |
3973 | ||
3974 | /* | |
d09495fa | 3975 | * See if the job is already completed, canceled, or aborted; if so, |
b423cd4c | 3976 | * we can't cancel... |
3977 | */ | |
3978 | ||
0a682745 | 3979 | if (job->state_value >= IPP_JOB_CANCELED && !purge) |
b423cd4c | 3980 | { |
e1d6a774 | 3981 | switch (job->state_value) |
3982 | { | |
d09495fa | 3983 | case IPP_JOB_CANCELED : |
e1d6a774 | 3984 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
d09495fa | 3985 | _("Job #%d is already canceled - can\'t cancel."), |
e1d6a774 | 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 | ||
b423cd4c | 4002 | return; |
4003 | } | |
ef416fc2 | 4004 | |
b423cd4c | 4005 | /* |
4006 | * Cancel the job and return... | |
4007 | */ | |
ef416fc2 | 4008 | |
0a682745 | 4009 | cupsdCancelJob(job, purge, IPP_JOB_CANCELED); |
b423cd4c | 4010 | cupsdCheckJobs(); |
ef416fc2 | 4011 | |
0a682745 MS |
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); | |
ef416fc2 | 4018 | |
b423cd4c | 4019 | con->response->request.status.status_code = IPP_OK; |
4020 | } | |
ef416fc2 | 4021 | |
ef416fc2 | 4022 | |
b423cd4c | 4023 | /* |
4024 | * 'cancel_subscription()' - Cancel a subscription. | |
4025 | */ | |
ef416fc2 | 4026 | |
b423cd4c | 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 */ | |
ef416fc2 | 4034 | |
ef416fc2 | 4035 | |
b423cd4c | 4036 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4037 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
4038 | con, con->http.fd, sub_id); | |
ef416fc2 | 4039 | |
b423cd4c | 4040 | /* |
4041 | * Is the subscription ID valid? | |
4042 | */ | |
ef416fc2 | 4043 | |
b423cd4c | 4044 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
4045 | { | |
4046 | /* | |
4047 | * Bad subscription ID... | |
4048 | */ | |
ef416fc2 | 4049 | |
b423cd4c | 4050 | send_ipp_status(con, IPP_NOT_FOUND, |
4051 | _("notify-subscription-id %d no good!"), sub_id); | |
4052 | return; | |
4053 | } | |
ef416fc2 | 4054 | |
b423cd4c | 4055 | /* |
4056 | * Check policy... | |
4057 | */ | |
ef416fc2 | 4058 | |
b423cd4c | 4059 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
4060 | DefaultPolicyPtr, | |
4061 | con, sub->owner)) != HTTP_OK) | |
4062 | { | |
f899b121 | 4063 | send_http_error(con, status, sub->dest); |
b423cd4c | 4064 | return; |
4065 | } | |
ef416fc2 | 4066 | |
b423cd4c | 4067 | /* |
4068 | * Cancel the subscription... | |
4069 | */ | |
ef416fc2 | 4070 | |
b423cd4c | 4071 | cupsdDeleteSubscription(sub, 1); |
ef416fc2 | 4072 | |
b423cd4c | 4073 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 4074 | } |
4075 | ||
4076 | ||
3dfe78b3 MS |
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 | ||
ef416fc2 | 4111 | /* |
b423cd4c | 4112 | * 'check_quotas()' - Check quotas for a printer and user. |
ef416fc2 | 4113 | */ |
4114 | ||
b423cd4c | 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 */ | |
ef416fc2 | 4118 | { |
b423cd4c | 4119 | int i; /* Looping var */ |
4120 | char username[33]; /* Username */ | |
4121 | cupsd_quota_t *q; /* Quota data */ | |
7594b224 | 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 | ||
b423cd4c | 4138 | struct passwd *pw; /* User password data */ |
7594b224 | 4139 | #endif /* HAVE_MBR_UID_TO_UUID */ |
ef416fc2 | 4140 | |
4141 | ||
b423cd4c | 4142 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", |
4143 | con, con->http.fd, p, p->name); | |
ef416fc2 | 4144 | |
b423cd4c | 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) | |
ef416fc2 | 4156 | { |
b423cd4c | 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 | } | |
ef416fc2 | 4181 | } |
4182 | ||
4183 | /* | |
b423cd4c | 4184 | * Check against users... |
ef416fc2 | 4185 | */ |
4186 | ||
b423cd4c | 4187 | if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0) |
4188 | return (1); | |
4189 | ||
4190 | if (p->num_users) | |
4191 | { | |
7594b224 | 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 | ||
b423cd4c | 4217 | pw = getpwnam(username); |
4218 | endpwent(); | |
7594b224 | 4219 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 4220 | |
4221 | for (i = 0; i < p->num_users; i ++) | |
4222 | if (p->users[i][0] == '@') | |
4223 | { | |
4224 | /* | |
4225 | * Check group membership... | |
4226 | */ | |
4227 | ||
7594b224 | 4228 | #ifdef HAVE_MBR_UID_TO_UUID |
c9fc04c6 MS |
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) | |
7594b224 | 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 | } | |
7594b224 | 4249 | |
c9fc04c6 MS |
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... | |
7594b224 | 4255 | */ |
4256 | ||
c9fc04c6 MS |
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; | |
7594b224 | 4261 | } |
c9fc04c6 MS |
4262 | |
4263 | /* | |
4264 | * Stop if we found a match... | |
4265 | */ | |
4266 | ||
4267 | if (is_member) | |
4268 | break; | |
4269 | ||
7594b224 | 4270 | #else |
b423cd4c | 4271 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) |
4272 | break; | |
7594b224 | 4273 | #endif /* HAVE_MBR_UID_TO_UUID */ |
4274 | } | |
4275 | #ifdef HAVE_MBR_UID_TO_UUID | |
4276 | else | |
4277 | { | |
c9fc04c6 MS |
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) | |
7594b224 | 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 | } | |
7594b224 | 4298 | |
c9fc04c6 MS |
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; | |
7594b224 | 4306 | } |
c9fc04c6 MS |
4307 | |
4308 | if (is_member) | |
4309 | break; | |
b423cd4c | 4310 | } |
7594b224 | 4311 | #else |
b423cd4c | 4312 | else if (!strcasecmp(username, p->users[i])) |
4313 | break; | |
7594b224 | 4314 | #endif /* HAVE_MBR_UID_TO_UUID */ |
b423cd4c | 4315 | |
4316 | if ((i < p->num_users) == p->deny_users) | |
ef416fc2 | 4317 | { |
b423cd4c | 4318 | cupsdLogMessage(CUPSD_LOG_INFO, |
4319 | "Denying user \"%s\" access to printer \"%s\"...", | |
4320 | username, p->name); | |
4321 | return (0); | |
ef416fc2 | 4322 | } |
b423cd4c | 4323 | } |
ef416fc2 | 4324 | |
4325 | /* | |
b423cd4c | 4326 | * Check quotas... |
ef416fc2 | 4327 | */ |
4328 | ||
7594b224 | 4329 | #ifdef __APPLE__ |
3d8365b8 | 4330 | if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL) |
7594b224 | 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 */ | |
bc44d920 | 4352 | return (-1); |
7594b224 | 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 */ | |
bc44d920 | 4361 | return (-1); |
7594b224 | 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 */ | |
bc44d920 | 4370 | return (-1); |
7594b224 | 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 */ | |
bc44d920 | 4379 | return (-1); |
7594b224 | 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__ */ | |
b423cd4c | 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); | |
bc44d920 | 4396 | return (-1); |
b423cd4c | 4397 | } |
ef416fc2 | 4398 | |
b423cd4c | 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); | |
bc44d920 | 4404 | return (-1); |
b423cd4c | 4405 | } |
4406 | } | |
4407 | ||
4408 | /* | |
4409 | * If we have gotten this far, we're done! | |
4410 | */ | |
4411 | ||
4412 | return (1); | |
ef416fc2 | 4413 | } |
4414 | ||
4415 | ||
4416 | /* | |
b423cd4c | 4417 | * 'copy_attribute()' - Copy a single attribute. |
ef416fc2 | 4418 | */ |
4419 | ||
b423cd4c | 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? */ | |
ef416fc2 | 4425 | { |
b423cd4c | 4426 | int i; /* Looping var */ |
4427 | ipp_attribute_t *toattr; /* Destination attribute */ | |
ef416fc2 | 4428 | |
4429 | ||
4430 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b423cd4c | 4431 | "copy_attribute(%p, %p[%s,%x,%x])", to, attr, |
4432 | attr->name ? attr->name : "(null)", attr->group_tag, | |
4433 | attr->value_tag); | |
ef416fc2 | 4434 | |
b423cd4c | 4435 | switch (attr->value_tag & ~IPP_TAG_COPY) |
4436 | { | |
4437 | case IPP_TAG_ZERO : | |
4438 | toattr = ippAddSeparator(to); | |
4439 | break; | |
ef416fc2 | 4440 | |
b423cd4c | 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); | |
ef416fc2 | 4445 | |
b423cd4c | 4446 | for (i = 0; i < attr->num_values; i ++) |
4447 | toattr->values[i].integer = attr->values[i].integer; | |
4448 | break; | |
ef416fc2 | 4449 | |
b423cd4c | 4450 | case IPP_TAG_BOOLEAN : |
4451 | toattr = ippAddBooleans(to, attr->group_tag, attr->name, | |
4452 | attr->num_values, NULL); | |
ef416fc2 | 4453 | |
b423cd4c | 4454 | for (i = 0; i < attr->num_values; i ++) |
4455 | toattr->values[i].boolean = attr->values[i].boolean; | |
4456 | break; | |
ef416fc2 | 4457 | |
b423cd4c | 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); | |
ef416fc2 | 4470 | |
b423cd4c | 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 ++) | |
bc44d920 | 4479 | toattr->values[i].string.text = |
4480 | _cupsStrAlloc(attr->values[i].string.text); | |
b423cd4c | 4481 | } |
4482 | break; | |
ef416fc2 | 4483 | |
b423cd4c | 4484 | case IPP_TAG_DATE : |
4485 | toattr = ippAddDate(to, attr->group_tag, attr->name, | |
4486 | attr->values[0].date); | |
4487 | break; | |
ef416fc2 | 4488 | |
b423cd4c | 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); | |
ef416fc2 | 4493 | |
b423cd4c | 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; | |
ef416fc2 | 4501 | |
b423cd4c | 4502 | case IPP_TAG_RANGE : |
4503 | toattr = ippAddRanges(to, attr->group_tag, attr->name, | |
4504 | attr->num_values, NULL, NULL); | |
ef416fc2 | 4505 | |
b423cd4c | 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; | |
ef416fc2 | 4512 | |
b423cd4c | 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); | |
ef416fc2 | 4518 | |
b423cd4c | 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 = | |
757d2cad | 4533 | _cupsStrAlloc(attr->values[i].string.charset); |
b423cd4c | 4534 | else |
4535 | toattr->values[i].string.charset = | |
4536 | toattr->values[0].string.charset; | |
ef416fc2 | 4537 | |
bc44d920 | 4538 | toattr->values[i].string.text = |
4539 | _cupsStrAlloc(attr->values[i].string.text); | |
b423cd4c | 4540 | } |
4541 | } | |
4542 | break; | |
ef416fc2 | 4543 | |
b423cd4c | 4544 | case IPP_TAG_BEGIN_COLLECTION : |
4545 | toattr = ippAddCollections(to, attr->group_tag, attr->name, | |
4546 | attr->num_values, NULL); | |
ef416fc2 | 4547 | |
b423cd4c | 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 | } | |
ef416fc2 | 4554 | break; |
ef416fc2 | 4555 | |
b423cd4c | 4556 | default : |
4557 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
4558 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 4559 | |
b423cd4c | 4560 | for (i = 0; i < attr->num_values; i ++) |
4561 | { | |
4562 | toattr->values[i].unknown.length = attr->values[i].unknown.length; | |
ef416fc2 | 4563 | |
b423cd4c | 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 | } | |
ef416fc2 | 4577 | |
b423cd4c | 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 */ | |
ef416fc2 | 4594 | |
ef416fc2 | 4595 | |
b423cd4c | 4596 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4597 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
4598 | to, from, ra, group, quickcopy); | |
ef416fc2 | 4599 | |
b423cd4c | 4600 | if (!to || !from) |
4601 | return; | |
e00b005a | 4602 | |
b423cd4c | 4603 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) |
ef416fc2 | 4604 | { |
4605 | /* | |
b423cd4c | 4606 | * Filter attributes as needed... |
ef416fc2 | 4607 | */ |
4608 | ||
2abf387c | 4609 | if ((group != IPP_TAG_ZERO && fromattr->group_tag != group && |
4610 | fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name) | |
b423cd4c | 4611 | continue; |
4612 | ||
4613 | if (!ra || cupsArrayFind(ra, fromattr->name)) | |
4614 | copy_attribute(to, fromattr, quickcopy); | |
ef416fc2 | 4615 | } |
b423cd4c | 4616 | } |
ef416fc2 | 4617 | |
ef416fc2 | 4618 | |
b423cd4c | 4619 | /* |
4620 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
4621 | * specified job. | |
4622 | */ | |
ef416fc2 | 4623 | |
b423cd4c | 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 */ | |
ef416fc2 | 4639 | |
ef416fc2 | 4640 | |
3dfe78b3 MS |
4641 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4642 | "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")", | |
09a101d6 | 4643 | con, con ? con->http.fd : -1, job, job->id, |
4644 | name ? name : "(null)"); | |
ef416fc2 | 4645 | |
b423cd4c | 4646 | /* |
4647 | * Find the banner; return if not found or "none"... | |
4648 | */ | |
ef416fc2 | 4649 | |
b423cd4c | 4650 | if (!name || !strcmp(name, "none") || |
4651 | (banner = cupsdFindBanner(name)) == NULL) | |
4652 | return (0); | |
ef416fc2 | 4653 | |
b423cd4c | 4654 | /* |
4655 | * Open the banner and job files... | |
4656 | */ | |
ef416fc2 | 4657 | |
b423cd4c | 4658 | if (add_file(con, job, banner->filetype, 0)) |
91c84a35 | 4659 | return (-1); |
ef416fc2 | 4660 | |
b423cd4c | 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, | |
3dfe78b3 | 4666 | "Unable to create banner job file %s - %s", |
b423cd4c | 4667 | filename, strerror(errno)); |
4668 | job->num_files --; | |
4669 | return (0); | |
4670 | } | |
4671 | ||
4672 | fchmod(cupsFileNumber(out), 0640); | |
4673 | fchown(cupsFileNumber(out), RunUser, Group); | |
ef416fc2 | 4674 | |
4675 | /* | |
b423cd4c | 4676 | * Try the localized banner file under the subdirectory... |
ef416fc2 | 4677 | */ |
4678 | ||
80ca4592 | 4679 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, |
b423cd4c | 4680 | sizeof(attrname)); |
4681 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
ef416fc2 | 4682 | { |
4683 | /* | |
b423cd4c | 4684 | * Convert ll-cc to ll_CC... |
ef416fc2 | 4685 | */ |
4686 | ||
b423cd4c | 4687 | attrname[2] = '_'; |
4688 | attrname[3] = toupper(attrname[3] & 255); | |
4689 | attrname[4] = toupper(attrname[4] & 255); | |
4690 | } | |
ef416fc2 | 4691 | |
b423cd4c | 4692 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4693 | attrname, name); | |
ef416fc2 | 4694 | |
b423cd4c | 4695 | if (access(filename, 0) && strlen(attrname) > 2) |
ef416fc2 | 4696 | { |
4697 | /* | |
b423cd4c | 4698 | * Wasn't able to find "ll_CC" locale file; try the non-national |
4699 | * localization banner directory. | |
ef416fc2 | 4700 | */ |
4701 | ||
b423cd4c | 4702 | attrname[2] = '\0'; |
ef416fc2 | 4703 | |
b423cd4c | 4704 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
4705 | attrname, name); | |
ef416fc2 | 4706 | } |
b423cd4c | 4707 | |
4708 | if (access(filename, 0)) | |
ef416fc2 | 4709 | { |
4710 | /* | |
b423cd4c | 4711 | * Use the non-localized banner file. |
ef416fc2 | 4712 | */ |
4713 | ||
b423cd4c | 4714 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); |
4715 | } | |
ef416fc2 | 4716 | |
b423cd4c | 4717 | if ((in = cupsFileOpen(filename, "r")) == NULL) |
4718 | { | |
4719 | cupsFileClose(out); | |
4720 | unlink(filename); | |
4721 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3dfe78b3 | 4722 | "Unable to open banner template file %s - %s", |
b423cd4c | 4723 | filename, strerror(errno)); |
4724 | job->num_files --; | |
4725 | return (0); | |
ef416fc2 | 4726 | } |
4727 | ||
4728 | /* | |
b423cd4c | 4729 | * Parse the file to the end... |
ef416fc2 | 4730 | */ |
4731 | ||
b423cd4c | 4732 | while ((ch = cupsFileGetChar(in)) != EOF) |
4733 | if (ch == '{') | |
4734 | { | |
4735 | /* | |
4736 | * Get an attribute name... | |
4737 | */ | |
ef416fc2 | 4738 | |
b423cd4c | 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; | |
ef416fc2 | 4746 | |
b423cd4c | 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 | } | |
ef416fc2 | 4758 | |
ef416fc2 | 4759 | /* |
b423cd4c | 4760 | * See if it is defined... |
ef416fc2 | 4761 | */ |
4762 | ||
b423cd4c | 4763 | if (attrname[0] == '?') |
4764 | s = attrname + 1; | |
4765 | else | |
4766 | s = attrname; | |
ef416fc2 | 4767 | |
b423cd4c | 4768 | if (!strcmp(s, "printer-name")) |
4769 | { | |
4770 | cupsFilePuts(out, job->dest); | |
4771 | continue; | |
ef416fc2 | 4772 | } |
b423cd4c | 4773 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) |
4774 | { | |
4775 | /* | |
4776 | * See if we have a leading question mark... | |
4777 | */ | |
ef416fc2 | 4778 | |
b423cd4c | 4779 | if (attrname[0] != '?') |
4780 | { | |
4781 | /* | |
4782 | * Nope, write to file as-is; probably a PostScript procedure... | |
4783 | */ | |
ef416fc2 | 4784 | |
b423cd4c | 4785 | cupsFilePrintf(out, "{%s}", attrname); |
4786 | } | |
ef416fc2 | 4787 | |
b423cd4c | 4788 | continue; |
4789 | } | |
ef416fc2 | 4790 | |
b423cd4c | 4791 | /* |
4792 | * Output value(s)... | |
4793 | */ | |
ef416fc2 | 4794 | |
b423cd4c | 4795 | for (i = 0; i < attr->num_values; i ++) |
4796 | { | |
4797 | if (i) | |
4798 | cupsFilePutChar(out, ','); | |
ef416fc2 | 4799 | |
b423cd4c | 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; | |
ef416fc2 | 4809 | |
b423cd4c | 4810 | case IPP_TAG_BOOLEAN : |
4811 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
4812 | break; | |
ef416fc2 | 4813 | |
b423cd4c | 4814 | case IPP_TAG_NOVALUE : |
4815 | cupsFilePuts(out, "novalue"); | |
4816 | break; | |
ef416fc2 | 4817 | |
b423cd4c | 4818 | case IPP_TAG_RANGE : |
4819 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
4820 | attr->values[i].range.upper); | |
4821 | break; | |
fa73b229 | 4822 | |
b423cd4c | 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; | |
fa73b229 | 4829 | |
b423cd4c | 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 | */ | |
fa73b229 | 4842 | |
b423cd4c | 4843 | const char *p; |
fa73b229 | 4844 | |
b423cd4c | 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; | |
fa73b229 | 4861 | |
b423cd4c | 4862 | default : |
4863 | break; /* anti-compiler-warning-code */ | |
4864 | } | |
4865 | } | |
4866 | } | |
4867 | else if (ch == '\\') /* Quoted char */ | |
4868 | { | |
4869 | ch = cupsFileGetChar(in); | |
fa73b229 | 4870 | |
b423cd4c | 4871 | if (ch != '{') /* Only do special handling for \{ */ |
4872 | cupsFilePutChar(out, '\\'); | |
fa73b229 | 4873 | |
b423cd4c | 4874 | cupsFilePutChar(out, ch); |
4875 | } | |
4876 | else | |
4877 | cupsFilePutChar(out, ch); | |
fa73b229 | 4878 | |
b423cd4c | 4879 | cupsFileClose(in); |
fa73b229 | 4880 | |
b423cd4c | 4881 | kbytes = (cupsFileTell(out) + 1023) / 1024; |
fa73b229 | 4882 | |
b423cd4c | 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); | |
fa73b229 | 4890 | } |
4891 | ||
4892 | ||
4893 | /* | |
b423cd4c | 4894 | * 'copy_file()' - Copy a PPD file or interface script... |
fa73b229 | 4895 | */ |
4896 | ||
b423cd4c | 4897 | static int /* O - 0 = success, -1 = error */ |
4898 | copy_file(const char *from, /* I - Source file */ | |
4899 | const char *to) /* I - Destination file */ | |
fa73b229 | 4900 | { |
b423cd4c | 4901 | cups_file_t *src, /* Source file */ |
4902 | *dst; /* Destination file */ | |
4903 | int bytes; /* Bytes to read/write */ | |
4904 | char buffer[2048]; /* Copy buffer */ | |
fa73b229 | 4905 | |
4906 | ||
b423cd4c | 4907 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); |
4908 | ||
fa73b229 | 4909 | /* |
b423cd4c | 4910 | * Open the source and destination file for a copy... |
fa73b229 | 4911 | */ |
4912 | ||
b423cd4c | 4913 | if ((src = cupsFileOpen(from, "rb")) == NULL) |
4914 | return (-1); | |
fa73b229 | 4915 | |
b423cd4c | 4916 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
4917 | { | |
4918 | cupsFileClose(src); | |
4919 | return (-1); | |
4920 | } | |
fa73b229 | 4921 | |
b423cd4c | 4922 | /* |
4923 | * Copy the source file to the destination... | |
4924 | */ | |
fa73b229 | 4925 | |
b423cd4c | 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 | } | |
fa73b229 | 4933 | |
b423cd4c | 4934 | /* |
4935 | * Close both files and return... | |
4936 | */ | |
fa73b229 | 4937 | |
b423cd4c | 4938 | cupsFileClose(src); |
fa73b229 | 4939 | |
b423cd4c | 4940 | return (cupsFileClose(dst)); |
4941 | } | |
fa73b229 | 4942 | |
fa73b229 | 4943 | |
b423cd4c | 4944 | /* |
4945 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
4946 | * as needed... | |
4947 | */ | |
e00b005a | 4948 | |
b423cd4c | 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 | { | |
f7deaa1a | 4954 | fd_set input; /* select() input set */ |
b423cd4c | 4955 | struct timeval timeout; /* select() timeout */ |
bc44d920 | 4956 | int maxfd; /* Max file descriptor for select() */ |
b423cd4c | 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 */ | |
2abf387c | 4965 | ppd_file_t *ppd; /* PPD file */ |
b423cd4c | 4966 | int bytes, /* Bytes from pipe */ |
4967 | total; /* Total bytes from pipe */ | |
2abf387c | 4968 | char buffer[2048]; /* Copy buffer */ |
b423cd4c | 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 */ | |
2abf387c | 4973 | cups_option_t *defaults; /* Default options */ |
b423cd4c | 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 */ | |
e00b005a | 4982 | |
e00b005a | 4983 | |
b423cd4c | 4984 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
4985 | "copy_model(con=%p, from=\"%s\", to=\"%s\")", | |
4986 | con, from, to); | |
e00b005a | 4987 | |
b423cd4c | 4988 | /* |
4989 | * Run cups-driverd to get the PPD file... | |
4990 | */ | |
e00b005a | 4991 | |
b423cd4c | 4992 | argv[0] = "cups-driverd"; |
4993 | argv[1] = "cat"; | |
4994 | argv[2] = (char *)from; | |
4995 | argv[3] = NULL; | |
e00b005a | 4996 | |
b423cd4c | 4997 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
e00b005a | 4998 | |
b423cd4c | 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 | ||
b423cd4c | 5007 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
5008 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
fa73b229 | 5009 | |
b423cd4c | 5010 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], |
a4924f6c | 5011 | -1, -1, 0, DefaultProfile, &temppid)) |
fa73b229 | 5012 | { |
b423cd4c | 5013 | close(tempfd); |
5014 | unlink(tempfile); | |
a4924f6c | 5015 | |
b423cd4c | 5016 | return (-1); |
fa73b229 | 5017 | } |
5018 | ||
b423cd4c | 5019 | close(temppipe[1]); |
fa73b229 | 5020 | |
b423cd4c | 5021 | /* |
5022 | * Wait up to 30 seconds for the PPD file to be copied... | |
5023 | */ | |
fa73b229 | 5024 | |
b423cd4c | 5025 | total = 0; |
fa73b229 | 5026 | |
b423cd4c | 5027 | if (temppipe[0] > CGIPipes[0]) |
5028 | maxfd = temppipe[0] + 1; | |
5029 | else | |
5030 | maxfd = CGIPipes[0] + 1; | |
ef416fc2 | 5031 | |
b423cd4c | 5032 | for (;;) |
5033 | { | |
5034 | /* | |
5035 | * See if we have data ready... | |
5036 | */ | |
ef416fc2 | 5037 | |
b423cd4c | 5038 | bytes = 0; |
ef416fc2 | 5039 | |
f7deaa1a | 5040 | FD_ZERO(&input); |
5041 | FD_SET(temppipe[0], &input); | |
5042 | FD_SET(CGIPipes[0], &input); | |
ef416fc2 | 5043 | |
b423cd4c | 5044 | timeout.tv_sec = 30; |
5045 | timeout.tv_usec = 0; | |
5046 | ||
f7deaa1a | 5047 | if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0) |
b423cd4c | 5048 | { |
5049 | if (errno == EINTR) | |
5050 | continue; | |
5051 | else | |
5052 | break; | |
5053 | } | |
5054 | else if (i == 0) | |
ef416fc2 | 5055 | { |
5056 | /* | |
b423cd4c | 5057 | * We have timed out... |
ef416fc2 | 5058 | */ |
5059 | ||
b423cd4c | 5060 | break; |
ef416fc2 | 5061 | } |
b423cd4c | 5062 | |
f7deaa1a | 5063 | if (FD_ISSET(temppipe[0], &input)) |
ef416fc2 | 5064 | { |
5065 | /* | |
b423cd4c | 5066 | * Read the PPD file from the pipe, and write it to the PPD file. |
ef416fc2 | 5067 | */ |
5068 | ||
b423cd4c | 5069 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) |
5070 | { | |
5071 | if (write(tempfd, buffer, bytes) < bytes) | |
5072 | break; | |
ef416fc2 | 5073 | |
b423cd4c | 5074 | total += bytes; |
5075 | } | |
5076 | else | |
5077 | break; | |
ef416fc2 | 5078 | } |
b423cd4c | 5079 | |
f7deaa1a | 5080 | if (FD_ISSET(CGIPipes[0], &input)) |
b423cd4c | 5081 | cupsdUpdateCGI(); |
ef416fc2 | 5082 | } |
5083 | ||
b423cd4c | 5084 | close(temppipe[0]); |
5085 | close(tempfd); | |
ef416fc2 | 5086 | |
b423cd4c | 5087 | if (!total) |
ef416fc2 | 5088 | { |
b423cd4c | 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); | |
ef416fc2 | 5096 | } |
5097 | ||
b423cd4c | 5098 | /* |
5099 | * Read the source file and see what page sizes are supported... | |
5100 | */ | |
fa73b229 | 5101 | |
2abf387c | 5102 | if ((ppd = ppdOpenFile(tempfile)) == NULL) |
b423cd4c | 5103 | { |
5104 | unlink(tempfile); | |
5105 | return (-1); | |
5106 | } | |
fa73b229 | 5107 | |
2abf387c | 5108 | have_letter = ppdPageSize(ppd, "Letter") != NULL; |
5109 | have_a4 = ppdPageSize(ppd, "A4") != NULL; | |
ef416fc2 | 5110 | |
5111 | /* | |
b423cd4c | 5112 | * Open the destination (if possible) and set the default options... |
ef416fc2 | 5113 | */ |
5114 | ||
b423cd4c | 5115 | num_defaults = 0; |
5116 | defaults = NULL; | |
5117 | cups_protocol[0] = '\0'; | |
ef416fc2 | 5118 | |
b423cd4c | 5119 | if ((dst = cupsFileOpen(to, "rb")) != NULL) |
ef416fc2 | 5120 | { |
5121 | /* | |
b423cd4c | 5122 | * Read all of the default lines from the old PPD... |
ef416fc2 | 5123 | */ |
5124 | ||
b423cd4c | 5125 | while (cupsFileGets(dst, buffer, sizeof(buffer))) |
5126 | if (!strncmp(buffer, "*Default", 8)) | |
5127 | { | |
5128 | /* | |
5129 | * Add the default option... | |
5130 | */ | |
ef416fc2 | 5131 | |
b423cd4c | 5132 | if (!ppd_parse_line(buffer, option, sizeof(option), |
5133 | choice, sizeof(choice))) | |
2abf387c | 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, | |
b423cd4c | 5147 | &defaults); |
2abf387c | 5148 | } |
b423cd4c | 5149 | } |
5150 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
5151 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
ef416fc2 | 5152 | |
b423cd4c | 5153 | cupsFileClose(dst); |
ef416fc2 | 5154 | } |
b423cd4c | 5155 | #ifdef HAVE_LIBPAPER |
5156 | else if ((paper_result = systempapername()) != NULL) | |
5157 | { | |
5158 | /* | |
5159 | * Set the default media sizes from the systemwide default... | |
5160 | */ | |
ef416fc2 | 5161 | |
b423cd4c | 5162 | strlcpy(system_paper, paper_result, sizeof(system_paper)); |
5163 | system_paper[0] = toupper(system_paper[0] & 255); | |
ef416fc2 | 5164 | |
b423cd4c | 5165 | if ((!strcmp(system_paper, "Letter") && have_letter) || |
5166 | (!strcmp(system_paper, "A4") && have_a4)) | |
5167 | { | |
f7deaa1a | 5168 | num_defaults = cupsAddOption("PageSize", system_paper, |
26d47ec6 | 5169 | num_defaults, &defaults); |
f7deaa1a | 5170 | num_defaults = cupsAddOption("PageRegion", system_paper, |
26d47ec6 | 5171 | num_defaults, &defaults); |
f7deaa1a | 5172 | num_defaults = cupsAddOption("PaperDimension", system_paper, |
26d47ec6 | 5173 | num_defaults, &defaults); |
f7deaa1a | 5174 | num_defaults = cupsAddOption("ImageableArea", system_paper, |
26d47ec6 | 5175 | num_defaults, &defaults); |
b423cd4c | 5176 | } |
ef416fc2 | 5177 | } |
b423cd4c | 5178 | #endif /* HAVE_LIBPAPER */ |
5179 | else | |
ef416fc2 | 5180 | { |
b423cd4c | 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") || | |
2abf387c | 5194 | !strncasecmp(DefaultLanguage, "en.", 3) || |
b423cd4c | 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 | { | |
2abf387c | 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); | |
b423cd4c | 5213 | } |
5214 | } | |
5215 | else if (have_a4) | |
5216 | { | |
5217 | /* | |
5218 | * The rest default to "a4" size... | |
5219 | */ | |
5220 | ||
2abf387c | 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); | |
b423cd4c | 5229 | } |
ef416fc2 | 5230 | } |
5231 | ||
2abf387c | 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 | ||
ef416fc2 | 5245 | /* |
b423cd4c | 5246 | * Open the destination file for a copy... |
ef416fc2 | 5247 | */ |
5248 | ||
b423cd4c | 5249 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
ef416fc2 | 5250 | { |
2abf387c | 5251 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 5252 | cupsFileClose(src); |
5253 | unlink(tempfile); | |
5254 | return (-1); | |
ef416fc2 | 5255 | } |
5256 | ||
5257 | /* | |
b423cd4c | 5258 | * Copy the source file to the destination... |
ef416fc2 | 5259 | */ |
5260 | ||
b423cd4c | 5261 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
ef416fc2 | 5262 | { |
b423cd4c | 5263 | if (!strncmp(buffer, "*Default", 8)) |
ef416fc2 | 5264 | { |
b423cd4c | 5265 | /* |
5266 | * Check for an previous default option choice... | |
5267 | */ | |
ef416fc2 | 5268 | |
b423cd4c | 5269 | if (!ppd_parse_line(buffer, option, sizeof(option), |
5270 | choice, sizeof(choice))) | |
ef416fc2 | 5271 | { |
2abf387c | 5272 | const char *val; /* Default option value */ |
ef416fc2 | 5273 | |
2abf387c | 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 | } | |
b423cd4c | 5283 | } |
ef416fc2 | 5284 | } |
b423cd4c | 5285 | |
5286 | cupsFilePrintf(dst, "%s\n", buffer); | |
ef416fc2 | 5287 | } |
5288 | ||
b423cd4c | 5289 | if (cups_protocol[0]) |
5290 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
5291 | ||
2abf387c | 5292 | cupsFreeOptions(num_defaults, defaults); |
b423cd4c | 5293 | |
ef416fc2 | 5294 | /* |
b423cd4c | 5295 | * Close both files and return... |
ef416fc2 | 5296 | */ |
5297 | ||
b423cd4c | 5298 | cupsFileClose(src); |
5299 | ||
5300 | unlink(tempfile); | |
5301 | ||
5302 | return (cupsFileClose(dst)); | |
5303 | } | |
5304 | ||
5305 | ||
5306 | /* | |
5307 | * 'copy_job_attrs()' - Copy job attributes. | |
5308 | */ | |
ef416fc2 | 5309 | |
b423cd4c | 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 */ | |
ef416fc2 | 5316 | |
ef416fc2 | 5317 | |
5318 | /* | |
b423cd4c | 5319 | * Send the requested attributes for each job... |
ef416fc2 | 5320 | */ |
5321 | ||
b423cd4c | 5322 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
5323 | con->servername, con->serverport, "/jobs/%d", | |
5324 | job->id); | |
ef416fc2 | 5325 | |
2e4ff8af MS |
5326 | if (!ra || cupsArrayFind(ra, "document-count")) |
5327 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
5328 | "document-count", job->num_files); | |
5329 | ||
b423cd4c | 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); | |
ef416fc2 | 5333 | |
b423cd4c | 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); | |
ef416fc2 | 5338 | |
b423cd4c | 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)); | |
ef416fc2 | 5342 | |
b423cd4c | 5343 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) |
5344 | add_job_state_reasons(con, job); | |
bd7854cb | 5345 | |
b423cd4c | 5346 | if (!ra || cupsArrayFind(ra, "job-uri")) |
5347 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
5348 | "job-uri", NULL, job_uri); | |
ef416fc2 | 5349 | |
b423cd4c | 5350 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0); |
5351 | } | |
ef416fc2 | 5352 | |
ef416fc2 | 5353 | |
b423cd4c | 5354 | /* |
5355 | * 'copy_printer_attrs()' - Copy printer attributes. | |
5356 | */ | |
ef416fc2 | 5357 | |
b423cd4c | 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 */ | |
ef416fc2 | 5369 | |
ef416fc2 | 5370 | |
b423cd4c | 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 | */ | |
ef416fc2 | 5375 | |
b423cd4c | 5376 | curtime = time(NULL); |
ef416fc2 | 5377 | |
b423cd4c | 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__ */ | |
ef416fc2 | 5385 | |
5a738aea MS |
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 | ||
323c5de1 | 5390 | if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert"))) |
5391 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING, | |
5392 | "printer-alert", NULL, printer->alert); | |
5393 | ||
5394 | if (printer->alert_description && | |
5395 | (!ra || cupsArrayFind(ra, "printer-alert-description"))) | |
5396 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5397 | "printer-alert-description", NULL, | |
5398 | printer->alert_description); | |
5399 | ||
b423cd4c | 5400 | if (!ra || cupsArrayFind(ra, "printer-current-time")) |
5401 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", | |
5402 | ippTimeToDate(curtime)); | |
ef416fc2 | 5403 | |
d9bca400 MS |
5404 | #ifdef HAVE_DNSSD |
5405 | if (!ra || cupsArrayFind(ra, "printer-dns-sd-name")) | |
5406 | { | |
5407 | if (printer->reg_name) | |
5408 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5409 | "printer-dns-sd-name", NULL, printer->reg_name); | |
5410 | else | |
5411 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE, | |
5412 | "printer-dns-sd-name", 0); | |
5413 | } | |
5414 | #endif /* HAVE_DNSSD */ | |
5415 | ||
b423cd4c | 5416 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) |
5417 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5418 | "printer-error-policy", NULL, printer->error_policy); | |
ef416fc2 | 5419 | |
b423cd4c | 5420 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) |
5421 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", | |
5422 | printer->accepting); | |
ef416fc2 | 5423 | |
b423cd4c | 5424 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) |
5425 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", | |
5426 | printer->shared); | |
ef416fc2 | 5427 | |
b423cd4c | 5428 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) |
5429 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
5430 | "printer-op-policy", NULL, printer->op_policy); | |
5431 | ||
5432 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
5433 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
5434 | printer->state); | |
5435 | ||
5436 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) | |
5437 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5438 | "printer-state-change-time", printer->state_time); | |
f7deaa1a | 5439 | |
b423cd4c | 5440 | if (MaxPrinterHistory > 0 && printer->num_history > 0 && |
5441 | cupsArrayFind(ra, "printer-state-history")) | |
ef416fc2 | 5442 | { |
5443 | /* | |
b423cd4c | 5444 | * Printer history is only sent if specifically requested, so that |
5445 | * older CUPS/IPP clients won't barf on the collection attributes. | |
ef416fc2 | 5446 | */ |
5447 | ||
b423cd4c | 5448 | history = ippAddCollections(con->response, IPP_TAG_PRINTER, |
5449 | "printer-state-history", | |
5450 | printer->num_history, NULL); | |
5451 | ||
5452 | for (i = 0; i < printer->num_history; i ++) | |
5453 | copy_attrs(history->values[i].collection = ippNew(), printer->history[i], | |
5454 | NULL, IPP_TAG_ZERO, 0); | |
ef416fc2 | 5455 | } |
5456 | ||
b423cd4c | 5457 | if (!ra || cupsArrayFind(ra, "printer-state-message")) |
5458 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
5459 | "printer-state-message", NULL, printer->state_message); | |
ef416fc2 | 5460 | |
b423cd4c | 5461 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) |
5462 | add_printer_state_reasons(con, printer); | |
ef416fc2 | 5463 | |
b423cd4c | 5464 | if (!ra || cupsArrayFind(ra, "printer-type")) |
5465 | { | |
5466 | int type; /* printer-type value */ | |
ef416fc2 | 5467 | |
ef416fc2 | 5468 | |
ef416fc2 | 5469 | /* |
b423cd4c | 5470 | * Add the CUPS-specific printer-type attribute... |
ef416fc2 | 5471 | */ |
5472 | ||
09a101d6 | 5473 | type = printer->type; |
b423cd4c | 5474 | |
5475 | if (printer == DefaultPrinter) | |
5476 | type |= CUPS_PRINTER_DEFAULT; | |
5477 | ||
5478 | if (!printer->accepting) | |
5479 | type |= CUPS_PRINTER_REJECTING; | |
5480 | ||
5481 | if (!printer->shared) | |
5482 | type |= CUPS_PRINTER_NOT_SHARED; | |
5483 | ||
5484 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", | |
5485 | type); | |
ef416fc2 | 5486 | } |
ef416fc2 | 5487 | |
b423cd4c | 5488 | if (!ra || cupsArrayFind(ra, "printer-up-time")) |
5489 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
5490 | "printer-up-time", curtime); | |
ef416fc2 | 5491 | |
b423cd4c | 5492 | if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) && |
5493 | !ippFindAttribute(printer->attrs, "printer-uri-supported", | |
5494 | IPP_TAG_URI)) | |
ef416fc2 | 5495 | { |
b423cd4c | 5496 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), |
5497 | "ipp", NULL, con->servername, con->serverport, | |
8ca02f3c | 5498 | (printer->type & CUPS_PRINTER_CLASS) ? |
5499 | "/classes/%s" : "/printers/%s", printer->name); | |
b423cd4c | 5500 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, |
5501 | "printer-uri-supported", NULL, printer_uri); | |
5502 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", | |
5503 | printer_uri); | |
5504 | } | |
ef416fc2 | 5505 | |
b423cd4c | 5506 | if (!ra || cupsArrayFind(ra, "queued-job-count")) |
5507 | add_queued_job_count(con, printer); | |
ef416fc2 | 5508 | |
b423cd4c | 5509 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0); |
5510 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY); | |
5511 | } | |
ef416fc2 | 5512 | |
ef416fc2 | 5513 | |
b423cd4c | 5514 | /* |
5515 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
5516 | */ | |
ef416fc2 | 5517 | |
b423cd4c | 5518 | static void |
5519 | copy_subscription_attrs( | |
5520 | cupsd_client_t *con, /* I - Client connection */ | |
5521 | cupsd_subscription_t *sub, /* I - Subscription */ | |
5522 | cups_array_t *ra) /* I - Requested attributes array */ | |
5523 | { | |
5524 | ipp_attribute_t *attr; /* Current attribute */ | |
5525 | char printer_uri[HTTP_MAX_URI]; | |
5526 | /* Printer URI */ | |
5527 | int count; /* Number of events */ | |
5528 | unsigned mask; /* Current event mask */ | |
5529 | const char *name; /* Current event name */ | |
ef416fc2 | 5530 | |
ef416fc2 | 5531 | |
b423cd4c | 5532 | /* |
5533 | * Copy the subscription attributes to the response using the | |
5534 | * requested-attributes attribute that may be provided by the client. | |
5535 | */ | |
ef416fc2 | 5536 | |
b423cd4c | 5537 | if (!ra || cupsArrayFind(ra, "notify-events")) |
5538 | { | |
5539 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
5540 | { | |
5541 | /* | |
5542 | * Simple event list... | |
5543 | */ | |
ef416fc2 | 5544 | |
b423cd4c | 5545 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, |
d09495fa | 5546 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), |
b423cd4c | 5547 | "notify-events", NULL, name); |
5548 | } | |
5549 | else | |
5550 | { | |
5551 | /* | |
5552 | * Complex event list... | |
5553 | */ | |
ef416fc2 | 5554 | |
b423cd4c | 5555 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5556 | if (sub->mask & mask) | |
5557 | count ++; | |
ef416fc2 | 5558 | |
b423cd4c | 5559 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, |
d09495fa | 5560 | (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY), |
b423cd4c | 5561 | "notify-events", count, NULL, NULL); |
ef416fc2 | 5562 | |
b423cd4c | 5563 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
5564 | if (sub->mask & mask) | |
5565 | { | |
5566 | attr->values[count].string.text = | |
5567 | (char *)cupsdEventName((cupsd_eventmask_t)mask); | |
ef416fc2 | 5568 | |
b423cd4c | 5569 | count ++; |
5570 | } | |
ef416fc2 | 5571 | } |
b423cd4c | 5572 | } |
ef416fc2 | 5573 | |
b423cd4c | 5574 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) |
5575 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5576 | "notify-job-id", sub->job->id); | |
ef416fc2 | 5577 | |
b423cd4c | 5578 | if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))) |
5579 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5580 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 5581 | |
b423cd4c | 5582 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) |
5583 | { | |
5584 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
5585 | "ipp", NULL, con->servername, con->serverport, | |
5586 | "/printers/%s", sub->dest->name); | |
5587 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5588 | "notify-printer-uri", NULL, printer_uri); | |
ef416fc2 | 5589 | } |
ef416fc2 | 5590 | |
b423cd4c | 5591 | if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))) |
5592 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
5593 | "notify-recipient-uri", NULL, sub->recipient); | |
5594 | else if (!ra || cupsArrayFind(ra, "notify-pull-method")) | |
5595 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
5596 | "notify-pull-method", NULL, "ippget"); | |
ef416fc2 | 5597 | |
b423cd4c | 5598 | if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")) |
5599 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
5600 | "notify-subscriber-user-name", NULL, sub->owner); | |
ef416fc2 | 5601 | |
b423cd4c | 5602 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) |
5603 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5604 | "notify-subscription-id", sub->id); | |
ef416fc2 | 5605 | |
b423cd4c | 5606 | if (!ra || cupsArrayFind(ra, "notify-time-interval")) |
5607 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
5608 | "notify-time-interval", sub->interval); | |
ef416fc2 | 5609 | |
b423cd4c | 5610 | if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data"))) |
5611 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
5612 | sub->user_data, sub->user_data_len); | |
5613 | } | |
ef416fc2 | 5614 | |
ef416fc2 | 5615 | |
b423cd4c | 5616 | /* |
5617 | * 'create_job()' - Print a file to a printer or class. | |
5618 | */ | |
5619 | ||
5620 | static void | |
5621 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
5622 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5623 | { | |
f7deaa1a | 5624 | cupsd_printer_t *printer; /* Printer */ |
5625 | cupsd_job_t *job; /* New job */ | |
b423cd4c | 5626 | |
ef416fc2 | 5627 | |
b423cd4c | 5628 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, |
5629 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5630 | |
f7deaa1a | 5631 | /* |
5632 | * Is the destination valid? | |
5633 | */ | |
5634 | ||
5635 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
5636 | { | |
5637 | /* | |
5638 | * Bad URI... | |
5639 | */ | |
5640 | ||
5641 | send_ipp_status(con, IPP_NOT_FOUND, | |
5642 | _("The printer or class was not found.")); | |
5643 | return; | |
5644 | } | |
5645 | ||
ef416fc2 | 5646 | /* |
b423cd4c | 5647 | * Create the job object... |
ef416fc2 | 5648 | */ |
5649 | ||
f7deaa1a | 5650 | if ((job = add_job(con, printer, NULL)) == NULL) |
b423cd4c | 5651 | return; |
ef416fc2 | 5652 | |
09a101d6 | 5653 | job->pending_timeout = 1; |
5654 | ||
ef416fc2 | 5655 | /* |
5656 | * Save and log the job... | |
5657 | */ | |
f7deaa1a | 5658 | |
09a101d6 | 5659 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Queued on \"%s\" by \"%s\".", |
ef416fc2 | 5660 | job->id, job->dest, job->username); |
ef416fc2 | 5661 | } |
5662 | ||
5663 | ||
5664 | /* | |
5665 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
5666 | */ | |
5667 | ||
5668 | static cups_array_t * /* O - Array of attributes or NULL */ | |
5669 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
5670 | { | |
5671 | int i; /* Looping var */ | |
5672 | ipp_attribute_t *requested; /* requested-attributes attribute */ | |
5673 | cups_array_t *ra; /* Requested attributes array */ | |
5674 | char *value; /* Current value */ | |
5675 | ||
5676 | ||
5677 | /* | |
5678 | * Get the requested-attributes attribute, and return NULL if we don't | |
5679 | * have one... | |
5680 | */ | |
5681 | ||
5682 | if ((requested = ippFindAttribute(request, "requested-attributes", | |
5683 | IPP_TAG_KEYWORD)) == NULL) | |
5684 | return (NULL); | |
5685 | ||
5686 | /* | |
5687 | * If the attribute contains a single "all" keyword, return NULL... | |
5688 | */ | |
5689 | ||
5690 | if (requested->num_values == 1 && | |
5691 | !strcmp(requested->values[0].string.text, "all")) | |
5692 | return (NULL); | |
5693 | ||
5694 | /* | |
5695 | * Create an array using "strcmp" as the comparison function... | |
5696 | */ | |
5697 | ||
5698 | ra = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
5699 | ||
5700 | for (i = 0; i < requested->num_values; i ++) | |
5701 | { | |
5702 | value = requested->values[i].string.text; | |
5703 | ||
5704 | if (!strcmp(value, "job-template")) | |
5705 | { | |
5706 | cupsArrayAdd(ra, "copies"); | |
5707 | cupsArrayAdd(ra, "copies-default"); | |
5708 | cupsArrayAdd(ra, "copies-supported"); | |
5709 | cupsArrayAdd(ra, "finishings"); | |
5710 | cupsArrayAdd(ra, "finishings-default"); | |
5711 | cupsArrayAdd(ra, "finishings-supported"); | |
5712 | cupsArrayAdd(ra, "job-hold-until"); | |
5713 | cupsArrayAdd(ra, "job-hold-until-default"); | |
5714 | cupsArrayAdd(ra, "job-hold-until-supported"); | |
5715 | cupsArrayAdd(ra, "job-priority"); | |
5716 | cupsArrayAdd(ra, "job-priority-default"); | |
5717 | cupsArrayAdd(ra, "job-priority-supported"); | |
5718 | cupsArrayAdd(ra, "job-sheets"); | |
5719 | cupsArrayAdd(ra, "job-sheets-default"); | |
5720 | cupsArrayAdd(ra, "job-sheets-supported"); | |
5721 | cupsArrayAdd(ra, "media"); | |
5722 | cupsArrayAdd(ra, "media-default"); | |
5723 | cupsArrayAdd(ra, "media-supported"); | |
5724 | cupsArrayAdd(ra, "multiple-document-handling"); | |
5725 | cupsArrayAdd(ra, "multiple-document-handling-default"); | |
5726 | cupsArrayAdd(ra, "multiple-document-handling-supported"); | |
5727 | cupsArrayAdd(ra, "number-up"); | |
5728 | cupsArrayAdd(ra, "number-up-default"); | |
5729 | cupsArrayAdd(ra, "number-up-supported"); | |
5730 | cupsArrayAdd(ra, "orientation-requested"); | |
5731 | cupsArrayAdd(ra, "orientation-requested-default"); | |
5732 | cupsArrayAdd(ra, "orientation-requested-supported"); | |
5733 | cupsArrayAdd(ra, "page-ranges"); | |
5734 | cupsArrayAdd(ra, "page-ranges-supported"); | |
5735 | cupsArrayAdd(ra, "printer-resolution"); | |
5736 | cupsArrayAdd(ra, "printer-resolution-default"); | |
5737 | cupsArrayAdd(ra, "printer-resolution-supported"); | |
5738 | cupsArrayAdd(ra, "print-quality"); | |
5739 | cupsArrayAdd(ra, "print-quality-default"); | |
5740 | cupsArrayAdd(ra, "print-quality-supported"); | |
5741 | cupsArrayAdd(ra, "sides"); | |
5742 | cupsArrayAdd(ra, "sides-default"); | |
5743 | cupsArrayAdd(ra, "sides-supported"); | |
5744 | } | |
5745 | else if (!strcmp(value, "job-description")) | |
5746 | { | |
5747 | cupsArrayAdd(ra, "date-time-at-completed"); | |
5748 | cupsArrayAdd(ra, "date-time-at-creation"); | |
5749 | cupsArrayAdd(ra, "date-time-at-processing"); | |
5750 | cupsArrayAdd(ra, "job-detailed-status-message"); | |
5751 | cupsArrayAdd(ra, "job-document-access-errors"); | |
5752 | cupsArrayAdd(ra, "job-id"); | |
5753 | cupsArrayAdd(ra, "job-impressions"); | |
5754 | cupsArrayAdd(ra, "job-impressions-completed"); | |
5755 | cupsArrayAdd(ra, "job-k-octets"); | |
5756 | cupsArrayAdd(ra, "job-k-octets-processed"); | |
5757 | cupsArrayAdd(ra, "job-media-sheets"); | |
5758 | cupsArrayAdd(ra, "job-media-sheets-completed"); | |
5759 | cupsArrayAdd(ra, "job-message-from-operator"); | |
5760 | cupsArrayAdd(ra, "job-more-info"); | |
5761 | cupsArrayAdd(ra, "job-name"); | |
5762 | cupsArrayAdd(ra, "job-originating-user-name"); | |
5763 | cupsArrayAdd(ra, "job-printer-up-time"); | |
5764 | cupsArrayAdd(ra, "job-printer-uri"); | |
5765 | cupsArrayAdd(ra, "job-state"); | |
5766 | cupsArrayAdd(ra, "job-state-message"); | |
5767 | cupsArrayAdd(ra, "job-state-reasons"); | |
5768 | cupsArrayAdd(ra, "job-uri"); | |
5769 | cupsArrayAdd(ra, "number-of-documents"); | |
5770 | cupsArrayAdd(ra, "number-of-intervening-jobs"); | |
5771 | cupsArrayAdd(ra, "output-device-assigned"); | |
5772 | cupsArrayAdd(ra, "time-at-completed"); | |
5773 | cupsArrayAdd(ra, "time-at-creation"); | |
5774 | cupsArrayAdd(ra, "time-at-processing"); | |
5775 | } | |
5776 | else if (!strcmp(value, "printer-description")) | |
5777 | { | |
5778 | cupsArrayAdd(ra, "charset-configured"); | |
5779 | cupsArrayAdd(ra, "charset-supported"); | |
5780 | cupsArrayAdd(ra, "color-supported"); | |
5781 | cupsArrayAdd(ra, "compression-supported"); | |
5782 | cupsArrayAdd(ra, "document-format-default"); | |
5783 | cupsArrayAdd(ra, "document-format-supported"); | |
5784 | cupsArrayAdd(ra, "generated-natural-language-supported"); | |
5785 | cupsArrayAdd(ra, "ipp-versions-supported"); | |
5786 | cupsArrayAdd(ra, "job-impressions-supported"); | |
5787 | cupsArrayAdd(ra, "job-k-octets-supported"); | |
5788 | cupsArrayAdd(ra, "job-media-sheets-supported"); | |
5789 | cupsArrayAdd(ra, "multiple-document-jobs-supported"); | |
5790 | cupsArrayAdd(ra, "multiple-operation-time-out"); | |
5791 | cupsArrayAdd(ra, "natural-language-configured"); | |
5792 | cupsArrayAdd(ra, "notify-attributes-supported"); | |
5793 | cupsArrayAdd(ra, "notify-lease-duration-default"); | |
5794 | cupsArrayAdd(ra, "notify-lease-duration-supported"); | |
5795 | cupsArrayAdd(ra, "notify-max-events-supported"); | |
f301802f | 5796 | cupsArrayAdd(ra, "notify-events-default"); |
5797 | cupsArrayAdd(ra, "notify-events-supported"); | |
ef416fc2 | 5798 | cupsArrayAdd(ra, "notify-pull-method-supported"); |
5799 | cupsArrayAdd(ra, "notify-schemes-supported"); | |
5800 | cupsArrayAdd(ra, "operations-supported"); | |
5801 | cupsArrayAdd(ra, "pages-per-minute"); | |
5802 | cupsArrayAdd(ra, "pages-per-minute-color"); | |
5803 | cupsArrayAdd(ra, "pdl-override-supported"); | |
323c5de1 | 5804 | cupsArrayAdd(ra, "printer-alert"); |
5805 | cupsArrayAdd(ra, "printer-alert-description"); | |
01ce6322 | 5806 | cupsArrayAdd(ra, "printer-commands"); |
ef416fc2 | 5807 | cupsArrayAdd(ra, "printer-current-time"); |
5808 | cupsArrayAdd(ra, "printer-driver-installer"); | |
01ce6322 | 5809 | cupsArrayAdd(ra, "printer-dns-sd-name"); |
ef416fc2 | 5810 | cupsArrayAdd(ra, "printer-info"); |
5811 | cupsArrayAdd(ra, "printer-is-accepting-jobs"); | |
5812 | cupsArrayAdd(ra, "printer-location"); | |
5813 | cupsArrayAdd(ra, "printer-make-and-model"); | |
5814 | cupsArrayAdd(ra, "printer-message-from-operator"); | |
5815 | cupsArrayAdd(ra, "printer-more-info"); | |
5816 | cupsArrayAdd(ra, "printer-more-info-manufacturer"); | |
5817 | cupsArrayAdd(ra, "printer-name"); | |
5818 | cupsArrayAdd(ra, "printer-state"); | |
5819 | cupsArrayAdd(ra, "printer-state-message"); | |
5820 | cupsArrayAdd(ra, "printer-state-reasons"); | |
01ce6322 | 5821 | cupsArrayAdd(ra, "printer-type"); |
ef416fc2 | 5822 | cupsArrayAdd(ra, "printer-up-time"); |
5823 | cupsArrayAdd(ra, "printer-uri-supported"); | |
5824 | cupsArrayAdd(ra, "queued-job-count"); | |
5825 | cupsArrayAdd(ra, "reference-uri-schemes-supported"); | |
5826 | cupsArrayAdd(ra, "uri-authentication-supported"); | |
5827 | cupsArrayAdd(ra, "uri-security-supported"); | |
5828 | } | |
f7deaa1a | 5829 | else if (!strcmp(value, "printer-defaults")) |
5830 | { | |
5831 | char *name; /* Option name */ | |
5832 | ||
5833 | ||
5834 | for (name = (char *)cupsArrayFirst(CommonDefaults); | |
5835 | name; | |
5836 | name = (char *)cupsArrayNext(CommonDefaults)) | |
5837 | cupsArrayAdd(ra, name); | |
5838 | } | |
ef416fc2 | 5839 | else if (!strcmp(value, "subscription-template")) |
5840 | { | |
5841 | cupsArrayAdd(ra, "notify-attributes"); | |
5842 | cupsArrayAdd(ra, "notify-charset"); | |
5843 | cupsArrayAdd(ra, "notify-events"); | |
5844 | cupsArrayAdd(ra, "notify-lease-duration"); | |
5845 | cupsArrayAdd(ra, "notify-natural-language"); | |
5846 | cupsArrayAdd(ra, "notify-pull-method"); | |
5847 | cupsArrayAdd(ra, "notify-recipient-uri"); | |
5848 | cupsArrayAdd(ra, "notify-time-interval"); | |
5849 | cupsArrayAdd(ra, "notify-user-data"); | |
5850 | } | |
5851 | else | |
5852 | cupsArrayAdd(ra, value); | |
5853 | } | |
5854 | ||
5855 | return (ra); | |
5856 | } | |
5857 | ||
5858 | ||
5859 | /* | |
5860 | * 'create_subscription()' - Create a notification subscription. | |
5861 | */ | |
5862 | ||
5863 | static void | |
5864 | create_subscription( | |
5865 | cupsd_client_t *con, /* I - Client connection */ | |
5866 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5867 | { | |
5868 | http_status_t status; /* Policy status */ | |
5869 | int i; /* Looping var */ | |
5870 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 5871 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ed486911 | 5872 | char scheme[HTTP_MAX_URI], |
5873 | /* Scheme portion of URI */ | |
ef416fc2 | 5874 | userpass[HTTP_MAX_URI], |
5875 | /* Username portion of URI */ | |
5876 | host[HTTP_MAX_URI], | |
5877 | /* Host portion of URI */ | |
5878 | resource[HTTP_MAX_URI]; | |
5879 | /* Resource portion of URI */ | |
5880 | int port; /* Port portion of URI */ | |
5881 | cupsd_printer_t *printer; /* Printer/class */ | |
5882 | cupsd_job_t *job; /* Job */ | |
5883 | int jobid; /* Job ID */ | |
5884 | cupsd_subscription_t *sub; /* Subscription object */ | |
bc44d920 | 5885 | const char *username, /* requesting-user-name or |
5886 | authenticated username */ | |
ef416fc2 | 5887 | *recipient, /* notify-recipient-uri */ |
5888 | *pullmethod; /* notify-pull-method */ | |
5889 | ipp_attribute_t *user_data; /* notify-user-data */ | |
5890 | int interval, /* notify-time-interval */ | |
5891 | lease; /* notify-lease-duration */ | |
5892 | unsigned mask; /* notify-events */ | |
f7deaa1a | 5893 | ipp_attribute_t *notify_events,/* notify-events(-default) */ |
5894 | *notify_lease; /* notify-lease-duration(-default) */ | |
ef416fc2 | 5895 | |
5896 | ||
bd7854cb | 5897 | #ifdef DEBUG |
5898 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5899 | { | |
5900 | if (attr->group_tag != IPP_TAG_ZERO) | |
3dfe78b3 | 5901 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag, |
bd7854cb | 5902 | attr->value_tag, attr->name); |
5903 | else | |
3dfe78b3 | 5904 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----"); |
bd7854cb | 5905 | } |
5906 | #endif /* DEBUG */ | |
5907 | ||
ef416fc2 | 5908 | /* |
5909 | * Is the destination valid? | |
5910 | */ | |
5911 | ||
fa73b229 | 5912 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
5913 | "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")", | |
5914 | con, con->http.fd, uri->values[0].string.text); | |
5915 | ||
ed486911 | 5916 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
5917 | sizeof(scheme), userpass, sizeof(userpass), host, | |
a4d04587 | 5918 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 5919 | |
5920 | if (!strcmp(resource, "/")) | |
5921 | { | |
ef416fc2 | 5922 | dtype = (cups_ptype_t)0; |
5923 | printer = NULL; | |
5924 | } | |
5925 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
5926 | { | |
ef416fc2 | 5927 | dtype = (cups_ptype_t)0; |
5928 | printer = NULL; | |
5929 | } | |
5930 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
5931 | { | |
ef416fc2 | 5932 | dtype = CUPS_PRINTER_CLASS; |
5933 | printer = NULL; | |
5934 | } | |
f7deaa1a | 5935 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 5936 | { |
5937 | /* | |
5938 | * Bad URI... | |
5939 | */ | |
5940 | ||
5941 | send_ipp_status(con, IPP_NOT_FOUND, | |
5942 | _("The printer or class was not found.")); | |
5943 | return; | |
5944 | } | |
5945 | ||
5946 | /* | |
5947 | * Check policy... | |
5948 | */ | |
5949 | ||
5950 | if (printer) | |
5951 | { | |
f7deaa1a | 5952 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
5953 | NULL)) != HTTP_OK) | |
ef416fc2 | 5954 | { |
f899b121 | 5955 | send_http_error(con, status, printer); |
ef416fc2 | 5956 | return; |
5957 | } | |
5958 | } | |
5959 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5960 | { | |
f899b121 | 5961 | send_http_error(con, status, NULL); |
ef416fc2 | 5962 | return; |
5963 | } | |
5964 | ||
5965 | /* | |
5966 | * Get the user that is requesting the subscription... | |
5967 | */ | |
5968 | ||
e00b005a | 5969 | username = get_username(con); |
ef416fc2 | 5970 | |
5971 | /* | |
5972 | * Find the first subscription group attribute; return if we have | |
5973 | * none... | |
5974 | */ | |
5975 | ||
5976 | for (attr = con->request->attrs; attr; attr = attr->next) | |
5977 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
5978 | break; | |
5979 | ||
5980 | if (!attr) | |
5981 | { | |
5982 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5983 | _("No subscription attributes in request!")); | |
5984 | return; | |
5985 | } | |
5986 | ||
5987 | /* | |
5988 | * Process the subscription attributes in the request... | |
5989 | */ | |
5990 | ||
fa73b229 | 5991 | con->response->request.status.status_code = IPP_BAD_REQUEST; |
5992 | ||
ef416fc2 | 5993 | while (attr) |
5994 | { | |
5995 | recipient = NULL; | |
5996 | pullmethod = NULL; | |
5997 | user_data = NULL; | |
5998 | interval = 0; | |
5999 | lease = DefaultLeaseDuration; | |
6000 | jobid = 0; | |
6001 | mask = CUPSD_EVENT_NONE; | |
6002 | ||
f7deaa1a | 6003 | if (printer) |
6004 | { | |
6005 | notify_events = ippFindAttribute(printer->attrs, "notify-events-default", | |
6006 | IPP_TAG_KEYWORD); | |
6007 | notify_lease = ippFindAttribute(printer->attrs, | |
6008 | "notify-lease-duration-default", | |
6009 | IPP_TAG_INTEGER); | |
6010 | ||
6011 | if (notify_lease) | |
6012 | lease = notify_lease->values[0].integer; | |
6013 | } | |
6014 | else | |
6015 | { | |
6016 | notify_events = NULL; | |
6017 | notify_lease = NULL; | |
6018 | } | |
6019 | ||
ef416fc2 | 6020 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
6021 | { | |
7594b224 | 6022 | if (!strcmp(attr->name, "notify-recipient-uri") && |
ef416fc2 | 6023 | attr->value_tag == IPP_TAG_URI) |
ed486911 | 6024 | { |
6025 | /* | |
6026 | * Validate the recipient scheme against the ServerBin/notifier | |
6027 | * directory... | |
6028 | */ | |
6029 | ||
6030 | char notifier[1024]; /* Notifier filename */ | |
6031 | ||
6032 | ||
ef416fc2 | 6033 | recipient = attr->values[0].string.text; |
ed486911 | 6034 | |
8ca02f3c | 6035 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, |
6036 | scheme, sizeof(scheme), userpass, sizeof(userpass), | |
6037 | host, sizeof(host), &port, | |
6038 | resource, sizeof(resource)) < HTTP_URI_OK) | |
ed486911 | 6039 | { |
6040 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7594b224 | 6041 | _("Bad notify-recipient-uri URI \"%s\"!"), recipient); |
ed486911 | 6042 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
6043 | "notify-status-code", IPP_URI_SCHEME); | |
6044 | return; | |
6045 | } | |
6046 | ||
6047 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
6048 | scheme); | |
6049 | if (access(notifier, X_OK)) | |
6050 | { | |
6051 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
bc44d920 | 6052 | _("notify-recipient-uri URI \"%s\" uses unknown " |
6053 | "scheme!"), recipient); | |
ed486911 | 6054 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, |
6055 | "notify-status-code", IPP_URI_SCHEME); | |
6056 | return; | |
6057 | } | |
3dfe78b3 MS |
6058 | |
6059 | if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient)) | |
6060 | { | |
6061 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6062 | _("notify-recipient-uri URI \"%s\" is already used!"), | |
6063 | recipient); | |
6064 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6065 | "notify-status-code", IPP_ATTRIBUTES); | |
6066 | return; | |
6067 | } | |
ed486911 | 6068 | } |
ef416fc2 | 6069 | else if (!strcmp(attr->name, "notify-pull-method") && |
6070 | attr->value_tag == IPP_TAG_KEYWORD) | |
ed486911 | 6071 | { |
ef416fc2 | 6072 | pullmethod = attr->values[0].string.text; |
ed486911 | 6073 | |
6074 | if (strcmp(pullmethod, "ippget")) | |
6075 | { | |
6076 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6077 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
6078 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
6079 | "notify-status-code", IPP_ATTRIBUTES); | |
6080 | return; | |
6081 | } | |
6082 | } | |
ef416fc2 | 6083 | else if (!strcmp(attr->name, "notify-charset") && |
6084 | attr->value_tag == IPP_TAG_CHARSET && | |
6085 | strcmp(attr->values[0].string.text, "us-ascii") && | |
6086 | strcmp(attr->values[0].string.text, "utf-8")) | |
6087 | { | |
6088 | send_ipp_status(con, IPP_CHARSET, | |
6089 | _("Character set \"%s\" not supported!"), | |
6090 | attr->values[0].string.text); | |
6091 | return; | |
6092 | } | |
6093 | else if (!strcmp(attr->name, "notify-natural-language") && | |
6094 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
6095 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
6096 | { | |
6097 | send_ipp_status(con, IPP_CHARSET, | |
6098 | _("Language \"%s\" not supported!"), | |
6099 | attr->values[0].string.text); | |
6100 | return; | |
6101 | } | |
6102 | else if (!strcmp(attr->name, "notify-user-data") && | |
6103 | attr->value_tag == IPP_TAG_STRING) | |
6104 | { | |
6105 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
6106 | { | |
6107 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
6108 | _("The notify-user-data value is too large " | |
6109 | "(%d > 63 octets)!"), | |
6110 | attr->values[0].unknown.length); | |
6111 | return; | |
6112 | } | |
6113 | ||
6114 | user_data = attr; | |
6115 | } | |
6116 | else if (!strcmp(attr->name, "notify-events") && | |
6117 | attr->value_tag == IPP_TAG_KEYWORD) | |
f7deaa1a | 6118 | notify_events = attr; |
ef416fc2 | 6119 | else if (!strcmp(attr->name, "notify-lease-duration") && |
6120 | attr->value_tag == IPP_TAG_INTEGER) | |
6121 | lease = attr->values[0].integer; | |
6122 | else if (!strcmp(attr->name, "notify-time-interval") && | |
6123 | attr->value_tag == IPP_TAG_INTEGER) | |
6124 | interval = attr->values[0].integer; | |
6125 | else if (!strcmp(attr->name, "notify-job-id") && | |
6126 | attr->value_tag == IPP_TAG_INTEGER) | |
6127 | jobid = attr->values[0].integer; | |
6128 | ||
6129 | attr = attr->next; | |
6130 | } | |
6131 | ||
f7deaa1a | 6132 | if (notify_events) |
6133 | { | |
6134 | for (i = 0; i < notify_events->num_values; i ++) | |
6135 | mask |= cupsdEventValue(notify_events->values[i].string.text); | |
6136 | } | |
6137 | ||
bd7854cb | 6138 | if (recipient) |
6139 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); | |
6140 | if (pullmethod) | |
6141 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
6142 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
6143 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
fa73b229 | 6144 | |
ef416fc2 | 6145 | if (!recipient && !pullmethod) |
6146 | break; | |
6147 | ||
6148 | if (mask == CUPSD_EVENT_NONE) | |
6149 | { | |
6150 | if (jobid) | |
6151 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
6152 | else if (printer) | |
6153 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
6154 | else | |
6155 | { | |
6156 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6157 | _("notify-events not specified!")); | |
6158 | return; | |
6159 | } | |
6160 | } | |
6161 | ||
bd7854cb | 6162 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) |
ef416fc2 | 6163 | { |
6164 | cupsdLogMessage(CUPSD_LOG_INFO, | |
6165 | "create_subscription: Limiting notify-lease-duration to " | |
6166 | "%d seconds.", | |
6167 | MaxLeaseDuration); | |
6168 | lease = MaxLeaseDuration; | |
6169 | } | |
6170 | ||
6171 | if (jobid) | |
6172 | { | |
6173 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6174 | { | |
6175 | send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid); | |
6176 | return; | |
6177 | } | |
6178 | } | |
6179 | else | |
6180 | job = NULL; | |
6181 | ||
6182 | sub = cupsdAddSubscription(mask, printer, job, recipient, 0); | |
6183 | ||
fa73b229 | 6184 | if (job) |
6185 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
6186 | sub->id, job->id); | |
6187 | else if (printer) | |
6188 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
6189 | "Added subscription %d for printer \"%s\"", | |
6190 | sub->id, printer->name); | |
6191 | else | |
6192 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server", | |
6193 | sub->id); | |
6194 | ||
ef416fc2 | 6195 | sub->interval = interval; |
6196 | sub->lease = lease; | |
bd7854cb | 6197 | sub->expire = lease ? time(NULL) + lease : 0; |
ef416fc2 | 6198 | |
6199 | cupsdSetString(&sub->owner, username); | |
6200 | ||
6201 | if (user_data) | |
6202 | { | |
6203 | sub->user_data_len = user_data->values[0].unknown.length; | |
6204 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
6205 | sub->user_data_len); | |
6206 | } | |
6207 | ||
6208 | ippAddSeparator(con->response); | |
6209 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
6210 | "notify-subscription-id", sub->id); | |
6211 | ||
fa73b229 | 6212 | con->response->request.status.status_code = IPP_OK; |
6213 | ||
ef416fc2 | 6214 | if (attr) |
6215 | attr = attr->next; | |
6216 | } | |
6217 | ||
3dfe78b3 | 6218 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 6219 | } |
6220 | ||
6221 | ||
6222 | /* | |
6223 | * 'delete_printer()' - Remove a printer or class from the system. | |
6224 | */ | |
6225 | ||
6226 | static void | |
6227 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
6228 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
6229 | { | |
6230 | http_status_t status; /* Policy status */ | |
bc44d920 | 6231 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 6232 | cupsd_printer_t *printer; /* Printer/class */ |
6233 | char filename[1024]; /* Script/PPD filename */ | |
6234 | ||
6235 | ||
6236 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
6237 | con->http.fd, uri->values[0].string.text); | |
6238 | ||
6239 | /* | |
6240 | * Do we have a valid URI? | |
6241 | */ | |
6242 | ||
f7deaa1a | 6243 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 6244 | { |
6245 | /* | |
6246 | * Bad URI... | |
6247 | */ | |
6248 | ||
6249 | send_ipp_status(con, IPP_NOT_FOUND, | |
6250 | _("The printer or class was not found.")); | |
6251 | return; | |
6252 | } | |
6253 | ||
6254 | /* | |
6255 | * Check policy... | |
6256 | */ | |
6257 | ||
6258 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6259 | { | |
f899b121 | 6260 | send_http_error(con, status, NULL); |
ef416fc2 | 6261 | return; |
6262 | } | |
6263 | ||
6264 | /* | |
6265 | * Remove old jobs... | |
6266 | */ | |
6267 | ||
f7deaa1a | 6268 | cupsdCancelJobs(printer->name, NULL, 1); |
ef416fc2 | 6269 | |
6270 | /* | |
6271 | * Remove old subscriptions and send a "deleted printer" event... | |
6272 | */ | |
6273 | ||
6274 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
6275 | "%s \"%s\" deleted by \"%s\".", | |
6276 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
f7deaa1a | 6277 | printer->name, get_username(con)); |
ef416fc2 | 6278 | |
6279 | cupsdExpireSubscriptions(printer, NULL); | |
f7deaa1a | 6280 | |
ef416fc2 | 6281 | /* |
6282 | * Remove any old PPD or script files... | |
6283 | */ | |
6284 | ||
f7deaa1a | 6285 | snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, |
6286 | printer->name); | |
ef416fc2 | 6287 | unlink(filename); |
6288 | ||
f7deaa1a | 6289 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, |
6290 | printer->name); | |
ef416fc2 | 6291 | unlink(filename); |
6292 | ||
568fa3fa MS |
6293 | #ifdef __APPLE__ |
6294 | /* | |
6295 | * Unregister color profiles... | |
6296 | */ | |
6297 | ||
6298 | apple_unregister_profiles(printer); | |
6299 | #endif /* __APPLE__ */ | |
6300 | ||
ef416fc2 | 6301 | if (dtype & CUPS_PRINTER_CLASS) |
6302 | { | |
f7deaa1a | 6303 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", |
6304 | printer->name, get_username(con)); | |
ef416fc2 | 6305 | |
6306 | cupsdDeletePrinter(printer, 0); | |
3dfe78b3 | 6307 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
ef416fc2 | 6308 | } |
6309 | else | |
6310 | { | |
f7deaa1a | 6311 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", |
6312 | printer->name, get_username(con)); | |
ef416fc2 | 6313 | |
6314 | cupsdDeletePrinter(printer, 0); | |
3dfe78b3 | 6315 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 6316 | } |
6317 | ||
3dfe78b3 | 6318 | cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP); |
ef416fc2 | 6319 | |
6320 | /* | |
6321 | * Return with no errors... | |
6322 | */ | |
6323 | ||
6324 | con->response->request.status.status_code = IPP_OK; | |
6325 | } | |
6326 | ||
6327 | ||
6328 | /* | |
6329 | * 'get_default()' - Get the default destination. | |
6330 | */ | |
6331 | ||
6332 | static void | |
6333 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
6334 | { | |
fa73b229 | 6335 | http_status_t status; /* Policy status */ |
6336 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 6337 | |
6338 | ||
6339 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd); | |
6340 | ||
6341 | /* | |
6342 | * Check policy... | |
6343 | */ | |
6344 | ||
6345 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6346 | { | |
f899b121 | 6347 | send_http_error(con, status, NULL); |
ef416fc2 | 6348 | return; |
6349 | } | |
6350 | ||
fa73b229 | 6351 | if (DefaultPrinter) |
ef416fc2 | 6352 | { |
fa73b229 | 6353 | ra = create_requested_array(con->request); |
ef416fc2 | 6354 | |
fa73b229 | 6355 | copy_printer_attrs(con, DefaultPrinter, ra); |
ef416fc2 | 6356 | |
fa73b229 | 6357 | cupsArrayDelete(ra); |
ef416fc2 | 6358 | |
fa73b229 | 6359 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6360 | } |
6361 | else | |
6362 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer")); | |
6363 | } | |
6364 | ||
6365 | ||
6366 | /* | |
6367 | * 'get_devices()' - Get the list of available devices on the local system. | |
6368 | */ | |
6369 | ||
6370 | static void | |
6371 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
6372 | { | |
6373 | http_status_t status; /* Policy status */ | |
ae71f5de MS |
6374 | ipp_attribute_t *limit, /* limit attribute */ |
6375 | *timeout, /* timeout attribute */ | |
6376 | *requested, /* requested-attributes attribute */ | |
6377 | *exclude; /* exclude-schemes attribute */ | |
ef416fc2 | 6378 | char command[1024], /* cups-deviced command */ |
6379 | options[1024], /* Options to pass to command */ | |
ae71f5de | 6380 | requested_str[256], |
89d46774 | 6381 | /* String for requested attributes */ |
ae71f5de MS |
6382 | exclude_str[512]; |
6383 | /* String for excluded attributes */ | |
ef416fc2 | 6384 | |
6385 | ||
6386 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd); | |
6387 | ||
6388 | /* | |
6389 | * Check policy... | |
6390 | */ | |
6391 | ||
6392 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6393 | { | |
f899b121 | 6394 | send_http_error(con, status, NULL); |
ef416fc2 | 6395 | return; |
6396 | } | |
6397 | ||
6398 | /* | |
6399 | * Run cups-deviced command with the given options... | |
6400 | */ | |
6401 | ||
ae71f5de MS |
6402 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
6403 | timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER); | |
ef416fc2 | 6404 | requested = ippFindAttribute(con->request, "requested-attributes", |
6405 | IPP_TAG_KEYWORD); | |
ae71f5de | 6406 | exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME); |
ef416fc2 | 6407 | |
6408 | if (requested) | |
89d46774 | 6409 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
ef416fc2 | 6410 | else |
89d46774 | 6411 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); |
ef416fc2 | 6412 | |
ae71f5de MS |
6413 | if (exclude) |
6414 | url_encode_attr(exclude, exclude_str, sizeof(exclude_str)); | |
6415 | else | |
6416 | exclude_str[0] = '\0'; | |
6417 | ||
ef416fc2 | 6418 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); |
6419 | snprintf(options, sizeof(options), | |
ae71f5de | 6420 | "%d+%d+%d+%d+%s%s%s", |
ef416fc2 | 6421 | con->request->request.op.request_id, |
ae71f5de MS |
6422 | limit ? limit->values[0].integer : 0, |
6423 | timeout ? timeout->values[0].integer : 10, | |
6424 | (int)User, | |
6425 | requested_str, | |
6426 | exclude_str[0] ? "%20" : "", exclude_str); | |
ef416fc2 | 6427 | |
6428 | if (cupsdSendCommand(con, command, options, 1)) | |
6429 | { | |
6430 | /* | |
6431 | * Command started successfully, don't send an IPP response here... | |
6432 | */ | |
6433 | ||
6434 | ippDelete(con->response); | |
6435 | con->response = NULL; | |
6436 | } | |
6437 | else | |
6438 | { | |
6439 | /* | |
6440 | * Command failed, return "internal error" so the user knows something | |
6441 | * went wrong... | |
6442 | */ | |
6443 | ||
6444 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
6445 | _("cups-deviced failed to execute.")); | |
6446 | } | |
6447 | } | |
6448 | ||
6449 | ||
2e4ff8af MS |
6450 | /* |
6451 | * 'get_document()' - Get a copy of a job file. | |
6452 | */ | |
6453 | ||
6454 | static void | |
6455 | get_document(cupsd_client_t *con, /* I - Client connection */ | |
6456 | ipp_attribute_t *uri) /* I - Job URI */ | |
6457 | { | |
6458 | http_status_t status; /* Policy status */ | |
6459 | ipp_attribute_t *attr; /* Current attribute */ | |
6460 | int jobid; /* Job ID */ | |
6461 | int docnum; /* Document number */ | |
6462 | cupsd_job_t *job; /* Current job */ | |
6463 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6464 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6465 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6466 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6467 | int port; /* Port portion of URI */ | |
6468 | char filename[1024], /* Filename for document */ | |
6469 | format[1024]; /* Format for document */ | |
6470 | ||
6471 | ||
6472 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con, | |
6473 | con->http.fd, uri->values[0].string.text); | |
6474 | ||
6475 | /* | |
6476 | * See if we have a job URI or a printer URI... | |
6477 | */ | |
6478 | ||
6479 | if (!strcmp(uri->name, "printer-uri")) | |
6480 | { | |
6481 | /* | |
6482 | * Got a printer URI; see if we also have a job-id attribute... | |
6483 | */ | |
6484 | ||
6485 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6486 | IPP_TAG_INTEGER)) == NULL) | |
6487 | { | |
6488 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6489 | _("Got a printer-uri attribute but no job-id!")); | |
6490 | return; | |
6491 | } | |
6492 | ||
6493 | jobid = attr->values[0].integer; | |
6494 | } | |
6495 | else | |
6496 | { | |
6497 | /* | |
6498 | * Got a job URI; parse it to get the job ID... | |
6499 | */ | |
6500 | ||
6501 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
6502 | sizeof(method), username, sizeof(username), host, | |
6503 | sizeof(host), &port, resource, sizeof(resource)); | |
6504 | ||
6505 | if (strncmp(resource, "/jobs/", 6)) | |
6506 | { | |
6507 | /* | |
6508 | * Not a valid URI! | |
6509 | */ | |
6510 | ||
6511 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6512 | _("Bad job-uri attribute \"%s\"!"), | |
6513 | uri->values[0].string.text); | |
6514 | return; | |
6515 | } | |
6516 | ||
6517 | jobid = atoi(resource + 6); | |
6518 | } | |
6519 | ||
6520 | /* | |
6521 | * See if the job exists... | |
6522 | */ | |
6523 | ||
6524 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6525 | { | |
6526 | /* | |
6527 | * Nope - return a "not found" error... | |
6528 | */ | |
6529 | ||
6530 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6531 | return; | |
6532 | } | |
6533 | ||
6534 | /* | |
6535 | * Check policy... | |
6536 | */ | |
6537 | ||
6538 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6539 | { | |
6540 | send_http_error(con, status, NULL); | |
6541 | return; | |
6542 | } | |
6543 | ||
6544 | /* | |
6545 | * Get the document number... | |
6546 | */ | |
6547 | ||
6548 | if ((attr = ippFindAttribute(con->request, "document-number", | |
6549 | IPP_TAG_INTEGER)) == NULL) | |
6550 | { | |
6551 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6552 | _("Missing document-number attribute!")); | |
6553 | return; | |
6554 | } | |
6555 | ||
6556 | if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files || | |
6557 | attr->num_values > 1) | |
6558 | { | |
6559 | send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."), | |
6560 | docnum, jobid); | |
6561 | return; | |
6562 | } | |
6563 | ||
6564 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid, | |
6565 | docnum); | |
6566 | if ((con->file = open(filename, O_RDONLY)) == -1) | |
6567 | { | |
6568 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6569 | "Unable to open document %d in job %d - %s", docnum, jobid, | |
6570 | strerror(errno)); | |
6571 | send_ipp_status(con, IPP_NOT_FOUND, | |
6572 | _("Unable to open document %d in job %d!"), docnum, jobid); | |
6573 | return; | |
6574 | } | |
6575 | ||
6576 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
6577 | ||
6578 | cupsdLoadJob(job); | |
6579 | ||
6580 | snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super, | |
6581 | job->filetypes[docnum - 1]->type); | |
6582 | ||
6583 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
6584 | NULL, format); | |
6585 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number", | |
6586 | docnum); | |
6587 | if ((attr = ippFindAttribute(job->attrs, "document-name", | |
6588 | IPP_TAG_NAME)) != NULL) | |
6589 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name", | |
6590 | NULL, attr->values[0].string.text); | |
6591 | } | |
6592 | ||
6593 | ||
ef416fc2 | 6594 | /* |
fa73b229 | 6595 | * 'get_job_attrs()' - Get job attributes. |
ef416fc2 | 6596 | */ |
6597 | ||
6598 | static void | |
fa73b229 | 6599 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ |
6600 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 6601 | { |
6602 | http_status_t status; /* Policy status */ | |
fa73b229 | 6603 | ipp_attribute_t *attr; /* Current attribute */ |
6604 | int jobid; /* Job ID */ | |
6605 | cupsd_job_t *job; /* Current job */ | |
ef416fc2 | 6606 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
6607 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6608 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6609 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6610 | int port; /* Port portion of URI */ | |
fa73b229 | 6611 | cups_array_t *ra; /* Requested attributes array */ |
ef416fc2 | 6612 | |
6613 | ||
fa73b229 | 6614 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, |
6615 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 6616 | |
6617 | /* | |
fa73b229 | 6618 | * See if we have a job URI or a printer URI... |
6619 | */ | |
6620 | ||
6621 | if (!strcmp(uri->name, "printer-uri")) | |
6622 | { | |
6623 | /* | |
6624 | * Got a printer URI; see if we also have a job-id attribute... | |
6625 | */ | |
6626 | ||
6627 | if ((attr = ippFindAttribute(con->request, "job-id", | |
6628 | IPP_TAG_INTEGER)) == NULL) | |
6629 | { | |
6630 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6631 | _("Got a printer-uri attribute but no job-id!")); | |
6632 | return; | |
6633 | } | |
6634 | ||
6635 | jobid = attr->values[0].integer; | |
6636 | } | |
6637 | else | |
6638 | { | |
6639 | /* | |
6640 | * Got a job URI; parse it to get the job ID... | |
6641 | */ | |
6642 | ||
a4d04587 | 6643 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6644 | sizeof(method), username, sizeof(username), host, | |
6645 | sizeof(host), &port, resource, sizeof(resource)); | |
fa73b229 | 6646 | |
6647 | if (strncmp(resource, "/jobs/", 6)) | |
6648 | { | |
6649 | /* | |
6650 | * Not a valid URI! | |
6651 | */ | |
6652 | ||
6653 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6654 | _("Bad job-uri attribute \"%s\"!"), | |
6655 | uri->values[0].string.text); | |
6656 | return; | |
6657 | } | |
6658 | ||
6659 | jobid = atoi(resource + 6); | |
6660 | } | |
6661 | ||
6662 | /* | |
6663 | * See if the job exists... | |
6664 | */ | |
6665 | ||
6666 | if ((job = cupsdFindJob(jobid)) == NULL) | |
6667 | { | |
6668 | /* | |
6669 | * Nope - return a "not found" error... | |
6670 | */ | |
6671 | ||
6672 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6673 | return; | |
6674 | } | |
6675 | ||
6676 | /* | |
6677 | * Check policy... | |
6678 | */ | |
6679 | ||
6680 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6681 | { | |
f899b121 | 6682 | send_http_error(con, status, NULL); |
fa73b229 | 6683 | return; |
6684 | } | |
6685 | ||
6686 | /* | |
6687 | * Copy attributes... | |
6688 | */ | |
6689 | ||
bd7854cb | 6690 | cupsdLoadJob(job); |
6691 | ||
fa73b229 | 6692 | ra = create_requested_array(con->request); |
6693 | copy_job_attrs(con, job, ra); | |
6694 | cupsArrayDelete(ra); | |
6695 | ||
6696 | con->response->request.status.status_code = IPP_OK; | |
6697 | } | |
6698 | ||
6699 | ||
6700 | /* | |
6701 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
6702 | */ | |
6703 | ||
6704 | static void | |
6705 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
6706 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6707 | { | |
6708 | http_status_t status; /* Policy status */ | |
6709 | ipp_attribute_t *attr; /* Current attribute */ | |
6710 | const char *dest; /* Destination */ | |
bc44d920 | 6711 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
fa73b229 | 6712 | cups_ptype_t dmask; /* Destination type mask */ |
f7deaa1a | 6713 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
fa73b229 | 6714 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6715 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6716 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6717 | int port; /* Port portion of URI */ | |
6718 | int completed; /* Completed jobs? */ | |
6719 | int first_job_id; /* First job ID */ | |
6720 | int limit; /* Maximum number of jobs to return */ | |
6721 | int count; /* Number of jobs that match */ | |
6722 | cupsd_job_t *job; /* Current job pointer */ | |
6723 | cupsd_printer_t *printer; /* Printer */ | |
6724 | cups_array_t *list; /* Which job list... */ | |
6725 | cups_array_t *ra; /* Requested attributes array */ | |
6726 | ||
6727 | ||
6728 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd, | |
6729 | uri->values[0].string.text); | |
6730 | ||
6731 | /* | |
6732 | * Is the destination valid? | |
ef416fc2 | 6733 | */ |
6734 | ||
f7deaa1a | 6735 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
6736 | sizeof(scheme), username, sizeof(username), host, | |
a4d04587 | 6737 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 6738 | |
fa73b229 | 6739 | if (!strcmp(resource, "/") || |
6740 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6)) | |
ef416fc2 | 6741 | { |
6742 | dest = NULL; | |
6743 | dtype = (cups_ptype_t)0; | |
6744 | dmask = (cups_ptype_t)0; | |
6745 | printer = NULL; | |
6746 | } | |
fa73b229 | 6747 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) |
ef416fc2 | 6748 | { |
6749 | dest = NULL; | |
6750 | dtype = (cups_ptype_t)0; | |
6751 | dmask = CUPS_PRINTER_CLASS; | |
6752 | printer = NULL; | |
6753 | } | |
fa73b229 | 6754 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) |
ef416fc2 | 6755 | { |
6756 | dest = NULL; | |
6757 | dtype = CUPS_PRINTER_CLASS; | |
6758 | dmask = CUPS_PRINTER_CLASS; | |
6759 | printer = NULL; | |
6760 | } | |
f7deaa1a | 6761 | else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype, |
6762 | &printer)) == NULL) | |
ef416fc2 | 6763 | { |
6764 | /* | |
6765 | * Bad URI... | |
6766 | */ | |
6767 | ||
6768 | send_ipp_status(con, IPP_NOT_FOUND, | |
6769 | _("The printer or class was not found.")); | |
6770 | return; | |
6771 | } | |
6772 | else | |
b86bc4cf | 6773 | { |
6774 | dtype &= CUPS_PRINTER_CLASS; | |
ef416fc2 | 6775 | dmask = CUPS_PRINTER_CLASS; |
b86bc4cf | 6776 | } |
ef416fc2 | 6777 | |
6778 | /* | |
6779 | * Check policy... | |
6780 | */ | |
6781 | ||
6782 | if (printer) | |
6783 | { | |
f7deaa1a | 6784 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, |
6785 | NULL)) != HTTP_OK) | |
ef416fc2 | 6786 | { |
f899b121 | 6787 | send_http_error(con, status, printer); |
ef416fc2 | 6788 | return; |
6789 | } | |
6790 | } | |
6791 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
6792 | { | |
f899b121 | 6793 | send_http_error(con, status, NULL); |
ef416fc2 | 6794 | return; |
6795 | } | |
6796 | ||
6797 | /* | |
6798 | * See if the "which-jobs" attribute have been specified... | |
6799 | */ | |
6800 | ||
fa73b229 | 6801 | if ((attr = ippFindAttribute(con->request, "which-jobs", |
6802 | IPP_TAG_KEYWORD)) != NULL && | |
ef416fc2 | 6803 | !strcmp(attr->values[0].string.text, "completed")) |
6804 | { | |
6805 | completed = 1; | |
6806 | list = Jobs; | |
6807 | } | |
6808 | else if (attr && !strcmp(attr->values[0].string.text, "all")) | |
6809 | { | |
6810 | completed = 0; | |
6811 | list = Jobs; | |
6812 | } | |
3dfe78b3 MS |
6813 | else if (attr && !strcmp(attr->values[0].string.text, "printing")) |
6814 | { | |
6815 | completed = 0; | |
6816 | list = PrintingJobs; | |
6817 | } | |
ef416fc2 | 6818 | else |
6819 | { | |
6820 | completed = 0; | |
6821 | list = ActiveJobs; | |
6822 | } | |
6823 | ||
6824 | /* | |
6825 | * See if they want to limit the number of jobs reported... | |
6826 | */ | |
6827 | ||
fa73b229 | 6828 | if ((attr = ippFindAttribute(con->request, "limit", |
6829 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 6830 | limit = attr->values[0].integer; |
6831 | else | |
3dfe78b3 | 6832 | limit = 0; |
ef416fc2 | 6833 | |
6834 | if ((attr = ippFindAttribute(con->request, "first-job-id", | |
6835 | IPP_TAG_INTEGER)) != NULL) | |
6836 | first_job_id = attr->values[0].integer; | |
6837 | else | |
6838 | first_job_id = 1; | |
6839 | ||
6840 | /* | |
6841 | * See if we only want to see jobs for a specific user... | |
6842 | */ | |
6843 | ||
fa73b229 | 6844 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
6845 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 6846 | attr->values[0].boolean) |
e00b005a | 6847 | strlcpy(username, get_username(con), sizeof(username)); |
ef416fc2 | 6848 | else |
6849 | username[0] = '\0'; | |
6850 | ||
fa73b229 | 6851 | ra = create_requested_array(con->request); |
ef416fc2 | 6852 | |
6853 | /* | |
6854 | * OK, build a list of jobs for this printer... | |
6855 | */ | |
6856 | ||
6857 | for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list); | |
3dfe78b3 | 6858 | (limit <= 0 || count < limit) && job; |
ef416fc2 | 6859 | job = (cupsd_job_t *)cupsArrayNext(list)) |
6860 | { | |
6861 | /* | |
6862 | * Filter out jobs that don't match... | |
6863 | */ | |
6864 | ||
6865 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id); | |
6866 | ||
0a682745 MS |
6867 | if (!job->dest || !job->username) |
6868 | cupsdLoadJob(job); | |
6869 | ||
6870 | if (!job->dest || !job->username) | |
6871 | continue; | |
6872 | ||
fa73b229 | 6873 | if ((dest && strcmp(job->dest, dest)) && |
6874 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
ef416fc2 | 6875 | continue; |
6876 | if ((job->dtype & dmask) != dtype && | |
fa73b229 | 6877 | (!job->printer || (job->printer->type & dmask) != dtype)) |
ef416fc2 | 6878 | continue; |
bd7854cb | 6879 | if (completed && job->state_value <= IPP_JOB_STOPPED) |
ef416fc2 | 6880 | continue; |
6881 | ||
6882 | if (job->id < first_job_id) | |
6883 | continue; | |
6884 | ||
bd7854cb | 6885 | cupsdLoadJob(job); |
6886 | ||
6887 | if (!job->attrs) | |
6888 | continue; | |
6889 | ||
7594b224 | 6890 | if (username[0] && strcasecmp(username, job->username)) |
6891 | continue; | |
6892 | ||
fa73b229 | 6893 | if (count > 0) |
6894 | ippAddSeparator(con->response); | |
6895 | ||
ef416fc2 | 6896 | count ++; |
6897 | ||
6898 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count); | |
6899 | ||
fa73b229 | 6900 | copy_job_attrs(con, job, ra); |
ef416fc2 | 6901 | } |
6902 | ||
fa73b229 | 6903 | cupsArrayDelete(ra); |
ef416fc2 | 6904 | |
fa73b229 | 6905 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 6906 | } |
6907 | ||
6908 | ||
6909 | /* | |
6910 | * 'get_notifications()' - Get events for a subscription. | |
6911 | */ | |
6912 | ||
6913 | static void | |
bd7854cb | 6914 | get_notifications(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 6915 | { |
bd7854cb | 6916 | int i, j; /* Looping vars */ |
6917 | http_status_t status; /* Policy status */ | |
6918 | cupsd_subscription_t *sub; /* Subscription */ | |
6919 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
6920 | *sequences; /* notify-sequence-numbers */ | |
6921 | int min_seq; /* Minimum sequence number */ | |
6922 | int interval; /* Poll interval */ | |
6923 | ||
6924 | ||
f7deaa1a | 6925 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])", |
bd7854cb | 6926 | con, con->http.fd); |
6927 | ||
6928 | /* | |
6929 | * Get subscription attributes... | |
6930 | */ | |
6931 | ||
6932 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
6933 | IPP_TAG_INTEGER); | |
6934 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
6935 | IPP_TAG_INTEGER); | |
6936 | ||
6937 | if (!ids) | |
6938 | { | |
6939 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6940 | _("Missing notify-subscription-ids attribute!")); | |
6941 | return; | |
6942 | } | |
6943 | ||
6944 | /* | |
6945 | * Are the subscription IDs valid? | |
6946 | */ | |
6947 | ||
6948 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
6949 | { | |
6950 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
6951 | { | |
6952 | /* | |
6953 | * Bad subscription ID... | |
6954 | */ | |
6955 | ||
6956 | send_ipp_status(con, IPP_NOT_FOUND, | |
6957 | _("notify-subscription-id %d no good!"), | |
6958 | ids->values[i].integer); | |
6959 | return; | |
6960 | } | |
6961 | ||
6962 | /* | |
6963 | * Check policy... | |
6964 | */ | |
6965 | ||
6966 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
6967 | DefaultPolicyPtr, | |
6968 | con, sub->owner)) != HTTP_OK) | |
6969 | { | |
f899b121 | 6970 | send_http_error(con, status, sub->dest); |
bd7854cb | 6971 | return; |
6972 | } | |
6973 | ||
6974 | /* | |
6975 | * Check the subscription type and update the interval accordingly. | |
6976 | */ | |
6977 | ||
6978 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
6979 | interval > 10) | |
6980 | interval = 10; | |
6981 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
6982 | interval = 0; | |
6983 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
6984 | interval > 30) | |
6985 | interval = 30; | |
6986 | } | |
6987 | ||
6988 | /* | |
6989 | * Tell the client to poll again in N seconds... | |
6990 | */ | |
6991 | ||
6992 | if (interval > 0) | |
6993 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6994 | "notify-get-interval", interval); | |
6995 | ||
6996 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
6997 | "printer-up-time", time(NULL)); | |
6998 | ||
6999 | /* | |
7000 | * Copy the subscription event attributes to the response. | |
7001 | */ | |
7002 | ||
7003 | con->response->request.status.status_code = | |
7004 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
7005 | ||
7006 | for (i = 0; i < ids->num_values; i ++) | |
7007 | { | |
7008 | /* | |
7009 | * Get the subscription and sequence number... | |
7010 | */ | |
7011 | ||
7012 | sub = cupsdFindSubscription(ids->values[i].integer); | |
7013 | ||
7014 | if (sequences && i < sequences->num_values) | |
7015 | min_seq = sequences->values[i].integer; | |
7016 | else | |
7017 | min_seq = 1; | |
7018 | ||
7019 | /* | |
7020 | * If we don't have any new events, nothing to do here... | |
7021 | */ | |
7022 | ||
7023 | if (min_seq > (sub->first_event_id + sub->num_events)) | |
7024 | continue; | |
7025 | ||
7026 | /* | |
7027 | * Otherwise copy all of the new events... | |
7028 | */ | |
7029 | ||
7030 | if (sub->first_event_id > min_seq) | |
7031 | j = 0; | |
7032 | else | |
7033 | j = min_seq - sub->first_event_id; | |
7034 | ||
b423cd4c | 7035 | for (; j < sub->num_events; j ++) |
7036 | { | |
7037 | ippAddSeparator(con->response); | |
ef416fc2 | 7038 | |
b423cd4c | 7039 | copy_attrs(con->response, sub->events[j]->attrs, NULL, |
7040 | IPP_TAG_EVENT_NOTIFICATION, 0); | |
7041 | } | |
7042 | } | |
7043 | } | |
ef416fc2 | 7044 | |
ef416fc2 | 7045 | |
b94498cf | 7046 | /* |
7047 | * 'get_ppd()' - Get a named PPD from the local system. | |
7048 | */ | |
7049 | ||
7050 | static void | |
7051 | get_ppd(cupsd_client_t *con, /* I - Client connection */ | |
7052 | ipp_attribute_t *uri) /* I - Printer URI or PPD name */ | |
7053 | { | |
7054 | http_status_t status; /* Policy status */ | |
7055 | cupsd_printer_t *dest; /* Destination */ | |
7056 | cups_ptype_t dtype; /* Destination type */ | |
7057 | ||
7058 | ||
7059 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con, | |
7060 | con->http.fd, uri, uri->name, uri->values[0].string.text); | |
7061 | ||
7062 | if (!strcmp(uri->name, "ppd-name")) | |
7063 | { | |
7064 | /* | |
7065 | * Return a PPD file from cups-driverd... | |
7066 | */ | |
7067 | ||
7068 | char command[1024], /* cups-driverd command */ | |
7069 | options[1024], /* Options to pass to command */ | |
7070 | ppd_name[1024]; /* ppd-name */ | |
7071 | ||
7072 | ||
7073 | /* | |
7074 | * Check policy... | |
7075 | */ | |
7076 | ||
7077 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
7078 | { | |
7079 | send_http_error(con, status, NULL); | |
7080 | return; | |
7081 | } | |
7082 | ||
7083 | /* | |
7084 | * Run cups-driverd command with the given options... | |
7085 | */ | |
7086 | ||
7087 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); | |
7088 | url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name)); | |
7089 | snprintf(options, sizeof(options), "get+%d+%s", | |
7090 | con->request->request.op.request_id, ppd_name); | |
7091 | ||
7092 | if (cupsdSendCommand(con, command, options, 0)) | |
7093 | { | |
7094 | /* | |
7095 | * Command started successfully, don't send an IPP response here... | |
7096 | */ | |
7097 | ||
7098 | ippDelete(con->response); | |
7099 | con->response = NULL; | |
7100 | } | |
7101 | else | |
7102 | { | |
7103 | /* | |
7104 | * Command failed, return "internal error" so the user knows something | |
7105 | * went wrong... | |
7106 | */ | |
7107 | ||
7108 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7109 | _("cups-driverd failed to execute.")); | |
7110 | } | |
7111 | } | |
7112 | else if (!strcmp(uri->name, "printer-uri") && | |
7113 | cupsdValidateDest(uri->values[0].string.text, &dtype, &dest)) | |
7114 | { | |
7115 | int i; /* Looping var */ | |
7116 | char filename[1024]; /* PPD filename */ | |
7117 | ||
7118 | ||
7119 | /* | |
7120 | * Check policy... | |
7121 | */ | |
7122 | ||
7123 | if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7124 | { | |
2fb76298 | 7125 | send_http_error(con, status, dest); |
b94498cf | 7126 | return; |
7127 | } | |
7128 | ||
7129 | /* | |
7130 | * See if we need the PPD for a class or remote printer... | |
7131 | */ | |
7132 | ||
bc44d920 | 7133 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, |
7134 | dest->name); | |
7135 | ||
7136 | if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0)) | |
b94498cf | 7137 | { |
3d8365b8 | 7138 | con->response->request.status.status_code = CUPS_SEE_OTHER; |
b94498cf | 7139 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, |
7140 | "printer-uri", NULL, dest->uri); | |
7141 | return; | |
7142 | } | |
7143 | else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)) | |
7144 | { | |
7145 | for (i = 0; i < dest->num_printers; i ++) | |
7146 | if (!(dest->printers[i]->type & | |
bc44d920 | 7147 | (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))) |
7148 | { | |
7149 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, | |
7150 | dest->printers[i]->name); | |
7151 | ||
7152 | if (!access(filename, 0)) | |
7153 | break; | |
7154 | } | |
b94498cf | 7155 | |
7156 | if (i < dest->num_printers) | |
7157 | dest = dest->printers[i]; | |
7158 | else | |
7159 | { | |
3d8365b8 | 7160 | con->response->request.status.status_code = CUPS_SEE_OTHER; |
b94498cf | 7161 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, |
7162 | "printer-uri", NULL, dest->printers[0]->uri); | |
7163 | return; | |
7164 | } | |
7165 | } | |
7166 | ||
7167 | /* | |
7168 | * Found the printer with the PPD file, now see if there is one... | |
7169 | */ | |
7170 | ||
b94498cf | 7171 | if ((con->file = open(filename, O_RDONLY)) < 0) |
7172 | { | |
7173 | send_ipp_status(con, IPP_NOT_FOUND, | |
7174 | _("The PPD file \"%s\" could not be opened: %s"), | |
3d8365b8 | 7175 | uri->values[0].string.text, strerror(errno)); |
b94498cf | 7176 | return; |
7177 | } | |
7178 | ||
7179 | fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); | |
7180 | ||
7181 | con->pipe_pid = 0; | |
7182 | ||
3d8365b8 | 7183 | con->response->request.status.status_code = IPP_OK; |
b94498cf | 7184 | } |
7185 | else | |
7186 | send_ipp_status(con, IPP_NOT_FOUND, | |
7187 | _("The PPD file \"%s\" could not be found."), | |
7188 | uri->values[0].string.text); | |
7189 | } | |
7190 | ||
7191 | ||
b423cd4c | 7192 | /* |
7193 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
7194 | */ | |
ef416fc2 | 7195 | |
b423cd4c | 7196 | static void |
7197 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
7198 | { | |
7199 | http_status_t status; /* Policy status */ | |
b423cd4c | 7200 | ipp_attribute_t *limit, /* Limit attribute */ |
b94498cf | 7201 | *device, /* ppd-device-id attribute */ |
7202 | *language, /* ppd-natural-language attribute */ | |
b423cd4c | 7203 | *make, /* ppd-make attribute */ |
b94498cf | 7204 | *model, /* ppd-make-and-model attribute */ |
3d8365b8 | 7205 | *model_number, /* ppd-model-number attribute */ |
b94498cf | 7206 | *product, /* ppd-product attribute */ |
7207 | *psversion, /* ppd-psverion attribute */ | |
3d8365b8 | 7208 | *type, /* ppd-type attribute */ |
b423cd4c | 7209 | *requested; /* requested-attributes attribute */ |
b94498cf | 7210 | char command[1024], /* cups-driverd command */ |
b423cd4c | 7211 | options[1024], /* Options to pass to command */ |
b94498cf | 7212 | device_str[256],/* Escaped ppd-device-id string */ |
7213 | language_str[256], | |
bc44d920 | 7214 | /* Escaped ppd-natural-language */ |
b94498cf | 7215 | make_str[256], /* Escaped ppd-make string */ |
7216 | model_str[256], /* Escaped ppd-make-and-model string */ | |
3d8365b8 | 7217 | model_number_str[256], |
7218 | /* ppd-model-number string */ | |
b94498cf | 7219 | product_str[256], |
7220 | /* Escaped ppd-product string */ | |
7221 | psversion_str[256], | |
7222 | /* Escaped ppd-psversion string */ | |
3d8365b8 | 7223 | type_str[256], /* Escaped ppd-type string */ |
b94498cf | 7224 | requested_str[256]; |
89d46774 | 7225 | /* String for requested attributes */ |
ef416fc2 | 7226 | |
ef416fc2 | 7227 | |
b423cd4c | 7228 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd); |
ef416fc2 | 7229 | |
7230 | /* | |
b423cd4c | 7231 | * Check policy... |
ef416fc2 | 7232 | */ |
7233 | ||
b423cd4c | 7234 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7235 | { |
f899b121 | 7236 | send_http_error(con, status, NULL); |
b423cd4c | 7237 | return; |
ef416fc2 | 7238 | } |
ef416fc2 | 7239 | |
b423cd4c | 7240 | /* |
7241 | * Run cups-driverd command with the given options... | |
7242 | */ | |
7243 | ||
3d8365b8 | 7244 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); |
7245 | device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT); | |
7246 | language = ippFindAttribute(con->request, "ppd-natural-language", | |
7247 | IPP_TAG_LANGUAGE); | |
7248 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
7249 | model = ippFindAttribute(con->request, "ppd-make-and-model", | |
7250 | IPP_TAG_TEXT); | |
7251 | model_number = ippFindAttribute(con->request, "ppd-model-number", | |
7252 | IPP_TAG_INTEGER); | |
7253 | product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT); | |
7254 | psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT); | |
7255 | type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD); | |
7256 | requested = ippFindAttribute(con->request, "requested-attributes", | |
7257 | IPP_TAG_KEYWORD); | |
b423cd4c | 7258 | |
7259 | if (requested) | |
89d46774 | 7260 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
7261 | else | |
7262 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); | |
ef416fc2 | 7263 | |
b94498cf | 7264 | if (device) |
7265 | url_encode_attr(device, device_str, sizeof(device_str)); | |
7266 | else | |
7267 | device_str[0] = '\0'; | |
7268 | ||
7269 | if (language) | |
7270 | url_encode_attr(language, language_str, sizeof(language_str)); | |
7271 | else | |
7272 | language_str[0] = '\0'; | |
7273 | ||
89d46774 | 7274 | if (make) |
7275 | url_encode_attr(make, make_str, sizeof(make_str)); | |
b423cd4c | 7276 | else |
89d46774 | 7277 | make_str[0] = '\0'; |
fa73b229 | 7278 | |
b94498cf | 7279 | if (model) |
7280 | url_encode_attr(model, model_str, sizeof(model_str)); | |
7281 | else | |
7282 | model_str[0] = '\0'; | |
7283 | ||
3d8365b8 | 7284 | if (model_number) |
7285 | snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d", | |
7286 | model_number->values[0].integer); | |
7287 | else | |
7288 | model_number_str[0] = '\0'; | |
7289 | ||
b94498cf | 7290 | if (product) |
7291 | url_encode_attr(product, product_str, sizeof(product_str)); | |
7292 | else | |
7293 | product_str[0] = '\0'; | |
7294 | ||
7295 | if (psversion) | |
7296 | url_encode_attr(psversion, psversion_str, sizeof(psversion_str)); | |
7297 | else | |
7298 | psversion_str[0] = '\0'; | |
7299 | ||
3d8365b8 | 7300 | if (type) |
7301 | url_encode_attr(type, type_str, sizeof(type_str)); | |
7302 | else | |
7303 | type_str[0] = '\0'; | |
7304 | ||
b423cd4c | 7305 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); |
3d8365b8 | 7306 | snprintf(options, sizeof(options), |
7307 | "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", | |
b423cd4c | 7308 | con->request->request.op.request_id, |
7309 | limit ? limit->values[0].integer : 0, | |
b94498cf | 7310 | requested_str, |
7311 | device ? "%20" : "", device_str, | |
7312 | language ? "%20" : "", language_str, | |
7313 | make ? "%20" : "", make_str, | |
7314 | model ? "%20" : "", model_str, | |
3d8365b8 | 7315 | model_number ? "%20" : "", model_number_str, |
b94498cf | 7316 | product ? "%20" : "", product_str, |
3d8365b8 | 7317 | psversion ? "%20" : "", psversion_str, |
7318 | type ? "%20" : "", type_str); | |
ef416fc2 | 7319 | |
b423cd4c | 7320 | if (cupsdSendCommand(con, command, options, 0)) |
7321 | { | |
7322 | /* | |
7323 | * Command started successfully, don't send an IPP response here... | |
7324 | */ | |
7325 | ||
7326 | ippDelete(con->response); | |
7327 | con->response = NULL; | |
7328 | } | |
7329 | else | |
7330 | { | |
7331 | /* | |
7332 | * Command failed, return "internal error" so the user knows something | |
7333 | * went wrong... | |
7334 | */ | |
7335 | ||
7336 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
7337 | _("cups-driverd failed to execute.")); | |
7338 | } | |
ef416fc2 | 7339 | } |
7340 | ||
7341 | ||
7342 | /* | |
b423cd4c | 7343 | * 'get_printer_attrs()' - Get printer attributes. |
ef416fc2 | 7344 | */ |
7345 | ||
7346 | static void | |
b423cd4c | 7347 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ |
7348 | ipp_attribute_t *uri) /* I - Printer URI */ | |
ef416fc2 | 7349 | { |
7350 | http_status_t status; /* Policy status */ | |
bc44d920 | 7351 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
b423cd4c | 7352 | cupsd_printer_t *printer; /* Printer/class */ |
ef416fc2 | 7353 | cups_array_t *ra; /* Requested attributes array */ |
7354 | ||
7355 | ||
b423cd4c | 7356 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, |
7357 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 7358 | |
7359 | /* | |
b423cd4c | 7360 | * Is the destination valid? |
ef416fc2 | 7361 | */ |
7362 | ||
f7deaa1a | 7363 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 7364 | { |
7365 | /* | |
b423cd4c | 7366 | * Bad URI... |
ef416fc2 | 7367 | */ |
7368 | ||
7369 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 7370 | _("The printer or class was not found.")); |
ef416fc2 | 7371 | return; |
7372 | } | |
7373 | ||
7374 | /* | |
7375 | * Check policy... | |
7376 | */ | |
7377 | ||
b423cd4c | 7378 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7379 | { |
f899b121 | 7380 | send_http_error(con, status, printer); |
ef416fc2 | 7381 | return; |
7382 | } | |
7383 | ||
7384 | /* | |
b423cd4c | 7385 | * Send the attributes... |
ef416fc2 | 7386 | */ |
7387 | ||
7388 | ra = create_requested_array(con->request); | |
7389 | ||
b423cd4c | 7390 | copy_printer_attrs(con, printer, ra); |
ef416fc2 | 7391 | |
7392 | cupsArrayDelete(ra); | |
7393 | ||
7394 | con->response->request.status.status_code = IPP_OK; | |
7395 | } | |
7396 | ||
7397 | ||
7398 | /* | |
b423cd4c | 7399 | * 'get_printers()' - Get a list of printers or classes. |
ef416fc2 | 7400 | */ |
7401 | ||
7402 | static void | |
b423cd4c | 7403 | get_printers(cupsd_client_t *con, /* I - Client connection */ |
7404 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
ef416fc2 | 7405 | { |
b423cd4c | 7406 | http_status_t status; /* Policy status */ |
7407 | ipp_attribute_t *attr; /* Current attribute */ | |
bc44d920 | 7408 | int limit; /* Max number of printers to return */ |
b423cd4c | 7409 | int count; /* Number of printers that match */ |
7410 | cupsd_printer_t *printer; /* Current printer pointer */ | |
7411 | int printer_type, /* printer-type attribute */ | |
7412 | printer_mask; /* printer-type-mask attribute */ | |
7413 | char *location; /* Location string */ | |
7414 | const char *username; /* Current user */ | |
7415 | char *first_printer_name; /* first-printer-name attribute */ | |
7416 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 7417 | |
ef416fc2 | 7418 | |
b423cd4c | 7419 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, |
7420 | con->http.fd, type); | |
ef416fc2 | 7421 | |
7422 | /* | |
7423 | * Check policy... | |
7424 | */ | |
7425 | ||
b423cd4c | 7426 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 7427 | { |
f899b121 | 7428 | send_http_error(con, status, NULL); |
ef416fc2 | 7429 | return; |
7430 | } | |
7431 | ||
7432 | /* | |
b423cd4c | 7433 | * Check for printers... |
ef416fc2 | 7434 | */ |
7435 | ||
b423cd4c | 7436 | if (!Printers || !cupsArrayCount(Printers)) |
7437 | { | |
7438 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
7439 | return; | |
7440 | } | |
7441 | ||
7442 | /* | |
7443 | * See if they want to limit the number of printers reported... | |
7444 | */ | |
ef416fc2 | 7445 | |
fa73b229 | 7446 | if ((attr = ippFindAttribute(con->request, "limit", |
7447 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 7448 | limit = attr->values[0].integer; |
7449 | else | |
b423cd4c | 7450 | limit = 10000000; |
7451 | ||
7452 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
7453 | IPP_TAG_NAME)) != NULL) | |
7454 | first_printer_name = attr->values[0].string.text; | |
7455 | else | |
7456 | first_printer_name = NULL; | |
ef416fc2 | 7457 | |
7458 | /* | |
b423cd4c | 7459 | * Support filtering... |
ef416fc2 | 7460 | */ |
7461 | ||
b423cd4c | 7462 | if ((attr = ippFindAttribute(con->request, "printer-type", |
7463 | IPP_TAG_ENUM)) != NULL) | |
7464 | printer_type = attr->values[0].integer; | |
ef416fc2 | 7465 | else |
b423cd4c | 7466 | printer_type = 0; |
ef416fc2 | 7467 | |
b423cd4c | 7468 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", |
7469 | IPP_TAG_ENUM)) != NULL) | |
7470 | printer_mask = attr->values[0].integer; | |
ef416fc2 | 7471 | else |
b423cd4c | 7472 | printer_mask = 0; |
e00b005a | 7473 | |
b423cd4c | 7474 | if ((attr = ippFindAttribute(con->request, "printer-location", |
7475 | IPP_TAG_TEXT)) != NULL) | |
7476 | location = attr->values[0].string.text; | |
7477 | else | |
7478 | location = NULL; | |
e00b005a | 7479 | |
7480 | if (con->username[0]) | |
b423cd4c | 7481 | username = con->username; |
e00b005a | 7482 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
7483 | IPP_TAG_NAME)) != NULL) | |
b423cd4c | 7484 | username = attr->values[0].string.text; |
e00b005a | 7485 | else |
b423cd4c | 7486 | username = NULL; |
ef416fc2 | 7487 | |
b423cd4c | 7488 | ra = create_requested_array(con->request); |
ef416fc2 | 7489 | |
7490 | /* | |
b423cd4c | 7491 | * OK, build a list of printers for this printer... |
ef416fc2 | 7492 | */ |
7493 | ||
b423cd4c | 7494 | if (first_printer_name) |
ef416fc2 | 7495 | { |
b423cd4c | 7496 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) |
7497 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 7498 | } |
7499 | else | |
b423cd4c | 7500 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
ef416fc2 | 7501 | |
b423cd4c | 7502 | for (count = 0; |
7503 | count < limit && printer; | |
7504 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
7505 | { | |
7506 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && | |
7507 | (printer->type & printer_mask) == printer_type && | |
7508 | (!location || !printer->location || | |
7509 | !strcasecmp(printer->location, location))) | |
ef416fc2 | 7510 | { |
7511 | /* | |
b423cd4c | 7512 | * If HideImplicitMembers is enabled, see if this printer or class |
7513 | * is a member of an implicit class... | |
ef416fc2 | 7514 | */ |
7515 | ||
b423cd4c | 7516 | if (ImplicitClasses && HideImplicitMembers && |
7517 | printer->in_implicit_class) | |
7518 | continue; | |
ef416fc2 | 7519 | |
b423cd4c | 7520 | /* |
7521 | * If a username is specified, see if it is allowed or denied | |
7522 | * access... | |
7523 | */ | |
ef416fc2 | 7524 | |
b423cd4c | 7525 | if (printer->num_users && username && !user_allowed(printer, username)) |
7526 | continue; | |
ef416fc2 | 7527 | |
b423cd4c | 7528 | /* |
7529 | * Add the group separator as needed... | |
7530 | */ | |
ef416fc2 | 7531 | |
b423cd4c | 7532 | if (count > 0) |
7533 | ippAddSeparator(con->response); | |
ef416fc2 | 7534 | |
b423cd4c | 7535 | count ++; |
ef416fc2 | 7536 | |
b423cd4c | 7537 | /* |
7538 | * Send the attributes... | |
7539 | */ | |
ef416fc2 | 7540 | |
b423cd4c | 7541 | copy_printer_attrs(con, printer, ra); |
7542 | } | |
ef416fc2 | 7543 | } |
7544 | ||
b423cd4c | 7545 | cupsArrayDelete(ra); |
ef416fc2 | 7546 | |
7547 | con->response->request.status.status_code = IPP_OK; | |
7548 | } | |
7549 | ||
7550 | ||
7551 | /* | |
b423cd4c | 7552 | * 'get_subscription_attrs()' - Get subscription attributes. |
ef416fc2 | 7553 | */ |
7554 | ||
7555 | static void | |
b423cd4c | 7556 | get_subscription_attrs( |
7557 | cupsd_client_t *con, /* I - Client connection */ | |
7558 | int sub_id) /* I - Subscription ID */ | |
ef416fc2 | 7559 | { |
b423cd4c | 7560 | http_status_t status; /* Policy status */ |
7561 | cupsd_subscription_t *sub; /* Subscription */ | |
7562 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 7563 | |
7564 | ||
b423cd4c | 7565 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7566 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
7567 | con, con->http.fd, sub_id); | |
ef416fc2 | 7568 | |
fa73b229 | 7569 | /* |
b423cd4c | 7570 | * Is the subscription ID valid? |
fa73b229 | 7571 | */ |
7572 | ||
b423cd4c | 7573 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
fa73b229 | 7574 | { |
7575 | /* | |
b423cd4c | 7576 | * Bad subscription ID... |
fa73b229 | 7577 | */ |
7578 | ||
7579 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 7580 | _("notify-subscription-id %d no good!"), sub_id); |
fa73b229 | 7581 | return; |
7582 | } | |
7583 | ||
7584 | /* | |
7585 | * Check policy... | |
7586 | */ | |
7587 | ||
b423cd4c | 7588 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
7589 | DefaultPolicyPtr, | |
7590 | con, sub->owner)) != HTTP_OK) | |
fa73b229 | 7591 | { |
f899b121 | 7592 | send_http_error(con, status, sub->dest); |
fa73b229 | 7593 | return; |
7594 | } | |
7595 | ||
ef416fc2 | 7596 | /* |
b423cd4c | 7597 | * Copy the subscription attributes to the response using the |
7598 | * requested-attributes attribute that may be provided by the client. | |
ef416fc2 | 7599 | */ |
7600 | ||
b423cd4c | 7601 | ra = create_requested_array(con->request); |
fa73b229 | 7602 | |
b423cd4c | 7603 | copy_subscription_attrs(con, sub, ra); |
ef416fc2 | 7604 | |
b423cd4c | 7605 | cupsArrayDelete(ra); |
fa73b229 | 7606 | |
b423cd4c | 7607 | con->response->request.status.status_code = IPP_OK; |
7608 | } | |
fa73b229 | 7609 | |
fa73b229 | 7610 | |
b423cd4c | 7611 | /* |
7612 | * 'get_subscriptions()' - Get subscriptions. | |
7613 | */ | |
ef416fc2 | 7614 | |
b423cd4c | 7615 | static void |
7616 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
7617 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
7618 | { | |
7619 | http_status_t status; /* Policy status */ | |
7620 | int count; /* Number of subscriptions */ | |
7621 | int limit; /* Limit */ | |
7622 | cupsd_subscription_t *sub; /* Subscription */ | |
7623 | cups_array_t *ra; /* Requested attributes array */ | |
7624 | ipp_attribute_t *attr; /* Attribute */ | |
bc44d920 | 7625 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
f7deaa1a | 7626 | char scheme[HTTP_MAX_URI], |
7627 | /* Scheme portion of URI */ | |
b423cd4c | 7628 | username[HTTP_MAX_URI], |
7629 | /* Username portion of URI */ | |
7630 | host[HTTP_MAX_URI], | |
7631 | /* Host portion of URI */ | |
7632 | resource[HTTP_MAX_URI]; | |
7633 | /* Resource portion of URI */ | |
7634 | int port; /* Port portion of URI */ | |
7635 | cupsd_job_t *job; /* Job pointer */ | |
7636 | cupsd_printer_t *printer; /* Printer */ | |
fa73b229 | 7637 | |
fa73b229 | 7638 | |
b423cd4c | 7639 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
7640 | "get_subscriptions(con=%p[%d], uri=%s)", | |
7641 | con, con->http.fd, uri->values[0].string.text); | |
7642 | ||
7643 | /* | |
7644 | * Is the destination valid? | |
7645 | */ | |
7646 | ||
f7deaa1a | 7647 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7648 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7649 | sizeof(host), &port, resource, sizeof(resource)); |
7650 | ||
7651 | if (!strcmp(resource, "/") || | |
7652 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
7653 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
7654 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
7655 | { | |
7656 | printer = NULL; | |
7657 | job = NULL; | |
ef416fc2 | 7658 | } |
b423cd4c | 7659 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) |
ef416fc2 | 7660 | { |
b423cd4c | 7661 | printer = NULL; |
7662 | job = cupsdFindJob(atoi(resource + 6)); | |
ef416fc2 | 7663 | |
b423cd4c | 7664 | if (!job) |
ef416fc2 | 7665 | { |
b423cd4c | 7666 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"), |
7667 | resource + 6); | |
ef416fc2 | 7668 | return; |
7669 | } | |
b423cd4c | 7670 | } |
f7deaa1a | 7671 | else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
b423cd4c | 7672 | { |
fa73b229 | 7673 | /* |
b423cd4c | 7674 | * Bad URI... |
fa73b229 | 7675 | */ |
7676 | ||
b423cd4c | 7677 | send_ipp_status(con, IPP_NOT_FOUND, |
7678 | _("The printer or class was not found.")); | |
7679 | return; | |
7680 | } | |
7681 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
7682 | IPP_TAG_INTEGER)) != NULL) | |
7683 | { | |
7684 | job = cupsdFindJob(attr->values[0].integer); | |
ef416fc2 | 7685 | |
b423cd4c | 7686 | if (!job) |
fa73b229 | 7687 | { |
b423cd4c | 7688 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), |
7689 | attr->values[0].integer); | |
fa73b229 | 7690 | return; |
7691 | } | |
ef416fc2 | 7692 | } |
b423cd4c | 7693 | else |
7694 | job = NULL; | |
ef416fc2 | 7695 | |
7696 | /* | |
b423cd4c | 7697 | * Check policy... |
ef416fc2 | 7698 | */ |
7699 | ||
b423cd4c | 7700 | if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr : |
7701 | DefaultPolicyPtr, | |
7702 | con, NULL)) != HTTP_OK) | |
ef416fc2 | 7703 | { |
f899b121 | 7704 | send_http_error(con, status, printer); |
b423cd4c | 7705 | return; |
7706 | } | |
ef416fc2 | 7707 | |
b423cd4c | 7708 | /* |
7709 | * Copy the subscription attributes to the response using the | |
7710 | * requested-attributes attribute that may be provided by the client. | |
7711 | */ | |
ef416fc2 | 7712 | |
b423cd4c | 7713 | ra = create_requested_array(con->request); |
ef416fc2 | 7714 | |
b423cd4c | 7715 | if ((attr = ippFindAttribute(con->request, "limit", |
7716 | IPP_TAG_INTEGER)) != NULL) | |
7717 | limit = attr->values[0].integer; | |
7718 | else | |
7719 | limit = 0; | |
ef416fc2 | 7720 | |
b423cd4c | 7721 | /* |
7722 | * See if we only want to see subscriptions for a specific user... | |
7723 | */ | |
ef416fc2 | 7724 | |
b423cd4c | 7725 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", |
7726 | IPP_TAG_BOOLEAN)) != NULL && | |
7727 | attr->values[0].boolean) | |
7728 | strlcpy(username, get_username(con), sizeof(username)); | |
fa73b229 | 7729 | else |
b423cd4c | 7730 | username[0] = '\0'; |
ef416fc2 | 7731 | |
b423cd4c | 7732 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; |
7733 | sub; | |
7734 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
7735 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
7736 | (!username[0] || !strcasecmp(username, sub->owner))) | |
fa73b229 | 7737 | { |
b423cd4c | 7738 | ippAddSeparator(con->response); |
7739 | copy_subscription_attrs(con, sub, ra); | |
ef416fc2 | 7740 | |
b423cd4c | 7741 | count ++; |
7742 | if (limit && count >= limit) | |
7743 | break; | |
7744 | } | |
ef416fc2 | 7745 | |
b423cd4c | 7746 | cupsArrayDelete(ra); |
fa73b229 | 7747 | |
b423cd4c | 7748 | if (count) |
7749 | con->response->request.status.status_code = IPP_OK; | |
7750 | else | |
7751 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
7752 | } | |
ef416fc2 | 7753 | |
ef416fc2 | 7754 | |
b423cd4c | 7755 | /* |
7756 | * 'get_username()' - Get the username associated with a request. | |
7757 | */ | |
ef416fc2 | 7758 | |
b423cd4c | 7759 | static const char * /* O - Username */ |
7760 | get_username(cupsd_client_t *con) /* I - Connection */ | |
7761 | { | |
7762 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 7763 | |
ef416fc2 | 7764 | |
b423cd4c | 7765 | if (con->username[0]) |
7766 | return (con->username); | |
7767 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
7768 | IPP_TAG_NAME)) != NULL) | |
7769 | return (attr->values[0].string.text); | |
7770 | else | |
7771 | return ("anonymous"); | |
ef416fc2 | 7772 | } |
7773 | ||
7774 | ||
7775 | /* | |
b423cd4c | 7776 | * 'hold_job()' - Hold a print job. |
ef416fc2 | 7777 | */ |
7778 | ||
b423cd4c | 7779 | static void |
7780 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
7781 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 7782 | { |
b423cd4c | 7783 | ipp_attribute_t *attr, /* Current job-hold-until */ |
7784 | *newattr; /* New job-hold-until */ | |
7785 | int jobid; /* Job ID */ | |
7786 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7787 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7788 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7789 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7790 | int port; /* Port portion of URI */ | |
7791 | cupsd_job_t *job; /* Job information */ | |
7792 | ||
ef416fc2 | 7793 | |
b423cd4c | 7794 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd, |
7795 | uri->values[0].string.text); | |
ef416fc2 | 7796 | |
7797 | /* | |
b423cd4c | 7798 | * See if we have a job URI or a printer URI... |
ef416fc2 | 7799 | */ |
7800 | ||
b423cd4c | 7801 | if (!strcmp(uri->name, "printer-uri")) |
7802 | { | |
7803 | /* | |
7804 | * Got a printer URI; see if we also have a job-id attribute... | |
7805 | */ | |
ef416fc2 | 7806 | |
b423cd4c | 7807 | if ((attr = ippFindAttribute(con->request, "job-id", |
7808 | IPP_TAG_INTEGER)) == NULL) | |
7809 | { | |
7810 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7811 | _("Got a printer-uri attribute but no job-id!")); | |
7812 | return; | |
7813 | } | |
ef416fc2 | 7814 | |
b423cd4c | 7815 | jobid = attr->values[0].integer; |
7816 | } | |
ef416fc2 | 7817 | else |
ef416fc2 | 7818 | { |
b423cd4c | 7819 | /* |
7820 | * Got a job URI; parse it to get the job ID... | |
7821 | */ | |
ef416fc2 | 7822 | |
b423cd4c | 7823 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7824 | sizeof(method), username, sizeof(username), host, | |
7825 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7826 | |
b423cd4c | 7827 | if (strncmp(resource, "/jobs/", 6)) |
7828 | { | |
7829 | /* | |
7830 | * Not a valid URI! | |
7831 | */ | |
ef416fc2 | 7832 | |
b423cd4c | 7833 | send_ipp_status(con, IPP_BAD_REQUEST, |
7834 | _("Bad job-uri attribute \"%s\"!"), | |
7835 | uri->values[0].string.text); | |
7836 | return; | |
7837 | } | |
ef416fc2 | 7838 | |
b423cd4c | 7839 | jobid = atoi(resource + 6); |
7840 | } | |
ef416fc2 | 7841 | |
ef416fc2 | 7842 | /* |
b423cd4c | 7843 | * See if the job exists... |
ef416fc2 | 7844 | */ |
7845 | ||
b423cd4c | 7846 | if ((job = cupsdFindJob(jobid)) == NULL) |
7847 | { | |
7848 | /* | |
7849 | * Nope - return a "not found" error... | |
7850 | */ | |
7851 | ||
7852 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7853 | return; | |
7854 | } | |
ef416fc2 | 7855 | |
7856 | /* | |
b423cd4c | 7857 | * See if the job is owned by the requesting user... |
ef416fc2 | 7858 | */ |
7859 | ||
b423cd4c | 7860 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
7861 | { | |
2fb76298 | 7862 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 7863 | return; |
7864 | } | |
ef416fc2 | 7865 | |
7866 | /* | |
b423cd4c | 7867 | * Hold the job and return... |
ef416fc2 | 7868 | */ |
7869 | ||
b423cd4c | 7870 | cupsdHoldJob(job); |
ef416fc2 | 7871 | |
01ce6322 | 7872 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 7873 | "Job held by user."); |
7874 | ||
b423cd4c | 7875 | if ((newattr = ippFindAttribute(con->request, "job-hold-until", |
7876 | IPP_TAG_KEYWORD)) == NULL) | |
7877 | newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 7878 | |
b423cd4c | 7879 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7880 | IPP_TAG_KEYWORD)) == NULL) | |
7881 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 7882 | |
b423cd4c | 7883 | if (attr) |
7884 | { | |
7885 | /* | |
7886 | * Free the old hold value and copy the new one over... | |
7887 | */ | |
ef416fc2 | 7888 | |
757d2cad | 7889 | _cupsStrFree(attr->values[0].string.text); |
ef416fc2 | 7890 | |
b423cd4c | 7891 | if (newattr) |
ef416fc2 | 7892 | { |
b423cd4c | 7893 | attr->value_tag = newattr->value_tag; |
7894 | attr->values[0].string.text = | |
757d2cad | 7895 | _cupsStrAlloc(newattr->values[0].string.text); |
b423cd4c | 7896 | } |
7897 | else | |
7898 | { | |
7899 | attr->value_tag = IPP_TAG_KEYWORD; | |
757d2cad | 7900 | attr->values[0].string.text = _cupsStrAlloc("indefinite"); |
ef416fc2 | 7901 | } |
7902 | ||
b423cd4c | 7903 | /* |
7904 | * Hold job until specified time... | |
7905 | */ | |
ef416fc2 | 7906 | |
b423cd4c | 7907 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
d09495fa | 7908 | |
01ce6322 | 7909 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 7910 | "Job job-hold-until value changed by user."); |
b423cd4c | 7911 | } |
ef416fc2 | 7912 | |
09a101d6 | 7913 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Held by \"%s\".", jobid, |
b423cd4c | 7914 | username); |
7915 | ||
7916 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 7917 | } |
7918 | ||
7919 | ||
7920 | /* | |
b423cd4c | 7921 | * 'move_job()' - Move a job to a new destination. |
ef416fc2 | 7922 | */ |
7923 | ||
7924 | static void | |
b423cd4c | 7925 | move_job(cupsd_client_t *con, /* I - Client connection */ |
7926 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 7927 | { |
7928 | http_status_t status; /* Policy status */ | |
7929 | ipp_attribute_t *attr; /* Current attribute */ | |
b423cd4c | 7930 | int jobid; /* Job ID */ |
ef416fc2 | 7931 | cupsd_job_t *job; /* Current job */ |
d09495fa | 7932 | const char *src; /* Source printer/class */ |
b423cd4c | 7933 | cups_ptype_t stype, /* Source type (printer or class) */ |
bc44d920 | 7934 | dtype; /* Destination type (printer/class) */ |
f7deaa1a | 7935 | char scheme[HTTP_MAX_URI], /* Scheme portion of URI */ |
ef416fc2 | 7936 | username[HTTP_MAX_URI], /* Username portion of URI */ |
7937 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
b423cd4c | 7938 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
ef416fc2 | 7939 | int port; /* Port portion of URI */ |
b423cd4c | 7940 | cupsd_printer_t *sprinter, /* Source printer */ |
7941 | *dprinter; /* Destination printer */ | |
ef416fc2 | 7942 | |
7943 | ||
b423cd4c | 7944 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd, |
ef416fc2 | 7945 | uri->values[0].string.text); |
7946 | ||
7947 | /* | |
b423cd4c | 7948 | * Get the new printer or class... |
ef416fc2 | 7949 | */ |
7950 | ||
b423cd4c | 7951 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", |
7952 | IPP_TAG_URI)) == NULL) | |
ef416fc2 | 7953 | { |
b423cd4c | 7954 | /* |
7955 | * Need job-printer-uri... | |
7956 | */ | |
ef416fc2 | 7957 | |
b423cd4c | 7958 | send_ipp_status(con, IPP_BAD_REQUEST, |
7959 | _("job-printer-uri attribute missing!")); | |
7960 | return; | |
ef416fc2 | 7961 | } |
f7faf1f5 | 7962 | |
f7deaa1a | 7963 | if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter)) |
ef416fc2 | 7964 | { |
b423cd4c | 7965 | /* |
7966 | * Bad URI... | |
7967 | */ | |
ef416fc2 | 7968 | |
b423cd4c | 7969 | send_ipp_status(con, IPP_NOT_FOUND, |
7970 | _("The printer or class was not found.")); | |
7971 | return; | |
ef416fc2 | 7972 | } |
7973 | ||
ef416fc2 | 7974 | /* |
b423cd4c | 7975 | * See if we have a job URI or a printer URI... |
ef416fc2 | 7976 | */ |
7977 | ||
f7deaa1a | 7978 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
7979 | sizeof(scheme), username, sizeof(username), host, | |
b423cd4c | 7980 | sizeof(host), &port, resource, sizeof(resource)); |
7981 | ||
7982 | if (!strcmp(uri->name, "printer-uri")) | |
ef416fc2 | 7983 | { |
7984 | /* | |
b423cd4c | 7985 | * Got a printer URI; see if we also have a job-id attribute... |
ef416fc2 | 7986 | */ |
7987 | ||
b423cd4c | 7988 | if ((attr = ippFindAttribute(con->request, "job-id", |
7989 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7990 | { |
b423cd4c | 7991 | /* |
7992 | * Move all jobs... | |
7993 | */ | |
7994 | ||
f7deaa1a | 7995 | if ((src = cupsdValidateDest(uri->values[0].string.text, &stype, |
7996 | &sprinter)) == NULL) | |
b423cd4c | 7997 | { |
7998 | /* | |
7999 | * Bad URI... | |
8000 | */ | |
8001 | ||
8002 | send_ipp_status(con, IPP_NOT_FOUND, | |
8003 | _("The printer or class was not found.")); | |
8004 | return; | |
8005 | } | |
8006 | ||
8007 | job = NULL; | |
8008 | } | |
8009 | else | |
8010 | { | |
8011 | /* | |
8012 | * Otherwise, just move a single job... | |
8013 | */ | |
8014 | ||
8015 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
8016 | { | |
8017 | /* | |
8018 | * Nope - return a "not found" error... | |
8019 | */ | |
8020 | ||
8021 | send_ipp_status(con, IPP_NOT_FOUND, | |
8022 | _("Job #%d does not exist!"), attr->values[0].integer); | |
8023 | return; | |
8024 | } | |
8025 | else | |
8026 | { | |
8027 | /* | |
8028 | * Job found, initialize source pointers... | |
8029 | */ | |
8030 | ||
8031 | src = NULL; | |
8032 | sprinter = NULL; | |
8033 | } | |
ef416fc2 | 8034 | } |
8035 | } | |
8036 | else | |
8037 | { | |
8038 | /* | |
b423cd4c | 8039 | * Got a job URI; parse it to get the job ID... |
ef416fc2 | 8040 | */ |
8041 | ||
b423cd4c | 8042 | if (strncmp(resource, "/jobs/", 6)) |
8043 | { | |
8044 | /* | |
8045 | * Not a valid URI! | |
8046 | */ | |
8047 | ||
8048 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8049 | _("Bad job-uri attribute \"%s\"!"), | |
8050 | uri->values[0].string.text); | |
8051 | return; | |
8052 | } | |
ef416fc2 | 8053 | |
ef416fc2 | 8054 | /* |
b423cd4c | 8055 | * See if the job exists... |
ef416fc2 | 8056 | */ |
8057 | ||
b423cd4c | 8058 | jobid = atoi(resource + 6); |
ef416fc2 | 8059 | |
b423cd4c | 8060 | if ((job = cupsdFindJob(jobid)) == NULL) |
ef416fc2 | 8061 | { |
8062 | /* | |
b423cd4c | 8063 | * Nope - return a "not found" error... |
ef416fc2 | 8064 | */ |
8065 | ||
b423cd4c | 8066 | send_ipp_status(con, IPP_NOT_FOUND, |
8067 | _("Job #%d does not exist!"), jobid); | |
8068 | return; | |
ef416fc2 | 8069 | } |
8070 | else | |
b423cd4c | 8071 | { |
8072 | /* | |
8073 | * Job found, initialize source pointers... | |
8074 | */ | |
ef416fc2 | 8075 | |
b423cd4c | 8076 | src = NULL; |
8077 | sprinter = NULL; | |
8078 | } | |
ef416fc2 | 8079 | } |
8080 | ||
ac884b6a MS |
8081 | /* |
8082 | * Check the policy of the destination printer... | |
8083 | */ | |
8084 | ||
8085 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, | |
8086 | job ? job->username : NULL)) != HTTP_OK) | |
8087 | { | |
8088 | send_http_error(con, status, dprinter); | |
8089 | return; | |
8090 | } | |
8091 | ||
ef416fc2 | 8092 | /* |
b423cd4c | 8093 | * Now move the job or jobs... |
ef416fc2 | 8094 | */ |
8095 | ||
b423cd4c | 8096 | if (job) |
8097 | { | |
8098 | /* | |
8099 | * See if the job has been completed... | |
8100 | */ | |
8101 | ||
8102 | if (job->state_value > IPP_JOB_STOPPED) | |
8103 | { | |
8104 | /* | |
8105 | * Return a "not-possible" error... | |
8106 | */ | |
8107 | ||
8108 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8109 | _("Job #%d is finished and cannot be altered!"), | |
8110 | job->id); | |
8111 | return; | |
8112 | } | |
ef416fc2 | 8113 | |
b423cd4c | 8114 | /* |
8115 | * See if the job is owned by the requesting user... | |
8116 | */ | |
ef416fc2 | 8117 | |
b423cd4c | 8118 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8119 | { | |
2fb76298 | 8120 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
b423cd4c | 8121 | return; |
8122 | } | |
ef416fc2 | 8123 | |
ef416fc2 | 8124 | /* |
b423cd4c | 8125 | * Move the job to a different printer or class... |
ef416fc2 | 8126 | */ |
8127 | ||
e53920b9 | 8128 | cupsdMoveJob(job, dprinter); |
ef416fc2 | 8129 | } |
b423cd4c | 8130 | else |
8131 | { | |
8132 | /* | |
8133 | * Got the source printer, now look through the jobs... | |
8134 | */ | |
ef416fc2 | 8135 | |
b423cd4c | 8136 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
8137 | job; | |
8138 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
8139 | { | |
8140 | /* | |
8141 | * See if the job is pointing at the source printer or has not been | |
8142 | * completed... | |
8143 | */ | |
ef416fc2 | 8144 | |
b423cd4c | 8145 | if (strcasecmp(job->dest, src) || |
8146 | job->state_value > IPP_JOB_STOPPED) | |
8147 | continue; | |
ef416fc2 | 8148 | |
b423cd4c | 8149 | /* |
8150 | * See if the job can be moved by the requesting user... | |
8151 | */ | |
ef416fc2 | 8152 | |
b423cd4c | 8153 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
8154 | continue; | |
ef416fc2 | 8155 | |
b423cd4c | 8156 | /* |
8157 | * Move the job to a different printer or class... | |
8158 | */ | |
ef416fc2 | 8159 | |
e53920b9 | 8160 | cupsdMoveJob(job, dprinter); |
b423cd4c | 8161 | } |
ef416fc2 | 8162 | } |
8163 | ||
8164 | /* | |
b423cd4c | 8165 | * Start jobs if possible... |
ef416fc2 | 8166 | */ |
8167 | ||
b423cd4c | 8168 | cupsdCheckJobs(); |
ef416fc2 | 8169 | |
8170 | /* | |
b423cd4c | 8171 | * Return with "everything is OK" status... |
ef416fc2 | 8172 | */ |
8173 | ||
b423cd4c | 8174 | con->response->request.status.status_code = IPP_OK; |
8175 | } | |
ef416fc2 | 8176 | |
ef416fc2 | 8177 | |
b423cd4c | 8178 | /* |
8179 | * 'ppd_parse_line()' - Parse a PPD default line. | |
8180 | */ | |
ef416fc2 | 8181 | |
b423cd4c | 8182 | static int /* O - 0 on success, -1 on failure */ |
8183 | ppd_parse_line(const char *line, /* I - Line */ | |
8184 | char *option, /* O - Option name */ | |
8185 | int olen, /* I - Size of option name */ | |
8186 | char *choice, /* O - Choice name */ | |
8187 | int clen) /* I - Size of choice name */ | |
8188 | { | |
8189 | /* | |
8190 | * Verify this is a default option line... | |
8191 | */ | |
ef416fc2 | 8192 | |
b423cd4c | 8193 | if (strncmp(line, "*Default", 8)) |
8194 | return (-1); | |
ef416fc2 | 8195 | |
b423cd4c | 8196 | /* |
8197 | * Read the option name... | |
8198 | */ | |
8199 | ||
8200 | for (line += 8, olen --; isalnum(*line & 255); line ++) | |
8201 | if (olen > 0) | |
8202 | { | |
8203 | *option++ = *line; | |
8204 | olen --; | |
ef416fc2 | 8205 | } |
8206 | ||
b423cd4c | 8207 | *option = '\0'; |
ef416fc2 | 8208 | |
b423cd4c | 8209 | /* |
8210 | * Skip everything else up to the colon (:)... | |
8211 | */ | |
ef416fc2 | 8212 | |
b423cd4c | 8213 | while (*line && *line != ':') |
8214 | line ++; | |
ef416fc2 | 8215 | |
b423cd4c | 8216 | if (!*line) |
8217 | return (-1); | |
ef416fc2 | 8218 | |
b423cd4c | 8219 | line ++; |
ef416fc2 | 8220 | |
b423cd4c | 8221 | /* |
8222 | * Now grab the option choice, skipping leading whitespace... | |
8223 | */ | |
ef416fc2 | 8224 | |
b423cd4c | 8225 | while (isspace(*line & 255)) |
8226 | line ++; | |
ef416fc2 | 8227 | |
b423cd4c | 8228 | for (clen --; isalnum(*line & 255); line ++) |
8229 | if (clen > 0) | |
8230 | { | |
8231 | *choice++ = *line; | |
8232 | clen --; | |
8233 | } | |
ef416fc2 | 8234 | |
b423cd4c | 8235 | *choice = '\0'; |
ef416fc2 | 8236 | |
b423cd4c | 8237 | /* |
8238 | * Return with no errors... | |
8239 | */ | |
ef416fc2 | 8240 | |
b423cd4c | 8241 | return (0); |
8242 | } | |
ef416fc2 | 8243 | |
ef416fc2 | 8244 | |
b423cd4c | 8245 | /* |
8246 | * 'print_job()' - Print a file to a printer or class. | |
8247 | */ | |
ef416fc2 | 8248 | |
b423cd4c | 8249 | static void |
8250 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
8251 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8252 | { | |
8253 | ipp_attribute_t *attr; /* Current attribute */ | |
8254 | ipp_attribute_t *format; /* Document-format attribute */ | |
f7deaa1a | 8255 | const char *default_format; /* document-format-default value */ |
b423cd4c | 8256 | cupsd_job_t *job; /* New job */ |
8257 | char filename[1024]; /* Job filename */ | |
8258 | mime_type_t *filetype; /* Type of file */ | |
8259 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
8260 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
8261 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
8262 | /* Textual name of mime type */ | |
8263 | cupsd_printer_t *printer; /* Printer data */ | |
8264 | struct stat fileinfo; /* File information */ | |
8265 | int kbytes; /* Size of file */ | |
8266 | int compression; /* Document compression */ | |
ef416fc2 | 8267 | |
ef416fc2 | 8268 | |
b423cd4c | 8269 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd, |
8270 | uri->values[0].string.text); | |
ef416fc2 | 8271 | |
b423cd4c | 8272 | /* |
8273 | * Validate print file attributes, for now just document-format and | |
8274 | * compression (CUPS only supports "none" and "gzip")... | |
8275 | */ | |
ef416fc2 | 8276 | |
b423cd4c | 8277 | compression = CUPS_FILE_NONE; |
ef416fc2 | 8278 | |
b423cd4c | 8279 | if ((attr = ippFindAttribute(con->request, "compression", |
8280 | IPP_TAG_KEYWORD)) != NULL) | |
8281 | { | |
8282 | if (strcmp(attr->values[0].string.text, "none") | |
8283 | #ifdef HAVE_LIBZ | |
8284 | && strcmp(attr->values[0].string.text, "gzip") | |
8285 | #endif /* HAVE_LIBZ */ | |
8286 | ) | |
8287 | { | |
8288 | send_ipp_status(con, IPP_ATTRIBUTES, | |
8289 | _("Unsupported compression \"%s\"!"), | |
8290 | attr->values[0].string.text); | |
8291 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
8292 | "compression", NULL, attr->values[0].string.text); | |
8293 | return; | |
8294 | } | |
ef416fc2 | 8295 | |
b423cd4c | 8296 | #ifdef HAVE_LIBZ |
8297 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
8298 | compression = CUPS_FILE_GZIP; | |
8299 | #endif /* HAVE_LIBZ */ | |
8300 | } | |
ef416fc2 | 8301 | |
b423cd4c | 8302 | /* |
8303 | * Do we have a file to print? | |
8304 | */ | |
ef416fc2 | 8305 | |
b423cd4c | 8306 | if (!con->filename) |
8307 | { | |
8308 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
8309 | return; | |
8310 | } | |
ef416fc2 | 8311 | |
f7deaa1a | 8312 | /* |
8313 | * Is the destination valid? | |
8314 | */ | |
8315 | ||
8316 | if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer)) | |
8317 | { | |
8318 | /* | |
8319 | * Bad URI... | |
8320 | */ | |
8321 | ||
8322 | send_ipp_status(con, IPP_NOT_FOUND, | |
8323 | _("The printer or class was not found.")); | |
8324 | return; | |
8325 | } | |
8326 | ||
b423cd4c | 8327 | /* |
8328 | * Is it a format we support? | |
8329 | */ | |
ef416fc2 | 8330 | |
b423cd4c | 8331 | if ((format = ippFindAttribute(con->request, "document-format", |
8332 | IPP_TAG_MIMETYPE)) != NULL) | |
8333 | { | |
8334 | /* | |
8335 | * Grab format from client... | |
8336 | */ | |
8337 | ||
f7deaa1a | 8338 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, |
8339 | type) != 2) | |
b423cd4c | 8340 | { |
8341 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8342 | _("Could not scan type \"%s\"!"), | |
8343 | format->values[0].string.text); | |
8344 | return; | |
ef416fc2 | 8345 | } |
b423cd4c | 8346 | } |
f7deaa1a | 8347 | else if ((default_format = cupsGetOption("document-format", |
8348 | printer->num_options, | |
8349 | printer->options)) != NULL) | |
8350 | { | |
8351 | /* | |
8352 | * Use default document format... | |
8353 | */ | |
8354 | ||
8355 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
8356 | { | |
8357 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8358 | _("Could not scan type \"%s\"!"), | |
8359 | default_format); | |
8360 | return; | |
8361 | } | |
8362 | } | |
b423cd4c | 8363 | else |
8364 | { | |
8365 | /* | |
f7deaa1a | 8366 | * Auto-type it! |
b423cd4c | 8367 | */ |
8368 | ||
8369 | strcpy(super, "application"); | |
8370 | strcpy(type, "octet-stream"); | |
8371 | } | |
ef416fc2 | 8372 | |
b423cd4c | 8373 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
8374 | { | |
ef416fc2 | 8375 | /* |
b423cd4c | 8376 | * Auto-type the file... |
ef416fc2 | 8377 | */ |
8378 | ||
b423cd4c | 8379 | ipp_attribute_t *doc_name; /* document-name attribute */ |
8380 | ||
8381 | ||
a0f6818e | 8382 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file..."); |
b423cd4c | 8383 | |
8384 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
8385 | filetype = mimeFileType(MimeDatabase, con->filename, | |
8386 | doc_name ? doc_name->values[0].string.text : NULL, | |
8387 | &compression); | |
8388 | ||
f7deaa1a | 8389 | if (!filetype) |
8390 | filetype = mimeType(MimeDatabase, super, type); | |
a0f6818e MS |
8391 | |
8392 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.", | |
8393 | filetype->super, filetype->type); | |
f7deaa1a | 8394 | } |
8395 | else | |
8396 | filetype = mimeType(MimeDatabase, super, type); | |
ef416fc2 | 8397 | |
f7deaa1a | 8398 | if (filetype && |
8399 | (!format || | |
8400 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) | |
8401 | { | |
8402 | /* | |
8403 | * Replace the document-format attribute value with the auto-typed or | |
8404 | * default one. | |
8405 | */ | |
b423cd4c | 8406 | |
f7deaa1a | 8407 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
8408 | filetype->type); | |
ed486911 | 8409 | |
f7deaa1a | 8410 | if (format) |
8411 | { | |
8412 | _cupsStrFree(format->values[0].string.text); | |
8413 | ||
8414 | format->values[0].string.text = _cupsStrAlloc(mimetype); | |
ef416fc2 | 8415 | } |
b423cd4c | 8416 | else |
f7deaa1a | 8417 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
8418 | "document-format", NULL, mimetype); | |
ef416fc2 | 8419 | } |
f7deaa1a | 8420 | else if (!filetype) |
b423cd4c | 8421 | { |
8422 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
8423 | _("Unsupported format \'%s/%s\'!"), super, type); | |
8424 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8425 | "Hint: Do you have the raw file printing rules enabled?"); | |
8426 | ||
8427 | if (format) | |
8428 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
8429 | "document-format", NULL, format->values[0].string.text); | |
8430 | ||
8431 | return; | |
8432 | } | |
8433 | ||
b423cd4c | 8434 | /* |
8435 | * Read any embedded job ticket info from PS files... | |
8436 | */ | |
8437 | ||
8438 | if (!strcasecmp(filetype->super, "application") && | |
8439 | !strcasecmp(filetype->type, "postscript")) | |
8440 | read_ps_job_ticket(con); | |
8441 | ||
8442 | /* | |
8443 | * Create the job object... | |
8444 | */ | |
8445 | ||
f7deaa1a | 8446 | if ((job = add_job(con, printer, filetype)) == NULL) |
b423cd4c | 8447 | return; |
8448 | ||
8449 | /* | |
8450 | * Update quota data... | |
8451 | */ | |
8452 | ||
8453 | if (stat(con->filename, &fileinfo)) | |
8454 | kbytes = 0; | |
8455 | else | |
8456 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
8457 | ||
8458 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
8459 | ||
8460 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
8461 | IPP_TAG_INTEGER)) != NULL) | |
8462 | attr->values[0].integer += kbytes; | |
8463 | ||
ef416fc2 | 8464 | /* |
8465 | * Add the job file... | |
8466 | */ | |
8467 | ||
8468 | if (add_file(con, job, filetype, compression)) | |
8469 | return; | |
8470 | ||
8471 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
8472 | job->num_files); | |
8473 | rename(con->filename, filename); | |
8474 | cupsdClearString(&con->filename); | |
8475 | ||
8476 | /* | |
8477 | * See if we need to add the ending sheet... | |
8478 | */ | |
8479 | ||
91c84a35 MS |
8480 | if (cupsdTimeoutJob(job)) |
8481 | return; | |
ef416fc2 | 8482 | |
ef416fc2 | 8483 | /* |
8484 | * Log and save the job... | |
8485 | */ | |
8486 | ||
a0f6818e MS |
8487 | cupsdLogMessage(CUPSD_LOG_INFO, |
8488 | "[Job %d] File of type %s/%s queued by \"%s\".", job->id, | |
8489 | filetype->super, filetype->type, job->username); | |
8490 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] hold_until=%d", job->id, | |
ef416fc2 | 8491 | (int)job->hold_until); |
8492 | ||
ef416fc2 | 8493 | /* |
8494 | * Start the job if possible... | |
8495 | */ | |
8496 | ||
8497 | cupsdCheckJobs(); | |
8498 | } | |
8499 | ||
8500 | ||
8501 | /* | |
8502 | * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file. | |
8503 | * | |
8504 | * This function only gets called when printing a single PostScript | |
8505 | * file using the Print-Job operation. It doesn't work for Create-Job + | |
8506 | * Send-File, since the job attributes need to be set at job creation | |
8507 | * time for banners to work. The embedded PS job ticket stuff is here | |
8508 | * only to allow the Windows printer driver for CUPS to pass in JCL | |
8509 | * options and IPP attributes which otherwise would be lost. | |
8510 | * | |
8511 | * The format of a PS job ticket is simple: | |
8512 | * | |
8513 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
8514 | * | |
8515 | * %cupsJobTicket: attr1=value1 | |
8516 | * %cupsJobTicket: attr2=value2 | |
8517 | * ... | |
8518 | * %cupsJobTicket: attrN=valueN | |
8519 | * | |
8520 | * Job ticket lines must appear immediately after the first line that | |
8521 | * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop | |
8522 | * looking for job ticket info when it finds a line that does not begin | |
8523 | * with "%cupsJobTicket:". | |
8524 | * | |
8525 | * The maximum length of a job ticket line, including the prefix, is | |
8526 | * 255 characters to conform with the Adobe DSC. | |
8527 | * | |
8528 | * Read-only attributes are rejected with a notice to the error log in | |
8529 | * case a malicious user tries anything. Since the job ticket is read | |
8530 | * prior to attribute validation in print_job(), job ticket attributes | |
8531 | * will go through the same validation as IPP attributes... | |
8532 | */ | |
8533 | ||
8534 | static void | |
8535 | read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */ | |
8536 | { | |
8537 | cups_file_t *fp; /* File to read from */ | |
8538 | char line[256]; /* Line data */ | |
8539 | int num_options; /* Number of options */ | |
8540 | cups_option_t *options; /* Options */ | |
8541 | ipp_t *ticket; /* New attributes */ | |
8542 | ipp_attribute_t *attr, /* Current attribute */ | |
8543 | *attr2, /* Job attribute */ | |
8544 | *prev2; /* Previous job attribute */ | |
8545 | ||
8546 | ||
8547 | /* | |
8548 | * First open the print file... | |
8549 | */ | |
8550 | ||
8551 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
8552 | { | |
8553 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8554 | "read_ps_job_ticket: Unable to open PostScript print file " | |
8555 | "- %s", | |
8556 | strerror(errno)); | |
8557 | return; | |
8558 | } | |
8559 | ||
8560 | /* | |
8561 | * Skip the first line... | |
8562 | */ | |
8563 | ||
8564 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
8565 | { | |
8566 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
8567 | "read_ps_job_ticket: Unable to read from PostScript print " | |
8568 | "file - %s", | |
8569 | strerror(errno)); | |
8570 | cupsFileClose(fp); | |
8571 | return; | |
8572 | } | |
8573 | ||
fa73b229 | 8574 | if (strncmp(line, "%!PS-Adobe-", 11)) |
ef416fc2 | 8575 | { |
8576 | /* | |
8577 | * Not a DSC-compliant file, so no job ticket info will be available... | |
8578 | */ | |
8579 | ||
8580 | cupsFileClose(fp); | |
8581 | return; | |
8582 | } | |
8583 | ||
8584 | /* | |
8585 | * Read job ticket info from the file... | |
8586 | */ | |
8587 | ||
8588 | num_options = 0; | |
8589 | options = NULL; | |
8590 | ||
fa73b229 | 8591 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 8592 | { |
8593 | /* | |
8594 | * Stop at the first non-ticket line... | |
8595 | */ | |
8596 | ||
fa73b229 | 8597 | if (strncmp(line, "%cupsJobTicket:", 15)) |
ef416fc2 | 8598 | break; |
8599 | ||
8600 | /* | |
8601 | * Add the options to the option array... | |
8602 | */ | |
8603 | ||
8604 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
8605 | } | |
8606 | ||
8607 | /* | |
8608 | * Done with the file; see if we have any options... | |
8609 | */ | |
8610 | ||
8611 | cupsFileClose(fp); | |
8612 | ||
8613 | if (num_options == 0) | |
8614 | return; | |
8615 | ||
8616 | /* | |
8617 | * OK, convert the options to an attribute list, and apply them to | |
8618 | * the request... | |
8619 | */ | |
8620 | ||
8621 | ticket = ippNew(); | |
8622 | cupsEncodeOptions(ticket, num_options, options); | |
8623 | ||
8624 | /* | |
8625 | * See what the user wants to change. | |
8626 | */ | |
8627 | ||
fa73b229 | 8628 | for (attr = ticket->attrs; attr; attr = attr->next) |
ef416fc2 | 8629 | { |
8630 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8631 | continue; | |
8632 | ||
8633 | if (!strcmp(attr->name, "job-originating-host-name") || | |
8634 | !strcmp(attr->name, "job-originating-user-name") || | |
8635 | !strcmp(attr->name, "job-media-sheets-completed") || | |
8636 | !strcmp(attr->name, "job-k-octets") || | |
8637 | !strcmp(attr->name, "job-id") || | |
8638 | !strncmp(attr->name, "job-state", 9) || | |
8639 | !strncmp(attr->name, "time-at-", 8)) | |
8640 | continue; /* Read-only attrs */ | |
8641 | ||
fa73b229 | 8642 | if ((attr2 = ippFindAttribute(con->request, attr->name, |
8643 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 8644 | { |
8645 | /* | |
8646 | * Some other value; first free the old value... | |
8647 | */ | |
8648 | ||
8649 | if (con->request->attrs == attr2) | |
8650 | { | |
8651 | con->request->attrs = attr2->next; | |
8652 | prev2 = NULL; | |
8653 | } | |
8654 | else | |
8655 | { | |
fa73b229 | 8656 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) |
ef416fc2 | 8657 | if (prev2->next == attr2) |
8658 | { | |
8659 | prev2->next = attr2->next; | |
8660 | break; | |
8661 | } | |
8662 | } | |
8663 | ||
8664 | if (con->request->last == attr2) | |
8665 | con->request->last = prev2; | |
8666 | ||
757d2cad | 8667 | _ippFreeAttr(attr2); |
ef416fc2 | 8668 | } |
8669 | ||
8670 | /* | |
8671 | * Add new option by copying it... | |
8672 | */ | |
8673 | ||
8674 | copy_attribute(con->request, attr, 0); | |
8675 | } | |
8676 | ||
8677 | /* | |
8678 | * Then free the attribute list and option array... | |
8679 | */ | |
8680 | ||
8681 | ippDelete(ticket); | |
8682 | cupsFreeOptions(num_options, options); | |
8683 | } | |
8684 | ||
8685 | ||
8686 | /* | |
8687 | * 'reject_jobs()' - Reject print jobs to a printer. | |
8688 | */ | |
8689 | ||
8690 | static void | |
8691 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
8692 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
8693 | { | |
8694 | http_status_t status; /* Policy status */ | |
bc44d920 | 8695 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 8696 | cupsd_printer_t *printer; /* Printer data */ |
8697 | ipp_attribute_t *attr; /* printer-state-message text */ | |
8698 | ||
8699 | ||
8700 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
8701 | con->http.fd, uri->values[0].string.text); | |
8702 | ||
8703 | /* | |
8704 | * Is the destination valid? | |
8705 | */ | |
8706 | ||
f7deaa1a | 8707 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 8708 | { |
8709 | /* | |
8710 | * Bad URI... | |
8711 | */ | |
8712 | ||
8713 | send_ipp_status(con, IPP_NOT_FOUND, | |
8714 | _("The printer or class was not found.")); | |
8715 | return; | |
8716 | } | |
8717 | ||
8718 | /* | |
8719 | * Check policy... | |
8720 | */ | |
8721 | ||
8722 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8723 | { | |
f899b121 | 8724 | send_http_error(con, status, printer); |
ef416fc2 | 8725 | return; |
8726 | } | |
8727 | ||
8728 | /* | |
8729 | * Reject jobs sent to the printer... | |
8730 | */ | |
8731 | ||
8732 | printer->accepting = 0; | |
8733 | ||
8734 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8735 | IPP_TAG_TEXT)) == NULL) | |
8736 | strcpy(printer->state_message, "Rejecting Jobs"); | |
8737 | else | |
8738 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8739 | sizeof(printer->state_message)); | |
8740 | ||
8741 | cupsdAddPrinterHistory(printer); | |
8742 | ||
8743 | if (dtype & CUPS_PRINTER_CLASS) | |
8744 | { | |
3dfe78b3 | 8745 | cupsdMarkDirty(CUPSD_DIRTY_CLASSES); |
ef416fc2 | 8746 | |
8747 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8748 | printer->name, get_username(con)); |
ef416fc2 | 8749 | } |
8750 | else | |
8751 | { | |
3dfe78b3 | 8752 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS); |
ef416fc2 | 8753 | |
8754 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
f7deaa1a | 8755 | printer->name, get_username(con)); |
ef416fc2 | 8756 | } |
8757 | ||
8758 | /* | |
8759 | * Everything was ok, so return OK status... | |
8760 | */ | |
8761 | ||
8762 | con->response->request.status.status_code = IPP_OK; | |
8763 | } | |
8764 | ||
8765 | ||
8766 | /* | |
8767 | * 'release_job()' - Release a held print job. | |
8768 | */ | |
8769 | ||
8770 | static void | |
8771 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
8772 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
8773 | { | |
8774 | ipp_attribute_t *attr; /* Current attribute */ | |
8775 | int jobid; /* Job ID */ | |
8776 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
8777 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
8778 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
8779 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
8780 | int port; /* Port portion of URI */ | |
8781 | cupsd_job_t *job; /* Job information */ | |
8782 | ||
8783 | ||
8784 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
8785 | con->http.fd, uri->values[0].string.text); | |
8786 | ||
8787 | /* | |
8788 | * See if we have a job URI or a printer URI... | |
8789 | */ | |
8790 | ||
8791 | if (!strcmp(uri->name, "printer-uri")) | |
8792 | { | |
8793 | /* | |
8794 | * Got a printer URI; see if we also have a job-id attribute... | |
8795 | */ | |
8796 | ||
fa73b229 | 8797 | if ((attr = ippFindAttribute(con->request, "job-id", |
8798 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8799 | { |
8800 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8801 | _("Got a printer-uri attribute but no job-id!")); | |
8802 | return; | |
8803 | } | |
8804 | ||
8805 | jobid = attr->values[0].integer; | |
8806 | } | |
8807 | else | |
8808 | { | |
8809 | /* | |
8810 | * Got a job URI; parse it to get the job ID... | |
8811 | */ | |
8812 | ||
a4d04587 | 8813 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8814 | sizeof(method), username, sizeof(username), host, | |
8815 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8816 | |
8817 | if (strncmp(resource, "/jobs/", 6)) | |
8818 | { | |
8819 | /* | |
8820 | * Not a valid URI! | |
8821 | */ | |
8822 | ||
8823 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8824 | _("Bad job-uri attribute \"%s\"!"), | |
8825 | uri->values[0].string.text); | |
8826 | return; | |
8827 | } | |
8828 | ||
8829 | jobid = atoi(resource + 6); | |
8830 | } | |
8831 | ||
8832 | /* | |
8833 | * See if the job exists... | |
8834 | */ | |
8835 | ||
8836 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8837 | { | |
8838 | /* | |
8839 | * Nope - return a "not found" error... | |
8840 | */ | |
8841 | ||
8842 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
8843 | return; | |
8844 | } | |
8845 | ||
8846 | /* | |
8847 | * See if job is "held"... | |
8848 | */ | |
8849 | ||
bd7854cb | 8850 | if (job->state_value != IPP_JOB_HELD) |
ef416fc2 | 8851 | { |
8852 | /* | |
8853 | * Nope - return a "not possible" error... | |
8854 | */ | |
8855 | ||
8856 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid); | |
8857 | return; | |
8858 | } | |
8859 | ||
8860 | /* | |
8861 | * See if the job is owned by the requesting user... | |
8862 | */ | |
8863 | ||
8864 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8865 | { | |
2fb76298 | 8866 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 8867 | return; |
8868 | } | |
8869 | ||
8870 | /* | |
8871 | * Reset the job-hold-until value to "no-hold"... | |
8872 | */ | |
8873 | ||
fa73b229 | 8874 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
8875 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 8876 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
8877 | ||
fa73b229 | 8878 | if (attr) |
ef416fc2 | 8879 | { |
757d2cad | 8880 | _cupsStrFree(attr->values[0].string.text); |
4400e98d | 8881 | |
ef416fc2 | 8882 | attr->value_tag = IPP_TAG_KEYWORD; |
757d2cad | 8883 | attr->values[0].string.text = _cupsStrAlloc("no-hold"); |
d09495fa | 8884 | |
01ce6322 | 8885 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 8886 | "Job job-hold-until value changed by user."); |
ef416fc2 | 8887 | } |
8888 | ||
8889 | /* | |
8890 | * Release the job and return... | |
8891 | */ | |
8892 | ||
8893 | cupsdReleaseJob(job); | |
8894 | ||
01ce6322 | 8895 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 8896 | "Job released by user."); |
8897 | ||
09a101d6 | 8898 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Released by \"%s\".", jobid, |
ef416fc2 | 8899 | username); |
8900 | ||
8901 | con->response->request.status.status_code = IPP_OK; | |
8902 | } | |
8903 | ||
8904 | ||
8905 | /* | |
8906 | * 'renew_subscription()' - Renew an existing subscription... | |
8907 | */ | |
8908 | ||
8909 | static void | |
8910 | renew_subscription( | |
8911 | cupsd_client_t *con, /* I - Client connection */ | |
8912 | int sub_id) /* I - Subscription ID */ | |
8913 | { | |
bd7854cb | 8914 | http_status_t status; /* Policy status */ |
8915 | cupsd_subscription_t *sub; /* Subscription */ | |
8916 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
8917 | ||
8918 | ||
8919 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
8920 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
8921 | con, con->http.fd, sub_id); | |
8922 | ||
8923 | /* | |
8924 | * Is the subscription ID valid? | |
8925 | */ | |
8926 | ||
8927 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
8928 | { | |
8929 | /* | |
8930 | * Bad subscription ID... | |
8931 | */ | |
8932 | ||
8933 | send_ipp_status(con, IPP_NOT_FOUND, | |
8934 | _("notify-subscription-id %d no good!"), sub_id); | |
8935 | return; | |
8936 | } | |
8937 | ||
8938 | if (sub->job) | |
8939 | { | |
8940 | /* | |
8941 | * Job subscriptions cannot be renewed... | |
8942 | */ | |
8943 | ||
8944 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8945 | _("Job subscriptions cannot be renewed!")); | |
8946 | return; | |
8947 | } | |
8948 | ||
8949 | /* | |
8950 | * Check policy... | |
8951 | */ | |
8952 | ||
8953 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
8954 | DefaultPolicyPtr, | |
8955 | con, sub->owner)) != HTTP_OK) | |
8956 | { | |
f899b121 | 8957 | send_http_error(con, status, sub->dest); |
bd7854cb | 8958 | return; |
8959 | } | |
8960 | ||
8961 | /* | |
8962 | * Renew the subscription... | |
8963 | */ | |
8964 | ||
8965 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
8966 | IPP_TAG_INTEGER); | |
8967 | ||
8968 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
8969 | ||
8970 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
8971 | { | |
8972 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8973 | "renew_subscription: Limiting notify-lease-duration to " | |
8974 | "%d seconds.", | |
8975 | MaxLeaseDuration); | |
8976 | sub->lease = MaxLeaseDuration; | |
8977 | } | |
8978 | ||
8979 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
8980 | ||
3dfe78b3 | 8981 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
bd7854cb | 8982 | |
8983 | con->response->request.status.status_code = IPP_OK; | |
b94498cf | 8984 | |
8985 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
8986 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 8987 | } |
8988 | ||
8989 | ||
8990 | /* | |
8991 | * 'restart_job()' - Restart an old print job. | |
8992 | */ | |
8993 | ||
8994 | static void | |
8995 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
8996 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
8997 | { | |
8998 | ipp_attribute_t *attr; /* Current attribute */ | |
8999 | int jobid; /* Job ID */ | |
9000 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
9001 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
9002 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
9003 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9004 | int port; /* Port portion of URI */ | |
9005 | cupsd_job_t *job; /* Job information */ | |
9006 | ||
9007 | ||
9008 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
9009 | con->http.fd, uri->values[0].string.text); | |
9010 | ||
9011 | /* | |
9012 | * See if we have a job URI or a printer URI... | |
9013 | */ | |
9014 | ||
9015 | if (!strcmp(uri->name, "printer-uri")) | |
9016 | { | |
9017 | /* | |
9018 | * Got a printer URI; see if we also have a job-id attribute... | |
9019 | */ | |
9020 | ||
fa73b229 | 9021 | if ((attr = ippFindAttribute(con->request, "job-id", |
9022 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9023 | { |
9024 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9025 | _("Got a printer-uri attribute but no job-id!")); | |
9026 | return; | |
9027 | } | |
9028 | ||
9029 | jobid = attr->values[0].integer; | |
9030 | } | |
9031 | else | |
9032 | { | |
9033 | /* | |
9034 | * Got a job URI; parse it to get the job ID... | |
9035 | */ | |
9036 | ||
a4d04587 | 9037 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9038 | sizeof(method), username, sizeof(username), host, | |
9039 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9040 | |
fa73b229 | 9041 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 9042 | { |
9043 | /* | |
9044 | * Not a valid URI! | |
9045 | */ | |
9046 | ||
9047 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9048 | _("Bad job-uri attribute \"%s\"!"), | |
9049 | uri->values[0].string.text); | |
9050 | return; | |
9051 | } | |
9052 | ||
9053 | jobid = atoi(resource + 6); | |
9054 | } | |
9055 | ||
9056 | /* | |
9057 | * See if the job exists... | |
9058 | */ | |
9059 | ||
9060 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9061 | { | |
9062 | /* | |
9063 | * Nope - return a "not found" error... | |
9064 | */ | |
9065 | ||
9066 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9067 | return; | |
9068 | } | |
9069 | ||
9070 | /* | |
9071 | * See if job is in any of the "completed" states... | |
9072 | */ | |
9073 | ||
bd7854cb | 9074 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 9075 | { |
9076 | /* | |
9077 | * Nope - return a "not possible" error... | |
9078 | */ | |
9079 | ||
9080 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"), | |
9081 | jobid); | |
9082 | return; | |
9083 | } | |
9084 | ||
9085 | /* | |
9086 | * See if we have retained the job files... | |
9087 | */ | |
9088 | ||
bd7854cb | 9089 | cupsdLoadJob(job); |
9090 | ||
07725fee | 9091 | if (!job->attrs || job->num_files == 0) |
ef416fc2 | 9092 | { |
9093 | /* | |
9094 | * Nope - return a "not possible" error... | |
9095 | */ | |
9096 | ||
9097 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
9098 | _("Job #%d cannot be restarted - no files!"), jobid); | |
9099 | return; | |
9100 | } | |
9101 | ||
9102 | /* | |
9103 | * See if the job is owned by the requesting user... | |
9104 | */ | |
9105 | ||
9106 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9107 | { | |
2fb76298 | 9108 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 9109 | return; |
9110 | } | |
9111 | ||
9112 | /* | |
9113 | * Restart the job and return... | |
9114 | */ | |
9115 | ||
9116 | cupsdRestartJob(job); | |
9117 | ||
09a101d6 | 9118 | cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Restarted by \"%s\".", jobid, |
ef416fc2 | 9119 | username); |
9120 | ||
9121 | con->response->request.status.status_code = IPP_OK; | |
9122 | } | |
9123 | ||
9124 | ||
9125 | /* | |
9126 | * 'save_auth_info()' - Save authentication information for a job. | |
9127 | */ | |
9128 | ||
9129 | static void | |
f7deaa1a | 9130 | save_auth_info( |
9131 | cupsd_client_t *con, /* I - Client connection */ | |
9132 | cupsd_job_t *job, /* I - Job */ | |
9133 | ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */ | |
ef416fc2 | 9134 | { |
09a101d6 | 9135 | int i; /* Looping var */ |
9136 | char filename[1024]; /* Job authentication filename */ | |
9137 | cups_file_t *fp; /* Job authentication file */ | |
9138 | char line[2048]; /* Line for file */ | |
9139 | cupsd_printer_t *dest; /* Destination printer/class */ | |
ef416fc2 | 9140 | |
9141 | ||
9142 | /* | |
9143 | * This function saves the in-memory authentication information for | |
9144 | * a job so that it can be used to authenticate with a remote host. | |
9145 | * The information is stored in a file that is readable only by the | |
f7deaa1a | 9146 | * root user. The fields are Base-64 encoded, each on a separate line, |
9147 | * followed by random number (up to 1024) of newlines to limit the | |
9148 | * amount of information that is exposed. | |
ef416fc2 | 9149 | * |
9150 | * Because of the potential for exposing of authentication information, | |
9151 | * this functionality is only enabled when running cupsd as root. | |
9152 | * | |
9153 | * This caching only works for the Basic and BasicDigest authentication | |
9154 | * types. Digest authentication cannot be cached this way, and in | |
9155 | * the future Kerberos authentication may make all of this obsolete. | |
9156 | * | |
9157 | * Authentication information is saved whenever an authenticated | |
9158 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
9159 | * performed. | |
9160 | * | |
9161 | * This information is deleted after a job is completed or canceled, | |
9162 | * so reprints may require subsequent re-authentication. | |
9163 | */ | |
9164 | ||
9165 | if (RunUser) | |
9166 | return; | |
9167 | ||
09a101d6 | 9168 | if ((dest = cupsdFindDest(job->dest)) == NULL) |
9169 | return; | |
9170 | ||
ef416fc2 | 9171 | /* |
9172 | * Create the authentication file and change permissions... | |
9173 | */ | |
9174 | ||
9175 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
9176 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
9177 | { | |
9178 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9179 | "Unable to save authentication info to \"%s\" - %s", | |
9180 | filename, strerror(errno)); | |
9181 | return; | |
9182 | } | |
9183 | ||
9184 | fchown(cupsFileNumber(fp), 0, 0); | |
9185 | fchmod(cupsFileNumber(fp), 0400); | |
9186 | ||
09a101d6 | 9187 | if (auth_info && auth_info->num_values == dest->num_auth_info_required) |
f7deaa1a | 9188 | { |
9189 | /* | |
09a101d6 | 9190 | * Write 1 to 3 auth values... |
f7deaa1a | 9191 | */ |
ef416fc2 | 9192 | |
09a101d6 | 9193 | cupsdClearString(&job->auth_username); |
9194 | cupsdClearString(&job->auth_domain); | |
9195 | cupsdClearString(&job->auth_password); | |
9196 | ||
f7deaa1a | 9197 | for (i = 0; i < auth_info->num_values; i ++) |
9198 | { | |
9199 | httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, | |
9200 | strlen(auth_info->values[i].string.text)); | |
9201 | cupsFilePrintf(fp, "%s\n", line); | |
09a101d6 | 9202 | |
9203 | if (!strcmp(dest->auth_info_required[i], "username")) | |
839a51c8 | 9204 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", |
09a101d6 | 9205 | auth_info->values[i].string.text); |
9206 | else if (!strcmp(dest->auth_info_required[i], "domain")) | |
839a51c8 | 9207 | cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s", |
09a101d6 | 9208 | auth_info->values[i].string.text); |
9209 | else if (!strcmp(dest->auth_info_required[i], "password")) | |
839a51c8 | 9210 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", |
09a101d6 | 9211 | auth_info->values[i].string.text); |
f7deaa1a | 9212 | } |
9213 | } | |
09a101d6 | 9214 | else if (con->username[0]) |
f7deaa1a | 9215 | { |
9216 | /* | |
9217 | * Write the authenticated username... | |
9218 | */ | |
ef416fc2 | 9219 | |
f7deaa1a | 9220 | httpEncode64_2(line, sizeof(line), con->username, strlen(con->username)); |
9221 | cupsFilePrintf(fp, "%s\n", line); | |
ef416fc2 | 9222 | |
839a51c8 | 9223 | cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username); |
09a101d6 | 9224 | cupsdClearString(&job->auth_domain); |
9225 | ||
f7deaa1a | 9226 | /* |
9227 | * Write the authenticated password... | |
9228 | */ | |
9229 | ||
9230 | httpEncode64_2(line, sizeof(line), con->password, strlen(con->password)); | |
9231 | cupsFilePrintf(fp, "%s\n", line); | |
09a101d6 | 9232 | |
839a51c8 | 9233 | cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password); |
f7deaa1a | 9234 | } |
ef416fc2 | 9235 | |
9236 | /* | |
9237 | * Write a random number of newlines to the end of the file... | |
9238 | */ | |
9239 | ||
9240 | for (i = (rand() % 1024); i >= 0; i --) | |
9241 | cupsFilePutChar(fp, '\n'); | |
9242 | ||
9243 | /* | |
9244 | * Close the file and return... | |
9245 | */ | |
9246 | ||
9247 | cupsFileClose(fp); | |
f7deaa1a | 9248 | |
9249 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
09a101d6 | 9250 | if (con->gss_have_creds) |
9251 | save_krb5_creds(con, job); | |
9252 | else if (job->ccname) | |
9253 | cupsdClearString(&(job->ccname)); | |
f7deaa1a | 9254 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ |
9255 | } | |
9256 | ||
9257 | ||
9258 | #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H) | |
9259 | /* | |
9260 | * 'save_krb5_creds()' - Save Kerberos credentials for the job. | |
9261 | */ | |
9262 | ||
9263 | static void | |
9264 | save_krb5_creds(cupsd_client_t *con, /* I - Client connection */ | |
9265 | cupsd_job_t *job) /* I - Job */ | |
9266 | { | |
7ff4fea9 MS |
9267 | # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL) |
9268 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9269 | "Sorry, your version of Kerberos does not support delegated " | |
9270 | "credentials!"); | |
9271 | return; | |
9272 | ||
9273 | # else | |
db1f069b MS |
9274 | krb5_error_code error; /* Kerberos error code */ |
9275 | OM_uint32 major_status, /* Major status code */ | |
9276 | minor_status; /* Minor status code */ | |
db1f069b | 9277 | krb5_principal principal; /* Kerberos principal */ |
f7deaa1a | 9278 | |
9279 | ||
7ff4fea9 | 9280 | # ifdef __APPLE__ |
f7deaa1a | 9281 | /* |
c24d2134 MS |
9282 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try |
9283 | * to use it... | |
f7deaa1a | 9284 | */ |
9285 | ||
c24d2134 | 9286 | if (krb5_init_context == NULL) |
f7deaa1a | 9287 | return; |
7ff4fea9 | 9288 | # endif /* __APPLE__ */ |
f7deaa1a | 9289 | |
cc0d019f MS |
9290 | /* |
9291 | * We MUST create a file-based cache because memory-based caches are | |
9292 | * only valid for the current process/address space. | |
7ff4fea9 MS |
9293 | * |
9294 | * Due to various bugs/features in different versions of Kerberos, we | |
9295 | * need either the krb5_cc_new_unique() function or Heimdal's version | |
9296 | * of krb5_cc_gen_new() to create a new FILE: credential cache that | |
9297 | * can be passed to the backend. These functions create a temporary | |
9298 | * file (typically in /tmp) containing the cached credentials, which | |
9299 | * are removed when we have successfully printed a job. | |
9300 | */ | |
9301 | ||
9302 | # ifdef HAVE_KRB5_CC_NEW_UNIQUE | |
9303 | if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL, | |
9304 | &(job->ccache))) != 0) | |
9305 | # else /* HAVE_HEIMDAL */ | |
0a682745 | 9306 | if ((error = krb5_cc_gen_new(KerberosContext, &krb5_fcc_ops, |
7ff4fea9 MS |
9307 | &(job->ccache))) != 0) |
9308 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE */ | |
9309 | { | |
db1f069b MS |
9310 | cupsdLogMessage(CUPSD_LOG_ERROR, |
9311 | "Unable to create new credentials cache (%d/%s)", | |
7ff4fea9 | 9312 | error, strerror(errno)); |
76cd9e37 | 9313 | job->ccache = NULL; |
f7deaa1a | 9314 | return; |
9315 | } | |
9316 | ||
db1f069b MS |
9317 | if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0) |
9318 | { | |
9319 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)", | |
9320 | error, strerror(errno)); | |
9321 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9322 | job->ccache = NULL; | |
9323 | return; | |
9324 | } | |
9325 | ||
9326 | if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal))) | |
9327 | { | |
9328 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
9329 | "Unable to initialize credentials cache (%d/%s)", error, | |
9330 | strerror(errno)); | |
9331 | krb5_cc_destroy(KerberosContext, job->ccache); | |
9332 | krb5_free_principal(KerberosContext, principal); | |
9333 | job->ccache = NULL; | |
9334 | return; | |
9335 | } | |
9336 | ||
9337 | krb5_free_principal(KerberosContext, principal); | |
9338 | ||
7ff4fea9 MS |
9339 | /* |
9340 | * Copy the user's credentials to the new cache file... | |
9341 | */ | |
9342 | ||
f7deaa1a | 9343 | major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred, |
c24d2134 | 9344 | job->ccache); |
f7deaa1a | 9345 | |
9346 | if (GSS_ERROR(major_status)) | |
9347 | { | |
9348 | cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status, | |
9349 | "Unable to import client credentials cache"); | |
c24d2134 | 9350 | krb5_cc_destroy(KerberosContext, job->ccache); |
76cd9e37 | 9351 | job->ccache = NULL; |
f7deaa1a | 9352 | return; |
9353 | } | |
9354 | ||
7ff4fea9 MS |
9355 | /* |
9356 | * Add the KRB5CCNAME environment variable to the job so that the | |
9357 | * backend can use the credentials when printing. | |
9358 | */ | |
9359 | ||
9360 | cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s", | |
c24d2134 | 9361 | krb5_cc_get_name(KerberosContext, job->ccache)); |
cc0d019f MS |
9362 | |
9363 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] save_krb5_creds: %s", job->id, | |
9364 | job->ccname); | |
7ff4fea9 | 9365 | # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */ |
ef416fc2 | 9366 | } |
f7deaa1a | 9367 | #endif /* HAVE_GSSAPI && HAVE_KRB5_H */ |
ef416fc2 | 9368 | |
9369 | ||
9370 | /* | |
9371 | * 'send_document()' - Send a file to a printer or class. | |
9372 | */ | |
9373 | ||
9374 | static void | |
9375 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
9376 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9377 | { | |
9378 | ipp_attribute_t *attr; /* Current attribute */ | |
a0f6818e MS |
9379 | ipp_attribute_t *format; /* Request's document-format attribute */ |
9380 | ipp_attribute_t *jformat; /* Job's document-format attribute */ | |
f7deaa1a | 9381 | const char *default_format;/* document-format-default value */ |
ef416fc2 | 9382 | int jobid; /* Job ID number */ |
9383 | cupsd_job_t *job; /* Current job */ | |
9384 | char job_uri[HTTP_MAX_URI], | |
9385 | /* Job URI */ | |
9386 | method[HTTP_MAX_URI], | |
9387 | /* Method portion of URI */ | |
9388 | username[HTTP_MAX_URI], | |
9389 | /* Username portion of URI */ | |
9390 | host[HTTP_MAX_URI], | |
9391 | /* Host portion of URI */ | |
9392 | resource[HTTP_MAX_URI]; | |
9393 | /* Resource portion of URI */ | |
9394 | int port; /* Port portion of URI */ | |
9395 | mime_type_t *filetype; /* Type of file */ | |
9396 | char super[MIME_MAX_SUPER], | |
9397 | /* Supertype of file */ | |
9398 | type[MIME_MAX_TYPE], | |
9399 | /* Subtype of file */ | |
9400 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
9401 | /* Textual name of mime type */ | |
9402 | char filename[1024]; /* Job filename */ | |
9403 | cupsd_printer_t *printer; /* Current printer */ | |
9404 | struct stat fileinfo; /* File information */ | |
9405 | int kbytes; /* Size of file */ | |
9406 | int compression; /* Type of compression */ | |
9407 | ||
9408 | ||
9409 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
9410 | con->http.fd, uri->values[0].string.text); | |
9411 | ||
9412 | /* | |
9413 | * See if we have a job URI or a printer URI... | |
9414 | */ | |
9415 | ||
9416 | if (!strcmp(uri->name, "printer-uri")) | |
9417 | { | |
9418 | /* | |
9419 | * Got a printer URI; see if we also have a job-id attribute... | |
9420 | */ | |
9421 | ||
fa73b229 | 9422 | if ((attr = ippFindAttribute(con->request, "job-id", |
9423 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9424 | { |
9425 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9426 | _("Got a printer-uri attribute but no job-id!")); | |
9427 | return; | |
9428 | } | |
9429 | ||
9430 | jobid = attr->values[0].integer; | |
9431 | } | |
9432 | else | |
9433 | { | |
9434 | /* | |
9435 | * Got a job URI; parse it to get the job ID... | |
9436 | */ | |
9437 | ||
a4d04587 | 9438 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9439 | sizeof(method), username, sizeof(username), host, | |
9440 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9441 | |
9442 | if (strncmp(resource, "/jobs/", 6)) | |
9443 | { | |
9444 | /* | |
9445 | * Not a valid URI! | |
9446 | */ | |
9447 | ||
9448 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9449 | _("Bad job-uri attribute \"%s\"!"), | |
9450 | uri->values[0].string.text); | |
9451 | return; | |
9452 | } | |
9453 | ||
9454 | jobid = atoi(resource + 6); | |
9455 | } | |
9456 | ||
9457 | /* | |
9458 | * See if the job exists... | |
9459 | */ | |
9460 | ||
9461 | if ((job = cupsdFindJob(jobid)) == NULL) | |
9462 | { | |
9463 | /* | |
9464 | * Nope - return a "not found" error... | |
9465 | */ | |
9466 | ||
9467 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
9468 | return; | |
9469 | } | |
9470 | ||
f301802f | 9471 | printer = cupsdFindDest(job->dest); |
9472 | ||
ef416fc2 | 9473 | /* |
9474 | * See if the job is owned by the requesting user... | |
9475 | */ | |
9476 | ||
9477 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
9478 | { | |
2fb76298 | 9479 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 9480 | return; |
9481 | } | |
9482 | ||
9483 | /* | |
9484 | * OK, see if the client is sending the document compressed - CUPS | |
9485 | * only supports "none" and "gzip". | |
9486 | */ | |
9487 | ||
9488 | compression = CUPS_FILE_NONE; | |
9489 | ||
fa73b229 | 9490 | if ((attr = ippFindAttribute(con->request, "compression", |
9491 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 9492 | { |
9493 | if (strcmp(attr->values[0].string.text, "none") | |
9494 | #ifdef HAVE_LIBZ | |
9495 | && strcmp(attr->values[0].string.text, "gzip") | |
9496 | #endif /* HAVE_LIBZ */ | |
9497 | ) | |
9498 | { | |
9499 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"), | |
9500 | attr->values[0].string.text); | |
9501 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9502 | "compression", NULL, attr->values[0].string.text); | |
9503 | return; | |
9504 | } | |
9505 | ||
9506 | #ifdef HAVE_LIBZ | |
9507 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
9508 | compression = CUPS_FILE_GZIP; | |
9509 | #endif /* HAVE_LIBZ */ | |
9510 | } | |
9511 | ||
9512 | /* | |
9513 | * Do we have a file to print? | |
9514 | */ | |
9515 | ||
9516 | if (!con->filename) | |
9517 | { | |
9518 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
9519 | return; | |
9520 | } | |
9521 | ||
9522 | /* | |
9523 | * Is it a format we support? | |
9524 | */ | |
9525 | ||
9526 | if ((format = ippFindAttribute(con->request, "document-format", | |
9527 | IPP_TAG_MIMETYPE)) != NULL) | |
9528 | { | |
9529 | /* | |
9530 | * Grab format from client... | |
9531 | */ | |
9532 | ||
bc44d920 | 9533 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", |
9534 | super, type) != 2) | |
ef416fc2 | 9535 | { |
9536 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
9537 | format->values[0].string.text); | |
9538 | return; | |
9539 | } | |
9540 | } | |
f7deaa1a | 9541 | else if ((default_format = cupsGetOption("document-format", |
9542 | printer->num_options, | |
9543 | printer->options)) != NULL) | |
9544 | { | |
9545 | /* | |
9546 | * Use default document format... | |
9547 | */ | |
9548 | ||
9549 | if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2) | |
9550 | { | |
9551 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9552 | _("Could not scan type \"%s\"!"), | |
9553 | default_format); | |
9554 | return; | |
9555 | } | |
9556 | } | |
ef416fc2 | 9557 | else |
9558 | { | |
9559 | /* | |
9560 | * No document format attribute? Auto-type it! | |
9561 | */ | |
9562 | ||
9563 | strcpy(super, "application"); | |
9564 | strcpy(type, "octet-stream"); | |
9565 | } | |
9566 | ||
fa73b229 | 9567 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
ef416fc2 | 9568 | { |
9569 | /* | |
9570 | * Auto-type the file... | |
9571 | */ | |
9572 | ||
bd7854cb | 9573 | ipp_attribute_t *doc_name; /* document-name attribute */ |
9574 | ||
9575 | ||
a0f6818e | 9576 | cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] Auto-typing file...", job->id); |
ef416fc2 | 9577 | |
bd7854cb | 9578 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
9579 | filetype = mimeFileType(MimeDatabase, con->filename, | |
9580 | doc_name ? doc_name->values[0].string.text : NULL, | |
9581 | &compression); | |
ef416fc2 | 9582 | |
f7deaa1a | 9583 | if (!filetype) |
f7faf1f5 | 9584 | filetype = mimeType(MimeDatabase, super, type); |
a0f6818e MS |
9585 | |
9586 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
9587 | "[Job %d] Request file type is %s/%s.", job->id, | |
9588 | filetype->super, filetype->type); | |
ed486911 | 9589 | } |
f7faf1f5 | 9590 | else |
9591 | filetype = mimeType(MimeDatabase, super, type); | |
9592 | ||
a0f6818e MS |
9593 | jformat = ippFindAttribute(job->attrs, "document-format", IPP_TAG_MIMETYPE); |
9594 | ||
f7deaa1a | 9595 | if (filetype && |
a0f6818e | 9596 | (!jformat || |
f7deaa1a | 9597 | (!strcmp(super, "application") && !strcmp(type, "octet-stream")))) |
9598 | { | |
9599 | /* | |
9600 | * Replace the document-format attribute value with the auto-typed or | |
9601 | * default one. | |
9602 | */ | |
9603 | ||
9604 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9605 | filetype->type); | |
9606 | ||
a0f6818e | 9607 | if (jformat) |
f7deaa1a | 9608 | { |
a0f6818e | 9609 | _cupsStrFree(jformat->values[0].string.text); |
f7deaa1a | 9610 | |
a0f6818e | 9611 | jformat->values[0].string.text = _cupsStrAlloc(mimetype); |
f7deaa1a | 9612 | } |
9613 | else | |
a0f6818e | 9614 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, |
f7deaa1a | 9615 | "document-format", NULL, mimetype); |
9616 | } | |
9617 | else if (!filetype) | |
ef416fc2 | 9618 | { |
9619 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9620 | _("Unsupported format \'%s/%s\'!"), super, type); | |
9621 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9622 | "Hint: Do you have the raw file printing rules enabled?"); | |
9623 | ||
9624 | if (format) | |
9625 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9626 | "document-format", NULL, format->values[0].string.text); | |
9627 | ||
9628 | return; | |
9629 | } | |
9630 | ||
80ca4592 | 9631 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) |
9632 | { | |
9633 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
9634 | filetype->type); | |
9635 | ||
9636 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9637 | _("Unsupported format \'%s\'!"), mimetype); | |
9638 | ||
9639 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9640 | "document-format", NULL, mimetype); | |
9641 | ||
9642 | return; | |
9643 | } | |
9644 | ||
ef416fc2 | 9645 | /* |
9646 | * Add the file to the job... | |
9647 | */ | |
9648 | ||
bd7854cb | 9649 | cupsdLoadJob(job); |
9650 | ||
ef416fc2 | 9651 | if (add_file(con, job, filetype, compression)) |
9652 | return; | |
9653 | ||
ef416fc2 | 9654 | if (stat(con->filename, &fileinfo)) |
9655 | kbytes = 0; | |
9656 | else | |
9657 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
9658 | ||
9659 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
9660 | ||
fa73b229 | 9661 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
9662 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 9663 | attr->values[0].integer += kbytes; |
9664 | ||
9665 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
9666 | job->num_files); | |
9667 | rename(con->filename, filename); | |
9668 | ||
9669 | cupsdClearString(&con->filename); | |
9670 | ||
9671 | cupsdLogMessage(CUPSD_LOG_INFO, | |
a0f6818e MS |
9672 | "[Job %d] File of type %s/%s queued by \"%s\".", job->id, |
9673 | filetype->super, filetype->type, job->username); | |
ef416fc2 | 9674 | |
9675 | /* | |
9676 | * Start the job if this is the last document... | |
9677 | */ | |
9678 | ||
fa73b229 | 9679 | if ((attr = ippFindAttribute(con->request, "last-document", |
9680 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 9681 | attr->values[0].boolean) |
9682 | { | |
9683 | /* | |
9684 | * See if we need to add the ending sheet... | |
9685 | */ | |
9686 | ||
91c84a35 MS |
9687 | if (cupsdTimeoutJob(job)) |
9688 | return; | |
ef416fc2 | 9689 | |
bd7854cb | 9690 | if (job->state_value == IPP_JOB_STOPPED) |
9691 | { | |
ef416fc2 | 9692 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9693 | job->state_value = IPP_JOB_PENDING; |
9694 | } | |
9695 | else if (job->state_value == IPP_JOB_HELD) | |
ef416fc2 | 9696 | { |
fa73b229 | 9697 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9698 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9699 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9700 | ||
fa73b229 | 9701 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
bd7854cb | 9702 | { |
ef416fc2 | 9703 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 9704 | job->state_value = IPP_JOB_PENDING; |
9705 | } | |
ef416fc2 | 9706 | } |
9707 | ||
3dfe78b3 MS |
9708 | job->dirty = 1; |
9709 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 9710 | |
9711 | /* | |
9712 | * Start the job if possible... Since cupsdCheckJobs() can cancel a | |
9713 | * job if it doesn't print, we need to re-find the job afterwards... | |
9714 | */ | |
9715 | ||
9716 | jobid = job->id; | |
9717 | ||
9718 | cupsdCheckJobs(); | |
9719 | ||
9720 | job = cupsdFindJob(jobid); | |
9721 | } | |
9722 | else | |
9723 | { | |
fa73b229 | 9724 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
9725 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 9726 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
9727 | ||
fa73b229 | 9728 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
ef416fc2 | 9729 | { |
9730 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 9731 | job->state_value = IPP_JOB_HELD; |
ef416fc2 | 9732 | job->hold_until = time(NULL) + 60; |
3dfe78b3 MS |
9733 | job->dirty = 1; |
9734 | ||
9735 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 9736 | } |
9737 | } | |
9738 | ||
9739 | /* | |
9740 | * Fill in the response info... | |
9741 | */ | |
9742 | ||
9743 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
9744 | LocalPort, jobid); | |
9745 | ||
9746 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
9747 | job_uri); | |
9748 | ||
9749 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
9750 | ||
9751 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
d09495fa | 9752 | job ? job->state_value : IPP_JOB_CANCELED); |
ef416fc2 | 9753 | add_job_state_reasons(con, job); |
9754 | ||
9755 | con->response->request.status.status_code = IPP_OK; | |
9756 | } | |
9757 | ||
9758 | ||
9759 | /* | |
9760 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
9761 | */ | |
9762 | ||
9763 | static void | |
f899b121 | 9764 | send_http_error( |
9765 | cupsd_client_t *con, /* I - Client connection */ | |
9766 | http_status_t status, /* I - HTTP status code */ | |
9767 | cupsd_printer_t *printer) /* I - Printer, if any */ | |
ef416fc2 | 9768 | { |
9769 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s", | |
9770 | ippOpString(con->request->request.op.operation_id), | |
9771 | httpStatus(status)); | |
9772 | ||
f899b121 | 9773 | if (status == HTTP_UNAUTHORIZED && |
9774 | printer && printer->num_auth_info_required > 0 && | |
9775 | !strcmp(printer->auth_info_required[0], "negotiate")) | |
5bd77a73 | 9776 | cupsdSendError(con, status, CUPSD_AUTH_NEGOTIATE); |
2fb76298 MS |
9777 | else if (printer) |
9778 | { | |
9779 | char resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
9780 | cupsd_location_t *auth; /* Pointer to authentication element */ | |
db0bd74a | 9781 | int auth_type; /* Type of authentication required */ |
2fb76298 MS |
9782 | |
9783 | ||
9784 | if (printer->type & CUPS_PRINTER_CLASS) | |
9785 | snprintf(resource, sizeof(resource), "/classes/%s", printer->name); | |
9786 | else | |
9787 | snprintf(resource, sizeof(resource), "/printers/%s", printer->name); | |
9788 | ||
9789 | if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL || | |
5bd77a73 | 9790 | auth->type == CUPSD_AUTH_NONE) |
db0bd74a MS |
9791 | auth = cupsdFindPolicyOp(printer->op_policy_ptr, |
9792 | con->request ? | |
9793 | con->request->request.op.operation_id : | |
9794 | IPP_PRINT_JOB); | |
9795 | ||
9796 | if (!auth) | |
9797 | auth_type = CUPSD_AUTH_NONE; | |
9798 | else if (auth->type == CUPSD_AUTH_DEFAULT) | |
9799 | auth_type = DefaultAuthType; | |
9800 | else | |
9801 | auth_type = auth->type; | |
2fb76298 | 9802 | |
db0bd74a | 9803 | cupsdSendError(con, status, auth_type); |
2fb76298 | 9804 | } |
f899b121 | 9805 | else |
5bd77a73 | 9806 | cupsdSendError(con, status, CUPSD_AUTH_NONE); |
ef416fc2 | 9807 | |
9808 | ippDelete(con->response); | |
9809 | con->response = NULL; | |
9810 | ||
9811 | return; | |
9812 | } | |
9813 | ||
9814 | ||
9815 | /* | |
9816 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
9817 | */ | |
9818 | ||
9819 | static void | |
9820 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
9821 | ipp_status_t status, /* I - IPP status code */ | |
9822 | const char *message, /* I - Status message */ | |
9823 | ...) /* I - Additional args as needed */ | |
9824 | { | |
9825 | va_list ap; /* Pointer to additional args */ | |
9826 | char formatted[1024]; /* Formatted errror message */ | |
9827 | ||
9828 | ||
3d8365b8 | 9829 | va_start(ap, message); |
9830 | vsnprintf(formatted, sizeof(formatted), | |
9831 | _cupsLangString(con->language, message), ap); | |
9832 | va_end(ap); | |
ef416fc2 | 9833 | |
3d8365b8 | 9834 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s", |
9835 | ippOpString(con->request->request.op.operation_id), | |
9836 | ippErrorString(status), formatted); | |
ef416fc2 | 9837 | |
9838 | con->response->request.status.status_code = status; | |
9839 | ||
fa73b229 | 9840 | if (ippFindAttribute(con->response, "attributes-charset", |
9841 | IPP_TAG_ZERO) == NULL) | |
ef416fc2 | 9842 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
9843 | "attributes-charset", NULL, DefaultCharset); | |
9844 | ||
9845 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
9846 | IPP_TAG_ZERO) == NULL) | |
9847 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
9848 | "attributes-natural-language", NULL, DefaultLanguage); | |
9849 | ||
3d8365b8 | 9850 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
9851 | "status-message", NULL, formatted); | |
ef416fc2 | 9852 | } |
9853 | ||
9854 | ||
9855 | /* | |
9856 | * 'set_default()' - Set the default destination... | |
9857 | */ | |
9858 | ||
9859 | static void | |
9860 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
9861 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9862 | { | |
9863 | http_status_t status; /* Policy status */ | |
bc44d920 | 9864 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 9865 | cupsd_printer_t *printer; /* Printer */ |
9866 | ||
9867 | ||
9868 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
9869 | con->http.fd, uri->values[0].string.text); | |
9870 | ||
9871 | /* | |
9872 | * Is the destination valid? | |
9873 | */ | |
9874 | ||
f7deaa1a | 9875 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 9876 | { |
9877 | /* | |
9878 | * Bad URI... | |
9879 | */ | |
9880 | ||
9881 | send_ipp_status(con, IPP_NOT_FOUND, | |
9882 | _("The printer or class was not found.")); | |
9883 | return; | |
9884 | } | |
9885 | ||
9886 | /* | |
9887 | * Check policy... | |
9888 | */ | |
9889 | ||
9890 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
9891 | { | |
f899b121 | 9892 | send_http_error(con, status, NULL); |
ef416fc2 | 9893 | return; |
9894 | } | |
9895 | ||
9896 | /* | |
9897 | * Set it as the default... | |
9898 | */ | |
9899 | ||
9900 | DefaultPrinter = printer; | |
9901 | ||
3dfe78b3 MS |
9902 | cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES | |
9903 | CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP); | |
ef416fc2 | 9904 | |
9905 | cupsdLogMessage(CUPSD_LOG_INFO, | |
f7deaa1a | 9906 | "Default destination set to \"%s\" by \"%s\".", |
9907 | printer->name, get_username(con)); | |
ef416fc2 | 9908 | |
9909 | /* | |
9910 | * Everything was ok, so return OK status... | |
9911 | */ | |
9912 | ||
9913 | con->response->request.status.status_code = IPP_OK; | |
9914 | } | |
9915 | ||
9916 | ||
9917 | /* | |
9918 | * 'set_job_attrs()' - Set job attributes. | |
9919 | */ | |
9920 | ||
9921 | static void | |
9922 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
9923 | ipp_attribute_t *uri) /* I - Job URI */ | |
9924 | { | |
9925 | ipp_attribute_t *attr, /* Current attribute */ | |
9926 | *attr2; /* Job attribute */ | |
9927 | int jobid; /* Job ID */ | |
9928 | cupsd_job_t *job; /* Current job */ | |
9929 | char method[HTTP_MAX_URI], | |
9930 | /* Method portion of URI */ | |
9931 | username[HTTP_MAX_URI], | |
9932 | /* Username portion of URI */ | |
9933 | host[HTTP_MAX_URI], | |
9934 | /* Host portion of URI */ | |
9935 | resource[HTTP_MAX_URI]; | |
9936 | /* Resource portion of URI */ | |
9937 | int port; /* Port portion of URI */ | |
d09495fa | 9938 | int event; /* Events? */ |
ef416fc2 | 9939 | |
9940 | ||
9941 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
9942 | con->http.fd, uri->values[0].string.text); | |
9943 | ||
9944 | /* | |
9945 | * Start with "everything is OK" status... | |
9946 | */ | |
9947 | ||
9948 | con->response->request.status.status_code = IPP_OK; | |
9949 | ||
9950 | /* | |
9951 | * See if we have a job URI or a printer URI... | |
9952 | */ | |
9953 | ||
fa73b229 | 9954 | if (!strcmp(uri->name, "printer-uri")) |
ef416fc2 | 9955 | { |
9956 | /* | |
9957 | * Got a printer URI; see if we also have a job-id attribute... | |
9958 | */ | |
9959 | ||
fa73b229 | 9960 | if ((attr = ippFindAttribute(con->request, "job-id", |
9961 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 9962 | { |
9963 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9964 | _("Got a printer-uri attribute but no job-id!")); | |
9965 | return; | |
9966 | } | |
9967 | ||
9968 | jobid = attr->values[0].integer; | |
9969 | } | |
9970 | else | |
9971 | { | |
9972 | /* | |
9973 | * Got a job URI; parse it to get the job ID... | |
9974 | */ | |
9975 | ||
a4d04587 | 9976 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9977 | sizeof(method), username, sizeof(username), host, | |
9978 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9979 | |
fa73b229 | 9980 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 9981 | { |
9982 | /* | |
9983 | * Not a valid URI! | |
9984 | */ | |
9985 | ||
9986 | send_ipp_status(con, IPP_BAD_REQUEST, | |
9987 | _("Bad job-uri attribute \"%s\"!"), | |
9988 | uri->values[0].string.text); | |
9989 | return; | |
9990 | } | |
9991 | ||
9992 | jobid = atoi(resource + 6); | |
9993 | } | |
9994 | ||
9995 | /* | |
9996 | * See if the job exists... | |
9997 | */ | |
9998 | ||
9999 | if ((job = cupsdFindJob(jobid)) == NULL) | |
10000 | { | |
10001 | /* | |
10002 | * Nope - return a "not found" error... | |
10003 | */ | |
10004 | ||
10005 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
10006 | return; | |
10007 | } | |
10008 | ||
10009 | /* | |
10010 | * See if the job has been completed... | |
10011 | */ | |
10012 | ||
bd7854cb | 10013 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 10014 | { |
10015 | /* | |
10016 | * Return a "not-possible" error... | |
10017 | */ | |
10018 | ||
10019 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10020 | _("Job #%d is finished and cannot be altered!"), jobid); | |
10021 | return; | |
10022 | } | |
10023 | ||
10024 | /* | |
10025 | * See if the job is owned by the requesting user... | |
10026 | */ | |
10027 | ||
10028 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
10029 | { | |
2fb76298 | 10030 | send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest)); |
ef416fc2 | 10031 | return; |
10032 | } | |
10033 | ||
10034 | /* | |
10035 | * See what the user wants to change. | |
10036 | */ | |
10037 | ||
bd7854cb | 10038 | cupsdLoadJob(job); |
10039 | ||
d09495fa | 10040 | event = 0; |
10041 | ||
fa73b229 | 10042 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 10043 | { |
10044 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
10045 | continue; | |
10046 | ||
10047 | if (!strcmp(attr->name, "attributes-charset") || | |
10048 | !strcmp(attr->name, "attributes-natural-language") || | |
10049 | !strcmp(attr->name, "document-compression") || | |
10050 | !strcmp(attr->name, "document-format") || | |
10051 | !strcmp(attr->name, "job-detailed-status-messages") || | |
10052 | !strcmp(attr->name, "job-document-access-errors") || | |
10053 | !strcmp(attr->name, "job-id") || | |
10054 | !strcmp(attr->name, "job-k-octets") || | |
10055 | !strcmp(attr->name, "job-originating-host-name") || | |
10056 | !strcmp(attr->name, "job-originating-user-name") || | |
10057 | !strcmp(attr->name, "job-printer-up-time") || | |
10058 | !strcmp(attr->name, "job-printer-uri") || | |
10059 | !strcmp(attr->name, "job-sheets") || | |
10060 | !strcmp(attr->name, "job-state-message") || | |
10061 | !strcmp(attr->name, "job-state-reasons") || | |
10062 | !strcmp(attr->name, "job-uri") || | |
10063 | !strcmp(attr->name, "number-of-documents") || | |
10064 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
10065 | !strcmp(attr->name, "output-device-assigned") || | |
10066 | !strncmp(attr->name, "date-time-at-", 13) || | |
10067 | !strncmp(attr->name, "job-impressions", 15) || | |
10068 | !strncmp(attr->name, "job-k-octets", 12) || | |
10069 | !strncmp(attr->name, "job-media-sheets", 16) || | |
10070 | !strncmp(attr->name, "time-at-", 8)) | |
10071 | { | |
10072 | /* | |
10073 | * Read-only attrs! | |
10074 | */ | |
10075 | ||
10076 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
10077 | _("%s cannot be changed."), attr->name); | |
10078 | ||
10079 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10080 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10081 | ||
10082 | continue; | |
10083 | } | |
10084 | ||
10085 | if (!strcmp(attr->name, "job-priority")) | |
10086 | { | |
10087 | /* | |
10088 | * Change the job priority... | |
10089 | */ | |
10090 | ||
10091 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10092 | { | |
10093 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!")); | |
10094 | ||
10095 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10096 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10097 | } | |
bd7854cb | 10098 | else if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 10099 | { |
10100 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10101 | _("Job is completed and cannot be changed.")); | |
10102 | return; | |
10103 | } | |
10104 | else if (con->response->request.status.status_code == IPP_OK) | |
d09495fa | 10105 | { |
ef416fc2 | 10106 | cupsdSetJobPriority(job, attr->values[0].integer); |
d9bca400 MS |
10107 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | |
10108 | CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED; | |
d09495fa | 10109 | } |
ef416fc2 | 10110 | } |
10111 | else if (!strcmp(attr->name, "job-state")) | |
10112 | { | |
10113 | /* | |
10114 | * Change the job state... | |
10115 | */ | |
10116 | ||
10117 | if (attr->value_tag != IPP_TAG_ENUM) | |
10118 | { | |
10119 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!")); | |
10120 | ||
10121 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
10122 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
10123 | } | |
10124 | else | |
10125 | { | |
10126 | switch (attr->values[0].integer) | |
10127 | { | |
10128 | case IPP_JOB_PENDING : | |
10129 | case IPP_JOB_HELD : | |
bd7854cb | 10130 | if (job->state_value > IPP_JOB_HELD) |
ef416fc2 | 10131 | { |
10132 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10133 | _("Job state cannot be changed.")); | |
10134 | return; | |
10135 | } | |
10136 | else if (con->response->request.status.status_code == IPP_OK) | |
bd7854cb | 10137 | { |
ef416fc2 | 10138 | job->state->values[0].integer = attr->values[0].integer; |
bc44d920 | 10139 | job->state_value = (ipp_jstate_t)attr->values[0].integer; |
d09495fa | 10140 | |
10141 | event |= CUPSD_EVENT_JOB_STATE; | |
bd7854cb | 10142 | } |
ef416fc2 | 10143 | break; |
10144 | ||
10145 | case IPP_JOB_PROCESSING : | |
10146 | case IPP_JOB_STOPPED : | |
bd7854cb | 10147 | if (job->state_value != attr->values[0].integer) |
ef416fc2 | 10148 | { |
10149 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10150 | _("Job state cannot be changed.")); | |
10151 | return; | |
10152 | } | |
10153 | break; | |
10154 | ||
d09495fa | 10155 | case IPP_JOB_CANCELED : |
ef416fc2 | 10156 | case IPP_JOB_ABORTED : |
10157 | case IPP_JOB_COMPLETED : | |
bd7854cb | 10158 | if (job->state_value > IPP_JOB_PROCESSING) |
ef416fc2 | 10159 | { |
10160 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10161 | _("Job state cannot be changed.")); | |
10162 | return; | |
10163 | } | |
10164 | else if (con->response->request.status.status_code == IPP_OK) | |
07725fee | 10165 | cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer); |
ef416fc2 | 10166 | break; |
10167 | } | |
10168 | } | |
10169 | } | |
10170 | else if (con->response->request.status.status_code != IPP_OK) | |
10171 | continue; | |
fa73b229 | 10172 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, |
10173 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 10174 | { |
10175 | /* | |
10176 | * Some other value; first free the old value... | |
10177 | */ | |
10178 | ||
10179 | if (job->attrs->prev) | |
10180 | job->attrs->prev->next = attr2->next; | |
10181 | else | |
10182 | job->attrs->attrs = attr2->next; | |
10183 | ||
10184 | if (job->attrs->last == attr2) | |
10185 | job->attrs->last = job->attrs->prev; | |
10186 | ||
757d2cad | 10187 | _ippFreeAttr(attr2); |
ef416fc2 | 10188 | |
10189 | /* | |
10190 | * Then copy the attribute... | |
10191 | */ | |
10192 | ||
10193 | copy_attribute(job->attrs, attr, 0); | |
10194 | ||
10195 | /* | |
10196 | * See if the job-name or job-hold-until is being changed. | |
10197 | */ | |
10198 | ||
10199 | if (!strcmp(attr->name, "job-hold-until")) | |
10200 | { | |
10201 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
10202 | ||
10203 | if (!strcmp(attr->values[0].string.text, "no-hold")) | |
10204 | cupsdReleaseJob(job); | |
10205 | else | |
10206 | cupsdHoldJob(job); | |
d09495fa | 10207 | |
10208 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE; | |
ef416fc2 | 10209 | } |
10210 | } | |
10211 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
10212 | { | |
10213 | /* | |
10214 | * Delete the attribute... | |
10215 | */ | |
10216 | ||
10217 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
10218 | IPP_TAG_ZERO)) != NULL) | |
10219 | { | |
10220 | if (job->attrs->prev) | |
10221 | job->attrs->prev->next = attr2->next; | |
10222 | else | |
10223 | job->attrs->attrs = attr2->next; | |
10224 | ||
10225 | if (attr2 == job->attrs->last) | |
10226 | job->attrs->last = job->attrs->prev; | |
10227 | ||
757d2cad | 10228 | _ippFreeAttr(attr2); |
d09495fa | 10229 | |
10230 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10231 | } |
10232 | } | |
10233 | else | |
10234 | { | |
10235 | /* | |
10236 | * Add new option by copying it... | |
10237 | */ | |
10238 | ||
10239 | copy_attribute(job->attrs, attr, 0); | |
d09495fa | 10240 | |
10241 | event |= CUPSD_EVENT_JOB_CONFIG_CHANGED; | |
ef416fc2 | 10242 | } |
10243 | } | |
10244 | ||
10245 | /* | |
10246 | * Save the job... | |
10247 | */ | |
10248 | ||
3dfe78b3 MS |
10249 | job->dirty = 1; |
10250 | cupsdMarkDirty(CUPSD_DIRTY_JOBS); | |
ef416fc2 | 10251 | |
d09495fa | 10252 | /* |
10253 | * Send events as needed... | |
10254 | */ | |
10255 | ||
d9bca400 | 10256 | if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED) |
01ce6322 MS |
10257 | cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED, |
10258 | cupsdFindDest(job->dest), job, | |
d9bca400 MS |
10259 | "Job priority changed by user."); |
10260 | ||
d09495fa | 10261 | if (event & CUPSD_EVENT_JOB_STATE) |
01ce6322 | 10262 | cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job, |
d09495fa | 10263 | job->state_value == IPP_JOB_HELD ? |
10264 | "Job held by user." : "Job restarted by user."); | |
10265 | ||
10266 | if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED) | |
01ce6322 | 10267 | cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job, |
d09495fa | 10268 | "Job options changed by user."); |
10269 | ||
ef416fc2 | 10270 | /* |
10271 | * Start jobs if possible... | |
10272 | */ | |
10273 | ||
10274 | cupsdCheckJobs(); | |
10275 | } | |
10276 | ||
10277 | ||
b423cd4c | 10278 | /* |
10279 | * 'set_printer_defaults()' - Set printer default options from a request. | |
10280 | */ | |
10281 | ||
10282 | static void | |
10283 | set_printer_defaults( | |
10284 | cupsd_client_t *con, /* I - Client connection */ | |
10285 | cupsd_printer_t *printer) /* I - Printer */ | |
10286 | { | |
10287 | int i; /* Looping var */ | |
10288 | ipp_attribute_t *attr; /* Current attribute */ | |
10289 | int namelen; /* Length of attribute name */ | |
10290 | char name[256], /* New attribute name */ | |
10291 | value[256]; /* String version of integer attrs */ | |
10292 | ||
10293 | ||
10294 | for (attr = con->request->attrs; attr; attr = attr->next) | |
10295 | { | |
10296 | /* | |
10297 | * Skip non-printer attributes... | |
10298 | */ | |
10299 | ||
10300 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
10301 | continue; | |
10302 | ||
10303 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
10304 | ||
10305 | if (!strcmp(attr->name, "job-sheets-default")) | |
10306 | { | |
10307 | /* | |
10308 | * Only allow keywords and names... | |
10309 | */ | |
10310 | ||
10311 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10312 | continue; | |
10313 | ||
10314 | /* | |
10315 | * Only allow job-sheets-default to be set when running without a | |
10316 | * system high classification level... | |
10317 | */ | |
10318 | ||
10319 | if (Classification) | |
10320 | continue; | |
10321 | ||
10322 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
10323 | ||
10324 | if (attr->num_values > 1) | |
10325 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
10326 | else | |
10327 | cupsdSetString(&printer->job_sheets[1], "none"); | |
10328 | } | |
10329 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
10330 | { | |
10331 | cupsdFreePrinterUsers(printer); | |
10332 | ||
10333 | printer->deny_users = 0; | |
10334 | ||
10335 | if (attr->value_tag == IPP_TAG_NAME && | |
10336 | (attr->num_values > 1 || | |
10337 | strcmp(attr->values[0].string.text, "all"))) | |
10338 | { | |
10339 | for (i = 0; i < attr->num_values; i ++) | |
10340 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10341 | } | |
10342 | } | |
10343 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
10344 | { | |
10345 | cupsdFreePrinterUsers(printer); | |
10346 | ||
10347 | printer->deny_users = 1; | |
10348 | ||
10349 | if (attr->value_tag == IPP_TAG_NAME && | |
10350 | (attr->num_values > 1 || | |
10351 | strcmp(attr->values[0].string.text, "none"))) | |
10352 | { | |
10353 | for (i = 0; i < attr->num_values; i ++) | |
10354 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
10355 | } | |
10356 | } | |
10357 | else if (!strcmp(attr->name, "job-quota-period")) | |
10358 | { | |
10359 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10360 | continue; | |
10361 | ||
10362 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
10363 | attr->values[0].integer); | |
10364 | cupsdFreeQuotas(printer); | |
10365 | ||
10366 | printer->quota_period = attr->values[0].integer; | |
10367 | } | |
10368 | else if (!strcmp(attr->name, "job-k-limit")) | |
10369 | { | |
10370 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10371 | continue; | |
10372 | ||
10373 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
10374 | attr->values[0].integer); | |
10375 | cupsdFreeQuotas(printer); | |
10376 | ||
10377 | printer->k_limit = attr->values[0].integer; | |
10378 | } | |
10379 | else if (!strcmp(attr->name, "job-page-limit")) | |
10380 | { | |
10381 | if (attr->value_tag != IPP_TAG_INTEGER) | |
10382 | continue; | |
10383 | ||
10384 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
10385 | attr->values[0].integer); | |
10386 | cupsdFreeQuotas(printer); | |
10387 | ||
10388 | printer->page_limit = attr->values[0].integer; | |
10389 | } | |
10390 | else if (!strcmp(attr->name, "printer-op-policy")) | |
10391 | { | |
10392 | cupsd_policy_t *p; /* Policy */ | |
10393 | ||
10394 | ||
10395 | if (attr->value_tag != IPP_TAG_NAME) | |
10396 | continue; | |
10397 | ||
10398 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
10399 | { | |
10400 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10401 | "Setting printer-op-policy to \"%s\"...", | |
10402 | attr->values[0].string.text); | |
10403 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
10404 | printer->op_policy_ptr = p; | |
10405 | } | |
10406 | else | |
10407 | { | |
10408 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10409 | _("Unknown printer-op-policy \"%s\"."), | |
10410 | attr->values[0].string.text); | |
10411 | return; | |
10412 | } | |
10413 | } | |
10414 | else if (!strcmp(attr->name, "printer-error-policy")) | |
10415 | { | |
10416 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
10417 | continue; | |
10418 | ||
10419 | if (strcmp(attr->values[0].string.text, "abort-job") && | |
10420 | strcmp(attr->values[0].string.text, "retry-job") && | |
10421 | strcmp(attr->values[0].string.text, "stop-printer")) | |
10422 | { | |
10423 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
10424 | _("Unknown printer-error-policy \"%s\"."), | |
10425 | attr->values[0].string.text); | |
10426 | return; | |
10427 | } | |
10428 | ||
10429 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10430 | "Setting printer-error-policy to \"%s\"...", | |
10431 | attr->values[0].string.text); | |
10432 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
10433 | } | |
b423cd4c | 10434 | |
10435 | /* | |
10436 | * Skip any other non-default attributes... | |
10437 | */ | |
10438 | ||
10439 | namelen = strlen(attr->name); | |
10440 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
10441 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
10442 | continue; | |
10443 | ||
10444 | /* | |
10445 | * OK, anything else must be a user-defined default... | |
10446 | */ | |
10447 | ||
10448 | strlcpy(name, attr->name, sizeof(name)); | |
10449 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
10450 | ||
10451 | switch (attr->value_tag) | |
10452 | { | |
10453 | case IPP_TAG_DELETEATTR : | |
10454 | printer->num_options = cupsRemoveOption(name, | |
10455 | printer->num_options, | |
10456 | &(printer->options)); | |
10457 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10458 | "Deleting %s", attr->name); | |
10459 | break; | |
10460 | ||
10461 | case IPP_TAG_NAME : | |
10462 | case IPP_TAG_KEYWORD : | |
10463 | case IPP_TAG_URI : | |
10464 | printer->num_options = cupsAddOption(name, | |
10465 | attr->values[0].string.text, | |
10466 | printer->num_options, | |
10467 | &(printer->options)); | |
10468 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10469 | "Setting %s to \"%s\"...", attr->name, | |
10470 | attr->values[0].string.text); | |
10471 | break; | |
10472 | ||
10473 | case IPP_TAG_BOOLEAN : | |
10474 | printer->num_options = cupsAddOption(name, | |
10475 | attr->values[0].boolean ? | |
10476 | "true" : "false", | |
10477 | printer->num_options, | |
10478 | &(printer->options)); | |
10479 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10480 | "Setting %s to %s...", attr->name, | |
10481 | attr->values[0].boolean ? "true" : "false"); | |
10482 | break; | |
10483 | ||
10484 | case IPP_TAG_INTEGER : | |
10485 | case IPP_TAG_ENUM : | |
10486 | sprintf(value, "%d", attr->values[0].integer); | |
10487 | printer->num_options = cupsAddOption(name, value, | |
10488 | printer->num_options, | |
10489 | &(printer->options)); | |
10490 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10491 | "Setting %s to %s...", attr->name, value); | |
10492 | break; | |
10493 | ||
10494 | case IPP_TAG_RANGE : | |
10495 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
10496 | attr->values[0].range.upper); | |
10497 | printer->num_options = cupsAddOption(name, value, | |
10498 | printer->num_options, | |
10499 | &(printer->options)); | |
10500 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10501 | "Setting %s to %s...", attr->name, value); | |
10502 | break; | |
10503 | ||
10504 | case IPP_TAG_RESOLUTION : | |
10505 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
10506 | attr->values[0].resolution.yres, | |
10507 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
10508 | "dpi" : "dpc"); | |
10509 | printer->num_options = cupsAddOption(name, value, | |
10510 | printer->num_options, | |
10511 | &(printer->options)); | |
10512 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
10513 | "Setting %s to %s...", attr->name, value); | |
10514 | break; | |
10515 | ||
10516 | default : | |
10517 | /* Do nothing for other values */ | |
10518 | break; | |
10519 | } | |
10520 | } | |
10521 | } | |
10522 | ||
10523 | ||
ef416fc2 | 10524 | /* |
10525 | * 'start_printer()' - Start a printer. | |
10526 | */ | |
10527 | ||
10528 | static void | |
10529 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
10530 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10531 | { | |
10532 | http_status_t status; /* Policy status */ | |
bc44d920 | 10533 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10534 | cupsd_printer_t *printer; /* Printer data */ |
10535 | ||
10536 | ||
10537 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
10538 | con->http.fd, uri->values[0].string.text); | |
10539 | ||
10540 | /* | |
10541 | * Is the destination valid? | |
10542 | */ | |
10543 | ||
f7deaa1a | 10544 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10545 | { |
10546 | /* | |
10547 | * Bad URI... | |
10548 | */ | |
10549 | ||
10550 | send_ipp_status(con, IPP_NOT_FOUND, | |
10551 | _("The printer or class was not found.")); | |
10552 | return; | |
10553 | } | |
10554 | ||
10555 | /* | |
10556 | * Check policy... | |
10557 | */ | |
10558 | ||
10559 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10560 | { | |
f899b121 | 10561 | send_http_error(con, status, printer); |
ef416fc2 | 10562 | return; |
10563 | } | |
10564 | ||
10565 | /* | |
10566 | * Start the printer... | |
10567 | */ | |
10568 | ||
10569 | printer->state_message[0] = '\0'; | |
10570 | ||
10571 | cupsdStartPrinter(printer, 1); | |
10572 | ||
10573 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 10574 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", |
10575 | printer->name, get_username(con)); | |
e00b005a | 10576 | else |
f7deaa1a | 10577 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", |
10578 | printer->name, get_username(con)); | |
ef416fc2 | 10579 | |
10580 | cupsdCheckJobs(); | |
10581 | ||
10582 | /* | |
10583 | * Everything was ok, so return OK status... | |
10584 | */ | |
10585 | ||
10586 | con->response->request.status.status_code = IPP_OK; | |
10587 | } | |
10588 | ||
10589 | ||
10590 | /* | |
10591 | * 'stop_printer()' - Stop a printer. | |
10592 | */ | |
10593 | ||
10594 | static void | |
10595 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
10596 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10597 | { | |
10598 | http_status_t status; /* Policy status */ | |
bc44d920 | 10599 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10600 | cupsd_printer_t *printer; /* Printer data */ |
10601 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
10602 | ||
10603 | ||
10604 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
10605 | con->http.fd, uri->values[0].string.text); | |
10606 | ||
10607 | /* | |
10608 | * Is the destination valid? | |
10609 | */ | |
10610 | ||
f7deaa1a | 10611 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10612 | { |
10613 | /* | |
10614 | * Bad URI... | |
10615 | */ | |
10616 | ||
10617 | send_ipp_status(con, IPP_NOT_FOUND, | |
10618 | _("The printer or class was not found.")); | |
10619 | return; | |
10620 | } | |
10621 | ||
10622 | /* | |
10623 | * Check policy... | |
10624 | */ | |
10625 | ||
10626 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10627 | { | |
f899b121 | 10628 | send_http_error(con, status, printer); |
ef416fc2 | 10629 | return; |
10630 | } | |
10631 | ||
10632 | /* | |
10633 | * Stop the printer... | |
10634 | */ | |
10635 | ||
10636 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
10637 | IPP_TAG_TEXT)) == NULL) | |
10638 | strcpy(printer->state_message, "Paused"); | |
10639 | else | |
10640 | { | |
10641 | strlcpy(printer->state_message, attr->values[0].string.text, | |
10642 | sizeof(printer->state_message)); | |
10643 | } | |
10644 | ||
10645 | cupsdStopPrinter(printer, 1); | |
10646 | ||
10647 | if (dtype & CUPS_PRINTER_CLASS) | |
f7deaa1a | 10648 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", |
10649 | printer->name, get_username(con)); | |
ef416fc2 | 10650 | else |
f7deaa1a | 10651 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", |
10652 | printer->name, get_username(con)); | |
ef416fc2 | 10653 | |
10654 | /* | |
10655 | * Everything was ok, so return OK status... | |
10656 | */ | |
10657 | ||
10658 | con->response->request.status.status_code = IPP_OK; | |
10659 | } | |
10660 | ||
10661 | ||
89d46774 | 10662 | /* |
10663 | * 'url_encode_attr()' - URL-encode a string attribute. | |
10664 | */ | |
10665 | ||
10666 | static void | |
10667 | url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */ | |
10668 | char *buffer,/* I - String buffer */ | |
10669 | int bufsize)/* I - Size of buffer */ | |
10670 | { | |
10671 | int i; /* Looping var */ | |
10672 | char *bufptr, /* Pointer into buffer */ | |
b94498cf | 10673 | *bufend; /* End of buffer */ |
89d46774 | 10674 | |
10675 | ||
10676 | strlcpy(buffer, attr->name, bufsize); | |
10677 | bufptr = buffer + strlen(buffer); | |
10678 | bufend = buffer + bufsize - 1; | |
10679 | ||
10680 | for (i = 0; i < attr->num_values; i ++) | |
10681 | { | |
10682 | if (bufptr >= bufend) | |
10683 | break; | |
10684 | ||
10685 | if (i) | |
10686 | *bufptr++ = ','; | |
10687 | else | |
10688 | *bufptr++ = '='; | |
10689 | ||
10690 | if (bufptr >= bufend) | |
10691 | break; | |
10692 | ||
10693 | *bufptr++ = '\''; | |
10694 | ||
b94498cf | 10695 | bufptr = url_encode_string(attr->values[i].string.text, |
10696 | bufptr, bufend - bufptr + 1); | |
89d46774 | 10697 | |
10698 | if (bufptr >= bufend) | |
10699 | break; | |
10700 | ||
10701 | *bufptr++ = '\''; | |
10702 | } | |
10703 | ||
10704 | *bufptr = '\0'; | |
10705 | } | |
10706 | ||
10707 | ||
b94498cf | 10708 | /* |
10709 | * 'url_encode_string()' - URL-encode a string. | |
10710 | */ | |
10711 | ||
10712 | static char * /* O - End of string */ | |
10713 | url_encode_string(const char *s, /* I - String */ | |
10714 | char *buffer, /* I - String buffer */ | |
10715 | int bufsize) /* I - Size of buffer */ | |
10716 | { | |
10717 | char *bufptr, /* Pointer into buffer */ | |
10718 | *bufend; /* End of buffer */ | |
10719 | static const char *hex = "0123456789ABCDEF"; | |
10720 | /* Hex digits */ | |
10721 | ||
10722 | ||
10723 | bufptr = buffer; | |
10724 | bufend = buffer + bufsize - 1; | |
10725 | ||
10726 | while (*s && bufptr < bufend) | |
10727 | { | |
10728 | if (*s == ' ' || *s == '%') | |
10729 | { | |
10730 | if (bufptr >= (bufend - 2)) | |
10731 | break; | |
10732 | ||
10733 | *bufptr++ = '%'; | |
10734 | *bufptr++ = hex[(*s >> 4) & 15]; | |
10735 | *bufptr++ = hex[*s & 15]; | |
10736 | ||
10737 | s ++; | |
10738 | } | |
10739 | else if (*s == '\'' || *s == '\\') | |
10740 | { | |
10741 | if (bufptr >= (bufend - 1)) | |
10742 | break; | |
10743 | ||
10744 | *bufptr++ = '\\'; | |
10745 | *bufptr++ = *s++; | |
10746 | } | |
10747 | else | |
10748 | *bufptr++ = *s++; | |
10749 | } | |
10750 | ||
10751 | *bufptr = '\0'; | |
10752 | ||
10753 | return (bufptr); | |
10754 | } | |
10755 | ||
10756 | ||
ef416fc2 | 10757 | /* |
10758 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
10759 | */ | |
10760 | ||
10761 | static int /* O - 0 if not allowed, 1 if allowed */ | |
10762 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
10763 | const char *username) /* I - Username */ | |
10764 | { | |
10765 | int i; /* Looping var */ | |
10766 | struct passwd *pw; /* User password data */ | |
5bd77a73 MS |
10767 | char baseuser[256], /* Base username */ |
10768 | *baseptr; /* Pointer to "@" in base username */ | |
ef416fc2 | 10769 | |
10770 | ||
10771 | if (p->num_users == 0) | |
10772 | return (1); | |
10773 | ||
10774 | if (!strcmp(username, "root")) | |
10775 | return (1); | |
10776 | ||
5bd77a73 MS |
10777 | if (strchr(username, '@')) |
10778 | { | |
10779 | /* | |
10780 | * Strip @REALM for username check... | |
10781 | */ | |
10782 | ||
10783 | strlcpy(baseuser, username, sizeof(baseuser)); | |
10784 | ||
10785 | if ((baseptr = strchr(baseuser, '@')) != NULL) | |
10786 | *baseptr = '\0'; | |
10787 | ||
10788 | username = baseuser; | |
10789 | } | |
10790 | ||
ef416fc2 | 10791 | pw = getpwnam(username); |
10792 | endpwent(); | |
10793 | ||
10794 | for (i = 0; i < p->num_users; i ++) | |
10795 | { | |
8922323b | 10796 | if (p->users[i][0] == '@') |
ef416fc2 | 10797 | { |
10798 | /* | |
10799 | * Check group membership... | |
10800 | */ | |
10801 | ||
10802 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
10803 | break; | |
10804 | } | |
8922323b MS |
10805 | else if (p->users[i][0] == '#') |
10806 | { | |
10807 | /* | |
10808 | * Check UUID... | |
10809 | */ | |
10810 | ||
10811 | if (cupsdCheckGroup(username, pw, p->users[i])) | |
10812 | break; | |
10813 | } | |
ef416fc2 | 10814 | else if (!strcasecmp(username, p->users[i])) |
10815 | break; | |
10816 | } | |
10817 | ||
10818 | return ((i < p->num_users) != p->deny_users); | |
10819 | } | |
10820 | ||
10821 | ||
10822 | /* | |
10823 | * 'validate_job()' - Validate printer options and destination. | |
10824 | */ | |
10825 | ||
10826 | static void | |
10827 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
10828 | ipp_attribute_t *uri) /* I - Printer URI */ | |
10829 | { | |
10830 | http_status_t status; /* Policy status */ | |
10831 | ipp_attribute_t *attr; /* Current attribute */ | |
10832 | ipp_attribute_t *format; /* Document-format attribute */ | |
bc44d920 | 10833 | cups_ptype_t dtype; /* Destination type (printer/class) */ |
ef416fc2 | 10834 | char super[MIME_MAX_SUPER], |
10835 | /* Supertype of file */ | |
10836 | type[MIME_MAX_TYPE]; | |
10837 | /* Subtype of file */ | |
10838 | cupsd_printer_t *printer; /* Printer */ | |
10839 | ||
10840 | ||
10841 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
10842 | con->http.fd, uri->values[0].string.text); | |
10843 | ||
10844 | /* | |
10845 | * OK, see if the client is sending the document compressed - CUPS | |
10846 | * doesn't support compression yet... | |
10847 | */ | |
10848 | ||
fa73b229 | 10849 | if ((attr = ippFindAttribute(con->request, "compression", |
10850 | IPP_TAG_KEYWORD)) != NULL && | |
10851 | !strcmp(attr->values[0].string.text, "none")) | |
ef416fc2 | 10852 | { |
10853 | send_ipp_status(con, IPP_ATTRIBUTES, | |
10854 | _("Unsupported compression attribute %s!"), | |
10855 | attr->values[0].string.text); | |
10856 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
10857 | "compression", NULL, attr->values[0].string.text); | |
10858 | return; | |
10859 | } | |
10860 | ||
10861 | /* | |
10862 | * Is it a format we support? | |
10863 | */ | |
10864 | ||
10865 | if ((format = ippFindAttribute(con->request, "document-format", | |
10866 | IPP_TAG_MIMETYPE)) != NULL) | |
10867 | { | |
bc44d920 | 10868 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", |
10869 | super, type) != 2) | |
ef416fc2 | 10870 | { |
10871 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
10872 | format->values[0].string.text); | |
10873 | return; | |
10874 | } | |
10875 | ||
fa73b229 | 10876 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && |
10877 | !mimeType(MimeDatabase, super, type)) | |
ef416fc2 | 10878 | { |
10879 | cupsdLogMessage(CUPSD_LOG_INFO, | |
10880 | "Hint: Do you have the raw file printing rules enabled?"); | |
10881 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
10882 | _("Unsupported format \"%s\"!"), | |
10883 | format->values[0].string.text); | |
10884 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
10885 | "document-format", NULL, format->values[0].string.text); | |
10886 | return; | |
10887 | } | |
10888 | } | |
10889 | ||
10890 | /* | |
10891 | * Is the destination valid? | |
10892 | */ | |
10893 | ||
f7deaa1a | 10894 | if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer)) |
ef416fc2 | 10895 | { |
10896 | /* | |
10897 | * Bad URI... | |
10898 | */ | |
10899 | ||
10900 | send_ipp_status(con, IPP_NOT_FOUND, | |
10901 | _("The printer or class was not found.")); | |
10902 | return; | |
10903 | } | |
10904 | ||
10905 | /* | |
10906 | * Check policy... | |
10907 | */ | |
10908 | ||
10909 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
10910 | { | |
f899b121 | 10911 | send_http_error(con, status, printer); |
ef416fc2 | 10912 | return; |
10913 | } | |
10914 | ||
10915 | /* | |
10916 | * Everything was ok, so return OK status... | |
10917 | */ | |
10918 | ||
10919 | con->response->request.status.status_code = IPP_OK; | |
10920 | } | |
10921 | ||
10922 | ||
10923 | /* | |
10924 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
10925 | */ | |
10926 | ||
bc44d920 | 10927 | static int /* O - 0 if name is no good, 1 if good */ |
ef416fc2 | 10928 | validate_name(const char *name) /* I - Name to check */ |
10929 | { | |
10930 | const char *ptr; /* Pointer into name */ | |
10931 | ||
10932 | ||
10933 | /* | |
10934 | * Scan the whole name... | |
10935 | */ | |
10936 | ||
10937 | for (ptr = name; *ptr; ptr ++) | |
f7deaa1a | 10938 | if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') |
ef416fc2 | 10939 | return (0); |
10940 | ||
10941 | /* | |
10942 | * All the characters are good; validate the length, too... | |
10943 | */ | |
10944 | ||
10945 | return ((ptr - name) < 128); | |
10946 | } | |
10947 | ||
10948 | ||
10949 | /* | |
10950 | * 'validate_user()' - Validate the user for the request. | |
10951 | */ | |
10952 | ||
10953 | static int /* O - 1 if permitted, 0 otherwise */ | |
10954 | validate_user(cupsd_job_t *job, /* I - Job */ | |
10955 | cupsd_client_t *con, /* I - Client connection */ | |
10956 | const char *owner, /* I - Owner of job/resource */ | |
10957 | char *username, /* O - Authenticated username */ | |
10958 | int userlen) /* I - Length of username */ | |
10959 | { | |
ef416fc2 | 10960 | cupsd_printer_t *printer; /* Printer for job */ |
10961 | ||
10962 | ||
10963 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
10964 | "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, " | |
10965 | "userlen=%d)", | |
91c84a35 MS |
10966 | job->id, con ? con->http.fd : 0, |
10967 | owner ? owner : "(null)", username, userlen); | |
ef416fc2 | 10968 | |
10969 | /* | |
10970 | * Validate input... | |
10971 | */ | |
10972 | ||
10973 | if (!con || !owner || !username || userlen <= 0) | |
10974 | return (0); | |
10975 | ||
10976 | /* | |
10977 | * Get the best authenticated username that is available. | |
10978 | */ | |
10979 | ||
e00b005a | 10980 | strlcpy(username, get_username(con), userlen); |
ef416fc2 | 10981 | |
10982 | /* | |
10983 | * Check the username against the owner... | |
10984 | */ | |
10985 | ||
80ca4592 | 10986 | printer = cupsdFindDest(job->dest); |
ef416fc2 | 10987 | |
10988 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
10989 | con, owner) == HTTP_OK); | |
10990 | } | |
10991 | ||
10992 | ||
10993 | /* | |
2e4ff8af | 10994 | * End of "$Id: ipp.c 7014 2007-10-10 21:57:43Z mike $". |
ef416fc2 | 10995 | */ |