]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: ipp.c 5686 2006-06-21 21:02:56Z mike $" |
ef416fc2 | 3 | * |
4 | * IPP routines for the Common UNIX Printing System (CUPS) scheduler. | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdProcessIPPRequest() - Process an incoming IPP request... | |
27 | * accept_jobs() - Accept print jobs to a printer. | |
28 | * add_class() - Add a class to the system. | |
29 | * add_file() - Add a file to a job. | |
b423cd4c | 30 | * add_job() - Add a job to a print queue. |
ef416fc2 | 31 | * add_job_state_reasons() - Add the "job-state-reasons" attribute based |
32 | * upon the job and printer state... | |
33 | * add_job_subscriptions() - Add any subcriptions for a job. | |
bd7854cb | 34 | * add_job_uuid() - Add job-uuid attribute to a job. |
ef416fc2 | 35 | * add_printer() - Add a printer to the system. |
36 | * add_printer_state_reasons() - Add the "printer-state-reasons" attribute | |
37 | * based upon the printer state... | |
38 | * add_queued_job_count() - Add the "queued-job-count" attribute for | |
b423cd4c | 39 | * apply_printer_defaults() - Apply printer default options to a job. |
ef416fc2 | 40 | * authenticate_job() - Set job authentication info. |
41 | * cancel_all_jobs() - Cancel all print jobs. | |
42 | * cancel_job() - Cancel a print job. | |
43 | * cancel_subscription() - Cancel a subscription. | |
44 | * check_quotas() - Check quotas for a printer and user. | |
45 | * copy_attribute() - Copy a single attribute. | |
46 | * copy_attrs() - Copy attributes from one request to another. | |
47 | * copy_banner() - Copy a banner file to the requests directory | |
48 | * for the specified job. | |
49 | * copy_file() - Copy a PPD file or interface script... | |
50 | * copy_model() - Copy a PPD model file, substituting default | |
51 | * values as needed... | |
fa73b229 | 52 | * copy_job_attrs() - Copy job attributes. |
53 | * copy_printer_attrs() - Copy printer attributes. | |
ef416fc2 | 54 | * copy_subscription_attrs() - Copy subscription attributes. |
55 | * create_job() - Print a file to a printer or class. | |
56 | * create_requested_array() - Create an array for the requested-attributes. | |
57 | * create_subscription() - Create a notification subscription. | |
58 | * delete_printer() - Remove a printer or class from the system. | |
59 | * get_default() - Get the default destination. | |
60 | * get_devices() - Get the list of available devices on the | |
61 | * local system. | |
ef416fc2 | 62 | * get_job_attrs() - Get job attributes. |
fa73b229 | 63 | * get_jobs() - Get a list of jobs for the specified printer. |
ef416fc2 | 64 | * get_notifications() - Get events for a subscription. |
65 | * get_ppds() - Get the list of PPD files on the local | |
66 | * system. | |
67 | * get_printer_attrs() - Get printer attributes. | |
68 | * get_printers() - Get a list of printers. | |
69 | * get_subscription_attrs() - Get subscription attributes. | |
70 | * get_subscriptions() - Get subscriptions. | |
e00b005a | 71 | * get_username() - Get the username associated with a request. |
ef416fc2 | 72 | * hold_job() - Hold a print job. |
73 | * move_job() - Move a job to a new destination. | |
74 | * ppd_add_default() - Add a PPD default choice. | |
75 | * ppd_parse_line() - Parse a PPD default line. | |
76 | * print_job() - Print a file to a printer or class. | |
77 | * read_ps_line() - Read a line from a PS file... | |
78 | * read_ps_job_ticket() - Reads a job ticket embedded in a PS file. | |
79 | * reject_jobs() - Reject print jobs to a printer. | |
80 | * release_job() - Release a held print job. | |
81 | * restart_job() - Restart an old print job. | |
82 | * save_auth_info() - Save authentication information for a job. | |
83 | * send_document() - Send a file to a printer or class. | |
84 | * send_http_error() - Send a HTTP error back to the IPP client. | |
85 | * send_ipp_status() - Send a status back to the IPP client. | |
86 | * set_default() - Set the default destination... | |
87 | * set_job_attrs() - Set job attributes. | |
b423cd4c | 88 | * set_printer_defaults() - Set printer default options from a request. |
ef416fc2 | 89 | * start_printer() - Start a printer. |
90 | * stop_printer() - Stop a printer. | |
89d46774 | 91 | * url_encode_attr() - URL-encode a string attribute. |
ef416fc2 | 92 | * user_allowed() - See if a user is allowed to print to a queue. |
93 | * validate_job() - Validate printer options and destination. | |
94 | * validate_name() - Make sure the printer name only contains | |
95 | * valid chars. | |
96 | * validate_user() - Validate the user for the request. | |
97 | */ | |
98 | ||
99 | /* | |
100 | * Include necessary headers... | |
101 | */ | |
102 | ||
103 | #include "cupsd.h" | |
104 | ||
105 | #ifdef HAVE_LIBPAPER | |
106 | # include <paper.h> | |
107 | #endif /* HAVE_LIBPAPER */ | |
108 | ||
109 | ||
110 | /* | |
111 | * PPD default choice structure... | |
112 | */ | |
113 | ||
114 | typedef struct | |
115 | { | |
116 | char option[PPD_MAX_NAME]; /* Main keyword (option name) */ | |
117 | char choice[PPD_MAX_NAME]; /* Option keyword (choice name) */ | |
118 | } ppd_default_t; | |
119 | ||
120 | ||
121 | /* | |
122 | * Local functions... | |
123 | */ | |
124 | ||
125 | static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
126 | static void add_class(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 127 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, |
128 | mime_type_t *filetype, int compression); | |
f7faf1f5 | 129 | static cupsd_job_t *add_job(cupsd_client_t *con, ipp_attribute_t *uri, |
130 | cupsd_printer_t **dprinter, | |
80ca4592 | 131 | mime_type_t *filetype); |
ef416fc2 | 132 | static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job); |
133 | static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job); | |
bd7854cb | 134 | static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job); |
ef416fc2 | 135 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
b423cd4c | 136 | static void add_printer_state_reasons(cupsd_client_t *con, |
137 | cupsd_printer_t *p); | |
ef416fc2 | 138 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); |
b423cd4c | 139 | static void apply_printer_defaults(cupsd_printer_t *printer, |
140 | cupsd_job_t *job); | |
ef416fc2 | 141 | static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri); |
142 | static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
143 | static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
144 | static void cancel_subscription(cupsd_client_t *con, int id); | |
145 | static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p); | |
146 | static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr, | |
147 | int quickcopy); | |
fa73b229 | 148 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, |
ef416fc2 | 149 | ipp_tag_t group, int quickcopy); |
b423cd4c | 150 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, |
151 | const char *name); | |
ef416fc2 | 152 | static int copy_file(const char *from, const char *to); |
b423cd4c | 153 | static int copy_model(cupsd_client_t *con, const char *from, |
154 | const char *to); | |
fa73b229 | 155 | static void copy_job_attrs(cupsd_client_t *con, |
156 | cupsd_job_t *job, | |
157 | cups_array_t *ra); | |
158 | static void copy_printer_attrs(cupsd_client_t *con, | |
159 | cupsd_printer_t *printer, | |
160 | cups_array_t *ra); | |
ef416fc2 | 161 | static void copy_subscription_attrs(cupsd_client_t *con, |
162 | cupsd_subscription_t *sub, | |
163 | cups_array_t *ra); | |
164 | static void create_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
165 | static cups_array_t *create_requested_array(ipp_t *request); | |
166 | static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri); | |
167 | static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
168 | static void get_default(cupsd_client_t *con); | |
169 | static void get_devices(cupsd_client_t *con); | |
170 | static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
171 | static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
bd7854cb | 172 | static void get_notifications(cupsd_client_t *con); |
ef416fc2 | 173 | static void get_ppds(cupsd_client_t *con); |
174 | static void get_printers(cupsd_client_t *con, int type); | |
175 | static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
176 | static void get_subscription_attrs(cupsd_client_t *con, int sub_id); | |
177 | static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); | |
e00b005a | 178 | static const char *get_username(cupsd_client_t *con); |
ef416fc2 | 179 | static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri); |
180 | static void move_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
181 | static int ppd_add_default(const char *option, const char *choice, | |
182 | int num_defaults, ppd_default_t **defaults); | |
183 | static int ppd_parse_line(const char *line, char *option, int olen, | |
184 | char *choice, int clen); | |
185 | static void print_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
186 | static void read_ps_job_ticket(cupsd_client_t *con); | |
187 | static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
188 | static void release_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
189 | static void renew_subscription(cupsd_client_t *con, int sub_id); | |
190 | static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
191 | static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job); | |
192 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); | |
193 | static void send_http_error(cupsd_client_t *con, http_status_t status); | |
194 | static void send_ipp_status(cupsd_client_t *con, ipp_status_t status, | |
195 | const char *message, ...) | |
196 | # ifdef __GNUC__ | |
197 | __attribute__ ((__format__ (__printf__, 3, 4))) | |
198 | # endif /* __GNUC__ */ | |
199 | ; | |
200 | static void set_default(cupsd_client_t *con, ipp_attribute_t *uri); | |
201 | static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 202 | static void set_printer_defaults(cupsd_client_t *con, |
203 | cupsd_printer_t *printer); | |
ef416fc2 | 204 | static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
205 | static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
89d46774 | 206 | static void url_encode_attr(ipp_attribute_t *attr, char *buffer, |
207 | int bufsize); | |
ef416fc2 | 208 | static int user_allowed(cupsd_printer_t *p, const char *username); |
209 | static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
210 | static int validate_name(const char *name); | |
211 | static int validate_user(cupsd_job_t *job, cupsd_client_t *con, | |
212 | const char *owner, char *username, | |
213 | int userlen); | |
214 | ||
215 | ||
216 | /* | |
217 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request... | |
218 | */ | |
219 | ||
220 | int /* O - 1 on success, 0 on failure */ | |
221 | cupsdProcessIPPRequest( | |
222 | cupsd_client_t *con) /* I - Client connection */ | |
223 | { | |
224 | ipp_tag_t group; /* Current group tag */ | |
225 | ipp_attribute_t *attr; /* Current attribute */ | |
226 | ipp_attribute_t *charset; /* Character set attribute */ | |
227 | ipp_attribute_t *language; /* Language attribute */ | |
228 | ipp_attribute_t *uri; /* Printer URI attribute */ | |
229 | ipp_attribute_t *username; /* requesting-user-name attr */ | |
230 | int sub_id; /* Subscription ID */ | |
231 | ||
232 | ||
233 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
234 | "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x", | |
235 | con, con->http.fd, con->request->request.op.operation_id); | |
236 | ||
237 | /* | |
238 | * First build an empty response message for this request... | |
239 | */ | |
240 | ||
241 | con->response = ippNew(); | |
242 | ||
243 | con->response->request.status.version[0] = con->request->request.op.version[0]; | |
244 | con->response->request.status.version[1] = con->request->request.op.version[1]; | |
245 | con->response->request.status.request_id = con->request->request.op.request_id; | |
246 | ||
247 | /* | |
248 | * Then validate the request header and required attributes... | |
249 | */ | |
250 | ||
251 | if (con->request->request.any.version[0] != 1) | |
252 | { | |
253 | /* | |
254 | * Return an error, since we only support IPP 1.x. | |
255 | */ | |
256 | ||
257 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
258 | "%04X %s Bad request version number %d.%d", | |
259 | IPP_VERSION_NOT_SUPPORTED, con->http.hostname, | |
260 | con->request->request.any.version[0], | |
261 | con->request->request.any.version[1]); | |
262 | ||
263 | send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED, | |
264 | _("Bad request version number %d.%d!"), | |
265 | con->request->request.any.version[0], | |
266 | con->request->request.any.version[1]); | |
267 | } | |
fa73b229 | 268 | else if (!con->request->attrs) |
ef416fc2 | 269 | { |
270 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
271 | "%04X %s No attributes in request", | |
272 | IPP_BAD_REQUEST, con->http.hostname); | |
273 | ||
274 | send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!")); | |
275 | } | |
276 | else | |
277 | { | |
278 | /* | |
279 | * Make sure that the attributes are provided in the correct order and | |
280 | * don't repeat groups... | |
281 | */ | |
282 | ||
283 | for (attr = con->request->attrs, group = attr->group_tag; | |
fa73b229 | 284 | attr; |
ef416fc2 | 285 | attr = attr->next) |
286 | if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO) | |
287 | { | |
288 | /* | |
289 | * Out of order; return an error... | |
290 | */ | |
291 | ||
292 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
293 | "%04X %s Attribute groups are out of order", | |
294 | IPP_BAD_REQUEST, con->http.hostname); | |
295 | ||
296 | send_ipp_status(con, IPP_BAD_REQUEST, | |
297 | _("Attribute groups are out of order (%x < %x)!"), | |
298 | attr->group_tag, group); | |
299 | break; | |
300 | } | |
301 | else | |
302 | group = attr->group_tag; | |
303 | ||
fa73b229 | 304 | if (!attr) |
ef416fc2 | 305 | { |
306 | /* | |
307 | * Then make sure that the first three attributes are: | |
308 | * | |
309 | * attributes-charset | |
310 | * attributes-natural-language | |
311 | * printer-uri/job-uri | |
312 | */ | |
313 | ||
314 | attr = con->request->attrs; | |
315 | if (attr && !strcmp(attr->name, "attributes-charset") && | |
fa73b229 | 316 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET) |
ef416fc2 | 317 | charset = attr; |
318 | else | |
319 | charset = NULL; | |
320 | ||
321 | if (attr) | |
322 | attr = attr->next; | |
323 | ||
324 | if (attr && !strcmp(attr->name, "attributes-natural-language") && | |
fa73b229 | 325 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE) |
ef416fc2 | 326 | language = attr; |
327 | else | |
328 | language = NULL; | |
329 | ||
fa73b229 | 330 | if ((attr = ippFindAttribute(con->request, "printer-uri", |
331 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 332 | uri = attr; |
fa73b229 | 333 | else if ((attr = ippFindAttribute(con->request, "job-uri", |
334 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 335 | uri = attr; |
336 | else | |
337 | uri = NULL; | |
338 | ||
339 | if (charset) | |
340 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
341 | "attributes-charset", NULL, charset->values[0].string.text); | |
342 | else | |
343 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
344 | "attributes-charset", NULL, DefaultCharset); | |
345 | ||
346 | if (language) | |
347 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
348 | "attributes-natural-language", NULL, | |
349 | language->values[0].string.text); | |
350 | else | |
351 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
352 | "attributes-natural-language", NULL, DefaultLanguage); | |
353 | ||
fa73b229 | 354 | if (!charset || !language || |
355 | (!uri && | |
ef416fc2 | 356 | con->request->request.op.operation_id != CUPS_GET_DEFAULT && |
357 | con->request->request.op.operation_id != CUPS_GET_PRINTERS && | |
358 | con->request->request.op.operation_id != CUPS_GET_CLASSES && | |
359 | con->request->request.op.operation_id != CUPS_GET_DEVICES && | |
360 | con->request->request.op.operation_id != CUPS_GET_PPDS)) | |
361 | { | |
362 | /* | |
363 | * Return an error, since attributes-charset, | |
364 | * attributes-natural-language, and printer-uri/job-uri are required | |
365 | * for all operations. | |
366 | */ | |
367 | ||
368 | if (!charset) | |
369 | { | |
370 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
371 | "Missing attributes-charset attribute!"); | |
372 | ||
373 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
374 | "%04X %s Missing attributes-charset attribute", | |
375 | IPP_BAD_REQUEST, con->http.hostname); | |
376 | } | |
377 | ||
378 | if (!language) | |
379 | { | |
380 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
381 | "Missing attributes-natural-language attribute!"); | |
382 | ||
383 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
384 | "%04X %s Missing attributes-natural-language attribute", | |
385 | IPP_BAD_REQUEST, con->http.hostname); | |
386 | } | |
387 | ||
388 | if (!uri) | |
389 | { | |
390 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
391 | "Missing printer-uri or job-uri attribute!"); | |
392 | ||
393 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
394 | "%04X %s Missing printer-uri or job-uri attribute", | |
395 | IPP_BAD_REQUEST, con->http.hostname); | |
396 | } | |
397 | ||
398 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
399 | ||
fa73b229 | 400 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 401 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
402 | "attr \"%s\": group_tag = %x, value_tag = %x", | |
403 | attr->name ? attr->name : "(null)", attr->group_tag, | |
404 | attr->value_tag); | |
405 | ||
406 | cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes..."); | |
407 | ||
408 | send_ipp_status(con, IPP_BAD_REQUEST, | |
409 | _("Missing required attributes!")); | |
410 | } | |
411 | else | |
412 | { | |
413 | /* | |
414 | * OK, all the checks pass so far; make sure requesting-user-name is | |
415 | * not "root" from a remote host... | |
416 | */ | |
417 | ||
418 | if ((username = ippFindAttribute(con->request, "requesting-user-name", | |
419 | IPP_TAG_NAME)) != NULL) | |
420 | { | |
421 | /* | |
422 | * Check for root user... | |
423 | */ | |
424 | ||
425 | if (!strcmp(username->values[0].string.text, "root") && | |
426 | strcasecmp(con->http.hostname, "localhost") && | |
427 | strcmp(con->username, "root")) | |
428 | { | |
429 | /* | |
430 | * Remote unauthenticated user masquerading as local root... | |
431 | */ | |
432 | ||
757d2cad | 433 | _cupsStrFree(username->values[0].string.text); |
434 | username->values[0].string.text = _cupsStrAlloc(RemoteRoot); | |
ef416fc2 | 435 | } |
436 | } | |
437 | ||
438 | if ((attr = ippFindAttribute(con->request, "notify-subscription-id", | |
439 | IPP_TAG_INTEGER)) != NULL) | |
440 | sub_id = attr->values[0].integer; | |
441 | else | |
442 | sub_id = 0; | |
443 | ||
444 | /* | |
445 | * Then try processing the operation... | |
446 | */ | |
447 | ||
448 | if (uri) | |
bd7854cb | 449 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", |
450 | ippOpString(con->request->request.op.operation_id), | |
451 | uri->values[0].string.text); | |
452 | else | |
453 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", | |
454 | ippOpString(con->request->request.op.operation_id)); | |
ef416fc2 | 455 | |
456 | switch (con->request->request.op.operation_id) | |
457 | { | |
458 | case IPP_PRINT_JOB : | |
459 | print_job(con, uri); | |
460 | break; | |
461 | ||
462 | case IPP_VALIDATE_JOB : | |
463 | validate_job(con, uri); | |
464 | break; | |
465 | ||
466 | case IPP_CREATE_JOB : | |
467 | create_job(con, uri); | |
468 | break; | |
469 | ||
470 | case IPP_SEND_DOCUMENT : | |
471 | send_document(con, uri); | |
472 | break; | |
473 | ||
474 | case IPP_CANCEL_JOB : | |
475 | cancel_job(con, uri); | |
476 | break; | |
477 | ||
478 | case IPP_GET_JOB_ATTRIBUTES : | |
479 | get_job_attrs(con, uri); | |
480 | break; | |
481 | ||
482 | case IPP_GET_JOBS : | |
483 | get_jobs(con, uri); | |
484 | break; | |
485 | ||
486 | case IPP_GET_PRINTER_ATTRIBUTES : | |
487 | get_printer_attrs(con, uri); | |
488 | break; | |
489 | ||
490 | case IPP_HOLD_JOB : | |
491 | hold_job(con, uri); | |
492 | break; | |
493 | ||
494 | case IPP_RELEASE_JOB : | |
495 | release_job(con, uri); | |
496 | break; | |
497 | ||
498 | case IPP_RESTART_JOB : | |
499 | restart_job(con, uri); | |
500 | break; | |
501 | ||
502 | case IPP_PAUSE_PRINTER : | |
503 | stop_printer(con, uri); | |
504 | break; | |
505 | ||
506 | case IPP_RESUME_PRINTER : | |
507 | start_printer(con, uri); | |
508 | break; | |
509 | ||
510 | case IPP_PURGE_JOBS : | |
511 | cancel_all_jobs(con, uri); | |
512 | break; | |
513 | ||
514 | case IPP_SET_JOB_ATTRIBUTES : | |
515 | set_job_attrs(con, uri); | |
516 | break; | |
517 | ||
518 | case CUPS_GET_DEFAULT : | |
519 | get_default(con); | |
520 | break; | |
521 | ||
522 | case CUPS_GET_PRINTERS : | |
523 | get_printers(con, 0); | |
524 | break; | |
525 | ||
526 | case CUPS_GET_CLASSES : | |
527 | get_printers(con, CUPS_PRINTER_CLASS); | |
528 | break; | |
529 | ||
530 | case CUPS_ADD_PRINTER : | |
531 | add_printer(con, uri); | |
532 | break; | |
533 | ||
534 | case CUPS_DELETE_PRINTER : | |
535 | delete_printer(con, uri); | |
536 | break; | |
537 | ||
538 | case CUPS_ADD_CLASS : | |
539 | add_class(con, uri); | |
540 | break; | |
541 | ||
542 | case CUPS_DELETE_CLASS : | |
543 | delete_printer(con, uri); | |
544 | break; | |
545 | ||
546 | case CUPS_ACCEPT_JOBS : | |
547 | case IPP_ENABLE_PRINTER : | |
548 | accept_jobs(con, uri); | |
549 | break; | |
550 | ||
551 | case CUPS_REJECT_JOBS : | |
552 | case IPP_DISABLE_PRINTER : | |
553 | reject_jobs(con, uri); | |
554 | break; | |
555 | ||
556 | case CUPS_SET_DEFAULT : | |
557 | set_default(con, uri); | |
558 | break; | |
559 | ||
560 | case CUPS_GET_DEVICES : | |
561 | get_devices(con); | |
562 | break; | |
563 | ||
564 | case CUPS_GET_PPDS : | |
565 | get_ppds(con); | |
566 | break; | |
567 | ||
568 | case CUPS_MOVE_JOB : | |
569 | move_job(con, uri); | |
570 | break; | |
571 | ||
572 | case CUPS_AUTHENTICATE_JOB : | |
573 | authenticate_job(con, uri); | |
574 | break; | |
575 | ||
576 | case IPP_CREATE_PRINTER_SUBSCRIPTION : | |
577 | case IPP_CREATE_JOB_SUBSCRIPTION : | |
578 | create_subscription(con, uri); | |
579 | break; | |
580 | ||
581 | case IPP_GET_SUBSCRIPTION_ATTRIBUTES : | |
582 | get_subscription_attrs(con, sub_id); | |
583 | break; | |
584 | ||
585 | case IPP_GET_SUBSCRIPTIONS : | |
586 | get_subscriptions(con, uri); | |
587 | break; | |
588 | ||
589 | case IPP_RENEW_SUBSCRIPTION : | |
590 | renew_subscription(con, sub_id); | |
591 | break; | |
592 | ||
593 | case IPP_CANCEL_SUBSCRIPTION : | |
594 | cancel_subscription(con, sub_id); | |
595 | break; | |
596 | ||
597 | case IPP_GET_NOTIFICATIONS : | |
bd7854cb | 598 | get_notifications(con); |
ef416fc2 | 599 | break; |
600 | ||
601 | default : | |
602 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
603 | "%04X %s Operation %04X (%s) not supported", | |
604 | IPP_OPERATION_NOT_SUPPORTED, con->http.hostname, | |
605 | con->request->request.op.operation_id, | |
606 | ippOpString(con->request->request.op.operation_id)); | |
607 | ||
608 | send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED, | |
609 | _("%s not supported!"), | |
610 | ippOpString(con->request->request.op.operation_id)); | |
611 | break; | |
612 | } | |
613 | } | |
614 | } | |
615 | } | |
616 | ||
617 | if (con->response) | |
618 | { | |
619 | /* | |
620 | * Sending data from the scheduler... | |
621 | */ | |
622 | ||
623 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
624 | "cupsdProcessIPPRequest: %d status_code=%x (%s)", | |
625 | con->http.fd, con->response->request.status.status_code, | |
626 | ippErrorString(con->response->request.status.status_code)); | |
627 | ||
628 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp")) | |
629 | { | |
f301802f | 630 | #ifdef CUPSD_USE_CHUNKING |
631 | /* | |
632 | * Because older versions of CUPS (1.1.17 and older) and some IPP | |
633 | * clients do not implement chunking properly, we should not use | |
634 | * chunking by default. This may become the default in future | |
635 | * CUPS releases, or we might add a configuration directive for | |
636 | * it. | |
637 | */ | |
638 | ||
ef416fc2 | 639 | if (con->http.version == HTTP_1_1) |
640 | { | |
641 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
642 | ||
643 | httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n"); | |
644 | } | |
645 | else | |
f301802f | 646 | #endif /* CUPSD_USE_CHUNKING */ |
ef416fc2 | 647 | { |
648 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
649 | con->http.data_remaining = ippLength(con->response); | |
650 | ||
651 | if (con->http.data_remaining < INT_MAX) | |
652 | con->http._data_remaining = con->http.data_remaining; | |
653 | else | |
654 | con->http._data_remaining = INT_MAX; | |
655 | ||
656 | httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n", | |
657 | CUPS_LLCAST con->http.data_remaining); | |
658 | } | |
659 | ||
660 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
661 | "cupsdProcessIPPRequest: Adding fd %d to OutputSet...", | |
662 | con->http.fd); | |
663 | ||
664 | FD_SET(con->http.fd, OutputSet); | |
665 | ||
666 | /* | |
667 | * Tell the caller the response header was sent successfully... | |
668 | */ | |
669 | ||
670 | return (1); | |
671 | } | |
672 | else | |
673 | { | |
674 | /* | |
675 | * Tell the caller the response header could not be sent... | |
676 | */ | |
677 | ||
678 | return (0); | |
679 | } | |
680 | } | |
681 | else | |
682 | { | |
683 | /* | |
684 | * Sending data from a subprocess like cups-deviced; tell the caller | |
685 | * everything is A-OK so far... | |
686 | */ | |
687 | ||
688 | return (1); | |
689 | } | |
690 | } | |
691 | ||
692 | ||
693 | /* | |
694 | * 'accept_jobs()' - Accept print jobs to a printer. | |
695 | */ | |
696 | ||
697 | static void | |
698 | accept_jobs(cupsd_client_t *con, /* I - Client connection */ | |
699 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
700 | { | |
701 | http_status_t status; /* Policy status */ | |
702 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 703 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
704 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
705 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
706 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
707 | int port; /* Port portion of URI */ | |
708 | const char *name; /* Printer name */ | |
ef416fc2 | 709 | cupsd_printer_t *printer; /* Printer data */ |
710 | ||
711 | ||
712 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con, | |
713 | con->http.fd, uri->values[0].string.text); | |
714 | ||
715 | /* | |
716 | * Is the destination valid? | |
717 | */ | |
718 | ||
f7faf1f5 | 719 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
720 | sizeof(method), username, sizeof(username), host, | |
721 | sizeof(host), &port, resource, sizeof(resource)); | |
722 | ||
723 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 724 | { |
725 | /* | |
726 | * Bad URI... | |
727 | */ | |
728 | ||
729 | send_ipp_status(con, IPP_NOT_FOUND, | |
730 | _("The printer or class was not found.")); | |
731 | return; | |
732 | } | |
733 | ||
734 | /* | |
735 | * Check policy... | |
736 | */ | |
737 | ||
738 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
739 | { | |
740 | send_http_error(con, status); | |
741 | return; | |
742 | } | |
743 | ||
744 | /* | |
745 | * Accept jobs sent to the printer... | |
746 | */ | |
747 | ||
748 | printer->accepting = 1; | |
749 | printer->state_message[0] = '\0'; | |
750 | ||
751 | cupsdAddPrinterHistory(printer); | |
752 | ||
753 | if (dtype & CUPS_PRINTER_CLASS) | |
754 | cupsdSaveAllClasses(); | |
755 | else | |
756 | cupsdSaveAllPrinters(); | |
757 | ||
f7faf1f5 | 758 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" now accepting jobs (\"%s\").", name, |
759 | get_username(con)); | |
ef416fc2 | 760 | |
761 | /* | |
762 | * Everything was ok, so return OK status... | |
763 | */ | |
764 | ||
765 | con->response->request.status.status_code = IPP_OK; | |
766 | } | |
767 | ||
768 | ||
769 | /* | |
770 | * 'add_class()' - Add a class to the system. | |
771 | */ | |
772 | ||
773 | static void | |
774 | add_class(cupsd_client_t *con, /* I - Client connection */ | |
775 | ipp_attribute_t *uri) /* I - URI of class */ | |
776 | { | |
777 | http_status_t status; /* Policy status */ | |
778 | int i; /* Looping var */ | |
779 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
780 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
781 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
782 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
783 | int port; /* Port portion of URI */ | |
784 | cupsd_printer_t *pclass, /* Class */ | |
785 | *member; /* Member printer/class */ | |
786 | cups_ptype_t dtype; /* Destination type */ | |
f7faf1f5 | 787 | const char *dest; /* Printer or class name */ |
ef416fc2 | 788 | ipp_attribute_t *attr; /* Printer attribute */ |
789 | int modify; /* Non-zero if we just modified */ | |
b423cd4c | 790 | char newname[IPP_MAX_NAME]; /* New class name */ |
e00b005a | 791 | int need_restart_job; /* Need to restart job? */ |
ef416fc2 | 792 | |
793 | ||
794 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con, | |
795 | con->http.fd, uri->values[0].string.text); | |
796 | ||
797 | /* | |
798 | * Do we have a valid URI? | |
799 | */ | |
800 | ||
a4d04587 | 801 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
802 | sizeof(method), username, sizeof(username), host, | |
803 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 804 | |
805 | ||
fa73b229 | 806 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) |
ef416fc2 | 807 | { |
808 | /* | |
809 | * No, return an error... | |
810 | */ | |
811 | ||
812 | send_ipp_status(con, IPP_BAD_REQUEST, | |
813 | _("The printer-uri must be of the form " | |
814 | "\"ipp://HOSTNAME/classes/CLASSNAME\".")); | |
815 | return; | |
816 | } | |
817 | ||
818 | /* | |
819 | * Do we have a valid printer name? | |
820 | */ | |
821 | ||
822 | if (!validate_name(resource + 9)) | |
823 | { | |
824 | /* | |
825 | * No, return an error... | |
826 | */ | |
827 | ||
828 | send_ipp_status(con, IPP_BAD_REQUEST, | |
829 | _("The printer-uri \"%s\" contains invalid characters."), | |
830 | uri->values[0].string.text); | |
831 | return; | |
832 | } | |
833 | ||
834 | /* | |
835 | * Check policy... | |
836 | */ | |
837 | ||
838 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
839 | { | |
840 | send_http_error(con, status); | |
841 | return; | |
842 | } | |
843 | ||
844 | /* | |
845 | * See if the class already exists; if not, create a new class... | |
846 | */ | |
847 | ||
848 | if ((pclass = cupsdFindClass(resource + 9)) == NULL) | |
849 | { | |
850 | /* | |
851 | * Class doesn't exist; see if we have a printer of the same name... | |
852 | */ | |
853 | ||
854 | if ((pclass = cupsdFindPrinter(resource + 9)) != NULL && | |
855 | !(pclass->type & CUPS_PRINTER_REMOTE)) | |
856 | { | |
857 | /* | |
858 | * Yes, return an error... | |
859 | */ | |
860 | ||
861 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
862 | _("A printer named \"%s\" already exists!"), | |
863 | resource + 9); | |
864 | return; | |
865 | } | |
866 | ||
867 | /* | |
868 | * No, add the pclass... | |
869 | */ | |
870 | ||
871 | pclass = cupsdAddClass(resource + 9); | |
872 | modify = 0; | |
873 | } | |
874 | else if (pclass->type & CUPS_PRINTER_IMPLICIT) | |
875 | { | |
876 | /* | |
877 | * Rename the implicit class to "AnyClass" or remove it... | |
878 | */ | |
879 | ||
880 | if (ImplicitAnyClasses) | |
881 | { | |
b423cd4c | 882 | snprintf(newname, sizeof(newname), "Any%s", resource + 9); |
883 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 884 | } |
885 | else | |
886 | cupsdDeletePrinter(pclass, 1); | |
887 | ||
888 | /* | |
889 | * Add the class as a new local class... | |
890 | */ | |
891 | ||
892 | pclass = cupsdAddClass(resource + 9); | |
893 | modify = 0; | |
894 | } | |
895 | else if (pclass->type & CUPS_PRINTER_REMOTE) | |
896 | { | |
897 | /* | |
898 | * Rename the remote class to "Class"... | |
899 | */ | |
900 | ||
b423cd4c | 901 | snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname); |
902 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 903 | |
904 | /* | |
905 | * Add the class as a new local class... | |
906 | */ | |
907 | ||
908 | pclass = cupsdAddClass(resource + 9); | |
909 | modify = 0; | |
910 | } | |
911 | else | |
912 | modify = 1; | |
913 | ||
914 | /* | |
915 | * Look for attributes and copy them over as needed... | |
916 | */ | |
917 | ||
e00b005a | 918 | need_restart_job = 0; |
919 | ||
fa73b229 | 920 | if ((attr = ippFindAttribute(con->request, "printer-location", |
921 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 922 | cupsdSetString(&pclass->location, attr->values[0].string.text); |
923 | ||
fa73b229 | 924 | if ((attr = ippFindAttribute(con->request, "printer-info", |
925 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 926 | cupsdSetString(&pclass->info, attr->values[0].string.text); |
927 | ||
fa73b229 | 928 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
929 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 930 | { |
931 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
932 | pclass->name, attr->values[0].boolean, pclass->accepting); | |
933 | ||
934 | pclass->accepting = attr->values[0].boolean; | |
935 | cupsdAddPrinterHistory(pclass); | |
936 | } | |
937 | ||
fa73b229 | 938 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
939 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 940 | { |
941 | if (pclass->shared && !attr->values[0].boolean) | |
942 | cupsdSendBrowseDelete(pclass); | |
943 | ||
944 | cupsdLogMessage(CUPSD_LOG_INFO, | |
945 | "Setting %s printer-is-shared to %d (was %d.)", | |
946 | pclass->name, attr->values[0].boolean, pclass->shared); | |
947 | ||
948 | pclass->shared = attr->values[0].boolean; | |
949 | } | |
950 | ||
fa73b229 | 951 | if ((attr = ippFindAttribute(con->request, "printer-state", |
952 | IPP_TAG_ENUM)) != NULL) | |
ef416fc2 | 953 | { |
954 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
955 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
956 | { | |
957 | send_ipp_status(con, IPP_BAD_REQUEST, | |
958 | _("Attempt to set %s printer-state to bad value %d!"), | |
959 | pclass->name, attr->values[0].integer); | |
960 | return; | |
961 | } | |
962 | ||
963 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", pclass->name, | |
964 | attr->values[0].integer, pclass->state); | |
965 | ||
966 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
967 | cupsdStopPrinter(pclass, 0); | |
968 | else | |
e00b005a | 969 | { |
ef416fc2 | 970 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); |
e00b005a | 971 | need_restart_job = 1; |
972 | } | |
ef416fc2 | 973 | } |
fa73b229 | 974 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
975 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 976 | { |
977 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
978 | sizeof(pclass->state_message)); | |
979 | cupsdAddPrinterHistory(pclass); | |
980 | } | |
fa73b229 | 981 | if ((attr = ippFindAttribute(con->request, "member-uris", |
982 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 983 | { |
984 | /* | |
985 | * Clear the printer array as needed... | |
986 | */ | |
987 | ||
e00b005a | 988 | need_restart_job = 1; |
989 | ||
ef416fc2 | 990 | if (pclass->num_printers > 0) |
991 | { | |
992 | free(pclass->printers); | |
993 | pclass->num_printers = 0; | |
994 | } | |
995 | ||
996 | /* | |
997 | * Add each printer or class that is listed... | |
998 | */ | |
999 | ||
1000 | for (i = 0; i < attr->num_values; i ++) | |
1001 | { | |
1002 | /* | |
1003 | * Search for the printer or class URI... | |
1004 | */ | |
1005 | ||
f7faf1f5 | 1006 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, method, |
1007 | sizeof(method), username, sizeof(username), host, | |
1008 | sizeof(host), &port, resource, sizeof(resource)); | |
1009 | ||
1010 | if ((dest = cupsdValidateDest(host, resource, &dtype, &member)) == NULL) | |
ef416fc2 | 1011 | { |
1012 | /* | |
1013 | * Bad URI... | |
1014 | */ | |
1015 | ||
1016 | send_ipp_status(con, IPP_NOT_FOUND, | |
1017 | _("The printer or class was not found.")); | |
1018 | return; | |
1019 | } | |
1020 | ||
1021 | /* | |
1022 | * Add it to the class... | |
1023 | */ | |
1024 | ||
1025 | cupsdAddPrinterToClass(pclass, member); | |
1026 | } | |
1027 | } | |
1028 | ||
b423cd4c | 1029 | set_printer_defaults(con, pclass); |
1030 | ||
ef416fc2 | 1031 | /* |
1032 | * Update the printer class attributes and return... | |
1033 | */ | |
1034 | ||
1035 | cupsdSetPrinterAttrs(pclass); | |
1036 | cupsdSaveAllClasses(); | |
e00b005a | 1037 | |
1038 | if (need_restart_job && pclass->job) | |
1039 | { | |
1040 | cupsd_job_t *job; | |
1041 | ||
1042 | /* | |
1043 | * Stop the current job and then restart it below... | |
1044 | */ | |
1045 | ||
1046 | job = (cupsd_job_t *)pclass->job; | |
1047 | ||
1048 | cupsdStopJob(job, 1); | |
bd7854cb | 1049 | |
e00b005a | 1050 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 1051 | job->state_value = IPP_JOB_PENDING; |
e00b005a | 1052 | } |
1053 | ||
1054 | if (need_restart_job) | |
1055 | cupsdCheckJobs(); | |
ef416fc2 | 1056 | |
1057 | cupsdWritePrintcap(); | |
1058 | ||
1059 | if (modify) | |
1060 | { | |
1061 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL, | |
1062 | "Class \"%s\" modified by \"%s\".", pclass->name, | |
e00b005a | 1063 | get_username(con)); |
ef416fc2 | 1064 | |
1065 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
e00b005a | 1066 | pclass->name, get_username(con)); |
ef416fc2 | 1067 | } |
1068 | else | |
1069 | { | |
1070 | cupsdAddPrinterHistory(pclass); | |
1071 | ||
1072 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL, | |
1073 | "New class \"%s\" added by \"%s\".", pclass->name, | |
e00b005a | 1074 | get_username(con)); |
ef416fc2 | 1075 | |
1076 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
e00b005a | 1077 | pclass->name, get_username(con)); |
ef416fc2 | 1078 | } |
1079 | ||
1080 | con->response->request.status.status_code = IPP_OK; | |
1081 | } | |
1082 | ||
1083 | ||
1084 | /* | |
1085 | * 'add_file()' - Add a file to a job. | |
1086 | */ | |
1087 | ||
1088 | static int /* O - 0 on success, -1 on error */ | |
1089 | add_file(cupsd_client_t *con, /* I - Connection to client */ | |
1090 | cupsd_job_t *job, /* I - Job to add to */ | |
1091 | mime_type_t *filetype, /* I - Type of file */ | |
1092 | int compression) /* I - Compression */ | |
1093 | { | |
1094 | mime_type_t **filetypes; /* New filetypes array... */ | |
1095 | int *compressions; /* New compressions array... */ | |
1096 | ||
1097 | ||
1098 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1099 | "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)", | |
1100 | con, con->http.fd, job->id, filetype->super, filetype->type, | |
1101 | compression); | |
1102 | ||
1103 | /* | |
1104 | * Add the file to the job... | |
1105 | */ | |
1106 | ||
1107 | if (job->num_files == 0) | |
1108 | { | |
1109 | compressions = (int *)malloc(sizeof(int)); | |
1110 | filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *)); | |
1111 | } | |
1112 | else | |
1113 | { | |
1114 | compressions = (int *)realloc(job->compressions, | |
1115 | (job->num_files + 1) * sizeof(int)); | |
1116 | filetypes = (mime_type_t **)realloc(job->filetypes, | |
1117 | (job->num_files + 1) * | |
1118 | sizeof(mime_type_t *)); | |
1119 | } | |
1120 | ||
fa73b229 | 1121 | if (!compressions || !filetypes) |
ef416fc2 | 1122 | { |
1123 | cupsdCancelJob(job, 1); | |
1124 | ||
1125 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1126 | _("Unable to allocate memory for file types!")); | |
1127 | return (-1); | |
1128 | } | |
1129 | ||
1130 | job->compressions = compressions; | |
1131 | job->compressions[job->num_files] = compression; | |
1132 | job->filetypes = filetypes; | |
1133 | job->filetypes[job->num_files] = filetype; | |
1134 | ||
1135 | job->num_files ++; | |
1136 | ||
1137 | return (0); | |
1138 | } | |
1139 | ||
1140 | ||
1141 | /* | |
b423cd4c | 1142 | * 'add_job()' - Add a job to a print queue. |
ef416fc2 | 1143 | */ |
1144 | ||
b423cd4c | 1145 | static cupsd_job_t * /* O - Job object */ |
1146 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
f7faf1f5 | 1147 | ipp_attribute_t *uri, /* I - printer-uri */ |
1148 | cupsd_printer_t **dprinter, /* I - Destination printer */ | |
80ca4592 | 1149 | mime_type_t *filetype) /* I - First print file type, if any */ |
ef416fc2 | 1150 | { |
b423cd4c | 1151 | http_status_t status; /* Policy status */ |
1152 | ipp_attribute_t *attr; /* Current attribute */ | |
f7faf1f5 | 1153 | const char *dest; /* Destination */ |
1154 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
b423cd4c | 1155 | const char *val; /* Default option value */ |
1156 | int priority; /* Job priority */ | |
1157 | char *title; /* Job name/title */ | |
1158 | cupsd_job_t *job; /* Current job */ | |
f7faf1f5 | 1159 | char job_uri[HTTP_MAX_URI], /* Job URI */ |
1160 | method[HTTP_MAX_URI], /* Method portion of URI */ | |
1161 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
1162 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
1163 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
1164 | int port; /* Port portion of URI */ | |
1165 | cupsd_printer_t *printer; /* Printer data */ | |
b423cd4c | 1166 | int kbytes; /* Size of print file */ |
1167 | int i; /* Looping var */ | |
1168 | int lowerpagerange; /* Page range bound */ | |
ef416fc2 | 1169 | |
1170 | ||
f7faf1f5 | 1171 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %s)", con, |
1172 | con->http.fd, uri->values[0].string.text); | |
1173 | ||
1174 | /* | |
1175 | * Is the destination valid? | |
1176 | */ | |
1177 | ||
1178 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
1179 | sizeof(method), username, sizeof(username), host, | |
1180 | sizeof(host), &port, resource, sizeof(resource)); | |
1181 | ||
1182 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
1183 | { | |
1184 | /* | |
1185 | * Bad URI... | |
1186 | */ | |
1187 | ||
1188 | send_ipp_status(con, IPP_NOT_FOUND, | |
1189 | _("The printer or class was not found.")); | |
1190 | return (NULL); | |
1191 | } | |
1192 | ||
1193 | if (dprinter) | |
1194 | *dprinter = printer; | |
ef416fc2 | 1195 | |
b423cd4c | 1196 | /* |
1197 | * Check remote printing to non-shared printer... | |
1198 | */ | |
ef416fc2 | 1199 | |
b423cd4c | 1200 | if (!printer->shared && |
1201 | strcasecmp(con->http.hostname, "localhost") && | |
1202 | strcasecmp(con->http.hostname, ServerName)) | |
1203 | { | |
1204 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
1205 | _("The printer or class is not shared!")); | |
1206 | return (NULL); | |
1207 | } | |
ef416fc2 | 1208 | |
b423cd4c | 1209 | /* |
1210 | * Check policy... | |
1211 | */ | |
ef416fc2 | 1212 | |
b423cd4c | 1213 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
1214 | { | |
1215 | send_http_error(con, status); | |
1216 | return (NULL); | |
1217 | } | |
1218 | else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) && !con->username[0]) | |
1219 | { | |
1220 | send_http_error(con, HTTP_UNAUTHORIZED); | |
1221 | return (NULL); | |
1222 | } | |
ef416fc2 | 1223 | |
b423cd4c | 1224 | /* |
1225 | * See if the printer is accepting jobs... | |
1226 | */ | |
1227 | ||
1228 | if (!printer->accepting) | |
1229 | { | |
1230 | send_ipp_status(con, IPP_NOT_ACCEPTING, | |
1231 | _("Destination \"%s\" is not accepting jobs."), | |
f7faf1f5 | 1232 | dest); |
b423cd4c | 1233 | return (NULL); |
ef416fc2 | 1234 | } |
ef416fc2 | 1235 | |
b423cd4c | 1236 | /* |
80ca4592 | 1237 | * Validate job template attributes; for now just document-format, |
1238 | * copies, and page-ranges... | |
b423cd4c | 1239 | */ |
ef416fc2 | 1240 | |
80ca4592 | 1241 | if (filetype && printer->filetypes && |
1242 | !cupsArrayFind(printer->filetypes, filetype)) | |
1243 | { | |
1244 | char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
1245 | /* MIME media type string */ | |
1246 | ||
1247 | ||
1248 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
1249 | filetype->type); | |
1250 | ||
1251 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
1252 | _("Unsupported format \'%s\'!"), mimetype); | |
1253 | ||
1254 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
1255 | "document-format", NULL, mimetype); | |
1256 | ||
1257 | return (NULL); | |
1258 | } | |
1259 | ||
b423cd4c | 1260 | if ((attr = ippFindAttribute(con->request, "copies", |
1261 | IPP_TAG_INTEGER)) != NULL) | |
1262 | { | |
1263 | if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies) | |
1264 | { | |
1265 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."), | |
1266 | attr->values[0].integer); | |
1267 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1268 | "copies", attr->values[0].integer); | |
1269 | return (NULL); | |
1270 | } | |
1271 | } | |
ef416fc2 | 1272 | |
b423cd4c | 1273 | if ((attr = ippFindAttribute(con->request, "page-ranges", |
1274 | IPP_TAG_RANGE)) != NULL) | |
1275 | { | |
1276 | for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++) | |
1277 | { | |
1278 | if (attr->values[i].range.lower < lowerpagerange || | |
1279 | attr->values[i].range.lower > attr->values[i].range.upper) | |
1280 | { | |
1281 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1282 | _("Bad page-ranges values %d-%d."), | |
1283 | attr->values[i].range.lower, | |
1284 | attr->values[i].range.upper); | |
1285 | return (NULL); | |
1286 | } | |
ef416fc2 | 1287 | |
b423cd4c | 1288 | lowerpagerange = attr->values[i].range.upper + 1; |
1289 | } | |
1290 | } | |
ef416fc2 | 1291 | |
1292 | /* | |
b423cd4c | 1293 | * Make sure we aren't over our limit... |
ef416fc2 | 1294 | */ |
1295 | ||
b423cd4c | 1296 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
1297 | cupsdCleanJobs(); | |
ef416fc2 | 1298 | |
f7faf1f5 | 1299 | if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs) |
b423cd4c | 1300 | { |
1301 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
1302 | _("Too many active jobs.")); | |
1303 | return (NULL); | |
1304 | } | |
1305 | ||
1306 | if (!check_quotas(con, printer)) | |
1307 | { | |
1308 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1309 | return (NULL); | |
1310 | } | |
ef416fc2 | 1311 | |
1312 | /* | |
b423cd4c | 1313 | * Create the job and set things up... |
ef416fc2 | 1314 | */ |
1315 | ||
b423cd4c | 1316 | if ((attr = ippFindAttribute(con->request, "job-priority", |
1317 | IPP_TAG_INTEGER)) != NULL) | |
1318 | priority = attr->values[0].integer; | |
1319 | else | |
ef416fc2 | 1320 | { |
b423cd4c | 1321 | if ((val = cupsGetOption("job-priority", printer->num_options, |
1322 | printer->options)) != NULL) | |
1323 | priority = atoi(val); | |
1324 | else | |
1325 | priority = 50; | |
ef416fc2 | 1326 | |
b423cd4c | 1327 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", |
1328 | priority); | |
1329 | } | |
ef416fc2 | 1330 | |
b423cd4c | 1331 | if ((attr = ippFindAttribute(con->request, "job-name", |
1332 | IPP_TAG_NAME)) != NULL) | |
1333 | title = attr->values[0].string.text; | |
1334 | else | |
1335 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1336 | title = "Untitled"); | |
ef416fc2 | 1337 | |
b423cd4c | 1338 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) |
1339 | { | |
1340 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
f7faf1f5 | 1341 | _("Unable to add job for destination \"%s\"!"), dest); |
b423cd4c | 1342 | return (NULL); |
1343 | } | |
ef416fc2 | 1344 | |
f7faf1f5 | 1345 | job->dtype = dtype; |
b423cd4c | 1346 | job->attrs = con->request; |
1347 | con->request = NULL; | |
ef416fc2 | 1348 | |
b423cd4c | 1349 | add_job_uuid(con, job); |
1350 | apply_printer_defaults(printer, job); | |
ef416fc2 | 1351 | |
b423cd4c | 1352 | attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME); |
ef416fc2 | 1353 | |
b423cd4c | 1354 | if (con->username[0]) |
1355 | { | |
1356 | cupsdSetString(&job->username, con->username); | |
ef416fc2 | 1357 | |
1358 | if (attr) | |
b423cd4c | 1359 | cupsdSetString(&attr->values[0].string.text, con->username); |
1360 | ||
1361 | save_auth_info(con, job); | |
ef416fc2 | 1362 | } |
b423cd4c | 1363 | else if (attr) |
1364 | { | |
1365 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1366 | "add_job: requesting-user-name=\"%s\"", | |
1367 | attr->values[0].string.text); | |
ef416fc2 | 1368 | |
b423cd4c | 1369 | cupsdSetString(&job->username, attr->values[0].string.text); |
1370 | } | |
1371 | else | |
1372 | cupsdSetString(&job->username, "anonymous"); | |
ef416fc2 | 1373 | |
b423cd4c | 1374 | if (!attr) |
1375 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1376 | "job-originating-user-name", NULL, job->username); | |
1377 | else | |
1378 | { | |
1379 | attr->group_tag = IPP_TAG_JOB; | |
757d2cad | 1380 | _cupsStrFree(attr->name); |
1381 | attr->name = _cupsStrAlloc("job-originating-user-name"); | |
b423cd4c | 1382 | } |
ef416fc2 | 1383 | |
b423cd4c | 1384 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", |
1385 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 1386 | { |
b423cd4c | 1387 | /* |
1388 | * Request contains a job-originating-host-name attribute; validate it... | |
1389 | */ | |
ef416fc2 | 1390 | |
b423cd4c | 1391 | if (attr->value_tag != IPP_TAG_NAME || |
1392 | attr->num_values != 1 || | |
1393 | strcmp(con->http.hostname, "localhost")) | |
ef416fc2 | 1394 | { |
1395 | /* | |
b423cd4c | 1396 | * Can't override the value if we aren't connected via localhost. |
1397 | * Also, we can only have 1 value and it must be a name value. | |
ef416fc2 | 1398 | */ |
1399 | ||
b423cd4c | 1400 | switch (attr->value_tag) |
1401 | { | |
1402 | case IPP_TAG_STRING : | |
1403 | case IPP_TAG_TEXTLANG : | |
1404 | case IPP_TAG_NAMELANG : | |
1405 | case IPP_TAG_TEXT : | |
1406 | case IPP_TAG_NAME : | |
1407 | case IPP_TAG_KEYWORD : | |
1408 | case IPP_TAG_URI : | |
1409 | case IPP_TAG_URISCHEME : | |
1410 | case IPP_TAG_CHARSET : | |
1411 | case IPP_TAG_LANGUAGE : | |
1412 | case IPP_TAG_MIMETYPE : | |
1413 | /* | |
1414 | * Free old strings... | |
1415 | */ | |
ef416fc2 | 1416 | |
b423cd4c | 1417 | for (i = 0; i < attr->num_values; i ++) |
1418 | { | |
757d2cad | 1419 | _cupsStrFree(attr->values[i].string.text); |
b423cd4c | 1420 | attr->values[i].string.text = NULL; |
1421 | if (attr->values[i].string.charset) | |
1422 | { | |
757d2cad | 1423 | _cupsStrFree(attr->values[i].string.charset); |
b423cd4c | 1424 | attr->values[i].string.charset = NULL; |
1425 | } | |
1426 | } | |
ef416fc2 | 1427 | |
b423cd4c | 1428 | default : |
1429 | break; | |
1430 | } | |
ef416fc2 | 1431 | |
b423cd4c | 1432 | /* |
1433 | * Use the default connection hostname instead... | |
1434 | */ | |
bd7854cb | 1435 | |
b423cd4c | 1436 | attr->value_tag = IPP_TAG_NAME; |
1437 | attr->num_values = 1; | |
757d2cad | 1438 | attr->values[0].string.text = _cupsStrAlloc(con->http.hostname); |
b423cd4c | 1439 | } |
bd7854cb | 1440 | |
b423cd4c | 1441 | attr->group_tag = IPP_TAG_JOB; |
1442 | } | |
1443 | else | |
1444 | { | |
1445 | /* | |
1446 | * No job-originating-host-name attribute, so use the hostname from | |
1447 | * the connection... | |
1448 | */ | |
bd7854cb | 1449 | |
b423cd4c | 1450 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, |
1451 | "job-originating-host-name", NULL, con->http.hostname); | |
1452 | } | |
bd7854cb | 1453 | |
b423cd4c | 1454 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", |
1455 | time(NULL)); | |
1456 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1457 | "time-at-processing", 0); | |
1458 | attr->value_tag = IPP_TAG_NOVALUE; | |
1459 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1460 | "time-at-completed", 0); | |
1461 | attr->value_tag = IPP_TAG_NOVALUE; | |
bd7854cb | 1462 | |
1463 | /* | |
b423cd4c | 1464 | * Add remaining job attributes... |
bd7854cb | 1465 | */ |
1466 | ||
b423cd4c | 1467 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
1468 | job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM, | |
1469 | "job-state", IPP_JOB_STOPPED); | |
1470 | job->state_value = job->state->values[0].integer; | |
1471 | job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1472 | "job-media-sheets-completed", 0); | |
1473 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, | |
1474 | printer->uri); | |
1475 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1476 | title); | |
bd7854cb | 1477 | |
b423cd4c | 1478 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
1479 | IPP_TAG_INTEGER)) != NULL) | |
1480 | attr->values[0].integer = 0; | |
1481 | else | |
1482 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1483 | "job-k-octets", 0); | |
bd7854cb | 1484 | |
b423cd4c | 1485 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
1486 | IPP_TAG_KEYWORD)) == NULL) | |
1487 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
1488 | if (!attr) | |
ef416fc2 | 1489 | { |
b423cd4c | 1490 | if ((val = cupsGetOption("job-hold-until", printer->num_options, |
1491 | printer->options)) == NULL) | |
1492 | val = "no-hold"; | |
ef416fc2 | 1493 | |
b423cd4c | 1494 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1495 | "job-hold-until", NULL, val); | |
ef416fc2 | 1496 | } |
b423cd4c | 1497 | if (attr && strcmp(attr->values[0].string.text, "no-hold") && |
1498 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 1499 | { |
1500 | /* | |
b423cd4c | 1501 | * Hold job until specified time... |
ef416fc2 | 1502 | */ |
1503 | ||
b423cd4c | 1504 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
e53920b9 | 1505 | |
1506 | job->state->values[0].integer = IPP_JOB_HELD; | |
1507 | job->state_value = IPP_JOB_HELD; | |
ef416fc2 | 1508 | } |
b423cd4c | 1509 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) |
ef416fc2 | 1510 | { |
e53920b9 | 1511 | job->hold_until = time(NULL) + 60; |
b423cd4c | 1512 | job->state->values[0].integer = IPP_JOB_HELD; |
1513 | job->state_value = IPP_JOB_HELD; | |
1514 | } | |
1515 | else | |
1516 | { | |
1517 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1518 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 1519 | } |
1520 | ||
b423cd4c | 1521 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
1522 | Classification) | |
ef416fc2 | 1523 | { |
1524 | /* | |
b423cd4c | 1525 | * Add job sheets options... |
ef416fc2 | 1526 | */ |
1527 | ||
b423cd4c | 1528 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1529 | IPP_TAG_ZERO)) == NULL) | |
ef416fc2 | 1530 | { |
b423cd4c | 1531 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1532 | "Adding default job-sheets values \"%s,%s\"...", | |
1533 | printer->job_sheets[0], printer->job_sheets[1]); | |
ef416fc2 | 1534 | |
b423cd4c | 1535 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", |
1536 | 2, NULL, NULL); | |
757d2cad | 1537 | attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]); |
1538 | attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]); | |
ef416fc2 | 1539 | } |
1540 | ||
b423cd4c | 1541 | job->job_sheets = attr; |
ef416fc2 | 1542 | |
ef416fc2 | 1543 | /* |
b423cd4c | 1544 | * Enforce classification level if set... |
ef416fc2 | 1545 | */ |
1546 | ||
b423cd4c | 1547 | if (Classification) |
ef416fc2 | 1548 | { |
b423cd4c | 1549 | cupsdLogMessage(CUPSD_LOG_INFO, |
1550 | "Classification=\"%s\", ClassifyOverride=%d", | |
1551 | Classification ? Classification : "(null)", | |
1552 | ClassifyOverride); | |
ef416fc2 | 1553 | |
b423cd4c | 1554 | if (ClassifyOverride) |
1555 | { | |
1556 | if (!strcmp(attr->values[0].string.text, "none") && | |
1557 | (attr->num_values == 1 || | |
1558 | !strcmp(attr->values[1].string.text, "none"))) | |
1559 | { | |
1560 | /* | |
1561 | * Force the leading banner to have the classification on it... | |
1562 | */ | |
ef416fc2 | 1563 | |
b423cd4c | 1564 | cupsdSetString(&attr->values[0].string.text, Classification); |
e00b005a | 1565 | |
b423cd4c | 1566 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " |
1567 | "job-sheets=\"%s,none\", " | |
1568 | "job-originating-user-name=\"%s\"", | |
1569 | job->id, Classification, job->username); | |
1570 | } | |
1571 | else if (attr->num_values == 2 && | |
1572 | strcmp(attr->values[0].string.text, | |
1573 | attr->values[1].string.text) && | |
1574 | strcmp(attr->values[0].string.text, "none") && | |
1575 | strcmp(attr->values[1].string.text, "none")) | |
1576 | { | |
1577 | /* | |
1578 | * Can't put two different security markings on the same document! | |
1579 | */ | |
ef416fc2 | 1580 | |
b423cd4c | 1581 | cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text); |
ef416fc2 | 1582 | |
b423cd4c | 1583 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " |
1584 | "job-sheets=\"%s,%s\", " | |
1585 | "job-originating-user-name=\"%s\"", | |
1586 | job->id, attr->values[0].string.text, | |
1587 | attr->values[1].string.text, job->username); | |
1588 | } | |
1589 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1590 | strcmp(attr->values[0].string.text, "none") && | |
1591 | (attr->num_values == 1 || | |
1592 | (strcmp(attr->values[1].string.text, Classification) && | |
1593 | strcmp(attr->values[1].string.text, "none")))) | |
1594 | { | |
1595 | if (attr->num_values == 1) | |
1596 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1597 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1598 | "job-sheets=\"%s\", " | |
1599 | "job-originating-user-name=\"%s\"", | |
1600 | job->id, attr->values[0].string.text, job->username); | |
1601 | else | |
1602 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1603 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1604 | "job-sheets=\"%s,%s\",fffff " | |
1605 | "job-originating-user-name=\"%s\"", | |
1606 | job->id, attr->values[0].string.text, | |
1607 | attr->values[1].string.text, job->username); | |
1608 | } | |
1609 | } | |
1610 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1611 | (attr->num_values == 1 || | |
1612 | strcmp(attr->values[1].string.text, Classification))) | |
ef416fc2 | 1613 | { |
1614 | /* | |
b423cd4c | 1615 | * Force the banner to have the classification on it... |
ef416fc2 | 1616 | */ |
1617 | ||
b423cd4c | 1618 | if (attr->num_values > 1 && |
1619 | !strcmp(attr->values[0].string.text, attr->values[1].string.text)) | |
1620 | { | |
1621 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
1622 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1623 | } | |
1624 | else | |
1625 | { | |
1626 | if (attr->num_values == 1 || | |
1627 | strcmp(attr->values[0].string.text, "none")) | |
1628 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
ef416fc2 | 1629 | |
b423cd4c | 1630 | if (attr->num_values > 1 && |
1631 | strcmp(attr->values[1].string.text, "none")) | |
1632 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1633 | } | |
ef416fc2 | 1634 | |
b423cd4c | 1635 | if (attr->num_values > 1) |
1636 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1637 | "[Job %d] CLASSIFICATION FORCED " | |
1638 | "job-sheets=\"%s,%s\", " | |
1639 | "job-originating-user-name=\"%s\"", | |
1640 | job->id, attr->values[0].string.text, | |
1641 | attr->values[1].string.text, job->username); | |
1642 | else | |
1643 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1644 | "[Job %d] CLASSIFICATION FORCED " | |
1645 | "job-sheets=\"%s\", " | |
1646 | "job-originating-user-name=\"%s\"", | |
1647 | job->id, Classification, job->username); | |
ef416fc2 | 1648 | } |
1649 | } | |
1650 | ||
b423cd4c | 1651 | /* |
1652 | * See if we need to add the starting sheet... | |
1653 | */ | |
ef416fc2 | 1654 | |
b423cd4c | 1655 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
ef416fc2 | 1656 | { |
b423cd4c | 1657 | cupsdLogMessage(CUPSD_LOG_INFO, |
1658 | "Adding start banner page \"%s\" to job %d.", | |
1659 | attr->values[0].string.text, job->id); | |
ef416fc2 | 1660 | |
b423cd4c | 1661 | kbytes = copy_banner(con, job, attr->values[0].string.text); |
ef416fc2 | 1662 | |
b423cd4c | 1663 | cupsdUpdateQuota(printer, job->username, 0, kbytes); |
1664 | } | |
ef416fc2 | 1665 | } |
b423cd4c | 1666 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1667 | IPP_TAG_ZERO)) != NULL) | |
1668 | job->sheets = attr; | |
ef416fc2 | 1669 | |
b423cd4c | 1670 | /* |
1671 | * Fill in the response info... | |
1672 | */ | |
ef416fc2 | 1673 | |
b423cd4c | 1674 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, |
1675 | LocalPort, job->id); | |
ef416fc2 | 1676 | |
b423cd4c | 1677 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
1678 | job_uri); | |
ef416fc2 | 1679 | |
b423cd4c | 1680 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
ef416fc2 | 1681 | |
b423cd4c | 1682 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", |
1683 | job->state_value); | |
1684 | add_job_state_reasons(con, job); | |
ef416fc2 | 1685 | |
b423cd4c | 1686 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 1687 | |
b423cd4c | 1688 | /* |
1689 | * Add any job subscriptions... | |
1690 | */ | |
ef416fc2 | 1691 | |
b423cd4c | 1692 | add_job_subscriptions(con, job); |
ef416fc2 | 1693 | |
b423cd4c | 1694 | /* |
1695 | * Set all but the first two attributes to the job attributes group... | |
1696 | */ | |
ef416fc2 | 1697 | |
b423cd4c | 1698 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) |
1699 | attr->group_tag = IPP_TAG_JOB; | |
ef416fc2 | 1700 | |
1701 | /* | |
b423cd4c | 1702 | * Fire the "job created" event... |
ef416fc2 | 1703 | */ |
1704 | ||
b423cd4c | 1705 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); |
ef416fc2 | 1706 | |
1707 | /* | |
b423cd4c | 1708 | * Return the new job... |
ef416fc2 | 1709 | */ |
1710 | ||
b423cd4c | 1711 | return (job); |
1712 | } | |
ef416fc2 | 1713 | |
ef416fc2 | 1714 | |
b423cd4c | 1715 | /* |
1716 | * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based | |
1717 | * upon the job and printer state... | |
1718 | */ | |
ef416fc2 | 1719 | |
b423cd4c | 1720 | static void |
1721 | add_job_state_reasons( | |
1722 | cupsd_client_t *con, /* I - Client connection */ | |
1723 | cupsd_job_t *job) /* I - Job info */ | |
1724 | { | |
1725 | cupsd_printer_t *dest; /* Destination printer */ | |
ef416fc2 | 1726 | |
ef416fc2 | 1727 | |
b423cd4c | 1728 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)", |
1729 | con, con->http.fd, job ? job->id : 0); | |
ef416fc2 | 1730 | |
b423cd4c | 1731 | switch (job ? job->state_value : IPP_JOB_CANCELLED) |
ef416fc2 | 1732 | { |
b423cd4c | 1733 | case IPP_JOB_PENDING : |
80ca4592 | 1734 | dest = cupsdFindDest(job->dest); |
bd7854cb | 1735 | |
b423cd4c | 1736 | if (dest && dest->state == IPP_PRINTER_STOPPED) |
1737 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1738 | "job-state-reasons", NULL, "printer-stopped"); | |
1739 | else | |
1740 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1741 | "job-state-reasons", NULL, "none"); | |
1742 | break; | |
ef416fc2 | 1743 | |
b423cd4c | 1744 | case IPP_JOB_HELD : |
1745 | if (ippFindAttribute(job->attrs, "job-hold-until", | |
1746 | IPP_TAG_KEYWORD) != NULL || | |
1747 | ippFindAttribute(job->attrs, "job-hold-until", | |
1748 | IPP_TAG_NAME) != NULL) | |
1749 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1750 | "job-state-reasons", NULL, "job-hold-until-specified"); | |
1751 | else | |
1752 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1753 | "job-state-reasons", NULL, "job-incoming"); | |
1754 | break; | |
ef416fc2 | 1755 | |
b423cd4c | 1756 | case IPP_JOB_PROCESSING : |
1757 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1758 | "job-state-reasons", NULL, "job-printing"); | |
1759 | break; | |
ef416fc2 | 1760 | |
b423cd4c | 1761 | case IPP_JOB_STOPPED : |
1762 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1763 | "job-state-reasons", NULL, "job-stopped"); | |
1764 | break; | |
ef416fc2 | 1765 | |
b423cd4c | 1766 | case IPP_JOB_CANCELLED : |
1767 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1768 | "job-state-reasons", NULL, "job-canceled-by-user"); | |
1769 | break; | |
ef416fc2 | 1770 | |
b423cd4c | 1771 | case IPP_JOB_ABORTED : |
1772 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1773 | "job-state-reasons", NULL, "aborted-by-system"); | |
1774 | break; | |
ef416fc2 | 1775 | |
b423cd4c | 1776 | case IPP_JOB_COMPLETED : |
1777 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1778 | "job-state-reasons", NULL, "job-completed-successfully"); | |
1779 | break; | |
ef416fc2 | 1780 | } |
ef416fc2 | 1781 | } |
1782 | ||
1783 | ||
1784 | /* | |
b423cd4c | 1785 | * 'add_job_subscriptions()' - Add any subcriptions for a job. |
ef416fc2 | 1786 | */ |
1787 | ||
1788 | static void | |
b423cd4c | 1789 | add_job_subscriptions( |
1790 | cupsd_client_t *con, /* I - Client connection */ | |
1791 | cupsd_job_t *job) /* I - Newly created job */ | |
ef416fc2 | 1792 | { |
b423cd4c | 1793 | int i; /* Looping var */ |
1794 | ipp_attribute_t *prev, /* Previous attribute */ | |
1795 | *next, /* Next attribute */ | |
1796 | *attr; /* Current attribute */ | |
1797 | cupsd_subscription_t *sub; /* Subscription object */ | |
1798 | const char *recipient, /* notify-recipient-uri */ | |
1799 | *pullmethod; /* notify-pull-method */ | |
1800 | ipp_attribute_t *user_data; /* notify-user-data */ | |
1801 | int interval; /* notify-time-interval */ | |
1802 | unsigned mask; /* notify-events */ | |
ef416fc2 | 1803 | |
ef416fc2 | 1804 | |
b423cd4c | 1805 | /* |
1806 | * Find the first subscription group attribute; return if we have | |
1807 | * none... | |
1808 | */ | |
ef416fc2 | 1809 | |
b423cd4c | 1810 | for (attr = job->attrs->attrs, prev = NULL; |
1811 | attr; | |
1812 | prev = attr, attr = attr->next) | |
1813 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
1814 | break; | |
ef416fc2 | 1815 | |
b423cd4c | 1816 | if (!attr) |
1817 | return; | |
ef416fc2 | 1818 | |
b423cd4c | 1819 | /* |
1820 | * Process the subscription attributes in the request... | |
1821 | */ | |
ef416fc2 | 1822 | |
b423cd4c | 1823 | while (attr) |
1824 | { | |
1825 | recipient = NULL; | |
1826 | pullmethod = NULL; | |
1827 | user_data = NULL; | |
1828 | interval = 0; | |
1829 | mask = CUPSD_EVENT_NONE; | |
ef416fc2 | 1830 | |
b423cd4c | 1831 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
1832 | { | |
1833 | if (!strcmp(attr->name, "notify-recipient") && | |
1834 | attr->value_tag == IPP_TAG_URI) | |
1835 | recipient = attr->values[0].string.text; | |
1836 | else if (!strcmp(attr->name, "notify-pull-method") && | |
1837 | attr->value_tag == IPP_TAG_KEYWORD) | |
1838 | pullmethod = attr->values[0].string.text; | |
1839 | else if (!strcmp(attr->name, "notify-charset") && | |
1840 | attr->value_tag == IPP_TAG_CHARSET && | |
1841 | strcmp(attr->values[0].string.text, "us-ascii") && | |
1842 | strcmp(attr->values[0].string.text, "utf-8")) | |
1843 | { | |
1844 | send_ipp_status(con, IPP_CHARSET, | |
1845 | _("Character set \"%s\" not supported!"), | |
1846 | attr->values[0].string.text); | |
1847 | return; | |
1848 | } | |
1849 | else if (!strcmp(attr->name, "notify-natural-language") && | |
1850 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
1851 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
1852 | { | |
1853 | send_ipp_status(con, IPP_CHARSET, | |
1854 | _("Language \"%s\" not supported!"), | |
1855 | attr->values[0].string.text); | |
1856 | return; | |
1857 | } | |
1858 | else if (!strcmp(attr->name, "notify-user-data") && | |
1859 | attr->value_tag == IPP_TAG_STRING) | |
1860 | { | |
1861 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
1862 | { | |
1863 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
1864 | _("The notify-user-data value is too large " | |
1865 | "(%d > 63 octets)!"), | |
1866 | attr->values[0].unknown.length); | |
1867 | return; | |
1868 | } | |
ef416fc2 | 1869 | |
b423cd4c | 1870 | user_data = attr; |
1871 | } | |
1872 | else if (!strcmp(attr->name, "notify-events") && | |
1873 | attr->value_tag == IPP_TAG_KEYWORD) | |
1874 | { | |
1875 | for (i = 0; i < attr->num_values; i ++) | |
1876 | mask |= cupsdEventValue(attr->values[i].string.text); | |
1877 | } | |
1878 | else if (!strcmp(attr->name, "notify-lease-duration")) | |
1879 | { | |
1880 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1881 | _("The notify-lease-duration attribute cannot be " | |
1882 | "used with job subscriptions.")); | |
1883 | return; | |
1884 | } | |
1885 | else if (!strcmp(attr->name, "notify-time-interval") && | |
1886 | attr->value_tag == IPP_TAG_INTEGER) | |
1887 | interval = attr->values[0].integer; | |
ef416fc2 | 1888 | |
b423cd4c | 1889 | attr = attr->next; |
1890 | } | |
ef416fc2 | 1891 | |
b423cd4c | 1892 | if (!recipient && !pullmethod) |
1893 | break; | |
ef416fc2 | 1894 | |
b423cd4c | 1895 | if (mask == CUPSD_EVENT_NONE) |
1896 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
ef416fc2 | 1897 | |
b423cd4c | 1898 | sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient, |
1899 | 0); | |
ef416fc2 | 1900 | |
b423cd4c | 1901 | sub->interval = interval; |
ef416fc2 | 1902 | |
b423cd4c | 1903 | cupsdSetString(&sub->owner, job->username); |
ef416fc2 | 1904 | |
b423cd4c | 1905 | if (user_data) |
ef416fc2 | 1906 | { |
b423cd4c | 1907 | sub->user_data_len = user_data->values[0].unknown.length; |
1908 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
1909 | sub->user_data_len); | |
ef416fc2 | 1910 | } |
1911 | ||
b423cd4c | 1912 | ippAddSeparator(con->response); |
1913 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
1914 | "notify-subscription-id", sub->id); | |
ef416fc2 | 1915 | |
b423cd4c | 1916 | if (attr) |
1917 | attr = attr->next; | |
ef416fc2 | 1918 | } |
1919 | ||
b423cd4c | 1920 | cupsdSaveAllSubscriptions(); |
1921 | ||
ef416fc2 | 1922 | /* |
b423cd4c | 1923 | * Remove all of the subscription attributes from the job request... |
ef416fc2 | 1924 | */ |
1925 | ||
b423cd4c | 1926 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) |
ef416fc2 | 1927 | { |
b423cd4c | 1928 | next = attr->next; |
ef416fc2 | 1929 | |
b423cd4c | 1930 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || |
1931 | attr->group_tag == IPP_TAG_ZERO) | |
1932 | { | |
1933 | /* | |
1934 | * Free and remove this attribute... | |
1935 | */ | |
ef416fc2 | 1936 | |
757d2cad | 1937 | _ippFreeAttr(attr); |
ef416fc2 | 1938 | |
b423cd4c | 1939 | if (prev) |
1940 | prev->next = next; | |
1941 | else | |
1942 | job->attrs->attrs = next; | |
1943 | } | |
1944 | else | |
1945 | prev = attr; | |
ef416fc2 | 1946 | } |
1947 | ||
b423cd4c | 1948 | job->attrs->last = prev; |
1949 | job->attrs->current = prev; | |
1950 | } | |
ef416fc2 | 1951 | |
ef416fc2 | 1952 | |
b423cd4c | 1953 | /* |
1954 | * 'add_job_uuid()' - Add job-uuid attribute to a job. | |
1955 | * | |
1956 | * See RFC 4122 for the definition of UUIDs and the format. | |
1957 | */ | |
1958 | ||
1959 | static void | |
1960 | add_job_uuid(cupsd_client_t *con, /* I - Client connection */ | |
1961 | cupsd_job_t *job) /* I - Job */ | |
1962 | { | |
1963 | char uuid[1024]; /* job-uuid string */ | |
1964 | ipp_attribute_t *attr; /* job-uuid attribute */ | |
1965 | _cups_md5_state_t md5state; /* MD5 state */ | |
1966 | unsigned char md5sum[16]; /* MD5 digest/sum */ | |
ef416fc2 | 1967 | |
ef416fc2 | 1968 | |
1969 | /* | |
b423cd4c | 1970 | * First see if the job already has a job-uuid attribute; if so, return... |
ef416fc2 | 1971 | */ |
1972 | ||
b423cd4c | 1973 | if ((attr = ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) != NULL) |
1974 | return; | |
ef416fc2 | 1975 | |
1976 | /* | |
b423cd4c | 1977 | * No job-uuid attribute, so build a version 3 UUID with the local job |
1978 | * ID at the end; see RFC 4122 for details. Start with the MD5 sum of | |
1979 | * the ServerName, server name and port that the client connected to, | |
1980 | * and local job ID... | |
ef416fc2 | 1981 | */ |
1982 | ||
b423cd4c | 1983 | snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername, |
1984 | con->serverport, job->id); | |
ef416fc2 | 1985 | |
757d2cad | 1986 | _cupsMD5Init(&md5state); |
1987 | _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid)); | |
1988 | _cupsMD5Finish(&md5state, md5sum); | |
ef416fc2 | 1989 | |
1990 | /* | |
b423cd4c | 1991 | * Format the UUID URI using the MD5 sum and job ID. |
ef416fc2 | 1992 | */ |
1993 | ||
b423cd4c | 1994 | snprintf(uuid, sizeof(uuid), |
1995 | "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" | |
1996 | "%02x%02x%02x%02x%02x%02x", | |
1997 | md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], | |
1998 | (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, | |
1999 | md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], | |
2000 | md5sum[14], md5sum[15]); | |
ef416fc2 | 2001 | |
b423cd4c | 2002 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid); |
ef416fc2 | 2003 | } |
2004 | ||
2005 | ||
2006 | /* | |
b423cd4c | 2007 | * 'add_printer()' - Add a printer to the system. |
ef416fc2 | 2008 | */ |
2009 | ||
2010 | static void | |
b423cd4c | 2011 | add_printer(cupsd_client_t *con, /* I - Client connection */ |
2012 | ipp_attribute_t *uri) /* I - URI of printer */ | |
ef416fc2 | 2013 | { |
2014 | http_status_t status; /* Policy status */ | |
b423cd4c | 2015 | int i; /* Looping var */ |
ef416fc2 | 2016 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2017 | username[HTTP_MAX_URI], /* Username portion of URI */ |
ef416fc2 | 2018 | host[HTTP_MAX_URI], /* Host portion of URI */ |
2019 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2020 | int port; /* Port portion of URI */ | |
b423cd4c | 2021 | cupsd_printer_t *printer; /* Printer/class */ |
2022 | ipp_attribute_t *attr; /* Printer attribute */ | |
2023 | cups_file_t *fp; /* Script/PPD file */ | |
2024 | char line[1024]; /* Line from file... */ | |
2025 | char srcfile[1024], /* Source Script/PPD file */ | |
2026 | dstfile[1024]; /* Destination Script/PPD file */ | |
2027 | int modify; /* Non-zero if we are modifying */ | |
2028 | char newname[IPP_MAX_NAME]; /* New printer name */ | |
2029 | int need_restart_job; /* Need to restart job? */ | |
ef416fc2 | 2030 | |
2031 | ||
b423cd4c | 2032 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, |
ef416fc2 | 2033 | con->http.fd, uri->values[0].string.text); |
2034 | ||
2035 | /* | |
b423cd4c | 2036 | * Do we have a valid URI? |
ef416fc2 | 2037 | */ |
2038 | ||
b423cd4c | 2039 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2040 | sizeof(method), username, sizeof(username), host, | |
2041 | sizeof(host), &port, resource, sizeof(resource)); | |
2042 | ||
2043 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
ef416fc2 | 2044 | { |
b423cd4c | 2045 | /* |
2046 | * No, return an error... | |
2047 | */ | |
2048 | ||
ef416fc2 | 2049 | send_ipp_status(con, IPP_BAD_REQUEST, |
b423cd4c | 2050 | _("The printer-uri must be of the form " |
2051 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
ef416fc2 | 2052 | return; |
2053 | } | |
2054 | ||
2055 | /* | |
b423cd4c | 2056 | * Do we have a valid printer name? |
ef416fc2 | 2057 | */ |
2058 | ||
b423cd4c | 2059 | if (!validate_name(resource + 10)) |
ef416fc2 | 2060 | { |
b423cd4c | 2061 | /* |
2062 | * No, return an error... | |
2063 | */ | |
2064 | ||
2065 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2066 | _("The printer-uri \"%s\" contains invalid characters."), | |
2067 | uri->values[0].string.text); | |
2068 | return; | |
ef416fc2 | 2069 | } |
ef416fc2 | 2070 | |
2071 | /* | |
b423cd4c | 2072 | * Check policy... |
ef416fc2 | 2073 | */ |
2074 | ||
b423cd4c | 2075 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2076 | { | |
2077 | send_http_error(con, status); | |
2078 | return; | |
2079 | } | |
ef416fc2 | 2080 | |
2081 | /* | |
b423cd4c | 2082 | * See if the printer already exists; if not, create a new printer... |
2083 | */ | |
ef416fc2 | 2084 | |
b423cd4c | 2085 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) |
ef416fc2 | 2086 | { |
2087 | /* | |
b423cd4c | 2088 | * Printer doesn't exist; see if we have a class of the same name... |
ef416fc2 | 2089 | */ |
2090 | ||
b423cd4c | 2091 | if ((printer = cupsdFindClass(resource + 10)) != NULL && |
2092 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 2093 | { |
b423cd4c | 2094 | /* |
2095 | * Yes, return an error... | |
2096 | */ | |
ef416fc2 | 2097 | |
b423cd4c | 2098 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2099 | _("A class named \"%s\" already exists!"), | |
2100 | resource + 10); | |
ef416fc2 | 2101 | return; |
2102 | } | |
2103 | ||
2104 | /* | |
b423cd4c | 2105 | * No, add the printer... |
ef416fc2 | 2106 | */ |
2107 | ||
b423cd4c | 2108 | printer = cupsdAddPrinter(resource + 10); |
2109 | modify = 0; | |
ef416fc2 | 2110 | } |
b423cd4c | 2111 | else if (printer->type & CUPS_PRINTER_IMPLICIT) |
ef416fc2 | 2112 | { |
2113 | /* | |
b423cd4c | 2114 | * Rename the implicit printer to "AnyPrinter" or delete it... |
ef416fc2 | 2115 | */ |
2116 | ||
b423cd4c | 2117 | if (ImplicitAnyClasses) |
ef416fc2 | 2118 | { |
b423cd4c | 2119 | snprintf(newname, sizeof(newname), "Any%s", resource + 10); |
2120 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2121 | } |
b423cd4c | 2122 | else |
2123 | cupsdDeletePrinter(printer, 1); | |
ef416fc2 | 2124 | |
2125 | /* | |
b423cd4c | 2126 | * Add the printer as a new local printer... |
ef416fc2 | 2127 | */ |
2128 | ||
b423cd4c | 2129 | printer = cupsdAddPrinter(resource + 10); |
2130 | modify = 0; | |
ef416fc2 | 2131 | } |
b423cd4c | 2132 | else if (printer->type & CUPS_PRINTER_REMOTE) |
2133 | { | |
2134 | /* | |
2135 | * Rename the remote printer to "Printer@server"... | |
2136 | */ | |
ef416fc2 | 2137 | |
b423cd4c | 2138 | snprintf(newname, sizeof(newname), "%s@%s", resource + 10, |
2139 | printer->hostname); | |
2140 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2141 | |
b423cd4c | 2142 | /* |
2143 | * Add the printer as a new local printer... | |
2144 | */ | |
ef416fc2 | 2145 | |
b423cd4c | 2146 | printer = cupsdAddPrinter(resource + 10); |
2147 | modify = 0; | |
2148 | } | |
2149 | else | |
2150 | modify = 1; | |
ef416fc2 | 2151 | |
b423cd4c | 2152 | /* |
2153 | * Look for attributes and copy them over as needed... | |
2154 | */ | |
ef416fc2 | 2155 | |
b423cd4c | 2156 | need_restart_job = 0; |
ef416fc2 | 2157 | |
b423cd4c | 2158 | if ((attr = ippFindAttribute(con->request, "printer-location", |
2159 | IPP_TAG_TEXT)) != NULL) | |
2160 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
ef416fc2 | 2161 | |
b423cd4c | 2162 | if ((attr = ippFindAttribute(con->request, "printer-info", |
2163 | IPP_TAG_TEXT)) != NULL) | |
2164 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
ef416fc2 | 2165 | |
b423cd4c | 2166 | if ((attr = ippFindAttribute(con->request, "device-uri", |
2167 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 2168 | { |
2169 | /* | |
b423cd4c | 2170 | * Do we have a valid device URI? |
ef416fc2 | 2171 | */ |
2172 | ||
b423cd4c | 2173 | need_restart_job = 1; |
ef416fc2 | 2174 | |
b423cd4c | 2175 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, method, |
2176 | sizeof(method), username, sizeof(username), host, | |
2177 | sizeof(host), &port, resource, sizeof(resource)); | |
2178 | ||
2179 | if (!strcmp(method, "file")) | |
ef416fc2 | 2180 | { |
2181 | /* | |
b423cd4c | 2182 | * See if the administrator has enabled file devices... |
ef416fc2 | 2183 | */ |
2184 | ||
b423cd4c | 2185 | if (!FileDevice && strcmp(resource, "/dev/null")) |
ef416fc2 | 2186 | { |
2187 | /* | |
b423cd4c | 2188 | * File devices are disabled and the URL is not file:/dev/null... |
ef416fc2 | 2189 | */ |
2190 | ||
b423cd4c | 2191 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2192 | _("File device URIs have been disabled! " | |
2193 | "To enable, see the FileDevice directive in " | |
2194 | "\"%s/cupsd.conf\"."), | |
2195 | ServerRoot); | |
ef416fc2 | 2196 | return; |
2197 | } | |
b423cd4c | 2198 | } |
2199 | else | |
2200 | { | |
ef416fc2 | 2201 | /* |
b423cd4c | 2202 | * See if the backend exists and is executable... |
ef416fc2 | 2203 | */ |
2204 | ||
b423cd4c | 2205 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, method); |
2206 | if (access(srcfile, X_OK)) | |
ef416fc2 | 2207 | { |
2208 | /* | |
b423cd4c | 2209 | * Could not find device in list! |
ef416fc2 | 2210 | */ |
ef416fc2 | 2211 | |
b423cd4c | 2212 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), |
2213 | attr->values[0].string.text); | |
2214 | return; | |
ef416fc2 | 2215 | } |
2216 | } | |
ef416fc2 | 2217 | |
b423cd4c | 2218 | cupsdLogMessage(CUPSD_LOG_INFO, |
2219 | "Setting %s device-uri to \"%s\" (was \"%s\".)", | |
2220 | printer->name, | |
2221 | cupsdSanitizeURI(attr->values[0].string.text, line, | |
2222 | sizeof(line)), | |
2223 | cupsdSanitizeURI(printer->device_uri, resource, | |
2224 | sizeof(resource))); | |
ef416fc2 | 2225 | |
b423cd4c | 2226 | cupsdSetString(&printer->device_uri, attr->values[0].string.text); |
ef416fc2 | 2227 | } |
2228 | ||
b423cd4c | 2229 | if ((attr = ippFindAttribute(con->request, "port-monitor", |
2230 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 2231 | { |
b423cd4c | 2232 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ |
ef416fc2 | 2233 | |
ef416fc2 | 2234 | |
b423cd4c | 2235 | need_restart_job = 1; |
ef416fc2 | 2236 | |
b423cd4c | 2237 | supported = ippFindAttribute(printer->attrs, "port-monitor-supported", |
2238 | IPP_TAG_KEYWORD); | |
2239 | for (i = 0; i < supported->num_values; i ++) | |
2240 | if (!strcmp(supported->values[i].string.text, | |
2241 | attr->values[0].string.text)) | |
2242 | break; | |
ef416fc2 | 2243 | |
b423cd4c | 2244 | if (i >= supported->num_values) |
2245 | { | |
2246 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"), | |
2247 | attr->values[0].string.text); | |
2248 | return; | |
2249 | } | |
ef416fc2 | 2250 | |
b423cd4c | 2251 | cupsdLogMessage(CUPSD_LOG_INFO, |
2252 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2253 | printer->name, attr->values[0].string.text, | |
2254 | printer->port_monitor); | |
ef416fc2 | 2255 | |
b423cd4c | 2256 | if (strcmp(attr->values[0].string.text, "none")) |
2257 | cupsdSetString(&printer->port_monitor, attr->values[0].string.text); | |
2258 | else | |
2259 | cupsdClearString(&printer->port_monitor); | |
2260 | } | |
ef416fc2 | 2261 | |
b423cd4c | 2262 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
2263 | IPP_TAG_BOOLEAN)) != NULL) | |
2264 | { | |
2265 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2266 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
2267 | printer->name, attr->values[0].boolean, printer->accepting); | |
ef416fc2 | 2268 | |
b423cd4c | 2269 | printer->accepting = attr->values[0].boolean; |
2270 | cupsdAddPrinterHistory(printer); | |
2271 | } | |
bd7854cb | 2272 | |
b423cd4c | 2273 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
2274 | IPP_TAG_BOOLEAN)) != NULL) | |
2275 | { | |
2276 | if (printer->shared && !attr->values[0].boolean) | |
2277 | cupsdSendBrowseDelete(printer); | |
bd7854cb | 2278 | |
b423cd4c | 2279 | cupsdLogMessage(CUPSD_LOG_INFO, |
2280 | "Setting %s printer-is-shared to %d (was %d.)", | |
2281 | printer->name, attr->values[0].boolean, printer->shared); | |
bd7854cb | 2282 | |
b423cd4c | 2283 | printer->shared = attr->values[0].boolean; |
2284 | } | |
bd7854cb | 2285 | |
b423cd4c | 2286 | if ((attr = ippFindAttribute(con->request, "printer-state", |
2287 | IPP_TAG_ENUM)) != NULL) | |
bd7854cb | 2288 | { |
b423cd4c | 2289 | if (attr->values[0].integer != IPP_PRINTER_IDLE && |
2290 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
2291 | { | |
2292 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"), | |
2293 | attr->values[0].integer); | |
2294 | return; | |
2295 | } | |
bd7854cb | 2296 | |
b423cd4c | 2297 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", printer->name, |
2298 | attr->values[0].integer, printer->state); | |
2299 | ||
2300 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
2301 | cupsdStopPrinter(printer, 0); | |
2302 | else | |
2303 | { | |
2304 | need_restart_job = 1; | |
2305 | cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0); | |
2306 | } | |
2307 | } | |
2308 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
2309 | IPP_TAG_TEXT)) != NULL) | |
2310 | { | |
2311 | strlcpy(printer->state_message, attr->values[0].string.text, | |
2312 | sizeof(printer->state_message)); | |
2313 | cupsdAddPrinterHistory(printer); | |
bd7854cb | 2314 | } |
2315 | ||
b423cd4c | 2316 | set_printer_defaults(con, printer); |
2317 | ||
bd7854cb | 2318 | /* |
b423cd4c | 2319 | * See if we have all required attributes... |
bd7854cb | 2320 | */ |
2321 | ||
b423cd4c | 2322 | if (!printer->device_uri) |
2323 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
bd7854cb | 2324 | |
2325 | /* | |
b423cd4c | 2326 | * See if we have an interface script or PPD file attached to the request... |
bd7854cb | 2327 | */ |
2328 | ||
b423cd4c | 2329 | if (con->filename) |
2330 | { | |
2331 | need_restart_job = 1; | |
bd7854cb | 2332 | |
b423cd4c | 2333 | strlcpy(srcfile, con->filename, sizeof(srcfile)); |
ef416fc2 | 2334 | |
b423cd4c | 2335 | if ((fp = cupsFileOpen(srcfile, "rb"))) |
2336 | { | |
2337 | /* | |
2338 | * Yes; get the first line from it... | |
2339 | */ | |
ef416fc2 | 2340 | |
b423cd4c | 2341 | line[0] = '\0'; |
2342 | cupsFileGets(fp, line, sizeof(line)); | |
2343 | cupsFileClose(fp); | |
ef416fc2 | 2344 | |
b423cd4c | 2345 | /* |
2346 | * Then see what kind of file it is... | |
2347 | */ | |
ef416fc2 | 2348 | |
b423cd4c | 2349 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2350 | printer->name); | |
ef416fc2 | 2351 | |
b423cd4c | 2352 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2353 | { | |
2354 | /* | |
2355 | * The new file is a PPD file, so remove any old interface script | |
2356 | * that might be lying around... | |
2357 | */ | |
ef416fc2 | 2358 | |
b423cd4c | 2359 | unlink(dstfile); |
2360 | } | |
2361 | else | |
2362 | { | |
2363 | /* | |
2364 | * This must be an interface script, so move the file over to the | |
2365 | * interfaces directory and make it executable... | |
2366 | */ | |
ef416fc2 | 2367 | |
b423cd4c | 2368 | if (copy_file(srcfile, dstfile)) |
2369 | { | |
2370 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2371 | _("Unable to copy interface script - %s!"), | |
2372 | strerror(errno)); | |
2373 | return; | |
2374 | } | |
2375 | else | |
2376 | { | |
2377 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2378 | "Copied interface script successfully!"); | |
2379 | chmod(dstfile, 0755); | |
2380 | } | |
2381 | } | |
ef416fc2 | 2382 | |
b423cd4c | 2383 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2384 | printer->name); | |
ef416fc2 | 2385 | |
b423cd4c | 2386 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2387 | { | |
2388 | /* | |
2389 | * The new file is a PPD file, so move the file over to the | |
2390 | * ppd directory and make it readable by all... | |
2391 | */ | |
ef416fc2 | 2392 | |
b423cd4c | 2393 | if (copy_file(srcfile, dstfile)) |
2394 | { | |
2395 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2396 | _("Unable to copy PPD file - %s!"), | |
2397 | strerror(errno)); | |
2398 | return; | |
2399 | } | |
2400 | else | |
2401 | { | |
2402 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2403 | "Copied PPD file successfully!"); | |
2404 | chmod(dstfile, 0644); | |
2405 | } | |
2406 | } | |
2407 | else | |
2408 | { | |
2409 | /* | |
2410 | * This must be an interface script, so remove any old PPD file that | |
2411 | * may be lying around... | |
2412 | */ | |
ef416fc2 | 2413 | |
b423cd4c | 2414 | unlink(dstfile); |
2415 | } | |
2416 | } | |
2417 | } | |
2418 | else if ((attr = ippFindAttribute(con->request, "ppd-name", | |
2419 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 2420 | { |
b423cd4c | 2421 | need_restart_job = 1; |
ef416fc2 | 2422 | |
b423cd4c | 2423 | if (!strcmp(attr->values[0].string.text, "raw")) |
ef416fc2 | 2424 | { |
b423cd4c | 2425 | /* |
2426 | * Raw driver, remove any existing PPD or interface script files. | |
2427 | */ | |
ef416fc2 | 2428 | |
b423cd4c | 2429 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2430 | printer->name); | |
2431 | unlink(dstfile); | |
ef416fc2 | 2432 | |
b423cd4c | 2433 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2434 | printer->name); | |
2435 | unlink(dstfile); | |
2436 | } | |
2437 | else | |
ef416fc2 | 2438 | { |
b423cd4c | 2439 | /* |
2440 | * PPD model file... | |
2441 | */ | |
2442 | ||
2443 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2444 | printer->name); | |
2445 | unlink(dstfile); | |
2446 | ||
2447 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2448 | printer->name); | |
2449 | ||
2450 | if (copy_model(con, attr->values[0].string.text, dstfile)) | |
2451 | { | |
2452 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!")); | |
2453 | return; | |
2454 | } | |
2455 | else | |
2456 | { | |
2457 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2458 | "Copied PPD file successfully!"); | |
2459 | chmod(dstfile, 0644); | |
2460 | } | |
ef416fc2 | 2461 | } |
2462 | } | |
2463 | ||
2464 | /* | |
b423cd4c | 2465 | * Update the printer attributes and return... |
ef416fc2 | 2466 | */ |
2467 | ||
b423cd4c | 2468 | cupsdSetPrinterAttrs(printer); |
2469 | cupsdSaveAllPrinters(); | |
ef416fc2 | 2470 | |
b423cd4c | 2471 | if (need_restart_job && printer->job) |
ef416fc2 | 2472 | { |
b423cd4c | 2473 | cupsd_job_t *job; |
ef416fc2 | 2474 | |
b423cd4c | 2475 | /* |
2476 | * Stop the current job and then restart it below... | |
2477 | */ | |
ef416fc2 | 2478 | |
b423cd4c | 2479 | job = (cupsd_job_t *)printer->job; |
ef416fc2 | 2480 | |
b423cd4c | 2481 | cupsdStopJob(job, 1); |
2482 | ||
2483 | job->state->values[0].integer = IPP_JOB_PENDING; | |
2484 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 2485 | } |
2486 | ||
b423cd4c | 2487 | if (need_restart_job) |
2488 | cupsdCheckJobs(); | |
ef416fc2 | 2489 | |
b423cd4c | 2490 | cupsdWritePrintcap(); |
2491 | ||
2492 | if (modify) | |
ef416fc2 | 2493 | { |
b423cd4c | 2494 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
2495 | "Printer \"%s\" modified by \"%s\".", printer->name, | |
2496 | get_username(con)); | |
ef416fc2 | 2497 | |
b423cd4c | 2498 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", |
2499 | printer->name, get_username(con)); | |
ef416fc2 | 2500 | } |
b423cd4c | 2501 | else |
2502 | { | |
2503 | cupsdAddPrinterHistory(printer); | |
ef416fc2 | 2504 | |
b423cd4c | 2505 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL, |
2506 | "New printer \"%s\" added by \"%s\".", printer->name, | |
2507 | get_username(con)); | |
ef416fc2 | 2508 | |
b423cd4c | 2509 | cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".", |
2510 | printer->name, get_username(con)); | |
2511 | } | |
2512 | ||
2513 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 2514 | } |
2515 | ||
2516 | ||
2517 | /* | |
b423cd4c | 2518 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute |
2519 | * based upon the printer state... | |
ef416fc2 | 2520 | */ |
2521 | ||
b423cd4c | 2522 | static void |
2523 | add_printer_state_reasons( | |
2524 | cupsd_client_t *con, /* I - Client connection */ | |
2525 | cupsd_printer_t *p) /* I - Printer info */ | |
ef416fc2 | 2526 | { |
b423cd4c | 2527 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2528 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
2529 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2530 | |
b423cd4c | 2531 | if (p->num_reasons == 0) |
2532 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2533 | "printer-state-reasons", NULL, | |
2534 | p->state == IPP_PRINTER_STOPPED ? "paused" : "none"); | |
2535 | else | |
2536 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2537 | "printer-state-reasons", p->num_reasons, NULL, | |
2538 | (const char * const *)p->reasons); | |
2539 | } | |
ef416fc2 | 2540 | |
ef416fc2 | 2541 | |
b423cd4c | 2542 | /* |
2543 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2544 | * the specified printer or class. | |
2545 | */ | |
ef416fc2 | 2546 | |
b423cd4c | 2547 | static void |
2548 | add_queued_job_count( | |
2549 | cupsd_client_t *con, /* I - Client connection */ | |
2550 | cupsd_printer_t *p) /* I - Printer or class */ | |
2551 | { | |
2552 | int count; /* Number of jobs on destination */ | |
ef416fc2 | 2553 | |
ef416fc2 | 2554 | |
b423cd4c | 2555 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", |
2556 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2557 | |
b423cd4c | 2558 | count = cupsdGetPrinterJobCount(p->name); |
ef416fc2 | 2559 | |
b423cd4c | 2560 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, |
2561 | "queued-job-count", count); | |
2562 | } | |
ef416fc2 | 2563 | |
ef416fc2 | 2564 | |
b423cd4c | 2565 | /* |
2566 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
2567 | */ | |
ef416fc2 | 2568 | |
b423cd4c | 2569 | static void |
2570 | apply_printer_defaults( | |
2571 | cupsd_printer_t *printer, /* I - Printer */ | |
2572 | cupsd_job_t *job) /* I - Job */ | |
2573 | { | |
2574 | int i, /* Looping var */ | |
2575 | num_options; /* Number of default options */ | |
2576 | cups_option_t *options, /* Default options */ | |
2577 | *option; /* Current option */ | |
ef416fc2 | 2578 | |
ef416fc2 | 2579 | |
b423cd4c | 2580 | /* |
2581 | * Collect all of the default options and add the missing ones to the | |
2582 | * job object... | |
2583 | */ | |
ef416fc2 | 2584 | |
b423cd4c | 2585 | for (i = printer->num_options, num_options = 0, option = printer->options; |
2586 | i > 0; | |
2587 | i --, option ++) | |
2588 | if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO)) | |
2589 | { | |
2590 | num_options = cupsAddOption(option->name, option->value, num_options, | |
2591 | &options); | |
2592 | } | |
ef416fc2 | 2593 | |
b423cd4c | 2594 | /* |
2595 | * Encode these options as attributes in the job object... | |
2596 | */ | |
ef416fc2 | 2597 | |
b423cd4c | 2598 | cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB); |
2599 | cupsFreeOptions(num_options, options); | |
2600 | } | |
2601 | ||
2602 | ||
2603 | /* | |
2604 | * 'authenticate_job()' - Set job authentication info. | |
2605 | */ | |
2606 | ||
2607 | static void | |
2608 | authenticate_job(cupsd_client_t *con, /* I - Client connection */ | |
2609 | ipp_attribute_t *uri) /* I - Job URI */ | |
2610 | { | |
2611 | ipp_attribute_t *attr; /* Job-id attribute */ | |
2612 | int jobid; /* Job ID */ | |
2613 | cupsd_job_t *job; /* Current job */ | |
2614 | char method[HTTP_MAX_URI], | |
2615 | /* Method portion of URI */ | |
2616 | username[HTTP_MAX_URI], | |
2617 | /* Username portion of URI */ | |
2618 | host[HTTP_MAX_URI], | |
2619 | /* Host portion of URI */ | |
2620 | resource[HTTP_MAX_URI]; | |
2621 | /* Resource portion of URI */ | |
2622 | int port; /* Port portion of URI */ | |
ef416fc2 | 2623 | |
ef416fc2 | 2624 | |
b423cd4c | 2625 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", |
2626 | con, con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 2627 | |
b423cd4c | 2628 | /* |
2629 | * Start with "everything is OK" status... | |
2630 | */ | |
ef416fc2 | 2631 | |
b423cd4c | 2632 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 2633 | |
b423cd4c | 2634 | /* |
2635 | * See if we have a job URI or a printer URI... | |
2636 | */ | |
ef416fc2 | 2637 | |
b423cd4c | 2638 | if (!strcmp(uri->name, "printer-uri")) |
2639 | { | |
2640 | /* | |
2641 | * Got a printer URI; see if we also have a job-id attribute... | |
2642 | */ | |
ef416fc2 | 2643 | |
b423cd4c | 2644 | if ((attr = ippFindAttribute(con->request, "job-id", |
2645 | IPP_TAG_INTEGER)) == NULL) | |
2646 | { | |
2647 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2648 | _("Got a printer-uri attribute but no job-id!")); | |
2649 | return; | |
2650 | } | |
2651 | ||
2652 | jobid = attr->values[0].integer; | |
2653 | } | |
2654 | else | |
2655 | { | |
2656 | /* | |
2657 | * Got a job URI; parse it to get the job ID... | |
2658 | */ | |
ef416fc2 | 2659 | |
b423cd4c | 2660 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2661 | sizeof(method), username, sizeof(username), host, | |
2662 | sizeof(host), &port, resource, sizeof(resource)); | |
2663 | ||
2664 | if (strncmp(resource, "/jobs/", 6)) | |
2665 | { | |
2666 | /* | |
2667 | * Not a valid URI! | |
2668 | */ | |
ef416fc2 | 2669 | |
b423cd4c | 2670 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"), |
2671 | uri->values[0].string.text); | |
2672 | return; | |
2673 | } | |
ef416fc2 | 2674 | |
b423cd4c | 2675 | jobid = atoi(resource + 6); |
2676 | } | |
ef416fc2 | 2677 | |
b423cd4c | 2678 | /* |
2679 | * See if the job exists... | |
2680 | */ | |
ef416fc2 | 2681 | |
b423cd4c | 2682 | if ((job = cupsdFindJob(jobid)) == NULL) |
2683 | { | |
2684 | /* | |
2685 | * Nope - return a "not found" error... | |
2686 | */ | |
ef416fc2 | 2687 | |
b423cd4c | 2688 | send_ipp_status(con, IPP_NOT_FOUND, |
2689 | _("Job #%d does not exist!"), jobid); | |
ef416fc2 | 2690 | return; |
b423cd4c | 2691 | } |
ef416fc2 | 2692 | |
b423cd4c | 2693 | /* |
2694 | * See if the job has been completed... | |
2695 | */ | |
2696 | ||
2697 | if (job->state_value != IPP_JOB_HELD) | |
ef416fc2 | 2698 | { |
2699 | /* | |
b423cd4c | 2700 | * Return a "not-possible" error... |
ef416fc2 | 2701 | */ |
2702 | ||
b423cd4c | 2703 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2704 | _("Job #%d is not held for authentication!"), | |
2705 | jobid); | |
2706 | return; | |
2707 | } | |
ef416fc2 | 2708 | |
b423cd4c | 2709 | /* |
2710 | * See if we have already authenticated... | |
2711 | */ | |
2712 | ||
2713 | if (!con->username[0]) | |
2714 | { | |
2715 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
2716 | _("No authentication information provided!")); | |
2717 | return; | |
2718 | } | |
2719 | ||
2720 | /* | |
2721 | * See if the job is owned by the requesting user... | |
2722 | */ | |
2723 | ||
2724 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
2725 | { | |
2726 | send_http_error(con, HTTP_UNAUTHORIZED); | |
2727 | return; | |
2728 | } | |
2729 | ||
2730 | /* | |
2731 | * Save the authentication information for this job... | |
2732 | */ | |
2733 | ||
2734 | save_auth_info(con, job); | |
2735 | ||
2736 | /* | |
2737 | * Reset the job-hold-until value to "no-hold"... | |
2738 | */ | |
2739 | ||
2740 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
2741 | IPP_TAG_KEYWORD)) == NULL) | |
2742 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
2743 | ||
2744 | if (attr) | |
2745 | { | |
2746 | attr->value_tag = IPP_TAG_KEYWORD; | |
2747 | cupsdSetString(&(attr->values[0].string.text), "no-hold"); | |
ef416fc2 | 2748 | } |
b423cd4c | 2749 | |
2750 | /* | |
2751 | * Release the job and return... | |
2752 | */ | |
2753 | ||
2754 | cupsdReleaseJob(job); | |
2755 | ||
2756 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was authenticated by \"%s\".", jobid, | |
2757 | con->username); | |
ef416fc2 | 2758 | } |
2759 | ||
2760 | ||
2761 | /* | |
b423cd4c | 2762 | * 'cancel_all_jobs()' - Cancel all print jobs. |
ef416fc2 | 2763 | */ |
2764 | ||
b423cd4c | 2765 | static void |
2766 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
2767 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 2768 | { |
b423cd4c | 2769 | http_status_t status; /* Policy status */ |
f7faf1f5 | 2770 | const char *dest; /* Destination */ |
b423cd4c | 2771 | cups_ptype_t dtype; /* Destination type */ |
f7faf1f5 | 2772 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2773 | userpass[HTTP_MAX_URI], /* Username portion of URI */ |
f7faf1f5 | 2774 | host[HTTP_MAX_URI], /* Host portion of URI */ |
b423cd4c | 2775 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
2776 | int port; /* Port portion of URI */ | |
2777 | ipp_attribute_t *attr; /* Attribute in request */ | |
2778 | const char *username; /* Username */ | |
2779 | int purge; /* Purge? */ | |
2780 | cupsd_printer_t *printer; /* Printer */ | |
ef416fc2 | 2781 | |
2782 | ||
b423cd4c | 2783 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, |
2784 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 2785 | |
2786 | /* | |
b423cd4c | 2787 | * See if we have a printer URI... |
ef416fc2 | 2788 | */ |
2789 | ||
b423cd4c | 2790 | if (strcmp(uri->name, "printer-uri")) |
2791 | { | |
2792 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2793 | _("The printer-uri attribute is required!")); | |
2794 | return; | |
2795 | } | |
ef416fc2 | 2796 | |
2797 | /* | |
b423cd4c | 2798 | * Get the username (if any) for the jobs we want to cancel (only if |
2799 | * "my-jobs" is specified... | |
ef416fc2 | 2800 | */ |
2801 | ||
b423cd4c | 2802 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
2803 | IPP_TAG_BOOLEAN)) != NULL && | |
2804 | attr->values[0].boolean) | |
ef416fc2 | 2805 | { |
b423cd4c | 2806 | if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
2807 | IPP_TAG_NAME)) != NULL) | |
2808 | username = attr->values[0].string.text; | |
2809 | else | |
2810 | { | |
2811 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2812 | _("Missing requesting-user-name attribute!")); | |
2813 | return; | |
2814 | } | |
ef416fc2 | 2815 | } |
b423cd4c | 2816 | else |
2817 | username = NULL; | |
ef416fc2 | 2818 | |
b423cd4c | 2819 | /* |
2820 | * Look for the "purge-jobs" attribute... | |
2821 | */ | |
2822 | ||
2823 | if ((attr = ippFindAttribute(con->request, "purge-jobs", | |
2824 | IPP_TAG_BOOLEAN)) != NULL) | |
2825 | purge = attr->values[0].boolean; | |
2826 | else | |
2827 | purge = 1; | |
ef416fc2 | 2828 | |
2829 | /* | |
b423cd4c | 2830 | * And if the destination is valid... |
ef416fc2 | 2831 | */ |
2832 | ||
f7faf1f5 | 2833 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2834 | sizeof(method), userpass, sizeof(userpass), host, | |
2835 | sizeof(host), &port, resource, sizeof(resource)); | |
2836 | ||
2837 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 2838 | { |
2839 | /* | |
b423cd4c | 2840 | * Bad URI? |
ef416fc2 | 2841 | */ |
2842 | ||
b423cd4c | 2843 | if ((!strncmp(resource, "/printers/", 10) && resource[10]) || |
2844 | (!strncmp(resource, "/classes/", 9) && resource[9])) | |
2845 | { | |
2846 | send_ipp_status(con, IPP_NOT_FOUND, | |
2847 | _("The printer or class was not found.")); | |
2848 | return; | |
2849 | } | |
f7faf1f5 | 2850 | else if (strcmp(resource, "/printers/")) |
2851 | { | |
2852 | send_ipp_status(con, IPP_NOT_FOUND, | |
2853 | _("The printer-uri \"%s\" is not valid."), | |
2854 | uri->values[0].string.text); | |
2855 | return; | |
2856 | } | |
ef416fc2 | 2857 | |
b423cd4c | 2858 | /* |
2859 | * Check policy... | |
2860 | */ | |
2861 | ||
2862 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
2863 | { | |
2864 | send_http_error(con, status); | |
2865 | return; | |
2866 | } | |
ef416fc2 | 2867 | |
ef416fc2 | 2868 | /* |
b423cd4c | 2869 | * Cancel all jobs on all printers... |
ef416fc2 | 2870 | */ |
2871 | ||
b423cd4c | 2872 | cupsdCancelJobs(NULL, username, purge); |
ef416fc2 | 2873 | |
b423cd4c | 2874 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", |
2875 | purge ? "purged" : "cancelled", get_username(con)); | |
ef416fc2 | 2876 | } |
b423cd4c | 2877 | else |
ef416fc2 | 2878 | { |
2879 | /* | |
b423cd4c | 2880 | * Check policy... |
ef416fc2 | 2881 | */ |
2882 | ||
f7faf1f5 | 2883 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
b423cd4c | 2884 | { |
2885 | send_http_error(con, status); | |
2886 | return; | |
2887 | } | |
ef416fc2 | 2888 | |
b423cd4c | 2889 | /* |
2890 | * Cancel all of the jobs on the named printer... | |
2891 | */ | |
2892 | ||
f7faf1f5 | 2893 | cupsdCancelJobs(dest, username, purge); |
b423cd4c | 2894 | |
2895 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
f7faf1f5 | 2896 | dest, purge ? "purged" : "cancelled", get_username(con)); |
ef416fc2 | 2897 | } |
2898 | ||
b423cd4c | 2899 | con->response->request.status.status_code = IPP_OK; |
2900 | } | |
2901 | ||
2902 | ||
2903 | /* | |
2904 | * 'cancel_job()' - Cancel a print job. | |
2905 | */ | |
2906 | ||
2907 | static void | |
2908 | cancel_job(cupsd_client_t *con, /* I - Client connection */ | |
2909 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
2910 | { | |
2911 | ipp_attribute_t *attr; /* Current attribute */ | |
2912 | int jobid; /* Job ID */ | |
f7faf1f5 | 2913 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2914 | username[HTTP_MAX_URI], /* Username portion of URI */ |
2915 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2916 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2917 | int port; /* Port portion of URI */ | |
2918 | cupsd_job_t *job; /* Job information */ | |
f7faf1f5 | 2919 | const char *dest; /* Destination */ |
b423cd4c | 2920 | cups_ptype_t dtype; /* Destination type (printer or class) */ |
2921 | cupsd_printer_t *printer; /* Printer data */ | |
2922 | ||
2923 | ||
2924 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
2925 | con->http.fd, uri->values[0].string.text); | |
2926 | ||
ef416fc2 | 2927 | /* |
b423cd4c | 2928 | * See if we have a job URI or a printer URI... |
ef416fc2 | 2929 | */ |
2930 | ||
b423cd4c | 2931 | if (!strcmp(uri->name, "printer-uri")) |
2932 | { | |
2933 | /* | |
2934 | * Got a printer URI; see if we also have a job-id attribute... | |
2935 | */ | |
2936 | ||
2937 | if ((attr = ippFindAttribute(con->request, "job-id", | |
2938 | IPP_TAG_INTEGER)) == NULL) | |
2939 | { | |
2940 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2941 | _("Got a printer-uri attribute but no job-id!")); | |
2942 | return; | |
2943 | } | |
2944 | ||
2945 | if ((jobid = attr->values[0].integer) == 0) | |
ef416fc2 | 2946 | { |
2947 | /* | |
b423cd4c | 2948 | * Find the current job on the specified printer... |
ef416fc2 | 2949 | */ |
2950 | ||
f7faf1f5 | 2951 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2952 | sizeof(method), username, sizeof(username), host, | |
2953 | sizeof(host), &port, resource, sizeof(resource)); | |
2954 | ||
2955 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 2956 | { |
2957 | /* | |
b423cd4c | 2958 | * Bad URI... |
ef416fc2 | 2959 | */ |
2960 | ||
b423cd4c | 2961 | send_ipp_status(con, IPP_NOT_FOUND, |
2962 | _("The printer or class was not found.")); | |
2963 | return; | |
ef416fc2 | 2964 | } |
2965 | ||
2966 | /* | |
b423cd4c | 2967 | * See if the printer is currently printing a job... |
ef416fc2 | 2968 | */ |
2969 | ||
b423cd4c | 2970 | if (printer->job) |
2971 | jobid = ((cupsd_job_t *)printer->job)->id; | |
ef416fc2 | 2972 | else |
ef416fc2 | 2973 | { |
2974 | /* | |
b423cd4c | 2975 | * No, see if there are any pending jobs... |
ef416fc2 | 2976 | */ |
b423cd4c | 2977 | |
2978 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
2979 | job; | |
2980 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
2981 | if (job->state_value <= IPP_JOB_PROCESSING && | |
f7faf1f5 | 2982 | !strcasecmp(job->dest, dest)) |
b423cd4c | 2983 | break; |
ef416fc2 | 2984 | |
b423cd4c | 2985 | if (job) |
2986 | jobid = job->id; | |
2987 | else | |
ef416fc2 | 2988 | { |
b423cd4c | 2989 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"), |
f7faf1f5 | 2990 | dest); |
b423cd4c | 2991 | return; |
2992 | } | |
2993 | } | |
2994 | } | |
2995 | } | |
2996 | else | |
2997 | { | |
2998 | /* | |
2999 | * Got a job URI; parse it to get the job ID... | |
3000 | */ | |
ef416fc2 | 3001 | |
f7faf1f5 | 3002 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
3003 | sizeof(method), username, sizeof(username), host, | |
b423cd4c | 3004 | sizeof(host), &port, resource, sizeof(resource)); |
3005 | ||
3006 | if (strncmp(resource, "/jobs/", 6)) | |
3007 | { | |
3008 | /* | |
3009 | * Not a valid URI! | |
3010 | */ | |
ef416fc2 | 3011 | |
b423cd4c | 3012 | send_ipp_status(con, IPP_BAD_REQUEST, |
3013 | _("Bad job-uri attribute \"%s\"!"), | |
3014 | uri->values[0].string.text); | |
3015 | return; | |
3016 | } | |
3017 | ||
3018 | jobid = atoi(resource + 6); | |
3019 | } | |
3020 | ||
3021 | /* | |
3022 | * See if the job exists... | |
3023 | */ | |
3024 | ||
3025 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3026 | { | |
3027 | /* | |
3028 | * Nope - return a "not found" error... | |
3029 | */ | |
3030 | ||
3031 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
3032 | return; | |
3033 | } | |
3034 | ||
3035 | /* | |
3036 | * See if the job is owned by the requesting user... | |
3037 | */ | |
3038 | ||
3039 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3040 | { | |
3041 | send_http_error(con, HTTP_UNAUTHORIZED); | |
3042 | return; | |
3043 | } | |
3044 | ||
3045 | /* | |
3046 | * See if the job is already completed, cancelled, or aborted; if so, | |
3047 | * we can't cancel... | |
3048 | */ | |
3049 | ||
3050 | if (job->state_value >= IPP_JOB_CANCELLED) | |
3051 | { | |
e1d6a774 | 3052 | switch (job->state_value) |
3053 | { | |
3054 | case IPP_JOB_CANCELLED : | |
3055 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3056 | _("Job #%d is already cancelled - can\'t cancel."), | |
3057 | jobid); | |
3058 | break; | |
3059 | ||
3060 | case IPP_JOB_ABORTED : | |
3061 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3062 | _("Job #%d is already aborted - can\'t cancel."), | |
3063 | jobid); | |
3064 | break; | |
3065 | ||
3066 | default : | |
3067 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3068 | _("Job #%d is already completed - can\'t cancel."), | |
3069 | jobid); | |
3070 | break; | |
3071 | } | |
3072 | ||
b423cd4c | 3073 | return; |
3074 | } | |
ef416fc2 | 3075 | |
b423cd4c | 3076 | /* |
3077 | * Cancel the job and return... | |
3078 | */ | |
ef416fc2 | 3079 | |
b423cd4c | 3080 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3081 | "Job cancelled by \"%s\".", username); | |
ef416fc2 | 3082 | |
b423cd4c | 3083 | cupsdCancelJob(job, 0); |
3084 | cupsdCheckJobs(); | |
ef416fc2 | 3085 | |
b423cd4c | 3086 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was cancelled by \"%s\".", jobid, |
3087 | username); | |
ef416fc2 | 3088 | |
b423cd4c | 3089 | con->response->request.status.status_code = IPP_OK; |
3090 | } | |
ef416fc2 | 3091 | |
ef416fc2 | 3092 | |
b423cd4c | 3093 | /* |
3094 | * 'cancel_subscription()' - Cancel a subscription. | |
3095 | */ | |
ef416fc2 | 3096 | |
b423cd4c | 3097 | static void |
3098 | cancel_subscription( | |
3099 | cupsd_client_t *con, /* I - Client connection */ | |
3100 | int sub_id) /* I - Subscription ID */ | |
3101 | { | |
3102 | http_status_t status; /* Policy status */ | |
3103 | cupsd_subscription_t *sub; /* Subscription */ | |
ef416fc2 | 3104 | |
ef416fc2 | 3105 | |
b423cd4c | 3106 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3107 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
3108 | con, con->http.fd, sub_id); | |
ef416fc2 | 3109 | |
b423cd4c | 3110 | /* |
3111 | * Is the subscription ID valid? | |
3112 | */ | |
ef416fc2 | 3113 | |
b423cd4c | 3114 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
3115 | { | |
3116 | /* | |
3117 | * Bad subscription ID... | |
3118 | */ | |
ef416fc2 | 3119 | |
b423cd4c | 3120 | send_ipp_status(con, IPP_NOT_FOUND, |
3121 | _("notify-subscription-id %d no good!"), sub_id); | |
3122 | return; | |
3123 | } | |
ef416fc2 | 3124 | |
b423cd4c | 3125 | /* |
3126 | * Check policy... | |
3127 | */ | |
ef416fc2 | 3128 | |
b423cd4c | 3129 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
3130 | DefaultPolicyPtr, | |
3131 | con, sub->owner)) != HTTP_OK) | |
3132 | { | |
3133 | send_http_error(con, status); | |
3134 | return; | |
3135 | } | |
ef416fc2 | 3136 | |
b423cd4c | 3137 | /* |
3138 | * Cancel the subscription... | |
3139 | */ | |
ef416fc2 | 3140 | |
b423cd4c | 3141 | cupsdDeleteSubscription(sub, 1); |
ef416fc2 | 3142 | |
b423cd4c | 3143 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 3144 | } |
3145 | ||
3146 | ||
3147 | /* | |
b423cd4c | 3148 | * 'check_quotas()' - Check quotas for a printer and user. |
ef416fc2 | 3149 | */ |
3150 | ||
b423cd4c | 3151 | static int /* O - 1 if OK, 0 if not */ |
3152 | check_quotas(cupsd_client_t *con, /* I - Client connection */ | |
3153 | cupsd_printer_t *p) /* I - Printer or class */ | |
ef416fc2 | 3154 | { |
b423cd4c | 3155 | int i; /* Looping var */ |
3156 | char username[33]; /* Username */ | |
3157 | cupsd_quota_t *q; /* Quota data */ | |
3158 | struct passwd *pw; /* User password data */ | |
ef416fc2 | 3159 | |
3160 | ||
b423cd4c | 3161 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", |
3162 | con, con->http.fd, p, p->name); | |
ef416fc2 | 3163 | |
3164 | /* | |
b423cd4c | 3165 | * Check input... |
ef416fc2 | 3166 | */ |
3167 | ||
b423cd4c | 3168 | if (!con || !p) |
3169 | return (0); | |
ef416fc2 | 3170 | |
b423cd4c | 3171 | /* |
3172 | * Figure out who is printing... | |
3173 | */ | |
3174 | ||
3175 | strlcpy(username, get_username(con), sizeof(username)); | |
3176 | ||
3177 | /* | |
3178 | * Check global active job limits for printers and users... | |
3179 | */ | |
3180 | ||
3181 | if (MaxJobsPerPrinter) | |
ef416fc2 | 3182 | { |
b423cd4c | 3183 | /* |
3184 | * Check if there are too many pending jobs on this printer... | |
3185 | */ | |
3186 | ||
3187 | if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter) | |
3188 | { | |
3189 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...", | |
3190 | p->name); | |
3191 | return (0); | |
3192 | } | |
3193 | } | |
3194 | ||
3195 | if (MaxJobsPerUser) | |
3196 | { | |
3197 | /* | |
3198 | * Check if there are too many pending jobs for this user... | |
3199 | */ | |
3200 | ||
3201 | if (cupsdGetUserJobCount(username) >= MaxJobsPerUser) | |
3202 | { | |
3203 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...", | |
3204 | username); | |
3205 | return (0); | |
3206 | } | |
ef416fc2 | 3207 | } |
3208 | ||
3209 | /* | |
b423cd4c | 3210 | * Check against users... |
ef416fc2 | 3211 | */ |
3212 | ||
b423cd4c | 3213 | if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0) |
3214 | return (1); | |
3215 | ||
3216 | if (p->num_users) | |
3217 | { | |
3218 | pw = getpwnam(username); | |
3219 | endpwent(); | |
3220 | ||
3221 | for (i = 0; i < p->num_users; i ++) | |
3222 | if (p->users[i][0] == '@') | |
3223 | { | |
3224 | /* | |
3225 | * Check group membership... | |
3226 | */ | |
3227 | ||
3228 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
3229 | break; | |
3230 | } | |
3231 | else if (!strcasecmp(username, p->users[i])) | |
3232 | break; | |
3233 | ||
3234 | if ((i < p->num_users) == p->deny_users) | |
ef416fc2 | 3235 | { |
b423cd4c | 3236 | cupsdLogMessage(CUPSD_LOG_INFO, |
3237 | "Denying user \"%s\" access to printer \"%s\"...", | |
3238 | username, p->name); | |
3239 | return (0); | |
ef416fc2 | 3240 | } |
b423cd4c | 3241 | } |
ef416fc2 | 3242 | |
3243 | /* | |
b423cd4c | 3244 | * Check quotas... |
ef416fc2 | 3245 | */ |
3246 | ||
b423cd4c | 3247 | if (p->k_limit || p->page_limit) |
3248 | { | |
3249 | if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL) | |
3250 | { | |
3251 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3252 | "Unable to allocate quota data for user \"%s\"!", | |
3253 | username); | |
3254 | return (0); | |
3255 | } | |
ef416fc2 | 3256 | |
b423cd4c | 3257 | if ((q->k_count >= p->k_limit && p->k_limit) || |
3258 | (q->page_count >= p->page_limit && p->page_limit)) | |
3259 | { | |
3260 | cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...", | |
3261 | username); | |
3262 | return (0); | |
3263 | } | |
3264 | } | |
3265 | ||
3266 | /* | |
3267 | * If we have gotten this far, we're done! | |
3268 | */ | |
3269 | ||
3270 | return (1); | |
ef416fc2 | 3271 | } |
3272 | ||
3273 | ||
3274 | /* | |
b423cd4c | 3275 | * 'copy_attribute()' - Copy a single attribute. |
ef416fc2 | 3276 | */ |
3277 | ||
b423cd4c | 3278 | static ipp_attribute_t * /* O - New attribute */ |
3279 | copy_attribute( | |
3280 | ipp_t *to, /* O - Destination request/response */ | |
3281 | ipp_attribute_t *attr, /* I - Attribute to copy */ | |
3282 | int quickcopy) /* I - Do a quick copy? */ | |
ef416fc2 | 3283 | { |
b423cd4c | 3284 | int i; /* Looping var */ |
3285 | ipp_attribute_t *toattr; /* Destination attribute */ | |
ef416fc2 | 3286 | |
3287 | ||
3288 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b423cd4c | 3289 | "copy_attribute(%p, %p[%s,%x,%x])", to, attr, |
3290 | attr->name ? attr->name : "(null)", attr->group_tag, | |
3291 | attr->value_tag); | |
ef416fc2 | 3292 | |
b423cd4c | 3293 | switch (attr->value_tag & ~IPP_TAG_COPY) |
3294 | { | |
3295 | case IPP_TAG_ZERO : | |
3296 | toattr = ippAddSeparator(to); | |
3297 | break; | |
ef416fc2 | 3298 | |
b423cd4c | 3299 | case IPP_TAG_INTEGER : |
3300 | case IPP_TAG_ENUM : | |
3301 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
3302 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 3303 | |
b423cd4c | 3304 | for (i = 0; i < attr->num_values; i ++) |
3305 | toattr->values[i].integer = attr->values[i].integer; | |
3306 | break; | |
ef416fc2 | 3307 | |
b423cd4c | 3308 | case IPP_TAG_BOOLEAN : |
3309 | toattr = ippAddBooleans(to, attr->group_tag, attr->name, | |
3310 | attr->num_values, NULL); | |
ef416fc2 | 3311 | |
b423cd4c | 3312 | for (i = 0; i < attr->num_values; i ++) |
3313 | toattr->values[i].boolean = attr->values[i].boolean; | |
3314 | break; | |
ef416fc2 | 3315 | |
b423cd4c | 3316 | case IPP_TAG_STRING : |
3317 | case IPP_TAG_TEXT : | |
3318 | case IPP_TAG_NAME : | |
3319 | case IPP_TAG_KEYWORD : | |
3320 | case IPP_TAG_URI : | |
3321 | case IPP_TAG_URISCHEME : | |
3322 | case IPP_TAG_CHARSET : | |
3323 | case IPP_TAG_LANGUAGE : | |
3324 | case IPP_TAG_MIMETYPE : | |
3325 | toattr = ippAddStrings(to, attr->group_tag, | |
3326 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
3327 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 3328 | |
b423cd4c | 3329 | if (quickcopy) |
3330 | { | |
3331 | for (i = 0; i < attr->num_values; i ++) | |
3332 | toattr->values[i].string.text = attr->values[i].string.text; | |
3333 | } | |
3334 | else | |
3335 | { | |
3336 | for (i = 0; i < attr->num_values; i ++) | |
757d2cad | 3337 | toattr->values[i].string.text = _cupsStrAlloc(attr->values[i].string.text); |
b423cd4c | 3338 | } |
3339 | break; | |
ef416fc2 | 3340 | |
b423cd4c | 3341 | case IPP_TAG_DATE : |
3342 | toattr = ippAddDate(to, attr->group_tag, attr->name, | |
3343 | attr->values[0].date); | |
3344 | break; | |
ef416fc2 | 3345 | |
b423cd4c | 3346 | case IPP_TAG_RESOLUTION : |
3347 | toattr = ippAddResolutions(to, attr->group_tag, attr->name, | |
3348 | attr->num_values, IPP_RES_PER_INCH, | |
3349 | NULL, NULL); | |
ef416fc2 | 3350 | |
b423cd4c | 3351 | for (i = 0; i < attr->num_values; i ++) |
3352 | { | |
3353 | toattr->values[i].resolution.xres = attr->values[i].resolution.xres; | |
3354 | toattr->values[i].resolution.yres = attr->values[i].resolution.yres; | |
3355 | toattr->values[i].resolution.units = attr->values[i].resolution.units; | |
3356 | } | |
3357 | break; | |
ef416fc2 | 3358 | |
b423cd4c | 3359 | case IPP_TAG_RANGE : |
3360 | toattr = ippAddRanges(to, attr->group_tag, attr->name, | |
3361 | attr->num_values, NULL, NULL); | |
ef416fc2 | 3362 | |
b423cd4c | 3363 | for (i = 0; i < attr->num_values; i ++) |
3364 | { | |
3365 | toattr->values[i].range.lower = attr->values[i].range.lower; | |
3366 | toattr->values[i].range.upper = attr->values[i].range.upper; | |
3367 | } | |
3368 | break; | |
ef416fc2 | 3369 | |
b423cd4c | 3370 | case IPP_TAG_TEXTLANG : |
3371 | case IPP_TAG_NAMELANG : | |
3372 | toattr = ippAddStrings(to, attr->group_tag, | |
3373 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
3374 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 3375 | |
b423cd4c | 3376 | if (quickcopy) |
3377 | { | |
3378 | for (i = 0; i < attr->num_values; i ++) | |
3379 | { | |
3380 | toattr->values[i].string.charset = attr->values[i].string.charset; | |
3381 | toattr->values[i].string.text = attr->values[i].string.text; | |
3382 | } | |
3383 | } | |
3384 | else | |
3385 | { | |
3386 | for (i = 0; i < attr->num_values; i ++) | |
3387 | { | |
3388 | if (!i) | |
3389 | toattr->values[i].string.charset = | |
757d2cad | 3390 | _cupsStrAlloc(attr->values[i].string.charset); |
b423cd4c | 3391 | else |
3392 | toattr->values[i].string.charset = | |
3393 | toattr->values[0].string.charset; | |
ef416fc2 | 3394 | |
757d2cad | 3395 | toattr->values[i].string.text = _cupsStrAlloc(attr->values[i].string.text); |
b423cd4c | 3396 | } |
3397 | } | |
3398 | break; | |
ef416fc2 | 3399 | |
b423cd4c | 3400 | case IPP_TAG_BEGIN_COLLECTION : |
3401 | toattr = ippAddCollections(to, attr->group_tag, attr->name, | |
3402 | attr->num_values, NULL); | |
ef416fc2 | 3403 | |
b423cd4c | 3404 | for (i = 0; i < attr->num_values; i ++) |
3405 | { | |
3406 | toattr->values[i].collection = ippNew(); | |
3407 | copy_attrs(toattr->values[i].collection, attr->values[i].collection, | |
3408 | NULL, IPP_TAG_ZERO, 0); | |
3409 | } | |
ef416fc2 | 3410 | break; |
ef416fc2 | 3411 | |
b423cd4c | 3412 | default : |
3413 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
3414 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 3415 | |
b423cd4c | 3416 | for (i = 0; i < attr->num_values; i ++) |
3417 | { | |
3418 | toattr->values[i].unknown.length = attr->values[i].unknown.length; | |
ef416fc2 | 3419 | |
b423cd4c | 3420 | if (toattr->values[i].unknown.length > 0) |
3421 | { | |
3422 | if ((toattr->values[i].unknown.data = | |
3423 | malloc(toattr->values[i].unknown.length)) == NULL) | |
3424 | toattr->values[i].unknown.length = 0; | |
3425 | else | |
3426 | memcpy(toattr->values[i].unknown.data, | |
3427 | attr->values[i].unknown.data, | |
3428 | toattr->values[i].unknown.length); | |
3429 | } | |
3430 | } | |
3431 | break; /* anti-compiler-warning-code */ | |
3432 | } | |
ef416fc2 | 3433 | |
b423cd4c | 3434 | return (toattr); |
3435 | } | |
3436 | ||
3437 | ||
3438 | /* | |
3439 | * 'copy_attrs()' - Copy attributes from one request to another. | |
3440 | */ | |
3441 | ||
3442 | static void | |
3443 | copy_attrs(ipp_t *to, /* I - Destination request */ | |
3444 | ipp_t *from, /* I - Source request */ | |
3445 | cups_array_t *ra, /* I - Requested attributes */ | |
3446 | ipp_tag_t group, /* I - Group to copy */ | |
3447 | int quickcopy) /* I - Do a quick copy? */ | |
3448 | { | |
3449 | ipp_attribute_t *fromattr; /* Source attribute */ | |
ef416fc2 | 3450 | |
ef416fc2 | 3451 | |
b423cd4c | 3452 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3453 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
3454 | to, from, ra, group, quickcopy); | |
ef416fc2 | 3455 | |
b423cd4c | 3456 | if (!to || !from) |
3457 | return; | |
e00b005a | 3458 | |
b423cd4c | 3459 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) |
ef416fc2 | 3460 | { |
3461 | /* | |
b423cd4c | 3462 | * Filter attributes as needed... |
ef416fc2 | 3463 | */ |
3464 | ||
b423cd4c | 3465 | if (group != IPP_TAG_ZERO && fromattr->group_tag != group && |
3466 | fromattr->group_tag != IPP_TAG_ZERO && !fromattr->name) | |
3467 | continue; | |
3468 | ||
3469 | if (!ra || cupsArrayFind(ra, fromattr->name)) | |
3470 | copy_attribute(to, fromattr, quickcopy); | |
ef416fc2 | 3471 | } |
b423cd4c | 3472 | } |
ef416fc2 | 3473 | |
ef416fc2 | 3474 | |
b423cd4c | 3475 | /* |
3476 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
3477 | * specified job. | |
3478 | */ | |
ef416fc2 | 3479 | |
b423cd4c | 3480 | static int /* O - Size of banner file in kbytes */ |
3481 | copy_banner(cupsd_client_t *con, /* I - Client connection */ | |
3482 | cupsd_job_t *job, /* I - Job information */ | |
3483 | const char *name) /* I - Name of banner */ | |
3484 | { | |
3485 | int i; /* Looping var */ | |
3486 | int kbytes; /* Size of banner file in kbytes */ | |
3487 | char filename[1024]; /* Job filename */ | |
3488 | cupsd_banner_t *banner; /* Pointer to banner */ | |
3489 | cups_file_t *in; /* Input file */ | |
3490 | cups_file_t *out; /* Output file */ | |
3491 | int ch; /* Character from file */ | |
3492 | char attrname[255], /* Name of attribute */ | |
3493 | *s; /* Pointer into name */ | |
3494 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 3495 | |
ef416fc2 | 3496 | |
b423cd4c | 3497 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner(%p[%d], %p[%d], %s)", |
3498 | con, con->http.fd, job, job->id, name ? name : "(null)"); | |
ef416fc2 | 3499 | |
b423cd4c | 3500 | /* |
3501 | * Find the banner; return if not found or "none"... | |
3502 | */ | |
ef416fc2 | 3503 | |
b423cd4c | 3504 | if (!name || !strcmp(name, "none") || |
3505 | (banner = cupsdFindBanner(name)) == NULL) | |
3506 | return (0); | |
ef416fc2 | 3507 | |
b423cd4c | 3508 | /* |
3509 | * Open the banner and job files... | |
3510 | */ | |
ef416fc2 | 3511 | |
b423cd4c | 3512 | if (add_file(con, job, banner->filetype, 0)) |
3513 | return (0); | |
ef416fc2 | 3514 | |
b423cd4c | 3515 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, |
3516 | job->num_files); | |
3517 | if ((out = cupsFileOpen(filename, "w")) == NULL) | |
3518 | { | |
3519 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3520 | "copy_banner: Unable to create banner job file %s - %s", | |
3521 | filename, strerror(errno)); | |
3522 | job->num_files --; | |
3523 | return (0); | |
3524 | } | |
3525 | ||
3526 | fchmod(cupsFileNumber(out), 0640); | |
3527 | fchown(cupsFileNumber(out), RunUser, Group); | |
ef416fc2 | 3528 | |
3529 | /* | |
b423cd4c | 3530 | * Try the localized banner file under the subdirectory... |
ef416fc2 | 3531 | */ |
3532 | ||
80ca4592 | 3533 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, |
b423cd4c | 3534 | sizeof(attrname)); |
3535 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
ef416fc2 | 3536 | { |
3537 | /* | |
b423cd4c | 3538 | * Convert ll-cc to ll_CC... |
ef416fc2 | 3539 | */ |
3540 | ||
b423cd4c | 3541 | attrname[2] = '_'; |
3542 | attrname[3] = toupper(attrname[3] & 255); | |
3543 | attrname[4] = toupper(attrname[4] & 255); | |
3544 | } | |
ef416fc2 | 3545 | |
b423cd4c | 3546 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
3547 | attrname, name); | |
ef416fc2 | 3548 | |
b423cd4c | 3549 | if (access(filename, 0) && strlen(attrname) > 2) |
ef416fc2 | 3550 | { |
3551 | /* | |
b423cd4c | 3552 | * Wasn't able to find "ll_CC" locale file; try the non-national |
3553 | * localization banner directory. | |
ef416fc2 | 3554 | */ |
3555 | ||
b423cd4c | 3556 | attrname[2] = '\0'; |
ef416fc2 | 3557 | |
b423cd4c | 3558 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
3559 | attrname, name); | |
ef416fc2 | 3560 | } |
b423cd4c | 3561 | |
3562 | if (access(filename, 0)) | |
ef416fc2 | 3563 | { |
3564 | /* | |
b423cd4c | 3565 | * Use the non-localized banner file. |
ef416fc2 | 3566 | */ |
3567 | ||
b423cd4c | 3568 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); |
3569 | } | |
ef416fc2 | 3570 | |
b423cd4c | 3571 | if ((in = cupsFileOpen(filename, "r")) == NULL) |
3572 | { | |
3573 | cupsFileClose(out); | |
3574 | unlink(filename); | |
3575 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3576 | "copy_banner: Unable to open banner template file %s - %s", | |
3577 | filename, strerror(errno)); | |
3578 | job->num_files --; | |
3579 | return (0); | |
ef416fc2 | 3580 | } |
3581 | ||
3582 | /* | |
b423cd4c | 3583 | * Parse the file to the end... |
ef416fc2 | 3584 | */ |
3585 | ||
b423cd4c | 3586 | while ((ch = cupsFileGetChar(in)) != EOF) |
3587 | if (ch == '{') | |
3588 | { | |
3589 | /* | |
3590 | * Get an attribute name... | |
3591 | */ | |
ef416fc2 | 3592 | |
b423cd4c | 3593 | for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;) |
3594 | if (!isalpha(ch & 255) && ch != '-' && ch != '?') | |
3595 | break; | |
3596 | else if (s < (attrname + sizeof(attrname) - 1)) | |
3597 | *s++ = ch; | |
3598 | else | |
3599 | break; | |
ef416fc2 | 3600 | |
b423cd4c | 3601 | *s = '\0'; |
3602 | ||
3603 | if (ch != '}') | |
3604 | { | |
3605 | /* | |
3606 | * Ignore { followed by stuff that is not an attribute name... | |
3607 | */ | |
3608 | ||
3609 | cupsFilePrintf(out, "{%s%c", attrname, ch); | |
3610 | continue; | |
3611 | } | |
ef416fc2 | 3612 | |
ef416fc2 | 3613 | /* |
b423cd4c | 3614 | * See if it is defined... |
ef416fc2 | 3615 | */ |
3616 | ||
b423cd4c | 3617 | if (attrname[0] == '?') |
3618 | s = attrname + 1; | |
3619 | else | |
3620 | s = attrname; | |
ef416fc2 | 3621 | |
b423cd4c | 3622 | if (!strcmp(s, "printer-name")) |
3623 | { | |
3624 | cupsFilePuts(out, job->dest); | |
3625 | continue; | |
ef416fc2 | 3626 | } |
b423cd4c | 3627 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) |
3628 | { | |
3629 | /* | |
3630 | * See if we have a leading question mark... | |
3631 | */ | |
ef416fc2 | 3632 | |
b423cd4c | 3633 | if (attrname[0] != '?') |
3634 | { | |
3635 | /* | |
3636 | * Nope, write to file as-is; probably a PostScript procedure... | |
3637 | */ | |
ef416fc2 | 3638 | |
b423cd4c | 3639 | cupsFilePrintf(out, "{%s}", attrname); |
3640 | } | |
ef416fc2 | 3641 | |
b423cd4c | 3642 | continue; |
3643 | } | |
ef416fc2 | 3644 | |
b423cd4c | 3645 | /* |
3646 | * Output value(s)... | |
3647 | */ | |
ef416fc2 | 3648 | |
b423cd4c | 3649 | for (i = 0; i < attr->num_values; i ++) |
3650 | { | |
3651 | if (i) | |
3652 | cupsFilePutChar(out, ','); | |
ef416fc2 | 3653 | |
b423cd4c | 3654 | switch (attr->value_tag) |
3655 | { | |
3656 | case IPP_TAG_INTEGER : | |
3657 | case IPP_TAG_ENUM : | |
3658 | if (!strncmp(s, "time-at-", 8)) | |
3659 | cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer)); | |
3660 | else | |
3661 | cupsFilePrintf(out, "%d", attr->values[i].integer); | |
3662 | break; | |
ef416fc2 | 3663 | |
b423cd4c | 3664 | case IPP_TAG_BOOLEAN : |
3665 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
3666 | break; | |
ef416fc2 | 3667 | |
b423cd4c | 3668 | case IPP_TAG_NOVALUE : |
3669 | cupsFilePuts(out, "novalue"); | |
3670 | break; | |
ef416fc2 | 3671 | |
b423cd4c | 3672 | case IPP_TAG_RANGE : |
3673 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
3674 | attr->values[i].range.upper); | |
3675 | break; | |
fa73b229 | 3676 | |
b423cd4c | 3677 | case IPP_TAG_RESOLUTION : |
3678 | cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres, | |
3679 | attr->values[i].resolution.yres, | |
3680 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
3681 | "dpi" : "dpc"); | |
3682 | break; | |
fa73b229 | 3683 | |
b423cd4c | 3684 | case IPP_TAG_URI : |
3685 | case IPP_TAG_STRING : | |
3686 | case IPP_TAG_TEXT : | |
3687 | case IPP_TAG_NAME : | |
3688 | case IPP_TAG_KEYWORD : | |
3689 | case IPP_TAG_CHARSET : | |
3690 | case IPP_TAG_LANGUAGE : | |
3691 | if (!strcasecmp(banner->filetype->type, "postscript")) | |
3692 | { | |
3693 | /* | |
3694 | * Need to quote strings for PS banners... | |
3695 | */ | |
fa73b229 | 3696 | |
b423cd4c | 3697 | const char *p; |
fa73b229 | 3698 | |
b423cd4c | 3699 | for (p = attr->values[i].string.text; *p; p ++) |
3700 | { | |
3701 | if (*p == '(' || *p == ')' || *p == '\\') | |
3702 | { | |
3703 | cupsFilePutChar(out, '\\'); | |
3704 | cupsFilePutChar(out, *p); | |
3705 | } | |
3706 | else if (*p < 32 || *p > 126) | |
3707 | cupsFilePrintf(out, "\\%03o", *p & 255); | |
3708 | else | |
3709 | cupsFilePutChar(out, *p); | |
3710 | } | |
3711 | } | |
3712 | else | |
3713 | cupsFilePuts(out, attr->values[i].string.text); | |
3714 | break; | |
fa73b229 | 3715 | |
b423cd4c | 3716 | default : |
3717 | break; /* anti-compiler-warning-code */ | |
3718 | } | |
3719 | } | |
3720 | } | |
3721 | else if (ch == '\\') /* Quoted char */ | |
3722 | { | |
3723 | ch = cupsFileGetChar(in); | |
fa73b229 | 3724 | |
b423cd4c | 3725 | if (ch != '{') /* Only do special handling for \{ */ |
3726 | cupsFilePutChar(out, '\\'); | |
fa73b229 | 3727 | |
b423cd4c | 3728 | cupsFilePutChar(out, ch); |
3729 | } | |
3730 | else | |
3731 | cupsFilePutChar(out, ch); | |
fa73b229 | 3732 | |
b423cd4c | 3733 | cupsFileClose(in); |
fa73b229 | 3734 | |
b423cd4c | 3735 | kbytes = (cupsFileTell(out) + 1023) / 1024; |
fa73b229 | 3736 | |
b423cd4c | 3737 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
3738 | IPP_TAG_INTEGER)) != NULL) | |
3739 | attr->values[0].integer += kbytes; | |
3740 | ||
3741 | cupsFileClose(out); | |
3742 | ||
3743 | return (kbytes); | |
fa73b229 | 3744 | } |
3745 | ||
3746 | ||
3747 | /* | |
b423cd4c | 3748 | * 'copy_file()' - Copy a PPD file or interface script... |
fa73b229 | 3749 | */ |
3750 | ||
b423cd4c | 3751 | static int /* O - 0 = success, -1 = error */ |
3752 | copy_file(const char *from, /* I - Source file */ | |
3753 | const char *to) /* I - Destination file */ | |
fa73b229 | 3754 | { |
b423cd4c | 3755 | cups_file_t *src, /* Source file */ |
3756 | *dst; /* Destination file */ | |
3757 | int bytes; /* Bytes to read/write */ | |
3758 | char buffer[2048]; /* Copy buffer */ | |
fa73b229 | 3759 | |
3760 | ||
b423cd4c | 3761 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); |
3762 | ||
fa73b229 | 3763 | /* |
b423cd4c | 3764 | * Open the source and destination file for a copy... |
fa73b229 | 3765 | */ |
3766 | ||
b423cd4c | 3767 | if ((src = cupsFileOpen(from, "rb")) == NULL) |
3768 | return (-1); | |
fa73b229 | 3769 | |
b423cd4c | 3770 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
3771 | { | |
3772 | cupsFileClose(src); | |
3773 | return (-1); | |
3774 | } | |
fa73b229 | 3775 | |
b423cd4c | 3776 | /* |
3777 | * Copy the source file to the destination... | |
3778 | */ | |
fa73b229 | 3779 | |
b423cd4c | 3780 | while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0) |
3781 | if (cupsFileWrite(dst, buffer, bytes) < bytes) | |
3782 | { | |
3783 | cupsFileClose(src); | |
3784 | cupsFileClose(dst); | |
3785 | return (-1); | |
3786 | } | |
fa73b229 | 3787 | |
b423cd4c | 3788 | /* |
3789 | * Close both files and return... | |
3790 | */ | |
fa73b229 | 3791 | |
b423cd4c | 3792 | cupsFileClose(src); |
fa73b229 | 3793 | |
b423cd4c | 3794 | return (cupsFileClose(dst)); |
3795 | } | |
fa73b229 | 3796 | |
fa73b229 | 3797 | |
b423cd4c | 3798 | /* |
3799 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
3800 | * as needed... | |
3801 | */ | |
e00b005a | 3802 | |
b423cd4c | 3803 | static int /* O - 0 = success, -1 = error */ |
3804 | copy_model(cupsd_client_t *con, /* I - Client connection */ | |
3805 | const char *from, /* I - Source file */ | |
3806 | const char *to) /* I - Destination file */ | |
3807 | { | |
3808 | fd_set *input; /* select() input set */ | |
3809 | struct timeval timeout; /* select() timeout */ | |
3810 | int maxfd; /* Maximum file descriptor for select() */ | |
3811 | char tempfile[1024]; /* Temporary PPD file */ | |
3812 | int tempfd; /* Temporary PPD file descriptor */ | |
3813 | int temppid; /* Process ID of cups-driverd */ | |
3814 | int temppipe[2]; /* Temporary pipes */ | |
3815 | char *argv[4], /* Command-line arguments */ | |
3816 | *envp[MAX_ENV]; /* Environment */ | |
3817 | cups_file_t *src, /* Source file */ | |
3818 | *dst; /* Destination file */ | |
3819 | int bytes, /* Bytes from pipe */ | |
3820 | total; /* Total bytes from pipe */ | |
3821 | char buffer[2048], /* Copy buffer */ | |
3822 | *ptr; /* Pointer into buffer */ | |
3823 | int i; /* Looping var */ | |
3824 | char option[PPD_MAX_NAME], /* Option name */ | |
3825 | choice[PPD_MAX_NAME]; /* Choice name */ | |
3826 | int num_defaults; /* Number of default options */ | |
3827 | ppd_default_t *defaults; /* Default options */ | |
3828 | char cups_protocol[PPD_MAX_LINE]; | |
3829 | /* cupsProtocol attribute */ | |
3830 | int have_letter, /* Have Letter size */ | |
3831 | have_a4; /* Have A4 size */ | |
3832 | #ifdef HAVE_LIBPAPER | |
3833 | char *paper_result; /* Paper size name from libpaper */ | |
3834 | char system_paper[64]; /* Paper size name buffer */ | |
3835 | #endif /* HAVE_LIBPAPER */ | |
e00b005a | 3836 | |
e00b005a | 3837 | |
b423cd4c | 3838 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3839 | "copy_model(con=%p, from=\"%s\", to=\"%s\")", | |
3840 | con, from, to); | |
e00b005a | 3841 | |
b423cd4c | 3842 | /* |
3843 | * Run cups-driverd to get the PPD file... | |
3844 | */ | |
e00b005a | 3845 | |
b423cd4c | 3846 | argv[0] = "cups-driverd"; |
3847 | argv[1] = "cat"; | |
3848 | argv[2] = (char *)from; | |
3849 | argv[3] = NULL; | |
e00b005a | 3850 | |
b423cd4c | 3851 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
e00b005a | 3852 | |
b423cd4c | 3853 | snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin); |
3854 | snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd); | |
3855 | tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
3856 | if (tempfd < 0) | |
3857 | return (-1); | |
3858 | ||
3859 | cupsdOpenPipe(temppipe); | |
3860 | ||
3861 | if ((input = calloc(1, SetSize)) == NULL) | |
3862 | { | |
3863 | close(tempfd); | |
3864 | unlink(tempfile); | |
3865 | ||
3866 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3867 | "copy_model: Unable to allocate %d bytes for select()...", | |
3868 | SetSize); | |
3869 | return (-1); | |
e00b005a | 3870 | } |
3871 | ||
b423cd4c | 3872 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
3873 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
fa73b229 | 3874 | |
b423cd4c | 3875 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], |
3876 | -1, 0, &temppid)) | |
fa73b229 | 3877 | { |
b423cd4c | 3878 | free(input); |
3879 | close(tempfd); | |
3880 | unlink(tempfile); | |
3881 | return (-1); | |
fa73b229 | 3882 | } |
3883 | ||
b423cd4c | 3884 | close(temppipe[1]); |
fa73b229 | 3885 | |
b423cd4c | 3886 | /* |
3887 | * Wait up to 30 seconds for the PPD file to be copied... | |
3888 | */ | |
fa73b229 | 3889 | |
b423cd4c | 3890 | total = 0; |
fa73b229 | 3891 | |
b423cd4c | 3892 | if (temppipe[0] > CGIPipes[0]) |
3893 | maxfd = temppipe[0] + 1; | |
3894 | else | |
3895 | maxfd = CGIPipes[0] + 1; | |
ef416fc2 | 3896 | |
b423cd4c | 3897 | for (;;) |
3898 | { | |
3899 | /* | |
3900 | * See if we have data ready... | |
3901 | */ | |
ef416fc2 | 3902 | |
b423cd4c | 3903 | bytes = 0; |
ef416fc2 | 3904 | |
b423cd4c | 3905 | FD_SET(temppipe[0], input); |
3906 | FD_SET(CGIPipes[0], input); | |
ef416fc2 | 3907 | |
b423cd4c | 3908 | timeout.tv_sec = 30; |
3909 | timeout.tv_usec = 0; | |
3910 | ||
3911 | if ((i = select(maxfd, input, NULL, NULL, &timeout)) < 0) | |
3912 | { | |
3913 | if (errno == EINTR) | |
3914 | continue; | |
3915 | else | |
3916 | break; | |
3917 | } | |
3918 | else if (i == 0) | |
ef416fc2 | 3919 | { |
3920 | /* | |
b423cd4c | 3921 | * We have timed out... |
ef416fc2 | 3922 | */ |
3923 | ||
b423cd4c | 3924 | break; |
ef416fc2 | 3925 | } |
b423cd4c | 3926 | |
3927 | if (FD_ISSET(temppipe[0], input)) | |
ef416fc2 | 3928 | { |
3929 | /* | |
b423cd4c | 3930 | * Read the PPD file from the pipe, and write it to the PPD file. |
ef416fc2 | 3931 | */ |
3932 | ||
b423cd4c | 3933 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) |
3934 | { | |
3935 | if (write(tempfd, buffer, bytes) < bytes) | |
3936 | break; | |
ef416fc2 | 3937 | |
b423cd4c | 3938 | total += bytes; |
3939 | } | |
3940 | else | |
3941 | break; | |
ef416fc2 | 3942 | } |
b423cd4c | 3943 | |
3944 | if (FD_ISSET(CGIPipes[0], input)) | |
3945 | cupsdUpdateCGI(); | |
ef416fc2 | 3946 | } |
3947 | ||
b423cd4c | 3948 | close(temppipe[0]); |
3949 | close(tempfd); | |
ef416fc2 | 3950 | |
b423cd4c | 3951 | free(input); |
ef416fc2 | 3952 | |
b423cd4c | 3953 | if (!total) |
ef416fc2 | 3954 | { |
b423cd4c | 3955 | /* |
3956 | * No data from cups-deviced... | |
3957 | */ | |
3958 | ||
3959 | cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!"); | |
3960 | unlink(tempfile); | |
3961 | return (-1); | |
ef416fc2 | 3962 | } |
3963 | ||
b423cd4c | 3964 | /* |
3965 | * Read the source file and see what page sizes are supported... | |
3966 | */ | |
fa73b229 | 3967 | |
b423cd4c | 3968 | if ((src = cupsFileOpen(tempfile, "rb")) == NULL) |
3969 | { | |
3970 | unlink(tempfile); | |
3971 | return (-1); | |
3972 | } | |
fa73b229 | 3973 | |
b423cd4c | 3974 | have_letter = 0; |
3975 | have_a4 = 0; | |
fa73b229 | 3976 | |
b423cd4c | 3977 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
3978 | if (!strncmp(buffer, "*PageSize ", 10)) | |
3979 | { | |
3980 | /* | |
3981 | * Strip UI text and command data from the end of the line... | |
3982 | */ | |
fa73b229 | 3983 | |
b423cd4c | 3984 | if ((ptr = strchr(buffer + 10, '/')) != NULL) |
3985 | *ptr = '\0'; | |
3986 | if ((ptr = strchr(buffer + 10, ':')) != NULL) | |
3987 | *ptr = '\0'; | |
ef416fc2 | 3988 | |
b423cd4c | 3989 | for (ptr = buffer + 10; isspace(*ptr); ptr ++); |
ef416fc2 | 3990 | |
b423cd4c | 3991 | /* |
3992 | * Look for Letter and A4 page sizes... | |
3993 | */ | |
ef416fc2 | 3994 | |
b423cd4c | 3995 | if (!strcmp(ptr, "Letter")) |
3996 | have_letter = 1; | |
ef416fc2 | 3997 | |
b423cd4c | 3998 | if (!strcmp(ptr, "A4")) |
3999 | have_a4 = 1; | |
4000 | } | |
ef416fc2 | 4001 | |
b423cd4c | 4002 | cupsFileRewind(src); |
ef416fc2 | 4003 | |
4004 | /* | |
b423cd4c | 4005 | * Open the destination (if possible) and set the default options... |
ef416fc2 | 4006 | */ |
4007 | ||
b423cd4c | 4008 | num_defaults = 0; |
4009 | defaults = NULL; | |
4010 | cups_protocol[0] = '\0'; | |
ef416fc2 | 4011 | |
b423cd4c | 4012 | if ((dst = cupsFileOpen(to, "rb")) != NULL) |
ef416fc2 | 4013 | { |
4014 | /* | |
b423cd4c | 4015 | * Read all of the default lines from the old PPD... |
ef416fc2 | 4016 | */ |
4017 | ||
b423cd4c | 4018 | while (cupsFileGets(dst, buffer, sizeof(buffer))) |
4019 | if (!strncmp(buffer, "*Default", 8)) | |
4020 | { | |
4021 | /* | |
4022 | * Add the default option... | |
4023 | */ | |
ef416fc2 | 4024 | |
b423cd4c | 4025 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4026 | choice, sizeof(choice))) | |
4027 | num_defaults = ppd_add_default(option, choice, num_defaults, | |
4028 | &defaults); | |
4029 | } | |
4030 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
4031 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
ef416fc2 | 4032 | |
b423cd4c | 4033 | cupsFileClose(dst); |
ef416fc2 | 4034 | } |
b423cd4c | 4035 | #ifdef HAVE_LIBPAPER |
4036 | else if ((paper_result = systempapername()) != NULL) | |
4037 | { | |
4038 | /* | |
4039 | * Set the default media sizes from the systemwide default... | |
4040 | */ | |
ef416fc2 | 4041 | |
b423cd4c | 4042 | strlcpy(system_paper, paper_result, sizeof(system_paper)); |
4043 | system_paper[0] = toupper(system_paper[0] & 255); | |
ef416fc2 | 4044 | |
b423cd4c | 4045 | if ((!strcmp(system_paper, "Letter") && have_letter) || |
4046 | (!strcmp(system_paper, "A4") && have_a4)) | |
4047 | { | |
4048 | num_defaults = ppd_add_default("PageSize", system_paper, | |
4049 | num_defaults, &defaults); | |
4050 | num_defaults = ppd_add_default("PageRegion", system_paper, | |
4051 | num_defaults, &defaults); | |
4052 | num_defaults = ppd_add_default("PaperDimension", system_paper, | |
4053 | num_defaults, &defaults); | |
4054 | num_defaults = ppd_add_default("ImageableArea", system_paper, | |
4055 | num_defaults, &defaults); | |
4056 | } | |
ef416fc2 | 4057 | } |
b423cd4c | 4058 | #endif /* HAVE_LIBPAPER */ |
4059 | else | |
ef416fc2 | 4060 | { |
b423cd4c | 4061 | /* |
4062 | * Add the default media sizes... | |
4063 | * | |
4064 | * Note: These values are generally not valid for large-format devices | |
4065 | * like plotters, however it is probably safe to say that those | |
4066 | * users will configure the media size after initially adding | |
4067 | * the device anyways... | |
4068 | */ | |
4069 | ||
4070 | if (!DefaultLanguage || | |
4071 | !strcasecmp(DefaultLanguage, "C") || | |
4072 | !strcasecmp(DefaultLanguage, "POSIX") || | |
4073 | !strcasecmp(DefaultLanguage, "en") || | |
4074 | !strncasecmp(DefaultLanguage, "en_US", 5) || | |
4075 | !strncasecmp(DefaultLanguage, "en_CA", 5) || | |
4076 | !strncasecmp(DefaultLanguage, "fr_CA", 5)) | |
4077 | { | |
4078 | /* | |
4079 | * These are the only locales that will default to "letter" size... | |
4080 | */ | |
4081 | ||
4082 | if (have_letter) | |
4083 | { | |
4084 | num_defaults = ppd_add_default("PageSize", "Letter", num_defaults, | |
4085 | &defaults); | |
4086 | num_defaults = ppd_add_default("PageRegion", "Letter", num_defaults, | |
4087 | &defaults); | |
4088 | num_defaults = ppd_add_default("PaperDimension", "Letter", num_defaults, | |
4089 | &defaults); | |
4090 | num_defaults = ppd_add_default("ImageableArea", "Letter", num_defaults, | |
4091 | &defaults); | |
4092 | } | |
4093 | } | |
4094 | else if (have_a4) | |
4095 | { | |
4096 | /* | |
4097 | * The rest default to "a4" size... | |
4098 | */ | |
4099 | ||
4100 | num_defaults = ppd_add_default("PageSize", "A4", num_defaults, | |
4101 | &defaults); | |
4102 | num_defaults = ppd_add_default("PageRegion", "A4", num_defaults, | |
4103 | &defaults); | |
4104 | num_defaults = ppd_add_default("PaperDimension", "A4", num_defaults, | |
4105 | &defaults); | |
4106 | num_defaults = ppd_add_default("ImageableArea", "A4", num_defaults, | |
4107 | &defaults); | |
4108 | } | |
ef416fc2 | 4109 | } |
4110 | ||
4111 | /* | |
b423cd4c | 4112 | * Open the destination file for a copy... |
ef416fc2 | 4113 | */ |
4114 | ||
b423cd4c | 4115 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
ef416fc2 | 4116 | { |
b423cd4c | 4117 | if (num_defaults > 0) |
4118 | free(defaults); | |
4119 | ||
4120 | cupsFileClose(src); | |
4121 | unlink(tempfile); | |
4122 | return (-1); | |
ef416fc2 | 4123 | } |
4124 | ||
4125 | /* | |
b423cd4c | 4126 | * Copy the source file to the destination... |
ef416fc2 | 4127 | */ |
4128 | ||
b423cd4c | 4129 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
ef416fc2 | 4130 | { |
b423cd4c | 4131 | if (!strncmp(buffer, "*Default", 8)) |
ef416fc2 | 4132 | { |
b423cd4c | 4133 | /* |
4134 | * Check for an previous default option choice... | |
4135 | */ | |
ef416fc2 | 4136 | |
b423cd4c | 4137 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4138 | choice, sizeof(choice))) | |
ef416fc2 | 4139 | { |
b423cd4c | 4140 | for (i = 0; i < num_defaults; i ++) |
4141 | if (!strcmp(option, defaults[i].option)) | |
4142 | { | |
4143 | /* | |
4144 | * Substitute the previous choice... | |
4145 | */ | |
ef416fc2 | 4146 | |
b423cd4c | 4147 | snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, |
4148 | defaults[i].choice); | |
4149 | break; | |
4150 | } | |
4151 | } | |
ef416fc2 | 4152 | } |
b423cd4c | 4153 | |
4154 | cupsFilePrintf(dst, "%s\n", buffer); | |
ef416fc2 | 4155 | } |
4156 | ||
b423cd4c | 4157 | if (cups_protocol[0]) |
4158 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
4159 | ||
4160 | if (num_defaults > 0) | |
4161 | free(defaults); | |
4162 | ||
ef416fc2 | 4163 | /* |
b423cd4c | 4164 | * Close both files and return... |
ef416fc2 | 4165 | */ |
4166 | ||
b423cd4c | 4167 | cupsFileClose(src); |
4168 | ||
4169 | unlink(tempfile); | |
4170 | ||
4171 | return (cupsFileClose(dst)); | |
4172 | } | |
4173 | ||
4174 | ||
4175 | /* | |
4176 | * 'copy_job_attrs()' - Copy job attributes. | |
4177 | */ | |
ef416fc2 | 4178 | |
b423cd4c | 4179 | static void |
4180 | copy_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
4181 | cupsd_job_t *job, /* I - Job */ | |
4182 | cups_array_t *ra) /* I - Requested attributes array */ | |
4183 | { | |
4184 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
ef416fc2 | 4185 | |
ef416fc2 | 4186 | |
4187 | /* | |
b423cd4c | 4188 | * Send the requested attributes for each job... |
ef416fc2 | 4189 | */ |
4190 | ||
b423cd4c | 4191 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
4192 | con->servername, con->serverport, "/jobs/%d", | |
4193 | job->id); | |
ef416fc2 | 4194 | |
b423cd4c | 4195 | if (!ra || cupsArrayFind(ra, "job-more-info")) |
4196 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
4197 | "job-more-info", NULL, job_uri); | |
ef416fc2 | 4198 | |
b423cd4c | 4199 | if (job->state_value > IPP_JOB_PROCESSING && |
4200 | (!ra || cupsArrayFind(ra, "job-preserved"))) | |
4201 | ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved", | |
4202 | job->num_files > 0); | |
ef416fc2 | 4203 | |
b423cd4c | 4204 | if (!ra || cupsArrayFind(ra, "job-printer-up-time")) |
4205 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
4206 | "job-printer-up-time", time(NULL)); | |
ef416fc2 | 4207 | |
b423cd4c | 4208 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) |
4209 | add_job_state_reasons(con, job); | |
bd7854cb | 4210 | |
b423cd4c | 4211 | if (!ra || cupsArrayFind(ra, "job-uri")) |
4212 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
4213 | "job-uri", NULL, job_uri); | |
ef416fc2 | 4214 | |
b423cd4c | 4215 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0); |
4216 | } | |
ef416fc2 | 4217 | |
ef416fc2 | 4218 | |
b423cd4c | 4219 | /* |
4220 | * 'copy_printer_attrs()' - Copy printer attributes. | |
4221 | */ | |
ef416fc2 | 4222 | |
b423cd4c | 4223 | static void |
4224 | copy_printer_attrs( | |
4225 | cupsd_client_t *con, /* I - Client connection */ | |
4226 | cupsd_printer_t *printer, /* I - Printer */ | |
4227 | cups_array_t *ra) /* I - Requested attributes array */ | |
4228 | { | |
4229 | char printer_uri[HTTP_MAX_URI]; | |
4230 | /* Printer URI */ | |
4231 | time_t curtime; /* Current time */ | |
4232 | int i; /* Looping var */ | |
4233 | ipp_attribute_t *history; /* History collection */ | |
ef416fc2 | 4234 | |
ef416fc2 | 4235 | |
b423cd4c | 4236 | /* |
4237 | * Copy the printer attributes to the response using requested-attributes | |
4238 | * and document-format attributes that may be provided by the client. | |
4239 | */ | |
ef416fc2 | 4240 | |
b423cd4c | 4241 | curtime = time(NULL); |
ef416fc2 | 4242 | |
b423cd4c | 4243 | #ifdef __APPLE__ |
4244 | if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) && | |
4245 | printer->recoverable) | |
4246 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
4247 | "com.apple.print.recoverable-message", NULL, | |
4248 | printer->recoverable); | |
4249 | #endif /* __APPLE__ */ | |
ef416fc2 | 4250 | |
b423cd4c | 4251 | if (!ra || cupsArrayFind(ra, "printer-current-time")) |
4252 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", | |
4253 | ippTimeToDate(curtime)); | |
ef416fc2 | 4254 | |
b423cd4c | 4255 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) |
4256 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
4257 | "printer-error-policy", NULL, printer->error_policy); | |
ef416fc2 | 4258 | |
b423cd4c | 4259 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) |
4260 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", | |
4261 | printer->accepting); | |
ef416fc2 | 4262 | |
b423cd4c | 4263 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) |
4264 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", | |
4265 | printer->shared); | |
ef416fc2 | 4266 | |
b423cd4c | 4267 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) |
4268 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
4269 | "printer-op-policy", NULL, printer->op_policy); | |
4270 | ||
4271 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
4272 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
4273 | printer->state); | |
4274 | ||
4275 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) | |
4276 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
4277 | "printer-state-change-time", printer->state_time); | |
4278 | ||
4279 | if (MaxPrinterHistory > 0 && printer->num_history > 0 && | |
4280 | cupsArrayFind(ra, "printer-state-history")) | |
ef416fc2 | 4281 | { |
4282 | /* | |
b423cd4c | 4283 | * Printer history is only sent if specifically requested, so that |
4284 | * older CUPS/IPP clients won't barf on the collection attributes. | |
ef416fc2 | 4285 | */ |
4286 | ||
b423cd4c | 4287 | history = ippAddCollections(con->response, IPP_TAG_PRINTER, |
4288 | "printer-state-history", | |
4289 | printer->num_history, NULL); | |
4290 | ||
4291 | for (i = 0; i < printer->num_history; i ++) | |
4292 | copy_attrs(history->values[i].collection = ippNew(), printer->history[i], | |
4293 | NULL, IPP_TAG_ZERO, 0); | |
ef416fc2 | 4294 | } |
4295 | ||
b423cd4c | 4296 | if (!ra || cupsArrayFind(ra, "printer-state-message")) |
4297 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
4298 | "printer-state-message", NULL, printer->state_message); | |
ef416fc2 | 4299 | |
b423cd4c | 4300 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) |
4301 | add_printer_state_reasons(con, printer); | |
ef416fc2 | 4302 | |
b423cd4c | 4303 | if (!ra || cupsArrayFind(ra, "printer-type")) |
4304 | { | |
4305 | int type; /* printer-type value */ | |
ef416fc2 | 4306 | |
ef416fc2 | 4307 | |
ef416fc2 | 4308 | /* |
b423cd4c | 4309 | * Add the CUPS-specific printer-type attribute... |
ef416fc2 | 4310 | */ |
4311 | ||
b423cd4c | 4312 | type = printer->type; |
4313 | ||
4314 | if (printer == DefaultPrinter) | |
4315 | type |= CUPS_PRINTER_DEFAULT; | |
4316 | ||
4317 | if (!printer->accepting) | |
4318 | type |= CUPS_PRINTER_REJECTING; | |
4319 | ||
4320 | if (!printer->shared) | |
4321 | type |= CUPS_PRINTER_NOT_SHARED; | |
4322 | ||
4323 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", | |
4324 | type); | |
ef416fc2 | 4325 | } |
ef416fc2 | 4326 | |
b423cd4c | 4327 | if (!ra || cupsArrayFind(ra, "printer-up-time")) |
4328 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
4329 | "printer-up-time", curtime); | |
ef416fc2 | 4330 | |
b423cd4c | 4331 | if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) && |
4332 | !ippFindAttribute(printer->attrs, "printer-uri-supported", | |
4333 | IPP_TAG_URI)) | |
ef416fc2 | 4334 | { |
b423cd4c | 4335 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), |
4336 | "ipp", NULL, con->servername, con->serverport, | |
4337 | "/printers/%s", printer->name); | |
4338 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, | |
4339 | "printer-uri-supported", NULL, printer_uri); | |
4340 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", | |
4341 | printer_uri); | |
4342 | } | |
ef416fc2 | 4343 | |
b423cd4c | 4344 | if (!ra || cupsArrayFind(ra, "queued-job-count")) |
4345 | add_queued_job_count(con, printer); | |
ef416fc2 | 4346 | |
b423cd4c | 4347 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0); |
4348 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY); | |
4349 | } | |
ef416fc2 | 4350 | |
ef416fc2 | 4351 | |
b423cd4c | 4352 | /* |
4353 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
4354 | */ | |
ef416fc2 | 4355 | |
b423cd4c | 4356 | static void |
4357 | copy_subscription_attrs( | |
4358 | cupsd_client_t *con, /* I - Client connection */ | |
4359 | cupsd_subscription_t *sub, /* I - Subscription */ | |
4360 | cups_array_t *ra) /* I - Requested attributes array */ | |
4361 | { | |
4362 | ipp_attribute_t *attr; /* Current attribute */ | |
4363 | char printer_uri[HTTP_MAX_URI]; | |
4364 | /* Printer URI */ | |
4365 | int count; /* Number of events */ | |
4366 | unsigned mask; /* Current event mask */ | |
4367 | const char *name; /* Current event name */ | |
ef416fc2 | 4368 | |
ef416fc2 | 4369 | |
b423cd4c | 4370 | /* |
4371 | * Copy the subscription attributes to the response using the | |
4372 | * requested-attributes attribute that may be provided by the client. | |
4373 | */ | |
ef416fc2 | 4374 | |
b423cd4c | 4375 | if (!ra || cupsArrayFind(ra, "notify-events")) |
4376 | { | |
4377 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
4378 | { | |
4379 | /* | |
4380 | * Simple event list... | |
4381 | */ | |
ef416fc2 | 4382 | |
b423cd4c | 4383 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, |
4384 | IPP_TAG_KEYWORD | IPP_TAG_COPY, | |
4385 | "notify-events", NULL, name); | |
4386 | } | |
4387 | else | |
4388 | { | |
4389 | /* | |
4390 | * Complex event list... | |
4391 | */ | |
ef416fc2 | 4392 | |
b423cd4c | 4393 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
4394 | if (sub->mask & mask) | |
4395 | count ++; | |
ef416fc2 | 4396 | |
b423cd4c | 4397 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, |
4398 | IPP_TAG_KEYWORD | IPP_TAG_COPY, | |
4399 | "notify-events", count, NULL, NULL); | |
ef416fc2 | 4400 | |
b423cd4c | 4401 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
4402 | if (sub->mask & mask) | |
4403 | { | |
4404 | attr->values[count].string.text = | |
4405 | (char *)cupsdEventName((cupsd_eventmask_t)mask); | |
ef416fc2 | 4406 | |
b423cd4c | 4407 | count ++; |
4408 | } | |
ef416fc2 | 4409 | } |
b423cd4c | 4410 | } |
ef416fc2 | 4411 | |
b423cd4c | 4412 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) |
4413 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4414 | "notify-job-id", sub->job->id); | |
ef416fc2 | 4415 | |
b423cd4c | 4416 | if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))) |
4417 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4418 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 4419 | |
b423cd4c | 4420 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) |
4421 | { | |
4422 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
4423 | "ipp", NULL, con->servername, con->serverport, | |
4424 | "/printers/%s", sub->dest->name); | |
4425 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
4426 | "notify-printer-uri", NULL, printer_uri); | |
ef416fc2 | 4427 | } |
ef416fc2 | 4428 | |
b423cd4c | 4429 | if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))) |
4430 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
4431 | "notify-recipient-uri", NULL, sub->recipient); | |
4432 | else if (!ra || cupsArrayFind(ra, "notify-pull-method")) | |
4433 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
4434 | "notify-pull-method", NULL, "ippget"); | |
ef416fc2 | 4435 | |
b423cd4c | 4436 | if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")) |
4437 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
4438 | "notify-subscriber-user-name", NULL, sub->owner); | |
ef416fc2 | 4439 | |
b423cd4c | 4440 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) |
4441 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4442 | "notify-subscription-id", sub->id); | |
ef416fc2 | 4443 | |
b423cd4c | 4444 | if (!ra || cupsArrayFind(ra, "notify-time-interval")) |
4445 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4446 | "notify-time-interval", sub->interval); | |
ef416fc2 | 4447 | |
b423cd4c | 4448 | if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data"))) |
4449 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
4450 | sub->user_data, sub->user_data_len); | |
4451 | } | |
ef416fc2 | 4452 | |
ef416fc2 | 4453 | |
b423cd4c | 4454 | /* |
4455 | * 'create_job()' - Print a file to a printer or class. | |
4456 | */ | |
4457 | ||
4458 | static void | |
4459 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
4460 | ipp_attribute_t *uri) /* I - Printer URI */ | |
4461 | { | |
f7faf1f5 | 4462 | cupsd_job_t *job; /* New job */ |
b423cd4c | 4463 | |
ef416fc2 | 4464 | |
b423cd4c | 4465 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, |
4466 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 4467 | |
4468 | /* | |
b423cd4c | 4469 | * Create the job object... |
ef416fc2 | 4470 | */ |
4471 | ||
f7faf1f5 | 4472 | if ((job = add_job(con, uri, NULL, NULL)) == NULL) |
b423cd4c | 4473 | return; |
ef416fc2 | 4474 | |
4475 | /* | |
4476 | * Save and log the job... | |
4477 | */ | |
4478 | ||
4479 | cupsdSaveJob(job); | |
4480 | ||
4481 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d created on \"%s\" by \"%s\".", | |
4482 | job->id, job->dest, job->username); | |
ef416fc2 | 4483 | } |
4484 | ||
4485 | ||
4486 | /* | |
4487 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
4488 | */ | |
4489 | ||
4490 | static cups_array_t * /* O - Array of attributes or NULL */ | |
4491 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
4492 | { | |
4493 | int i; /* Looping var */ | |
4494 | ipp_attribute_t *requested; /* requested-attributes attribute */ | |
4495 | cups_array_t *ra; /* Requested attributes array */ | |
4496 | char *value; /* Current value */ | |
4497 | ||
4498 | ||
4499 | /* | |
4500 | * Get the requested-attributes attribute, and return NULL if we don't | |
4501 | * have one... | |
4502 | */ | |
4503 | ||
4504 | if ((requested = ippFindAttribute(request, "requested-attributes", | |
4505 | IPP_TAG_KEYWORD)) == NULL) | |
4506 | return (NULL); | |
4507 | ||
4508 | /* | |
4509 | * If the attribute contains a single "all" keyword, return NULL... | |
4510 | */ | |
4511 | ||
4512 | if (requested->num_values == 1 && | |
4513 | !strcmp(requested->values[0].string.text, "all")) | |
4514 | return (NULL); | |
4515 | ||
4516 | /* | |
4517 | * Create an array using "strcmp" as the comparison function... | |
4518 | */ | |
4519 | ||
4520 | ra = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
4521 | ||
4522 | for (i = 0; i < requested->num_values; i ++) | |
4523 | { | |
4524 | value = requested->values[i].string.text; | |
4525 | ||
4526 | if (!strcmp(value, "job-template")) | |
4527 | { | |
4528 | cupsArrayAdd(ra, "copies"); | |
4529 | cupsArrayAdd(ra, "copies-default"); | |
4530 | cupsArrayAdd(ra, "copies-supported"); | |
4531 | cupsArrayAdd(ra, "finishings"); | |
4532 | cupsArrayAdd(ra, "finishings-default"); | |
4533 | cupsArrayAdd(ra, "finishings-supported"); | |
4534 | cupsArrayAdd(ra, "job-hold-until"); | |
4535 | cupsArrayAdd(ra, "job-hold-until-default"); | |
4536 | cupsArrayAdd(ra, "job-hold-until-supported"); | |
4537 | cupsArrayAdd(ra, "job-priority"); | |
4538 | cupsArrayAdd(ra, "job-priority-default"); | |
4539 | cupsArrayAdd(ra, "job-priority-supported"); | |
4540 | cupsArrayAdd(ra, "job-sheets"); | |
4541 | cupsArrayAdd(ra, "job-sheets-default"); | |
4542 | cupsArrayAdd(ra, "job-sheets-supported"); | |
4543 | cupsArrayAdd(ra, "media"); | |
4544 | cupsArrayAdd(ra, "media-default"); | |
4545 | cupsArrayAdd(ra, "media-supported"); | |
4546 | cupsArrayAdd(ra, "multiple-document-handling"); | |
4547 | cupsArrayAdd(ra, "multiple-document-handling-default"); | |
4548 | cupsArrayAdd(ra, "multiple-document-handling-supported"); | |
4549 | cupsArrayAdd(ra, "number-up"); | |
4550 | cupsArrayAdd(ra, "number-up-default"); | |
4551 | cupsArrayAdd(ra, "number-up-supported"); | |
4552 | cupsArrayAdd(ra, "orientation-requested"); | |
4553 | cupsArrayAdd(ra, "orientation-requested-default"); | |
4554 | cupsArrayAdd(ra, "orientation-requested-supported"); | |
4555 | cupsArrayAdd(ra, "page-ranges"); | |
4556 | cupsArrayAdd(ra, "page-ranges-supported"); | |
4557 | cupsArrayAdd(ra, "printer-resolution"); | |
4558 | cupsArrayAdd(ra, "printer-resolution-default"); | |
4559 | cupsArrayAdd(ra, "printer-resolution-supported"); | |
4560 | cupsArrayAdd(ra, "print-quality"); | |
4561 | cupsArrayAdd(ra, "print-quality-default"); | |
4562 | cupsArrayAdd(ra, "print-quality-supported"); | |
4563 | cupsArrayAdd(ra, "sides"); | |
4564 | cupsArrayAdd(ra, "sides-default"); | |
4565 | cupsArrayAdd(ra, "sides-supported"); | |
4566 | } | |
4567 | else if (!strcmp(value, "job-description")) | |
4568 | { | |
4569 | cupsArrayAdd(ra, "date-time-at-completed"); | |
4570 | cupsArrayAdd(ra, "date-time-at-creation"); | |
4571 | cupsArrayAdd(ra, "date-time-at-processing"); | |
4572 | cupsArrayAdd(ra, "job-detailed-status-message"); | |
4573 | cupsArrayAdd(ra, "job-document-access-errors"); | |
4574 | cupsArrayAdd(ra, "job-id"); | |
4575 | cupsArrayAdd(ra, "job-impressions"); | |
4576 | cupsArrayAdd(ra, "job-impressions-completed"); | |
4577 | cupsArrayAdd(ra, "job-k-octets"); | |
4578 | cupsArrayAdd(ra, "job-k-octets-processed"); | |
4579 | cupsArrayAdd(ra, "job-media-sheets"); | |
4580 | cupsArrayAdd(ra, "job-media-sheets-completed"); | |
4581 | cupsArrayAdd(ra, "job-message-from-operator"); | |
4582 | cupsArrayAdd(ra, "job-more-info"); | |
4583 | cupsArrayAdd(ra, "job-name"); | |
4584 | cupsArrayAdd(ra, "job-originating-user-name"); | |
4585 | cupsArrayAdd(ra, "job-printer-up-time"); | |
4586 | cupsArrayAdd(ra, "job-printer-uri"); | |
4587 | cupsArrayAdd(ra, "job-state"); | |
4588 | cupsArrayAdd(ra, "job-state-message"); | |
4589 | cupsArrayAdd(ra, "job-state-reasons"); | |
4590 | cupsArrayAdd(ra, "job-uri"); | |
4591 | cupsArrayAdd(ra, "number-of-documents"); | |
4592 | cupsArrayAdd(ra, "number-of-intervening-jobs"); | |
4593 | cupsArrayAdd(ra, "output-device-assigned"); | |
4594 | cupsArrayAdd(ra, "time-at-completed"); | |
4595 | cupsArrayAdd(ra, "time-at-creation"); | |
4596 | cupsArrayAdd(ra, "time-at-processing"); | |
4597 | } | |
4598 | else if (!strcmp(value, "printer-description")) | |
4599 | { | |
4600 | cupsArrayAdd(ra, "charset-configured"); | |
4601 | cupsArrayAdd(ra, "charset-supported"); | |
4602 | cupsArrayAdd(ra, "color-supported"); | |
4603 | cupsArrayAdd(ra, "compression-supported"); | |
4604 | cupsArrayAdd(ra, "document-format-default"); | |
4605 | cupsArrayAdd(ra, "document-format-supported"); | |
4606 | cupsArrayAdd(ra, "generated-natural-language-supported"); | |
4607 | cupsArrayAdd(ra, "ipp-versions-supported"); | |
4608 | cupsArrayAdd(ra, "job-impressions-supported"); | |
4609 | cupsArrayAdd(ra, "job-k-octets-supported"); | |
4610 | cupsArrayAdd(ra, "job-media-sheets-supported"); | |
4611 | cupsArrayAdd(ra, "multiple-document-jobs-supported"); | |
4612 | cupsArrayAdd(ra, "multiple-operation-time-out"); | |
4613 | cupsArrayAdd(ra, "natural-language-configured"); | |
4614 | cupsArrayAdd(ra, "notify-attributes-supported"); | |
4615 | cupsArrayAdd(ra, "notify-lease-duration-default"); | |
4616 | cupsArrayAdd(ra, "notify-lease-duration-supported"); | |
4617 | cupsArrayAdd(ra, "notify-max-events-supported"); | |
f301802f | 4618 | cupsArrayAdd(ra, "notify-events-default"); |
4619 | cupsArrayAdd(ra, "notify-events-supported"); | |
ef416fc2 | 4620 | cupsArrayAdd(ra, "notify-pull-method-supported"); |
4621 | cupsArrayAdd(ra, "notify-schemes-supported"); | |
4622 | cupsArrayAdd(ra, "operations-supported"); | |
4623 | cupsArrayAdd(ra, "pages-per-minute"); | |
4624 | cupsArrayAdd(ra, "pages-per-minute-color"); | |
4625 | cupsArrayAdd(ra, "pdl-override-supported"); | |
4626 | cupsArrayAdd(ra, "printer-current-time"); | |
4627 | cupsArrayAdd(ra, "printer-driver-installer"); | |
4628 | cupsArrayAdd(ra, "printer-info"); | |
4629 | cupsArrayAdd(ra, "printer-is-accepting-jobs"); | |
4630 | cupsArrayAdd(ra, "printer-location"); | |
4631 | cupsArrayAdd(ra, "printer-make-and-model"); | |
4632 | cupsArrayAdd(ra, "printer-message-from-operator"); | |
4633 | cupsArrayAdd(ra, "printer-more-info"); | |
4634 | cupsArrayAdd(ra, "printer-more-info-manufacturer"); | |
4635 | cupsArrayAdd(ra, "printer-name"); | |
4636 | cupsArrayAdd(ra, "printer-state"); | |
4637 | cupsArrayAdd(ra, "printer-state-message"); | |
4638 | cupsArrayAdd(ra, "printer-state-reasons"); | |
4639 | cupsArrayAdd(ra, "printer-up-time"); | |
4640 | cupsArrayAdd(ra, "printer-uri-supported"); | |
4641 | cupsArrayAdd(ra, "queued-job-count"); | |
4642 | cupsArrayAdd(ra, "reference-uri-schemes-supported"); | |
4643 | cupsArrayAdd(ra, "uri-authentication-supported"); | |
4644 | cupsArrayAdd(ra, "uri-security-supported"); | |
4645 | } | |
4646 | else if (!strcmp(value, "subscription-template")) | |
4647 | { | |
4648 | cupsArrayAdd(ra, "notify-attributes"); | |
4649 | cupsArrayAdd(ra, "notify-charset"); | |
4650 | cupsArrayAdd(ra, "notify-events"); | |
4651 | cupsArrayAdd(ra, "notify-lease-duration"); | |
4652 | cupsArrayAdd(ra, "notify-natural-language"); | |
4653 | cupsArrayAdd(ra, "notify-pull-method"); | |
4654 | cupsArrayAdd(ra, "notify-recipient-uri"); | |
4655 | cupsArrayAdd(ra, "notify-time-interval"); | |
4656 | cupsArrayAdd(ra, "notify-user-data"); | |
4657 | } | |
4658 | else | |
4659 | cupsArrayAdd(ra, value); | |
4660 | } | |
4661 | ||
4662 | return (ra); | |
4663 | } | |
4664 | ||
4665 | ||
4666 | /* | |
4667 | * 'create_subscription()' - Create a notification subscription. | |
4668 | */ | |
4669 | ||
4670 | static void | |
4671 | create_subscription( | |
4672 | cupsd_client_t *con, /* I - Client connection */ | |
4673 | ipp_attribute_t *uri) /* I - Printer URI */ | |
4674 | { | |
4675 | http_status_t status; /* Policy status */ | |
4676 | int i; /* Looping var */ | |
4677 | ipp_attribute_t *attr; /* Current attribute */ | |
4678 | const char *dest; /* Destination */ | |
4679 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
ed486911 | 4680 | char scheme[HTTP_MAX_URI], |
4681 | /* Scheme portion of URI */ | |
ef416fc2 | 4682 | userpass[HTTP_MAX_URI], |
4683 | /* Username portion of URI */ | |
4684 | host[HTTP_MAX_URI], | |
4685 | /* Host portion of URI */ | |
4686 | resource[HTTP_MAX_URI]; | |
4687 | /* Resource portion of URI */ | |
4688 | int port; /* Port portion of URI */ | |
4689 | cupsd_printer_t *printer; /* Printer/class */ | |
4690 | cupsd_job_t *job; /* Job */ | |
4691 | int jobid; /* Job ID */ | |
4692 | cupsd_subscription_t *sub; /* Subscription object */ | |
4693 | const char *username, /* requesting-user-name or authenticated username */ | |
4694 | *recipient, /* notify-recipient-uri */ | |
4695 | *pullmethod; /* notify-pull-method */ | |
4696 | ipp_attribute_t *user_data; /* notify-user-data */ | |
4697 | int interval, /* notify-time-interval */ | |
4698 | lease; /* notify-lease-duration */ | |
4699 | unsigned mask; /* notify-events */ | |
4700 | ||
4701 | ||
bd7854cb | 4702 | #ifdef DEBUG |
4703 | for (attr = con->request->attrs; attr; attr = attr->next) | |
4704 | { | |
4705 | if (attr->group_tag != IPP_TAG_ZERO) | |
4706 | cupsdLogMessage(CUPSD_LOG_DEBUG, "g%04x v%04x %s", attr->group_tag, | |
4707 | attr->value_tag, attr->name); | |
4708 | else | |
4709 | cupsdLogMessage(CUPSD_LOG_DEBUG, "----SEP----"); | |
4710 | } | |
4711 | #endif /* DEBUG */ | |
4712 | ||
ef416fc2 | 4713 | /* |
4714 | * Is the destination valid? | |
4715 | */ | |
4716 | ||
fa73b229 | 4717 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
4718 | "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")", | |
4719 | con, con->http.fd, uri->values[0].string.text); | |
4720 | ||
ed486911 | 4721 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme, |
4722 | sizeof(scheme), userpass, sizeof(userpass), host, | |
a4d04587 | 4723 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 4724 | |
4725 | if (!strcmp(resource, "/")) | |
4726 | { | |
4727 | dest = NULL; | |
4728 | dtype = (cups_ptype_t)0; | |
4729 | printer = NULL; | |
4730 | } | |
4731 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
4732 | { | |
4733 | dest = NULL; | |
4734 | dtype = (cups_ptype_t)0; | |
4735 | printer = NULL; | |
4736 | } | |
4737 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
4738 | { | |
4739 | dest = NULL; | |
4740 | dtype = CUPS_PRINTER_CLASS; | |
4741 | printer = NULL; | |
4742 | } | |
f7faf1f5 | 4743 | else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) |
ef416fc2 | 4744 | { |
4745 | /* | |
4746 | * Bad URI... | |
4747 | */ | |
4748 | ||
4749 | send_ipp_status(con, IPP_NOT_FOUND, | |
4750 | _("The printer or class was not found.")); | |
4751 | return; | |
4752 | } | |
4753 | ||
4754 | /* | |
4755 | * Check policy... | |
4756 | */ | |
4757 | ||
4758 | if (printer) | |
4759 | { | |
f7faf1f5 | 4760 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 4761 | { |
4762 | send_http_error(con, status); | |
4763 | return; | |
4764 | } | |
4765 | } | |
4766 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
4767 | { | |
4768 | send_http_error(con, status); | |
4769 | return; | |
4770 | } | |
4771 | ||
4772 | /* | |
4773 | * Get the user that is requesting the subscription... | |
4774 | */ | |
4775 | ||
e00b005a | 4776 | username = get_username(con); |
ef416fc2 | 4777 | |
4778 | /* | |
4779 | * Find the first subscription group attribute; return if we have | |
4780 | * none... | |
4781 | */ | |
4782 | ||
4783 | for (attr = con->request->attrs; attr; attr = attr->next) | |
4784 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
4785 | break; | |
4786 | ||
4787 | if (!attr) | |
4788 | { | |
4789 | send_ipp_status(con, IPP_BAD_REQUEST, | |
4790 | _("No subscription attributes in request!")); | |
4791 | return; | |
4792 | } | |
4793 | ||
4794 | /* | |
4795 | * Process the subscription attributes in the request... | |
4796 | */ | |
4797 | ||
fa73b229 | 4798 | con->response->request.status.status_code = IPP_BAD_REQUEST; |
4799 | ||
ef416fc2 | 4800 | while (attr) |
4801 | { | |
4802 | recipient = NULL; | |
4803 | pullmethod = NULL; | |
4804 | user_data = NULL; | |
4805 | interval = 0; | |
4806 | lease = DefaultLeaseDuration; | |
4807 | jobid = 0; | |
4808 | mask = CUPSD_EVENT_NONE; | |
4809 | ||
4810 | while (attr && attr->group_tag != IPP_TAG_ZERO) | |
4811 | { | |
4812 | if (!strcmp(attr->name, "notify-recipient") && | |
4813 | attr->value_tag == IPP_TAG_URI) | |
ed486911 | 4814 | { |
4815 | /* | |
4816 | * Validate the recipient scheme against the ServerBin/notifier | |
4817 | * directory... | |
4818 | */ | |
4819 | ||
4820 | char notifier[1024]; /* Notifier filename */ | |
4821 | ||
4822 | ||
ef416fc2 | 4823 | recipient = attr->values[0].string.text; |
ed486911 | 4824 | |
4825 | if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient, scheme, | |
4826 | sizeof(scheme), userpass, sizeof(userpass), host, | |
4827 | sizeof(host), &port, resource, sizeof(resource))) | |
4828 | { | |
4829 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
4830 | _("Bad notify-recipient URI \"%s\"!"), recipient); | |
4831 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
4832 | "notify-status-code", IPP_URI_SCHEME); | |
4833 | return; | |
4834 | } | |
4835 | ||
4836 | snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, | |
4837 | scheme); | |
4838 | if (access(notifier, X_OK)) | |
4839 | { | |
4840 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
4841 | _("notify-recipient URI \"%s\" uses unknown scheme!"), | |
4842 | recipient); | |
4843 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
4844 | "notify-status-code", IPP_URI_SCHEME); | |
4845 | return; | |
4846 | } | |
4847 | } | |
ef416fc2 | 4848 | else if (!strcmp(attr->name, "notify-pull-method") && |
4849 | attr->value_tag == IPP_TAG_KEYWORD) | |
ed486911 | 4850 | { |
ef416fc2 | 4851 | pullmethod = attr->values[0].string.text; |
ed486911 | 4852 | |
4853 | if (strcmp(pullmethod, "ippget")) | |
4854 | { | |
4855 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
4856 | _("Bad notify-pull-method \"%s\"!"), pullmethod); | |
4857 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, | |
4858 | "notify-status-code", IPP_ATTRIBUTES); | |
4859 | return; | |
4860 | } | |
4861 | } | |
ef416fc2 | 4862 | else if (!strcmp(attr->name, "notify-charset") && |
4863 | attr->value_tag == IPP_TAG_CHARSET && | |
4864 | strcmp(attr->values[0].string.text, "us-ascii") && | |
4865 | strcmp(attr->values[0].string.text, "utf-8")) | |
4866 | { | |
4867 | send_ipp_status(con, IPP_CHARSET, | |
4868 | _("Character set \"%s\" not supported!"), | |
4869 | attr->values[0].string.text); | |
4870 | return; | |
4871 | } | |
4872 | else if (!strcmp(attr->name, "notify-natural-language") && | |
4873 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
4874 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
4875 | { | |
4876 | send_ipp_status(con, IPP_CHARSET, | |
4877 | _("Language \"%s\" not supported!"), | |
4878 | attr->values[0].string.text); | |
4879 | return; | |
4880 | } | |
4881 | else if (!strcmp(attr->name, "notify-user-data") && | |
4882 | attr->value_tag == IPP_TAG_STRING) | |
4883 | { | |
4884 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
4885 | { | |
4886 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
4887 | _("The notify-user-data value is too large " | |
4888 | "(%d > 63 octets)!"), | |
4889 | attr->values[0].unknown.length); | |
4890 | return; | |
4891 | } | |
4892 | ||
4893 | user_data = attr; | |
4894 | } | |
4895 | else if (!strcmp(attr->name, "notify-events") && | |
4896 | attr->value_tag == IPP_TAG_KEYWORD) | |
4897 | { | |
4898 | for (i = 0; i < attr->num_values; i ++) | |
4899 | mask |= cupsdEventValue(attr->values[i].string.text); | |
4900 | } | |
4901 | else if (!strcmp(attr->name, "notify-lease-duration") && | |
4902 | attr->value_tag == IPP_TAG_INTEGER) | |
4903 | lease = attr->values[0].integer; | |
4904 | else if (!strcmp(attr->name, "notify-time-interval") && | |
4905 | attr->value_tag == IPP_TAG_INTEGER) | |
4906 | interval = attr->values[0].integer; | |
4907 | else if (!strcmp(attr->name, "notify-job-id") && | |
4908 | attr->value_tag == IPP_TAG_INTEGER) | |
4909 | jobid = attr->values[0].integer; | |
4910 | ||
4911 | attr = attr->next; | |
4912 | } | |
4913 | ||
bd7854cb | 4914 | if (recipient) |
4915 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); | |
4916 | if (pullmethod) | |
4917 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
4918 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
4919 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
fa73b229 | 4920 | |
ef416fc2 | 4921 | if (!recipient && !pullmethod) |
4922 | break; | |
4923 | ||
4924 | if (mask == CUPSD_EVENT_NONE) | |
4925 | { | |
4926 | if (jobid) | |
4927 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
4928 | else if (printer) | |
4929 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
4930 | else | |
4931 | { | |
4932 | send_ipp_status(con, IPP_BAD_REQUEST, | |
4933 | _("notify-events not specified!")); | |
4934 | return; | |
4935 | } | |
4936 | } | |
4937 | ||
bd7854cb | 4938 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) |
ef416fc2 | 4939 | { |
4940 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4941 | "create_subscription: Limiting notify-lease-duration to " | |
4942 | "%d seconds.", | |
4943 | MaxLeaseDuration); | |
4944 | lease = MaxLeaseDuration; | |
4945 | } | |
4946 | ||
4947 | if (jobid) | |
4948 | { | |
4949 | if ((job = cupsdFindJob(jobid)) == NULL) | |
4950 | { | |
4951 | send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid); | |
4952 | return; | |
4953 | } | |
4954 | } | |
4955 | else | |
4956 | job = NULL; | |
4957 | ||
4958 | sub = cupsdAddSubscription(mask, printer, job, recipient, 0); | |
4959 | ||
fa73b229 | 4960 | if (job) |
4961 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
4962 | sub->id, job->id); | |
4963 | else if (printer) | |
4964 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4965 | "Added subscription %d for printer \"%s\"", | |
4966 | sub->id, printer->name); | |
4967 | else | |
4968 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server", | |
4969 | sub->id); | |
4970 | ||
ef416fc2 | 4971 | sub->interval = interval; |
4972 | sub->lease = lease; | |
bd7854cb | 4973 | sub->expire = lease ? time(NULL) + lease : 0; |
ef416fc2 | 4974 | |
4975 | cupsdSetString(&sub->owner, username); | |
4976 | ||
4977 | if (user_data) | |
4978 | { | |
4979 | sub->user_data_len = user_data->values[0].unknown.length; | |
4980 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
4981 | sub->user_data_len); | |
4982 | } | |
4983 | ||
4984 | ippAddSeparator(con->response); | |
4985 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4986 | "notify-subscription-id", sub->id); | |
4987 | ||
fa73b229 | 4988 | con->response->request.status.status_code = IPP_OK; |
4989 | ||
ef416fc2 | 4990 | if (attr) |
4991 | attr = attr->next; | |
4992 | } | |
4993 | ||
4994 | cupsdSaveAllSubscriptions(); | |
4995 | ||
ef416fc2 | 4996 | } |
4997 | ||
4998 | ||
4999 | /* | |
5000 | * 'delete_printer()' - Remove a printer or class from the system. | |
5001 | */ | |
5002 | ||
5003 | static void | |
5004 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
5005 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
5006 | { | |
5007 | http_status_t status; /* Policy status */ | |
f7faf1f5 | 5008 | const char *dest; /* Destination */ |
ef416fc2 | 5009 | cups_ptype_t dtype; /* Destination type (printer or class) */ |
f7faf1f5 | 5010 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
5011 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
5012 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
5013 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
5014 | int port; /* Port portion of URI */ | |
ef416fc2 | 5015 | cupsd_printer_t *printer; /* Printer/class */ |
5016 | char filename[1024]; /* Script/PPD filename */ | |
5017 | ||
5018 | ||
5019 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
5020 | con->http.fd, uri->values[0].string.text); | |
5021 | ||
5022 | /* | |
5023 | * Do we have a valid URI? | |
5024 | */ | |
5025 | ||
f7faf1f5 | 5026 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5027 | sizeof(method), username, sizeof(username), host, | |
5028 | sizeof(host), &port, resource, sizeof(resource)); | |
5029 | ||
5030 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 5031 | { |
5032 | /* | |
5033 | * Bad URI... | |
5034 | */ | |
5035 | ||
5036 | send_ipp_status(con, IPP_NOT_FOUND, | |
5037 | _("The printer or class was not found.")); | |
5038 | return; | |
5039 | } | |
5040 | ||
5041 | /* | |
5042 | * Check policy... | |
5043 | */ | |
5044 | ||
5045 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5046 | { | |
5047 | send_http_error(con, status); | |
5048 | return; | |
5049 | } | |
5050 | ||
5051 | /* | |
5052 | * Remove old jobs... | |
5053 | */ | |
5054 | ||
f7faf1f5 | 5055 | cupsdCancelJobs(dest, NULL, 1); |
ef416fc2 | 5056 | |
5057 | /* | |
5058 | * Remove old subscriptions and send a "deleted printer" event... | |
5059 | */ | |
5060 | ||
5061 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
5062 | "%s \"%s\" deleted by \"%s\".", | |
5063 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
f7faf1f5 | 5064 | dest, get_username(con)); |
ef416fc2 | 5065 | |
5066 | cupsdExpireSubscriptions(printer, NULL); | |
5067 | ||
5068 | /* | |
5069 | * Remove any old PPD or script files... | |
5070 | */ | |
5071 | ||
f7faf1f5 | 5072 | snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, dest); |
ef416fc2 | 5073 | unlink(filename); |
5074 | ||
f7faf1f5 | 5075 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest); |
ef416fc2 | 5076 | unlink(filename); |
5077 | ||
5078 | if (dtype & CUPS_PRINTER_CLASS) | |
5079 | { | |
f7faf1f5 | 5080 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", dest, |
5081 | get_username(con)); | |
ef416fc2 | 5082 | |
5083 | cupsdDeletePrinter(printer, 0); | |
5084 | cupsdSaveAllClasses(); | |
5085 | } | |
5086 | else | |
5087 | { | |
f7faf1f5 | 5088 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", dest, |
5089 | get_username(con)); | |
ef416fc2 | 5090 | |
5091 | cupsdDeletePrinter(printer, 0); | |
5092 | cupsdSaveAllPrinters(); | |
5093 | } | |
5094 | ||
5095 | cupsdWritePrintcap(); | |
5096 | ||
5097 | /* | |
5098 | * Return with no errors... | |
5099 | */ | |
5100 | ||
5101 | con->response->request.status.status_code = IPP_OK; | |
5102 | } | |
5103 | ||
5104 | ||
5105 | /* | |
5106 | * 'get_default()' - Get the default destination. | |
5107 | */ | |
5108 | ||
5109 | static void | |
5110 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
5111 | { | |
fa73b229 | 5112 | http_status_t status; /* Policy status */ |
5113 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5114 | |
5115 | ||
5116 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd); | |
5117 | ||
5118 | /* | |
5119 | * Check policy... | |
5120 | */ | |
5121 | ||
5122 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5123 | { | |
5124 | send_http_error(con, status); | |
5125 | return; | |
5126 | } | |
5127 | ||
fa73b229 | 5128 | if (DefaultPrinter) |
ef416fc2 | 5129 | { |
fa73b229 | 5130 | ra = create_requested_array(con->request); |
ef416fc2 | 5131 | |
fa73b229 | 5132 | copy_printer_attrs(con, DefaultPrinter, ra); |
ef416fc2 | 5133 | |
fa73b229 | 5134 | cupsArrayDelete(ra); |
ef416fc2 | 5135 | |
fa73b229 | 5136 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 5137 | } |
5138 | else | |
5139 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer")); | |
5140 | } | |
5141 | ||
5142 | ||
5143 | /* | |
5144 | * 'get_devices()' - Get the list of available devices on the local system. | |
5145 | */ | |
5146 | ||
5147 | static void | |
5148 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
5149 | { | |
5150 | http_status_t status; /* Policy status */ | |
ef416fc2 | 5151 | ipp_attribute_t *limit, /* Limit attribute */ |
5152 | *requested; /* requested-attributes attribute */ | |
5153 | char command[1024], /* cups-deviced command */ | |
5154 | options[1024], /* Options to pass to command */ | |
89d46774 | 5155 | requested_str[256]; |
5156 | /* String for requested attributes */ | |
ef416fc2 | 5157 | |
5158 | ||
5159 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd); | |
5160 | ||
5161 | /* | |
5162 | * Check policy... | |
5163 | */ | |
5164 | ||
5165 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5166 | { | |
5167 | send_http_error(con, status); | |
5168 | return; | |
5169 | } | |
5170 | ||
5171 | /* | |
5172 | * Run cups-deviced command with the given options... | |
5173 | */ | |
5174 | ||
5175 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
5176 | requested = ippFindAttribute(con->request, "requested-attributes", | |
5177 | IPP_TAG_KEYWORD); | |
5178 | ||
5179 | if (requested) | |
89d46774 | 5180 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
ef416fc2 | 5181 | else |
89d46774 | 5182 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); |
ef416fc2 | 5183 | |
5184 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); | |
5185 | snprintf(options, sizeof(options), | |
89d46774 | 5186 | "%d+%d+%d+%s", |
ef416fc2 | 5187 | con->request->request.op.request_id, |
e1d6a774 | 5188 | limit ? limit->values[0].integer : 0, (int)User, |
89d46774 | 5189 | requested_str); |
ef416fc2 | 5190 | |
5191 | if (cupsdSendCommand(con, command, options, 1)) | |
5192 | { | |
5193 | /* | |
5194 | * Command started successfully, don't send an IPP response here... | |
5195 | */ | |
5196 | ||
5197 | ippDelete(con->response); | |
5198 | con->response = NULL; | |
5199 | } | |
5200 | else | |
5201 | { | |
5202 | /* | |
5203 | * Command failed, return "internal error" so the user knows something | |
5204 | * went wrong... | |
5205 | */ | |
5206 | ||
5207 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
5208 | _("cups-deviced failed to execute.")); | |
5209 | } | |
5210 | } | |
5211 | ||
5212 | ||
5213 | /* | |
fa73b229 | 5214 | * 'get_job_attrs()' - Get job attributes. |
ef416fc2 | 5215 | */ |
5216 | ||
5217 | static void | |
fa73b229 | 5218 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ |
5219 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 5220 | { |
5221 | http_status_t status; /* Policy status */ | |
fa73b229 | 5222 | ipp_attribute_t *attr; /* Current attribute */ |
5223 | int jobid; /* Job ID */ | |
5224 | cupsd_job_t *job; /* Current job */ | |
ef416fc2 | 5225 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
5226 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
5227 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
5228 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
5229 | int port; /* Port portion of URI */ | |
fa73b229 | 5230 | cups_array_t *ra; /* Requested attributes array */ |
ef416fc2 | 5231 | |
5232 | ||
fa73b229 | 5233 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, |
5234 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5235 | |
5236 | /* | |
fa73b229 | 5237 | * See if we have a job URI or a printer URI... |
5238 | */ | |
5239 | ||
5240 | if (!strcmp(uri->name, "printer-uri")) | |
5241 | { | |
5242 | /* | |
5243 | * Got a printer URI; see if we also have a job-id attribute... | |
5244 | */ | |
5245 | ||
5246 | if ((attr = ippFindAttribute(con->request, "job-id", | |
5247 | IPP_TAG_INTEGER)) == NULL) | |
5248 | { | |
5249 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5250 | _("Got a printer-uri attribute but no job-id!")); | |
5251 | return; | |
5252 | } | |
5253 | ||
5254 | jobid = attr->values[0].integer; | |
5255 | } | |
5256 | else | |
5257 | { | |
5258 | /* | |
5259 | * Got a job URI; parse it to get the job ID... | |
5260 | */ | |
5261 | ||
a4d04587 | 5262 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5263 | sizeof(method), username, sizeof(username), host, | |
5264 | sizeof(host), &port, resource, sizeof(resource)); | |
fa73b229 | 5265 | |
5266 | if (strncmp(resource, "/jobs/", 6)) | |
5267 | { | |
5268 | /* | |
5269 | * Not a valid URI! | |
5270 | */ | |
5271 | ||
5272 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5273 | _("Bad job-uri attribute \"%s\"!"), | |
5274 | uri->values[0].string.text); | |
5275 | return; | |
5276 | } | |
5277 | ||
5278 | jobid = atoi(resource + 6); | |
5279 | } | |
5280 | ||
5281 | /* | |
5282 | * See if the job exists... | |
5283 | */ | |
5284 | ||
5285 | if ((job = cupsdFindJob(jobid)) == NULL) | |
5286 | { | |
5287 | /* | |
5288 | * Nope - return a "not found" error... | |
5289 | */ | |
5290 | ||
5291 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
5292 | return; | |
5293 | } | |
5294 | ||
5295 | /* | |
5296 | * Check policy... | |
5297 | */ | |
5298 | ||
5299 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5300 | { | |
5301 | send_http_error(con, status); | |
5302 | return; | |
5303 | } | |
5304 | ||
5305 | /* | |
5306 | * Copy attributes... | |
5307 | */ | |
5308 | ||
bd7854cb | 5309 | cupsdLoadJob(job); |
5310 | ||
fa73b229 | 5311 | ra = create_requested_array(con->request); |
5312 | copy_job_attrs(con, job, ra); | |
5313 | cupsArrayDelete(ra); | |
5314 | ||
5315 | con->response->request.status.status_code = IPP_OK; | |
5316 | } | |
5317 | ||
5318 | ||
5319 | /* | |
5320 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
5321 | */ | |
5322 | ||
5323 | static void | |
5324 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
5325 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5326 | { | |
5327 | http_status_t status; /* Policy status */ | |
5328 | ipp_attribute_t *attr; /* Current attribute */ | |
5329 | const char *dest; /* Destination */ | |
5330 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
5331 | cups_ptype_t dmask; /* Destination type mask */ | |
f7faf1f5 | 5332 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
fa73b229 | 5333 | username[HTTP_MAX_URI], /* Username portion of URI */ |
5334 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
5335 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
5336 | int port; /* Port portion of URI */ | |
5337 | int completed; /* Completed jobs? */ | |
5338 | int first_job_id; /* First job ID */ | |
5339 | int limit; /* Maximum number of jobs to return */ | |
5340 | int count; /* Number of jobs that match */ | |
5341 | cupsd_job_t *job; /* Current job pointer */ | |
5342 | cupsd_printer_t *printer; /* Printer */ | |
5343 | cups_array_t *list; /* Which job list... */ | |
5344 | cups_array_t *ra; /* Requested attributes array */ | |
5345 | ||
5346 | ||
5347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd, | |
5348 | uri->values[0].string.text); | |
5349 | ||
5350 | /* | |
5351 | * Is the destination valid? | |
ef416fc2 | 5352 | */ |
5353 | ||
f7faf1f5 | 5354 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5355 | sizeof(method), username, sizeof(username), host, | |
a4d04587 | 5356 | sizeof(host), &port, resource, sizeof(resource)); |
ef416fc2 | 5357 | |
fa73b229 | 5358 | if (!strcmp(resource, "/") || |
5359 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6)) | |
ef416fc2 | 5360 | { |
5361 | dest = NULL; | |
5362 | dtype = (cups_ptype_t)0; | |
5363 | dmask = (cups_ptype_t)0; | |
5364 | printer = NULL; | |
5365 | } | |
fa73b229 | 5366 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) |
ef416fc2 | 5367 | { |
5368 | dest = NULL; | |
5369 | dtype = (cups_ptype_t)0; | |
5370 | dmask = CUPS_PRINTER_CLASS; | |
5371 | printer = NULL; | |
5372 | } | |
fa73b229 | 5373 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) |
ef416fc2 | 5374 | { |
5375 | dest = NULL; | |
5376 | dtype = CUPS_PRINTER_CLASS; | |
5377 | dmask = CUPS_PRINTER_CLASS; | |
5378 | printer = NULL; | |
5379 | } | |
f7faf1f5 | 5380 | else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) |
ef416fc2 | 5381 | { |
5382 | /* | |
5383 | * Bad URI... | |
5384 | */ | |
5385 | ||
5386 | send_ipp_status(con, IPP_NOT_FOUND, | |
5387 | _("The printer or class was not found.")); | |
5388 | return; | |
5389 | } | |
5390 | else | |
5391 | dmask = CUPS_PRINTER_CLASS; | |
5392 | ||
5393 | /* | |
5394 | * Check policy... | |
5395 | */ | |
5396 | ||
5397 | if (printer) | |
5398 | { | |
f7faf1f5 | 5399 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5400 | { |
5401 | send_http_error(con, status); | |
5402 | return; | |
5403 | } | |
5404 | } | |
5405 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5406 | { | |
5407 | send_http_error(con, status); | |
5408 | return; | |
5409 | } | |
5410 | ||
5411 | /* | |
5412 | * See if the "which-jobs" attribute have been specified... | |
5413 | */ | |
5414 | ||
fa73b229 | 5415 | if ((attr = ippFindAttribute(con->request, "which-jobs", |
5416 | IPP_TAG_KEYWORD)) != NULL && | |
ef416fc2 | 5417 | !strcmp(attr->values[0].string.text, "completed")) |
5418 | { | |
5419 | completed = 1; | |
5420 | list = Jobs; | |
5421 | } | |
5422 | else if (attr && !strcmp(attr->values[0].string.text, "all")) | |
5423 | { | |
5424 | completed = 0; | |
5425 | list = Jobs; | |
5426 | } | |
5427 | else | |
5428 | { | |
5429 | completed = 0; | |
5430 | list = ActiveJobs; | |
5431 | } | |
5432 | ||
5433 | /* | |
5434 | * See if they want to limit the number of jobs reported... | |
5435 | */ | |
5436 | ||
fa73b229 | 5437 | if ((attr = ippFindAttribute(con->request, "limit", |
5438 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 5439 | limit = attr->values[0].integer; |
5440 | else | |
5441 | limit = 1000000; | |
5442 | ||
5443 | if ((attr = ippFindAttribute(con->request, "first-job-id", | |
5444 | IPP_TAG_INTEGER)) != NULL) | |
5445 | first_job_id = attr->values[0].integer; | |
5446 | else | |
5447 | first_job_id = 1; | |
5448 | ||
5449 | /* | |
5450 | * See if we only want to see jobs for a specific user... | |
5451 | */ | |
5452 | ||
fa73b229 | 5453 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
5454 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 5455 | attr->values[0].boolean) |
e00b005a | 5456 | strlcpy(username, get_username(con), sizeof(username)); |
ef416fc2 | 5457 | else |
5458 | username[0] = '\0'; | |
5459 | ||
fa73b229 | 5460 | ra = create_requested_array(con->request); |
ef416fc2 | 5461 | |
5462 | /* | |
5463 | * OK, build a list of jobs for this printer... | |
5464 | */ | |
5465 | ||
5466 | for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list); | |
5467 | count < limit && job; | |
5468 | job = (cupsd_job_t *)cupsArrayNext(list)) | |
5469 | { | |
5470 | /* | |
5471 | * Filter out jobs that don't match... | |
5472 | */ | |
5473 | ||
5474 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id); | |
5475 | ||
fa73b229 | 5476 | if ((dest && strcmp(job->dest, dest)) && |
5477 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
ef416fc2 | 5478 | continue; |
5479 | if ((job->dtype & dmask) != dtype && | |
fa73b229 | 5480 | (!job->printer || (job->printer->type & dmask) != dtype)) |
ef416fc2 | 5481 | continue; |
fa73b229 | 5482 | if (username[0] && strcasecmp(username, job->username)) |
ef416fc2 | 5483 | continue; |
5484 | ||
bd7854cb | 5485 | if (completed && job->state_value <= IPP_JOB_STOPPED) |
ef416fc2 | 5486 | continue; |
5487 | ||
5488 | if (job->id < first_job_id) | |
5489 | continue; | |
5490 | ||
bd7854cb | 5491 | cupsdLoadJob(job); |
5492 | ||
5493 | if (!job->attrs) | |
5494 | continue; | |
5495 | ||
fa73b229 | 5496 | if (count > 0) |
5497 | ippAddSeparator(con->response); | |
5498 | ||
ef416fc2 | 5499 | count ++; |
5500 | ||
5501 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count); | |
5502 | ||
fa73b229 | 5503 | copy_job_attrs(con, job, ra); |
ef416fc2 | 5504 | } |
5505 | ||
fa73b229 | 5506 | cupsArrayDelete(ra); |
ef416fc2 | 5507 | |
fa73b229 | 5508 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 5509 | } |
5510 | ||
5511 | ||
5512 | /* | |
5513 | * 'get_notifications()' - Get events for a subscription. | |
5514 | */ | |
5515 | ||
5516 | static void | |
bd7854cb | 5517 | get_notifications(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 5518 | { |
bd7854cb | 5519 | int i, j; /* Looping vars */ |
5520 | http_status_t status; /* Policy status */ | |
5521 | cupsd_subscription_t *sub; /* Subscription */ | |
5522 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
5523 | *sequences; /* notify-sequence-numbers */ | |
5524 | int min_seq; /* Minimum sequence number */ | |
5525 | int interval; /* Poll interval */ | |
5526 | ||
5527 | ||
5528 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_subscription_attrs(con=%p[%d])", | |
5529 | con, con->http.fd); | |
5530 | ||
5531 | /* | |
5532 | * Get subscription attributes... | |
5533 | */ | |
5534 | ||
5535 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
5536 | IPP_TAG_INTEGER); | |
5537 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
5538 | IPP_TAG_INTEGER); | |
5539 | ||
5540 | if (!ids) | |
5541 | { | |
5542 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5543 | _("Missing notify-subscription-ids attribute!")); | |
5544 | return; | |
5545 | } | |
5546 | ||
5547 | /* | |
5548 | * Are the subscription IDs valid? | |
5549 | */ | |
5550 | ||
5551 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
5552 | { | |
5553 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
5554 | { | |
5555 | /* | |
5556 | * Bad subscription ID... | |
5557 | */ | |
5558 | ||
5559 | send_ipp_status(con, IPP_NOT_FOUND, | |
5560 | _("notify-subscription-id %d no good!"), | |
5561 | ids->values[i].integer); | |
5562 | return; | |
5563 | } | |
5564 | ||
5565 | /* | |
5566 | * Check policy... | |
5567 | */ | |
5568 | ||
5569 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
5570 | DefaultPolicyPtr, | |
5571 | con, sub->owner)) != HTTP_OK) | |
5572 | { | |
5573 | send_http_error(con, status); | |
5574 | return; | |
5575 | } | |
5576 | ||
5577 | /* | |
5578 | * Check the subscription type and update the interval accordingly. | |
5579 | */ | |
5580 | ||
5581 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
5582 | interval > 10) | |
5583 | interval = 10; | |
5584 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
5585 | interval = 0; | |
5586 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
5587 | interval > 30) | |
5588 | interval = 30; | |
5589 | } | |
5590 | ||
5591 | /* | |
5592 | * Tell the client to poll again in N seconds... | |
5593 | */ | |
5594 | ||
5595 | if (interval > 0) | |
5596 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
5597 | "notify-get-interval", interval); | |
5598 | ||
5599 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
5600 | "printer-up-time", time(NULL)); | |
5601 | ||
5602 | /* | |
5603 | * Copy the subscription event attributes to the response. | |
5604 | */ | |
5605 | ||
5606 | con->response->request.status.status_code = | |
5607 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
5608 | ||
5609 | for (i = 0; i < ids->num_values; i ++) | |
5610 | { | |
5611 | /* | |
5612 | * Get the subscription and sequence number... | |
5613 | */ | |
5614 | ||
5615 | sub = cupsdFindSubscription(ids->values[i].integer); | |
5616 | ||
5617 | if (sequences && i < sequences->num_values) | |
5618 | min_seq = sequences->values[i].integer; | |
5619 | else | |
5620 | min_seq = 1; | |
5621 | ||
5622 | /* | |
5623 | * If we don't have any new events, nothing to do here... | |
5624 | */ | |
5625 | ||
5626 | if (min_seq > (sub->first_event_id + sub->num_events)) | |
5627 | continue; | |
5628 | ||
5629 | /* | |
5630 | * Otherwise copy all of the new events... | |
5631 | */ | |
5632 | ||
5633 | if (sub->first_event_id > min_seq) | |
5634 | j = 0; | |
5635 | else | |
5636 | j = min_seq - sub->first_event_id; | |
5637 | ||
b423cd4c | 5638 | for (; j < sub->num_events; j ++) |
5639 | { | |
5640 | ippAddSeparator(con->response); | |
ef416fc2 | 5641 | |
b423cd4c | 5642 | copy_attrs(con->response, sub->events[j]->attrs, NULL, |
5643 | IPP_TAG_EVENT_NOTIFICATION, 0); | |
5644 | } | |
5645 | } | |
5646 | } | |
ef416fc2 | 5647 | |
ef416fc2 | 5648 | |
b423cd4c | 5649 | /* |
5650 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
5651 | */ | |
ef416fc2 | 5652 | |
b423cd4c | 5653 | static void |
5654 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
5655 | { | |
5656 | http_status_t status; /* Policy status */ | |
b423cd4c | 5657 | ipp_attribute_t *limit, /* Limit attribute */ |
5658 | *make, /* ppd-make attribute */ | |
5659 | *requested; /* requested-attributes attribute */ | |
5660 | char command[1024], /* cups-deviced command */ | |
5661 | options[1024], /* Options to pass to command */ | |
89d46774 | 5662 | requested_str[256], |
5663 | /* String for requested attributes */ | |
5664 | make_str[256]; /* Escaped ppd-make string */ | |
ef416fc2 | 5665 | |
ef416fc2 | 5666 | |
b423cd4c | 5667 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd); |
ef416fc2 | 5668 | |
5669 | /* | |
b423cd4c | 5670 | * Check policy... |
ef416fc2 | 5671 | */ |
5672 | ||
b423cd4c | 5673 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5674 | { |
b423cd4c | 5675 | send_http_error(con, status); |
5676 | return; | |
ef416fc2 | 5677 | } |
ef416fc2 | 5678 | |
b423cd4c | 5679 | /* |
5680 | * Run cups-driverd command with the given options... | |
5681 | */ | |
5682 | ||
5683 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
5684 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
5685 | requested = ippFindAttribute(con->request, "requested-attributes", | |
5686 | IPP_TAG_KEYWORD); | |
5687 | ||
5688 | if (requested) | |
89d46774 | 5689 | url_encode_attr(requested, requested_str, sizeof(requested_str)); |
5690 | else | |
5691 | strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str)); | |
ef416fc2 | 5692 | |
89d46774 | 5693 | if (make) |
5694 | url_encode_attr(make, make_str, sizeof(make_str)); | |
b423cd4c | 5695 | else |
89d46774 | 5696 | make_str[0] = '\0'; |
fa73b229 | 5697 | |
b423cd4c | 5698 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); |
89d46774 | 5699 | snprintf(options, sizeof(options), "list+%d+%d+%s%s%s", |
b423cd4c | 5700 | con->request->request.op.request_id, |
5701 | limit ? limit->values[0].integer : 0, | |
89d46774 | 5702 | requested_str, make ? "%20" : "", make_str); |
ef416fc2 | 5703 | |
b423cd4c | 5704 | if (cupsdSendCommand(con, command, options, 0)) |
5705 | { | |
5706 | /* | |
5707 | * Command started successfully, don't send an IPP response here... | |
5708 | */ | |
5709 | ||
5710 | ippDelete(con->response); | |
5711 | con->response = NULL; | |
5712 | } | |
5713 | else | |
5714 | { | |
5715 | /* | |
5716 | * Command failed, return "internal error" so the user knows something | |
5717 | * went wrong... | |
5718 | */ | |
5719 | ||
5720 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
5721 | _("cups-driverd failed to execute.")); | |
5722 | } | |
ef416fc2 | 5723 | } |
5724 | ||
5725 | ||
5726 | /* | |
b423cd4c | 5727 | * 'get_printer_attrs()' - Get printer attributes. |
ef416fc2 | 5728 | */ |
5729 | ||
5730 | static void | |
b423cd4c | 5731 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ |
5732 | ipp_attribute_t *uri) /* I - Printer URI */ | |
ef416fc2 | 5733 | { |
5734 | http_status_t status; /* Policy status */ | |
f7faf1f5 | 5735 | const char *dest; /* Destination */ |
b423cd4c | 5736 | cups_ptype_t dtype; /* Destination type (printer or class) */ |
f7faf1f5 | 5737 | char method[HTTP_MAX_URI], |
5738 | /* Method portion of URI */ | |
5739 | username[HTTP_MAX_URI], | |
5740 | /* Username portion of URI */ | |
5741 | host[HTTP_MAX_URI], | |
5742 | /* Host portion of URI */ | |
5743 | resource[HTTP_MAX_URI]; | |
5744 | /* Resource portion of URI */ | |
5745 | int port; /* Port portion of URI */ | |
b423cd4c | 5746 | cupsd_printer_t *printer; /* Printer/class */ |
ef416fc2 | 5747 | cups_array_t *ra; /* Requested attributes array */ |
5748 | ||
5749 | ||
b423cd4c | 5750 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, |
5751 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5752 | |
5753 | /* | |
b423cd4c | 5754 | * Is the destination valid? |
ef416fc2 | 5755 | */ |
5756 | ||
f7faf1f5 | 5757 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5758 | sizeof(method), username, sizeof(username), host, | |
5759 | sizeof(host), &port, resource, sizeof(resource)); | |
5760 | ||
5761 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 5762 | { |
5763 | /* | |
b423cd4c | 5764 | * Bad URI... |
ef416fc2 | 5765 | */ |
5766 | ||
5767 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 5768 | _("The printer or class was not found.")); |
ef416fc2 | 5769 | return; |
5770 | } | |
5771 | ||
5772 | /* | |
5773 | * Check policy... | |
5774 | */ | |
5775 | ||
b423cd4c | 5776 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5777 | { |
5778 | send_http_error(con, status); | |
5779 | return; | |
5780 | } | |
5781 | ||
5782 | /* | |
b423cd4c | 5783 | * Send the attributes... |
ef416fc2 | 5784 | */ |
5785 | ||
5786 | ra = create_requested_array(con->request); | |
5787 | ||
b423cd4c | 5788 | copy_printer_attrs(con, printer, ra); |
ef416fc2 | 5789 | |
5790 | cupsArrayDelete(ra); | |
5791 | ||
5792 | con->response->request.status.status_code = IPP_OK; | |
5793 | } | |
5794 | ||
5795 | ||
5796 | /* | |
b423cd4c | 5797 | * 'get_printers()' - Get a list of printers or classes. |
ef416fc2 | 5798 | */ |
5799 | ||
5800 | static void | |
b423cd4c | 5801 | get_printers(cupsd_client_t *con, /* I - Client connection */ |
5802 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
ef416fc2 | 5803 | { |
b423cd4c | 5804 | http_status_t status; /* Policy status */ |
5805 | ipp_attribute_t *attr; /* Current attribute */ | |
5806 | int limit; /* Maximum number of printers to return */ | |
5807 | int count; /* Number of printers that match */ | |
5808 | cupsd_printer_t *printer; /* Current printer pointer */ | |
5809 | int printer_type, /* printer-type attribute */ | |
5810 | printer_mask; /* printer-type-mask attribute */ | |
5811 | char *location; /* Location string */ | |
5812 | const char *username; /* Current user */ | |
5813 | char *first_printer_name; /* first-printer-name attribute */ | |
5814 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5815 | |
ef416fc2 | 5816 | |
b423cd4c | 5817 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, |
5818 | con->http.fd, type); | |
ef416fc2 | 5819 | |
5820 | /* | |
5821 | * Check policy... | |
5822 | */ | |
5823 | ||
b423cd4c | 5824 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5825 | { |
5826 | send_http_error(con, status); | |
5827 | return; | |
5828 | } | |
5829 | ||
5830 | /* | |
b423cd4c | 5831 | * Check for printers... |
ef416fc2 | 5832 | */ |
5833 | ||
b423cd4c | 5834 | if (!Printers || !cupsArrayCount(Printers)) |
5835 | { | |
5836 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
5837 | return; | |
5838 | } | |
5839 | ||
5840 | /* | |
5841 | * See if they want to limit the number of printers reported... | |
5842 | */ | |
ef416fc2 | 5843 | |
fa73b229 | 5844 | if ((attr = ippFindAttribute(con->request, "limit", |
5845 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 5846 | limit = attr->values[0].integer; |
5847 | else | |
b423cd4c | 5848 | limit = 10000000; |
5849 | ||
5850 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
5851 | IPP_TAG_NAME)) != NULL) | |
5852 | first_printer_name = attr->values[0].string.text; | |
5853 | else | |
5854 | first_printer_name = NULL; | |
ef416fc2 | 5855 | |
5856 | /* | |
b423cd4c | 5857 | * Support filtering... |
ef416fc2 | 5858 | */ |
5859 | ||
b423cd4c | 5860 | if ((attr = ippFindAttribute(con->request, "printer-type", |
5861 | IPP_TAG_ENUM)) != NULL) | |
5862 | printer_type = attr->values[0].integer; | |
ef416fc2 | 5863 | else |
b423cd4c | 5864 | printer_type = 0; |
ef416fc2 | 5865 | |
b423cd4c | 5866 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", |
5867 | IPP_TAG_ENUM)) != NULL) | |
5868 | printer_mask = attr->values[0].integer; | |
ef416fc2 | 5869 | else |
b423cd4c | 5870 | printer_mask = 0; |
e00b005a | 5871 | |
b423cd4c | 5872 | if ((attr = ippFindAttribute(con->request, "printer-location", |
5873 | IPP_TAG_TEXT)) != NULL) | |
5874 | location = attr->values[0].string.text; | |
5875 | else | |
5876 | location = NULL; | |
e00b005a | 5877 | |
5878 | if (con->username[0]) | |
b423cd4c | 5879 | username = con->username; |
e00b005a | 5880 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
5881 | IPP_TAG_NAME)) != NULL) | |
b423cd4c | 5882 | username = attr->values[0].string.text; |
e00b005a | 5883 | else |
b423cd4c | 5884 | username = NULL; |
ef416fc2 | 5885 | |
b423cd4c | 5886 | ra = create_requested_array(con->request); |
ef416fc2 | 5887 | |
5888 | /* | |
b423cd4c | 5889 | * OK, build a list of printers for this printer... |
ef416fc2 | 5890 | */ |
5891 | ||
b423cd4c | 5892 | if (first_printer_name) |
ef416fc2 | 5893 | { |
b423cd4c | 5894 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) |
5895 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 5896 | } |
5897 | else | |
b423cd4c | 5898 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
ef416fc2 | 5899 | |
b423cd4c | 5900 | for (count = 0; |
5901 | count < limit && printer; | |
5902 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
5903 | { | |
5904 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && | |
5905 | (printer->type & printer_mask) == printer_type && | |
5906 | (!location || !printer->location || | |
5907 | !strcasecmp(printer->location, location))) | |
ef416fc2 | 5908 | { |
5909 | /* | |
b423cd4c | 5910 | * If HideImplicitMembers is enabled, see if this printer or class |
5911 | * is a member of an implicit class... | |
ef416fc2 | 5912 | */ |
5913 | ||
b423cd4c | 5914 | if (ImplicitClasses && HideImplicitMembers && |
5915 | printer->in_implicit_class) | |
5916 | continue; | |
ef416fc2 | 5917 | |
b423cd4c | 5918 | /* |
5919 | * If a username is specified, see if it is allowed or denied | |
5920 | * access... | |
5921 | */ | |
ef416fc2 | 5922 | |
b423cd4c | 5923 | if (printer->num_users && username && !user_allowed(printer, username)) |
5924 | continue; | |
ef416fc2 | 5925 | |
b423cd4c | 5926 | /* |
5927 | * Add the group separator as needed... | |
5928 | */ | |
ef416fc2 | 5929 | |
b423cd4c | 5930 | if (count > 0) |
5931 | ippAddSeparator(con->response); | |
ef416fc2 | 5932 | |
b423cd4c | 5933 | count ++; |
ef416fc2 | 5934 | |
b423cd4c | 5935 | /* |
5936 | * Send the attributes... | |
5937 | */ | |
ef416fc2 | 5938 | |
b423cd4c | 5939 | copy_printer_attrs(con, printer, ra); |
5940 | } | |
ef416fc2 | 5941 | } |
5942 | ||
b423cd4c | 5943 | cupsArrayDelete(ra); |
ef416fc2 | 5944 | |
5945 | con->response->request.status.status_code = IPP_OK; | |
5946 | } | |
5947 | ||
5948 | ||
5949 | /* | |
b423cd4c | 5950 | * 'get_subscription_attrs()' - Get subscription attributes. |
ef416fc2 | 5951 | */ |
5952 | ||
5953 | static void | |
b423cd4c | 5954 | get_subscription_attrs( |
5955 | cupsd_client_t *con, /* I - Client connection */ | |
5956 | int sub_id) /* I - Subscription ID */ | |
ef416fc2 | 5957 | { |
b423cd4c | 5958 | http_status_t status; /* Policy status */ |
5959 | cupsd_subscription_t *sub; /* Subscription */ | |
5960 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5961 | |
5962 | ||
b423cd4c | 5963 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
5964 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
5965 | con, con->http.fd, sub_id); | |
ef416fc2 | 5966 | |
fa73b229 | 5967 | /* |
b423cd4c | 5968 | * Is the subscription ID valid? |
fa73b229 | 5969 | */ |
5970 | ||
b423cd4c | 5971 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
fa73b229 | 5972 | { |
5973 | /* | |
b423cd4c | 5974 | * Bad subscription ID... |
fa73b229 | 5975 | */ |
5976 | ||
5977 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 5978 | _("notify-subscription-id %d no good!"), sub_id); |
fa73b229 | 5979 | return; |
5980 | } | |
5981 | ||
5982 | /* | |
5983 | * Check policy... | |
5984 | */ | |
5985 | ||
b423cd4c | 5986 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
5987 | DefaultPolicyPtr, | |
5988 | con, sub->owner)) != HTTP_OK) | |
fa73b229 | 5989 | { |
5990 | send_http_error(con, status); | |
5991 | return; | |
5992 | } | |
5993 | ||
ef416fc2 | 5994 | /* |
b423cd4c | 5995 | * Copy the subscription attributes to the response using the |
5996 | * requested-attributes attribute that may be provided by the client. | |
ef416fc2 | 5997 | */ |
5998 | ||
b423cd4c | 5999 | ra = create_requested_array(con->request); |
fa73b229 | 6000 | |
b423cd4c | 6001 | copy_subscription_attrs(con, sub, ra); |
ef416fc2 | 6002 | |
b423cd4c | 6003 | cupsArrayDelete(ra); |
fa73b229 | 6004 | |
b423cd4c | 6005 | con->response->request.status.status_code = IPP_OK; |
6006 | } | |
fa73b229 | 6007 | |
fa73b229 | 6008 | |
b423cd4c | 6009 | /* |
6010 | * 'get_subscriptions()' - Get subscriptions. | |
6011 | */ | |
ef416fc2 | 6012 | |
b423cd4c | 6013 | static void |
6014 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
6015 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
6016 | { | |
6017 | http_status_t status; /* Policy status */ | |
6018 | int count; /* Number of subscriptions */ | |
6019 | int limit; /* Limit */ | |
6020 | cupsd_subscription_t *sub; /* Subscription */ | |
6021 | cups_array_t *ra; /* Requested attributes array */ | |
6022 | ipp_attribute_t *attr; /* Attribute */ | |
6023 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 6024 | char method[HTTP_MAX_URI], |
6025 | /* Method portion of URI */ | |
b423cd4c | 6026 | username[HTTP_MAX_URI], |
6027 | /* Username portion of URI */ | |
6028 | host[HTTP_MAX_URI], | |
6029 | /* Host portion of URI */ | |
6030 | resource[HTTP_MAX_URI]; | |
6031 | /* Resource portion of URI */ | |
6032 | int port; /* Port portion of URI */ | |
6033 | cupsd_job_t *job; /* Job pointer */ | |
6034 | cupsd_printer_t *printer; /* Printer */ | |
fa73b229 | 6035 | |
fa73b229 | 6036 | |
b423cd4c | 6037 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
6038 | "get_subscriptions(con=%p[%d], uri=%s)", | |
6039 | con, con->http.fd, uri->values[0].string.text); | |
6040 | ||
6041 | /* | |
6042 | * Is the destination valid? | |
6043 | */ | |
6044 | ||
f7faf1f5 | 6045 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6046 | sizeof(method), username, sizeof(username), host, | |
b423cd4c | 6047 | sizeof(host), &port, resource, sizeof(resource)); |
6048 | ||
6049 | if (!strcmp(resource, "/") || | |
6050 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
6051 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
6052 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
6053 | { | |
6054 | printer = NULL; | |
6055 | job = NULL; | |
ef416fc2 | 6056 | } |
b423cd4c | 6057 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) |
ef416fc2 | 6058 | { |
b423cd4c | 6059 | printer = NULL; |
6060 | job = cupsdFindJob(atoi(resource + 6)); | |
ef416fc2 | 6061 | |
b423cd4c | 6062 | if (!job) |
ef416fc2 | 6063 | { |
b423cd4c | 6064 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"), |
6065 | resource + 6); | |
ef416fc2 | 6066 | return; |
6067 | } | |
b423cd4c | 6068 | } |
f7faf1f5 | 6069 | else if (!cupsdValidateDest(host, resource, &dtype, &printer)) |
b423cd4c | 6070 | { |
fa73b229 | 6071 | /* |
b423cd4c | 6072 | * Bad URI... |
fa73b229 | 6073 | */ |
6074 | ||
b423cd4c | 6075 | send_ipp_status(con, IPP_NOT_FOUND, |
6076 | _("The printer or class was not found.")); | |
6077 | return; | |
6078 | } | |
6079 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
6080 | IPP_TAG_INTEGER)) != NULL) | |
6081 | { | |
6082 | job = cupsdFindJob(attr->values[0].integer); | |
ef416fc2 | 6083 | |
b423cd4c | 6084 | if (!job) |
fa73b229 | 6085 | { |
b423cd4c | 6086 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), |
6087 | attr->values[0].integer); | |
fa73b229 | 6088 | return; |
6089 | } | |
ef416fc2 | 6090 | } |
b423cd4c | 6091 | else |
6092 | job = NULL; | |
ef416fc2 | 6093 | |
6094 | /* | |
b423cd4c | 6095 | * Check policy... |
ef416fc2 | 6096 | */ |
6097 | ||
b423cd4c | 6098 | if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr : |
6099 | DefaultPolicyPtr, | |
6100 | con, NULL)) != HTTP_OK) | |
ef416fc2 | 6101 | { |
b423cd4c | 6102 | send_http_error(con, status); |
6103 | return; | |
6104 | } | |
ef416fc2 | 6105 | |
b423cd4c | 6106 | /* |
6107 | * Copy the subscription attributes to the response using the | |
6108 | * requested-attributes attribute that may be provided by the client. | |
6109 | */ | |
ef416fc2 | 6110 | |
b423cd4c | 6111 | ra = create_requested_array(con->request); |
ef416fc2 | 6112 | |
b423cd4c | 6113 | if ((attr = ippFindAttribute(con->request, "limit", |
6114 | IPP_TAG_INTEGER)) != NULL) | |
6115 | limit = attr->values[0].integer; | |
6116 | else | |
6117 | limit = 0; | |
ef416fc2 | 6118 | |
b423cd4c | 6119 | /* |
6120 | * See if we only want to see subscriptions for a specific user... | |
6121 | */ | |
ef416fc2 | 6122 | |
b423cd4c | 6123 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", |
6124 | IPP_TAG_BOOLEAN)) != NULL && | |
6125 | attr->values[0].boolean) | |
6126 | strlcpy(username, get_username(con), sizeof(username)); | |
fa73b229 | 6127 | else |
b423cd4c | 6128 | username[0] = '\0'; |
ef416fc2 | 6129 | |
b423cd4c | 6130 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; |
6131 | sub; | |
6132 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
6133 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
6134 | (!username[0] || !strcasecmp(username, sub->owner))) | |
fa73b229 | 6135 | { |
b423cd4c | 6136 | ippAddSeparator(con->response); |
6137 | copy_subscription_attrs(con, sub, ra); | |
ef416fc2 | 6138 | |
b423cd4c | 6139 | count ++; |
6140 | if (limit && count >= limit) | |
6141 | break; | |
6142 | } | |
ef416fc2 | 6143 | |
b423cd4c | 6144 | cupsArrayDelete(ra); |
fa73b229 | 6145 | |
b423cd4c | 6146 | if (count) |
6147 | con->response->request.status.status_code = IPP_OK; | |
6148 | else | |
6149 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
6150 | } | |
ef416fc2 | 6151 | |
ef416fc2 | 6152 | |
b423cd4c | 6153 | /* |
6154 | * 'get_username()' - Get the username associated with a request. | |
6155 | */ | |
ef416fc2 | 6156 | |
b423cd4c | 6157 | static const char * /* O - Username */ |
6158 | get_username(cupsd_client_t *con) /* I - Connection */ | |
6159 | { | |
6160 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 6161 | |
ef416fc2 | 6162 | |
b423cd4c | 6163 | if (con->username[0]) |
6164 | return (con->username); | |
6165 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
6166 | IPP_TAG_NAME)) != NULL) | |
6167 | return (attr->values[0].string.text); | |
6168 | else | |
6169 | return ("anonymous"); | |
ef416fc2 | 6170 | } |
6171 | ||
6172 | ||
6173 | /* | |
b423cd4c | 6174 | * 'hold_job()' - Hold a print job. |
ef416fc2 | 6175 | */ |
6176 | ||
b423cd4c | 6177 | static void |
6178 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
6179 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 6180 | { |
b423cd4c | 6181 | ipp_attribute_t *attr, /* Current job-hold-until */ |
6182 | *newattr; /* New job-hold-until */ | |
6183 | int jobid; /* Job ID */ | |
6184 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6185 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6186 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6187 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6188 | int port; /* Port portion of URI */ | |
6189 | cupsd_job_t *job; /* Job information */ | |
6190 | ||
ef416fc2 | 6191 | |
b423cd4c | 6192 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd, |
6193 | uri->values[0].string.text); | |
ef416fc2 | 6194 | |
6195 | /* | |
b423cd4c | 6196 | * See if we have a job URI or a printer URI... |
ef416fc2 | 6197 | */ |
6198 | ||
b423cd4c | 6199 | if (!strcmp(uri->name, "printer-uri")) |
6200 | { | |
6201 | /* | |
6202 | * Got a printer URI; see if we also have a job-id attribute... | |
6203 | */ | |
ef416fc2 | 6204 | |
b423cd4c | 6205 | if ((attr = ippFindAttribute(con->request, "job-id", |
6206 | IPP_TAG_INTEGER)) == NULL) | |
6207 | { | |
6208 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6209 | _("Got a printer-uri attribute but no job-id!")); | |
6210 | return; | |
6211 | } | |
ef416fc2 | 6212 | |
b423cd4c | 6213 | jobid = attr->values[0].integer; |
6214 | } | |
ef416fc2 | 6215 | else |
ef416fc2 | 6216 | { |
b423cd4c | 6217 | /* |
6218 | * Got a job URI; parse it to get the job ID... | |
6219 | */ | |
ef416fc2 | 6220 | |
b423cd4c | 6221 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6222 | sizeof(method), username, sizeof(username), host, | |
6223 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 6224 | |
b423cd4c | 6225 | if (strncmp(resource, "/jobs/", 6)) |
6226 | { | |
6227 | /* | |
6228 | * Not a valid URI! | |
6229 | */ | |
ef416fc2 | 6230 | |
b423cd4c | 6231 | send_ipp_status(con, IPP_BAD_REQUEST, |
6232 | _("Bad job-uri attribute \"%s\"!"), | |
6233 | uri->values[0].string.text); | |
6234 | return; | |
6235 | } | |
ef416fc2 | 6236 | |
b423cd4c | 6237 | jobid = atoi(resource + 6); |
6238 | } | |
ef416fc2 | 6239 | |
ef416fc2 | 6240 | /* |
b423cd4c | 6241 | * See if the job exists... |
ef416fc2 | 6242 | */ |
6243 | ||
b423cd4c | 6244 | if ((job = cupsdFindJob(jobid)) == NULL) |
6245 | { | |
6246 | /* | |
6247 | * Nope - return a "not found" error... | |
6248 | */ | |
6249 | ||
6250 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6251 | return; | |
6252 | } | |
ef416fc2 | 6253 | |
6254 | /* | |
b423cd4c | 6255 | * See if the job is owned by the requesting user... |
ef416fc2 | 6256 | */ |
6257 | ||
b423cd4c | 6258 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6259 | { | |
6260 | send_http_error(con, HTTP_UNAUTHORIZED); | |
6261 | return; | |
6262 | } | |
ef416fc2 | 6263 | |
6264 | /* | |
b423cd4c | 6265 | * Hold the job and return... |
ef416fc2 | 6266 | */ |
6267 | ||
b423cd4c | 6268 | cupsdHoldJob(job); |
ef416fc2 | 6269 | |
b423cd4c | 6270 | if ((newattr = ippFindAttribute(con->request, "job-hold-until", |
6271 | IPP_TAG_KEYWORD)) == NULL) | |
6272 | newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 6273 | |
b423cd4c | 6274 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
6275 | IPP_TAG_KEYWORD)) == NULL) | |
6276 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 6277 | |
b423cd4c | 6278 | if (attr) |
6279 | { | |
6280 | /* | |
6281 | * Free the old hold value and copy the new one over... | |
6282 | */ | |
ef416fc2 | 6283 | |
757d2cad | 6284 | _cupsStrFree(attr->values[0].string.text); |
ef416fc2 | 6285 | |
b423cd4c | 6286 | if (newattr) |
ef416fc2 | 6287 | { |
b423cd4c | 6288 | attr->value_tag = newattr->value_tag; |
6289 | attr->values[0].string.text = | |
757d2cad | 6290 | _cupsStrAlloc(newattr->values[0].string.text); |
b423cd4c | 6291 | } |
6292 | else | |
6293 | { | |
6294 | attr->value_tag = IPP_TAG_KEYWORD; | |
757d2cad | 6295 | attr->values[0].string.text = _cupsStrAlloc("indefinite"); |
ef416fc2 | 6296 | } |
6297 | ||
b423cd4c | 6298 | /* |
6299 | * Hold job until specified time... | |
6300 | */ | |
ef416fc2 | 6301 | |
b423cd4c | 6302 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
6303 | } | |
ef416fc2 | 6304 | |
b423cd4c | 6305 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was held by \"%s\".", jobid, |
6306 | username); | |
6307 | ||
6308 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 6309 | } |
6310 | ||
6311 | ||
6312 | /* | |
b423cd4c | 6313 | * 'move_job()' - Move a job to a new destination. |
ef416fc2 | 6314 | */ |
6315 | ||
6316 | static void | |
b423cd4c | 6317 | move_job(cupsd_client_t *con, /* I - Client connection */ |
6318 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 6319 | { |
6320 | http_status_t status; /* Policy status */ | |
6321 | ipp_attribute_t *attr; /* Current attribute */ | |
b423cd4c | 6322 | int jobid; /* Job ID */ |
ef416fc2 | 6323 | cupsd_job_t *job; /* Current job */ |
b423cd4c | 6324 | const char *src, /* Source printer/class */ |
6325 | *dest; /* Destination */ | |
6326 | cups_ptype_t stype, /* Source type (printer or class) */ | |
6327 | dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 6328 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
ef416fc2 | 6329 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6330 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
b423cd4c | 6331 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
ef416fc2 | 6332 | int port; /* Port portion of URI */ |
b423cd4c | 6333 | cupsd_printer_t *sprinter, /* Source printer */ |
6334 | *dprinter; /* Destination printer */ | |
ef416fc2 | 6335 | |
6336 | ||
b423cd4c | 6337 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd, |
ef416fc2 | 6338 | uri->values[0].string.text); |
6339 | ||
6340 | /* | |
b423cd4c | 6341 | * Get the new printer or class... |
ef416fc2 | 6342 | */ |
6343 | ||
b423cd4c | 6344 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", |
6345 | IPP_TAG_URI)) == NULL) | |
ef416fc2 | 6346 | { |
b423cd4c | 6347 | /* |
6348 | * Need job-printer-uri... | |
6349 | */ | |
ef416fc2 | 6350 | |
b423cd4c | 6351 | send_ipp_status(con, IPP_BAD_REQUEST, |
6352 | _("job-printer-uri attribute missing!")); | |
6353 | return; | |
ef416fc2 | 6354 | } |
b423cd4c | 6355 | |
f7faf1f5 | 6356 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, method, |
6357 | sizeof(method), username, sizeof(username), host, | |
6358 | sizeof(host), &port, resource, sizeof(resource)); | |
6359 | ||
6360 | if ((dest = cupsdValidateDest(host, resource, &dtype, &dprinter)) == NULL) | |
ef416fc2 | 6361 | { |
b423cd4c | 6362 | /* |
6363 | * Bad URI... | |
6364 | */ | |
ef416fc2 | 6365 | |
b423cd4c | 6366 | send_ipp_status(con, IPP_NOT_FOUND, |
6367 | _("The printer or class was not found.")); | |
6368 | return; | |
ef416fc2 | 6369 | } |
6370 | ||
6371 | /* | |
b423cd4c | 6372 | * Check policy... |
ef416fc2 | 6373 | */ |
6374 | ||
f7faf1f5 | 6375 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 6376 | { |
b423cd4c | 6377 | send_http_error(con, status); |
ef416fc2 | 6378 | return; |
6379 | } | |
6380 | ||
6381 | /* | |
b423cd4c | 6382 | * See if we have a job URI or a printer URI... |
ef416fc2 | 6383 | */ |
6384 | ||
f7faf1f5 | 6385 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6386 | sizeof(method), username, sizeof(username), host, | |
b423cd4c | 6387 | sizeof(host), &port, resource, sizeof(resource)); |
6388 | ||
6389 | if (!strcmp(uri->name, "printer-uri")) | |
ef416fc2 | 6390 | { |
6391 | /* | |
b423cd4c | 6392 | * Got a printer URI; see if we also have a job-id attribute... |
ef416fc2 | 6393 | */ |
6394 | ||
b423cd4c | 6395 | if ((attr = ippFindAttribute(con->request, "job-id", |
6396 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 6397 | { |
b423cd4c | 6398 | /* |
6399 | * Move all jobs... | |
6400 | */ | |
6401 | ||
f7faf1f5 | 6402 | if ((src = cupsdValidateDest(host, resource, &stype, &sprinter)) == NULL) |
b423cd4c | 6403 | { |
6404 | /* | |
6405 | * Bad URI... | |
6406 | */ | |
6407 | ||
6408 | send_ipp_status(con, IPP_NOT_FOUND, | |
6409 | _("The printer or class was not found.")); | |
6410 | return; | |
6411 | } | |
6412 | ||
6413 | job = NULL; | |
6414 | } | |
6415 | else | |
6416 | { | |
6417 | /* | |
6418 | * Otherwise, just move a single job... | |
6419 | */ | |
6420 | ||
6421 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
6422 | { | |
6423 | /* | |
6424 | * Nope - return a "not found" error... | |
6425 | */ | |
6426 | ||
6427 | send_ipp_status(con, IPP_NOT_FOUND, | |
6428 | _("Job #%d does not exist!"), attr->values[0].integer); | |
6429 | return; | |
6430 | } | |
6431 | else | |
6432 | { | |
6433 | /* | |
6434 | * Job found, initialize source pointers... | |
6435 | */ | |
6436 | ||
6437 | src = NULL; | |
6438 | sprinter = NULL; | |
6439 | } | |
ef416fc2 | 6440 | } |
6441 | } | |
6442 | else | |
6443 | { | |
6444 | /* | |
b423cd4c | 6445 | * Got a job URI; parse it to get the job ID... |
ef416fc2 | 6446 | */ |
6447 | ||
b423cd4c | 6448 | if (strncmp(resource, "/jobs/", 6)) |
6449 | { | |
6450 | /* | |
6451 | * Not a valid URI! | |
6452 | */ | |
6453 | ||
6454 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6455 | _("Bad job-uri attribute \"%s\"!"), | |
6456 | uri->values[0].string.text); | |
6457 | return; | |
6458 | } | |
ef416fc2 | 6459 | |
ef416fc2 | 6460 | /* |
b423cd4c | 6461 | * See if the job exists... |
ef416fc2 | 6462 | */ |
6463 | ||
b423cd4c | 6464 | jobid = atoi(resource + 6); |
ef416fc2 | 6465 | |
b423cd4c | 6466 | if ((job = cupsdFindJob(jobid)) == NULL) |
ef416fc2 | 6467 | { |
6468 | /* | |
b423cd4c | 6469 | * Nope - return a "not found" error... |
ef416fc2 | 6470 | */ |
6471 | ||
b423cd4c | 6472 | send_ipp_status(con, IPP_NOT_FOUND, |
6473 | _("Job #%d does not exist!"), jobid); | |
6474 | return; | |
ef416fc2 | 6475 | } |
6476 | else | |
b423cd4c | 6477 | { |
6478 | /* | |
6479 | * Job found, initialize source pointers... | |
6480 | */ | |
ef416fc2 | 6481 | |
b423cd4c | 6482 | src = NULL; |
6483 | sprinter = NULL; | |
6484 | } | |
ef416fc2 | 6485 | } |
6486 | ||
ef416fc2 | 6487 | /* |
b423cd4c | 6488 | * Now move the job or jobs... |
ef416fc2 | 6489 | */ |
6490 | ||
b423cd4c | 6491 | if (job) |
6492 | { | |
6493 | /* | |
6494 | * See if the job has been completed... | |
6495 | */ | |
6496 | ||
6497 | if (job->state_value > IPP_JOB_STOPPED) | |
6498 | { | |
6499 | /* | |
6500 | * Return a "not-possible" error... | |
6501 | */ | |
6502 | ||
6503 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6504 | _("Job #%d is finished and cannot be altered!"), | |
6505 | job->id); | |
6506 | return; | |
6507 | } | |
ef416fc2 | 6508 | |
b423cd4c | 6509 | /* |
6510 | * See if the job is owned by the requesting user... | |
6511 | */ | |
ef416fc2 | 6512 | |
b423cd4c | 6513 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6514 | { | |
6515 | send_http_error(con, HTTP_UNAUTHORIZED); | |
6516 | return; | |
6517 | } | |
ef416fc2 | 6518 | |
ef416fc2 | 6519 | /* |
b423cd4c | 6520 | * Move the job to a different printer or class... |
ef416fc2 | 6521 | */ |
6522 | ||
e53920b9 | 6523 | cupsdMoveJob(job, dprinter); |
ef416fc2 | 6524 | } |
b423cd4c | 6525 | else |
6526 | { | |
6527 | /* | |
6528 | * Got the source printer, now look through the jobs... | |
6529 | */ | |
ef416fc2 | 6530 | |
b423cd4c | 6531 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
6532 | job; | |
6533 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
6534 | { | |
6535 | /* | |
6536 | * See if the job is pointing at the source printer or has not been | |
6537 | * completed... | |
6538 | */ | |
ef416fc2 | 6539 | |
b423cd4c | 6540 | if (strcasecmp(job->dest, src) || |
6541 | job->state_value > IPP_JOB_STOPPED) | |
6542 | continue; | |
ef416fc2 | 6543 | |
b423cd4c | 6544 | /* |
6545 | * See if the job can be moved by the requesting user... | |
6546 | */ | |
ef416fc2 | 6547 | |
b423cd4c | 6548 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6549 | continue; | |
ef416fc2 | 6550 | |
b423cd4c | 6551 | /* |
6552 | * Move the job to a different printer or class... | |
6553 | */ | |
ef416fc2 | 6554 | |
e53920b9 | 6555 | cupsdMoveJob(job, dprinter); |
b423cd4c | 6556 | } |
ef416fc2 | 6557 | } |
6558 | ||
6559 | /* | |
b423cd4c | 6560 | * Start jobs if possible... |
ef416fc2 | 6561 | */ |
6562 | ||
b423cd4c | 6563 | cupsdCheckJobs(); |
ef416fc2 | 6564 | |
6565 | /* | |
b423cd4c | 6566 | * Return with "everything is OK" status... |
ef416fc2 | 6567 | */ |
6568 | ||
b423cd4c | 6569 | con->response->request.status.status_code = IPP_OK; |
6570 | } | |
ef416fc2 | 6571 | |
ef416fc2 | 6572 | |
b423cd4c | 6573 | /* |
6574 | * 'ppd_add_default()' - Add a PPD default choice. | |
6575 | */ | |
ef416fc2 | 6576 | |
b423cd4c | 6577 | static int /* O - Number of defaults */ |
6578 | ppd_add_default( | |
6579 | const char *option, /* I - Option name */ | |
6580 | const char *choice, /* I - Choice name */ | |
6581 | int num_defaults, /* I - Number of defaults */ | |
6582 | ppd_default_t **defaults) /* IO - Defaults */ | |
6583 | { | |
6584 | int i; /* Looping var */ | |
6585 | ppd_default_t *temp; /* Temporary defaults array */ | |
ef416fc2 | 6586 | |
bd7854cb | 6587 | |
ef416fc2 | 6588 | /* |
b423cd4c | 6589 | * First check if the option already has a default value; the PPD spec |
6590 | * says that the first one is used... | |
ef416fc2 | 6591 | */ |
6592 | ||
b423cd4c | 6593 | for (i = 0, temp = *defaults; i < num_defaults; i ++) |
6594 | if (!strcmp(option, temp[i].option)) | |
6595 | return (num_defaults); | |
ef416fc2 | 6596 | |
b423cd4c | 6597 | /* |
6598 | * Now add the option... | |
6599 | */ | |
ef416fc2 | 6600 | |
b423cd4c | 6601 | if (num_defaults == 0) |
6602 | temp = malloc(sizeof(ppd_default_t)); | |
ef416fc2 | 6603 | else |
b423cd4c | 6604 | temp = realloc(*defaults, (num_defaults + 1) * sizeof(ppd_default_t)); |
ef416fc2 | 6605 | |
b423cd4c | 6606 | if (!temp) |
ef416fc2 | 6607 | { |
b423cd4c | 6608 | cupsdLogMessage(CUPSD_LOG_ERROR, "ppd_add_default: Unable to add default value for \"%s\" - %s", |
6609 | option, strerror(errno)); | |
6610 | return (num_defaults); | |
ef416fc2 | 6611 | } |
6612 | ||
b423cd4c | 6613 | *defaults = temp; |
6614 | temp += num_defaults; | |
ef416fc2 | 6615 | |
b423cd4c | 6616 | strlcpy(temp->option, option, sizeof(temp->option)); |
6617 | strlcpy(temp->choice, choice, sizeof(temp->choice)); | |
ef416fc2 | 6618 | |
b423cd4c | 6619 | return (num_defaults + 1); |
6620 | } | |
ef416fc2 | 6621 | |
ef416fc2 | 6622 | |
b423cd4c | 6623 | /* |
6624 | * 'ppd_parse_line()' - Parse a PPD default line. | |
6625 | */ | |
ef416fc2 | 6626 | |
b423cd4c | 6627 | static int /* O - 0 on success, -1 on failure */ |
6628 | ppd_parse_line(const char *line, /* I - Line */ | |
6629 | char *option, /* O - Option name */ | |
6630 | int olen, /* I - Size of option name */ | |
6631 | char *choice, /* O - Choice name */ | |
6632 | int clen) /* I - Size of choice name */ | |
6633 | { | |
6634 | /* | |
6635 | * Verify this is a default option line... | |
6636 | */ | |
ef416fc2 | 6637 | |
b423cd4c | 6638 | if (strncmp(line, "*Default", 8)) |
6639 | return (-1); | |
ef416fc2 | 6640 | |
b423cd4c | 6641 | /* |
6642 | * Read the option name... | |
6643 | */ | |
6644 | ||
6645 | for (line += 8, olen --; isalnum(*line & 255); line ++) | |
6646 | if (olen > 0) | |
6647 | { | |
6648 | *option++ = *line; | |
6649 | olen --; | |
ef416fc2 | 6650 | } |
6651 | ||
b423cd4c | 6652 | *option = '\0'; |
ef416fc2 | 6653 | |
b423cd4c | 6654 | /* |
6655 | * Skip everything else up to the colon (:)... | |
6656 | */ | |
ef416fc2 | 6657 | |
b423cd4c | 6658 | while (*line && *line != ':') |
6659 | line ++; | |
ef416fc2 | 6660 | |
b423cd4c | 6661 | if (!*line) |
6662 | return (-1); | |
ef416fc2 | 6663 | |
b423cd4c | 6664 | line ++; |
ef416fc2 | 6665 | |
b423cd4c | 6666 | /* |
6667 | * Now grab the option choice, skipping leading whitespace... | |
6668 | */ | |
ef416fc2 | 6669 | |
b423cd4c | 6670 | while (isspace(*line & 255)) |
6671 | line ++; | |
ef416fc2 | 6672 | |
b423cd4c | 6673 | for (clen --; isalnum(*line & 255); line ++) |
6674 | if (clen > 0) | |
6675 | { | |
6676 | *choice++ = *line; | |
6677 | clen --; | |
6678 | } | |
ef416fc2 | 6679 | |
b423cd4c | 6680 | *choice = '\0'; |
ef416fc2 | 6681 | |
b423cd4c | 6682 | /* |
6683 | * Return with no errors... | |
6684 | */ | |
ef416fc2 | 6685 | |
b423cd4c | 6686 | return (0); |
6687 | } | |
ef416fc2 | 6688 | |
ef416fc2 | 6689 | |
b423cd4c | 6690 | /* |
6691 | * 'print_job()' - Print a file to a printer or class. | |
6692 | */ | |
ef416fc2 | 6693 | |
b423cd4c | 6694 | static void |
6695 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
6696 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6697 | { | |
6698 | ipp_attribute_t *attr; /* Current attribute */ | |
6699 | ipp_attribute_t *format; /* Document-format attribute */ | |
6700 | cupsd_job_t *job; /* New job */ | |
6701 | char filename[1024]; /* Job filename */ | |
6702 | mime_type_t *filetype; /* Type of file */ | |
6703 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
6704 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
6705 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
6706 | /* Textual name of mime type */ | |
6707 | cupsd_printer_t *printer; /* Printer data */ | |
6708 | struct stat fileinfo; /* File information */ | |
6709 | int kbytes; /* Size of file */ | |
6710 | int compression; /* Document compression */ | |
ef416fc2 | 6711 | |
ef416fc2 | 6712 | |
b423cd4c | 6713 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd, |
6714 | uri->values[0].string.text); | |
ef416fc2 | 6715 | |
b423cd4c | 6716 | /* |
6717 | * Validate print file attributes, for now just document-format and | |
6718 | * compression (CUPS only supports "none" and "gzip")... | |
6719 | */ | |
ef416fc2 | 6720 | |
b423cd4c | 6721 | compression = CUPS_FILE_NONE; |
ef416fc2 | 6722 | |
b423cd4c | 6723 | if ((attr = ippFindAttribute(con->request, "compression", |
6724 | IPP_TAG_KEYWORD)) != NULL) | |
6725 | { | |
6726 | if (strcmp(attr->values[0].string.text, "none") | |
6727 | #ifdef HAVE_LIBZ | |
6728 | && strcmp(attr->values[0].string.text, "gzip") | |
6729 | #endif /* HAVE_LIBZ */ | |
6730 | ) | |
6731 | { | |
6732 | send_ipp_status(con, IPP_ATTRIBUTES, | |
6733 | _("Unsupported compression \"%s\"!"), | |
6734 | attr->values[0].string.text); | |
6735 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
6736 | "compression", NULL, attr->values[0].string.text); | |
6737 | return; | |
6738 | } | |
ef416fc2 | 6739 | |
b423cd4c | 6740 | #ifdef HAVE_LIBZ |
6741 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
6742 | compression = CUPS_FILE_GZIP; | |
6743 | #endif /* HAVE_LIBZ */ | |
6744 | } | |
ef416fc2 | 6745 | |
b423cd4c | 6746 | /* |
6747 | * Do we have a file to print? | |
6748 | */ | |
ef416fc2 | 6749 | |
b423cd4c | 6750 | if (!con->filename) |
6751 | { | |
6752 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
6753 | return; | |
6754 | } | |
ef416fc2 | 6755 | |
b423cd4c | 6756 | /* |
6757 | * Is it a format we support? | |
6758 | */ | |
ef416fc2 | 6759 | |
b423cd4c | 6760 | if ((format = ippFindAttribute(con->request, "document-format", |
6761 | IPP_TAG_MIMETYPE)) != NULL) | |
6762 | { | |
6763 | /* | |
6764 | * Grab format from client... | |
6765 | */ | |
6766 | ||
f7faf1f5 | 6767 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) |
b423cd4c | 6768 | { |
6769 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6770 | _("Could not scan type \"%s\"!"), | |
6771 | format->values[0].string.text); | |
6772 | return; | |
ef416fc2 | 6773 | } |
b423cd4c | 6774 | } |
6775 | else | |
6776 | { | |
6777 | /* | |
f7faf1f5 | 6778 | * No document format attribute? Auto-type it! |
b423cd4c | 6779 | */ |
6780 | ||
6781 | strcpy(super, "application"); | |
6782 | strcpy(type, "octet-stream"); | |
6783 | } | |
ef416fc2 | 6784 | |
b423cd4c | 6785 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
6786 | { | |
ef416fc2 | 6787 | /* |
b423cd4c | 6788 | * Auto-type the file... |
ef416fc2 | 6789 | */ |
6790 | ||
b423cd4c | 6791 | ipp_attribute_t *doc_name; /* document-name attribute */ |
6792 | ||
6793 | ||
6794 | cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: auto-typing file..."); | |
6795 | ||
6796 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
6797 | filetype = mimeFileType(MimeDatabase, con->filename, | |
6798 | doc_name ? doc_name->values[0].string.text : NULL, | |
6799 | &compression); | |
6800 | ||
f7faf1f5 | 6801 | if (filetype) |
6802 | { | |
6803 | /* | |
6804 | * Replace the document-format attribute value with the auto-typed one. | |
6805 | */ | |
ef416fc2 | 6806 | |
f7faf1f5 | 6807 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
6808 | filetype->type); | |
b423cd4c | 6809 | |
f7faf1f5 | 6810 | if (format) |
6811 | { | |
6812 | _cupsStrFree(format->values[0].string.text); | |
ed486911 | 6813 | |
f7faf1f5 | 6814 | format->values[0].string.text = _cupsStrAlloc(mimetype); |
6815 | } | |
6816 | else | |
6817 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
6818 | "document-format", NULL, mimetype); | |
ef416fc2 | 6819 | } |
b423cd4c | 6820 | else |
f7faf1f5 | 6821 | filetype = mimeType(MimeDatabase, super, type); |
ef416fc2 | 6822 | } |
f7faf1f5 | 6823 | else |
6824 | filetype = mimeType(MimeDatabase, super, type); | |
6825 | ||
6826 | if (!filetype) | |
b423cd4c | 6827 | { |
6828 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
6829 | _("Unsupported format \'%s/%s\'!"), super, type); | |
6830 | cupsdLogMessage(CUPSD_LOG_INFO, | |
6831 | "Hint: Do you have the raw file printing rules enabled?"); | |
6832 | ||
6833 | if (format) | |
6834 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
6835 | "document-format", NULL, format->values[0].string.text); | |
6836 | ||
6837 | return; | |
6838 | } | |
6839 | ||
6840 | cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: request file type is %s/%s.", | |
80ca4592 | 6841 | filetype->super, filetype->type); |
b423cd4c | 6842 | |
6843 | /* | |
6844 | * Read any embedded job ticket info from PS files... | |
6845 | */ | |
6846 | ||
6847 | if (!strcasecmp(filetype->super, "application") && | |
6848 | !strcasecmp(filetype->type, "postscript")) | |
6849 | read_ps_job_ticket(con); | |
6850 | ||
6851 | /* | |
6852 | * Create the job object... | |
6853 | */ | |
6854 | ||
f7faf1f5 | 6855 | if ((job = add_job(con, uri, &printer, filetype)) == NULL) |
b423cd4c | 6856 | return; |
6857 | ||
6858 | /* | |
6859 | * Update quota data... | |
6860 | */ | |
6861 | ||
6862 | if (stat(con->filename, &fileinfo)) | |
6863 | kbytes = 0; | |
6864 | else | |
6865 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
6866 | ||
6867 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
6868 | ||
6869 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
6870 | IPP_TAG_INTEGER)) != NULL) | |
6871 | attr->values[0].integer += kbytes; | |
6872 | ||
ef416fc2 | 6873 | /* |
6874 | * Add the job file... | |
6875 | */ | |
6876 | ||
6877 | if (add_file(con, job, filetype, compression)) | |
6878 | return; | |
6879 | ||
6880 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
6881 | job->num_files); | |
6882 | rename(con->filename, filename); | |
6883 | cupsdClearString(&con->filename); | |
6884 | ||
6885 | /* | |
6886 | * See if we need to add the ending sheet... | |
6887 | */ | |
6888 | ||
f301802f | 6889 | attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME); |
6890 | ||
ef416fc2 | 6891 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && |
f301802f | 6892 | attr && attr->num_values > 1) |
ef416fc2 | 6893 | { |
6894 | /* | |
6895 | * Yes... | |
6896 | */ | |
6897 | ||
6898 | cupsdLogMessage(CUPSD_LOG_INFO, "Adding end banner page \"%s\" to job %d.", | |
6899 | attr->values[1].string.text, job->id); | |
6900 | ||
6901 | kbytes = copy_banner(con, job, attr->values[1].string.text); | |
6902 | ||
6903 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
6904 | } | |
6905 | ||
ef416fc2 | 6906 | /* |
6907 | * Log and save the job... | |
6908 | */ | |
6909 | ||
6910 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d queued on \"%s\" by \"%s\".", job->id, | |
6911 | job->dest, job->username); | |
6912 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Job %d hold_until = %d", job->id, | |
6913 | (int)job->hold_until); | |
6914 | ||
6915 | cupsdSaveJob(job); | |
6916 | ||
ef416fc2 | 6917 | /* |
6918 | * Start the job if possible... | |
6919 | */ | |
6920 | ||
6921 | cupsdCheckJobs(); | |
6922 | } | |
6923 | ||
6924 | ||
6925 | /* | |
6926 | * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file. | |
6927 | * | |
6928 | * This function only gets called when printing a single PostScript | |
6929 | * file using the Print-Job operation. It doesn't work for Create-Job + | |
6930 | * Send-File, since the job attributes need to be set at job creation | |
6931 | * time for banners to work. The embedded PS job ticket stuff is here | |
6932 | * only to allow the Windows printer driver for CUPS to pass in JCL | |
6933 | * options and IPP attributes which otherwise would be lost. | |
6934 | * | |
6935 | * The format of a PS job ticket is simple: | |
6936 | * | |
6937 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
6938 | * | |
6939 | * %cupsJobTicket: attr1=value1 | |
6940 | * %cupsJobTicket: attr2=value2 | |
6941 | * ... | |
6942 | * %cupsJobTicket: attrN=valueN | |
6943 | * | |
6944 | * Job ticket lines must appear immediately after the first line that | |
6945 | * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop | |
6946 | * looking for job ticket info when it finds a line that does not begin | |
6947 | * with "%cupsJobTicket:". | |
6948 | * | |
6949 | * The maximum length of a job ticket line, including the prefix, is | |
6950 | * 255 characters to conform with the Adobe DSC. | |
6951 | * | |
6952 | * Read-only attributes are rejected with a notice to the error log in | |
6953 | * case a malicious user tries anything. Since the job ticket is read | |
6954 | * prior to attribute validation in print_job(), job ticket attributes | |
6955 | * will go through the same validation as IPP attributes... | |
6956 | */ | |
6957 | ||
6958 | static void | |
6959 | read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */ | |
6960 | { | |
6961 | cups_file_t *fp; /* File to read from */ | |
6962 | char line[256]; /* Line data */ | |
6963 | int num_options; /* Number of options */ | |
6964 | cups_option_t *options; /* Options */ | |
6965 | ipp_t *ticket; /* New attributes */ | |
6966 | ipp_attribute_t *attr, /* Current attribute */ | |
6967 | *attr2, /* Job attribute */ | |
6968 | *prev2; /* Previous job attribute */ | |
6969 | ||
6970 | ||
6971 | /* | |
6972 | * First open the print file... | |
6973 | */ | |
6974 | ||
6975 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
6976 | { | |
6977 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6978 | "read_ps_job_ticket: Unable to open PostScript print file " | |
6979 | "- %s", | |
6980 | strerror(errno)); | |
6981 | return; | |
6982 | } | |
6983 | ||
6984 | /* | |
6985 | * Skip the first line... | |
6986 | */ | |
6987 | ||
6988 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
6989 | { | |
6990 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6991 | "read_ps_job_ticket: Unable to read from PostScript print " | |
6992 | "file - %s", | |
6993 | strerror(errno)); | |
6994 | cupsFileClose(fp); | |
6995 | return; | |
6996 | } | |
6997 | ||
fa73b229 | 6998 | if (strncmp(line, "%!PS-Adobe-", 11)) |
ef416fc2 | 6999 | { |
7000 | /* | |
7001 | * Not a DSC-compliant file, so no job ticket info will be available... | |
7002 | */ | |
7003 | ||
7004 | cupsFileClose(fp); | |
7005 | return; | |
7006 | } | |
7007 | ||
7008 | /* | |
7009 | * Read job ticket info from the file... | |
7010 | */ | |
7011 | ||
7012 | num_options = 0; | |
7013 | options = NULL; | |
7014 | ||
fa73b229 | 7015 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 7016 | { |
7017 | /* | |
7018 | * Stop at the first non-ticket line... | |
7019 | */ | |
7020 | ||
fa73b229 | 7021 | if (strncmp(line, "%cupsJobTicket:", 15)) |
ef416fc2 | 7022 | break; |
7023 | ||
7024 | /* | |
7025 | * Add the options to the option array... | |
7026 | */ | |
7027 | ||
7028 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
7029 | } | |
7030 | ||
7031 | /* | |
7032 | * Done with the file; see if we have any options... | |
7033 | */ | |
7034 | ||
7035 | cupsFileClose(fp); | |
7036 | ||
7037 | if (num_options == 0) | |
7038 | return; | |
7039 | ||
7040 | /* | |
7041 | * OK, convert the options to an attribute list, and apply them to | |
7042 | * the request... | |
7043 | */ | |
7044 | ||
7045 | ticket = ippNew(); | |
7046 | cupsEncodeOptions(ticket, num_options, options); | |
7047 | ||
7048 | /* | |
7049 | * See what the user wants to change. | |
7050 | */ | |
7051 | ||
fa73b229 | 7052 | for (attr = ticket->attrs; attr; attr = attr->next) |
ef416fc2 | 7053 | { |
7054 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
7055 | continue; | |
7056 | ||
7057 | if (!strcmp(attr->name, "job-originating-host-name") || | |
7058 | !strcmp(attr->name, "job-originating-user-name") || | |
7059 | !strcmp(attr->name, "job-media-sheets-completed") || | |
7060 | !strcmp(attr->name, "job-k-octets") || | |
7061 | !strcmp(attr->name, "job-id") || | |
7062 | !strncmp(attr->name, "job-state", 9) || | |
7063 | !strncmp(attr->name, "time-at-", 8)) | |
7064 | continue; /* Read-only attrs */ | |
7065 | ||
fa73b229 | 7066 | if ((attr2 = ippFindAttribute(con->request, attr->name, |
7067 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 7068 | { |
7069 | /* | |
7070 | * Some other value; first free the old value... | |
7071 | */ | |
7072 | ||
7073 | if (con->request->attrs == attr2) | |
7074 | { | |
7075 | con->request->attrs = attr2->next; | |
7076 | prev2 = NULL; | |
7077 | } | |
7078 | else | |
7079 | { | |
fa73b229 | 7080 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) |
ef416fc2 | 7081 | if (prev2->next == attr2) |
7082 | { | |
7083 | prev2->next = attr2->next; | |
7084 | break; | |
7085 | } | |
7086 | } | |
7087 | ||
7088 | if (con->request->last == attr2) | |
7089 | con->request->last = prev2; | |
7090 | ||
757d2cad | 7091 | _ippFreeAttr(attr2); |
ef416fc2 | 7092 | } |
7093 | ||
7094 | /* | |
7095 | * Add new option by copying it... | |
7096 | */ | |
7097 | ||
7098 | copy_attribute(con->request, attr, 0); | |
7099 | } | |
7100 | ||
7101 | /* | |
7102 | * Then free the attribute list and option array... | |
7103 | */ | |
7104 | ||
7105 | ippDelete(ticket); | |
7106 | cupsFreeOptions(num_options, options); | |
7107 | } | |
7108 | ||
7109 | ||
7110 | /* | |
7111 | * 'reject_jobs()' - Reject print jobs to a printer. | |
7112 | */ | |
7113 | ||
7114 | static void | |
7115 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
7116 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
7117 | { | |
7118 | http_status_t status; /* Policy status */ | |
7119 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 7120 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
7121 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7122 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7123 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7124 | int port; /* Port portion of URI */ | |
7125 | const char *name; /* Printer name */ | |
ef416fc2 | 7126 | cupsd_printer_t *printer; /* Printer data */ |
7127 | ipp_attribute_t *attr; /* printer-state-message text */ | |
7128 | ||
7129 | ||
7130 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
7131 | con->http.fd, uri->values[0].string.text); | |
7132 | ||
7133 | /* | |
7134 | * Is the destination valid? | |
7135 | */ | |
7136 | ||
f7faf1f5 | 7137 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7138 | sizeof(method), username, sizeof(username), host, | |
7139 | sizeof(host), &port, resource, sizeof(resource)); | |
7140 | ||
7141 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 7142 | { |
7143 | /* | |
7144 | * Bad URI... | |
7145 | */ | |
7146 | ||
7147 | send_ipp_status(con, IPP_NOT_FOUND, | |
7148 | _("The printer or class was not found.")); | |
7149 | return; | |
7150 | } | |
7151 | ||
7152 | /* | |
7153 | * Check policy... | |
7154 | */ | |
7155 | ||
7156 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7157 | { | |
7158 | send_http_error(con, status); | |
7159 | return; | |
7160 | } | |
7161 | ||
7162 | /* | |
7163 | * Reject jobs sent to the printer... | |
7164 | */ | |
7165 | ||
7166 | printer->accepting = 0; | |
7167 | ||
7168 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
7169 | IPP_TAG_TEXT)) == NULL) | |
7170 | strcpy(printer->state_message, "Rejecting Jobs"); | |
7171 | else | |
7172 | strlcpy(printer->state_message, attr->values[0].string.text, | |
7173 | sizeof(printer->state_message)); | |
7174 | ||
7175 | cupsdAddPrinterHistory(printer); | |
7176 | ||
7177 | if (dtype & CUPS_PRINTER_CLASS) | |
7178 | { | |
7179 | cupsdSaveAllClasses(); | |
7180 | ||
7181 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
f7faf1f5 | 7182 | name, get_username(con)); |
ef416fc2 | 7183 | } |
7184 | else | |
7185 | { | |
7186 | cupsdSaveAllPrinters(); | |
7187 | ||
7188 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
f7faf1f5 | 7189 | name, get_username(con)); |
ef416fc2 | 7190 | } |
7191 | ||
7192 | /* | |
7193 | * Everything was ok, so return OK status... | |
7194 | */ | |
7195 | ||
7196 | con->response->request.status.status_code = IPP_OK; | |
7197 | } | |
7198 | ||
7199 | ||
7200 | /* | |
7201 | * 'release_job()' - Release a held print job. | |
7202 | */ | |
7203 | ||
7204 | static void | |
7205 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
7206 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
7207 | { | |
7208 | ipp_attribute_t *attr; /* Current attribute */ | |
7209 | int jobid; /* Job ID */ | |
7210 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7211 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7212 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7213 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7214 | int port; /* Port portion of URI */ | |
7215 | cupsd_job_t *job; /* Job information */ | |
7216 | ||
7217 | ||
7218 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
7219 | con->http.fd, uri->values[0].string.text); | |
7220 | ||
7221 | /* | |
7222 | * See if we have a job URI or a printer URI... | |
7223 | */ | |
7224 | ||
7225 | if (!strcmp(uri->name, "printer-uri")) | |
7226 | { | |
7227 | /* | |
7228 | * Got a printer URI; see if we also have a job-id attribute... | |
7229 | */ | |
7230 | ||
fa73b229 | 7231 | if ((attr = ippFindAttribute(con->request, "job-id", |
7232 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7233 | { |
7234 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7235 | _("Got a printer-uri attribute but no job-id!")); | |
7236 | return; | |
7237 | } | |
7238 | ||
7239 | jobid = attr->values[0].integer; | |
7240 | } | |
7241 | else | |
7242 | { | |
7243 | /* | |
7244 | * Got a job URI; parse it to get the job ID... | |
7245 | */ | |
7246 | ||
a4d04587 | 7247 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7248 | sizeof(method), username, sizeof(username), host, | |
7249 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7250 | |
7251 | if (strncmp(resource, "/jobs/", 6)) | |
7252 | { | |
7253 | /* | |
7254 | * Not a valid URI! | |
7255 | */ | |
7256 | ||
7257 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7258 | _("Bad job-uri attribute \"%s\"!"), | |
7259 | uri->values[0].string.text); | |
7260 | return; | |
7261 | } | |
7262 | ||
7263 | jobid = atoi(resource + 6); | |
7264 | } | |
7265 | ||
7266 | /* | |
7267 | * See if the job exists... | |
7268 | */ | |
7269 | ||
7270 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7271 | { | |
7272 | /* | |
7273 | * Nope - return a "not found" error... | |
7274 | */ | |
7275 | ||
7276 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7277 | return; | |
7278 | } | |
7279 | ||
7280 | /* | |
7281 | * See if job is "held"... | |
7282 | */ | |
7283 | ||
bd7854cb | 7284 | if (job->state_value != IPP_JOB_HELD) |
ef416fc2 | 7285 | { |
7286 | /* | |
7287 | * Nope - return a "not possible" error... | |
7288 | */ | |
7289 | ||
7290 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid); | |
7291 | return; | |
7292 | } | |
7293 | ||
7294 | /* | |
7295 | * See if the job is owned by the requesting user... | |
7296 | */ | |
7297 | ||
7298 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7299 | { | |
bd7854cb | 7300 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7301 | return; |
7302 | } | |
7303 | ||
7304 | /* | |
7305 | * Reset the job-hold-until value to "no-hold"... | |
7306 | */ | |
7307 | ||
fa73b229 | 7308 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7309 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7310 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7311 | ||
fa73b229 | 7312 | if (attr) |
ef416fc2 | 7313 | { |
757d2cad | 7314 | _cupsStrFree(attr->values[0].string.text); |
4400e98d | 7315 | |
ef416fc2 | 7316 | attr->value_tag = IPP_TAG_KEYWORD; |
757d2cad | 7317 | attr->values[0].string.text = _cupsStrAlloc("no-hold"); |
ef416fc2 | 7318 | } |
7319 | ||
7320 | /* | |
7321 | * Release the job and return... | |
7322 | */ | |
7323 | ||
7324 | cupsdReleaseJob(job); | |
7325 | ||
7326 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was released by \"%s\".", jobid, | |
7327 | username); | |
7328 | ||
7329 | con->response->request.status.status_code = IPP_OK; | |
7330 | } | |
7331 | ||
7332 | ||
7333 | /* | |
7334 | * 'renew_subscription()' - Renew an existing subscription... | |
7335 | */ | |
7336 | ||
7337 | static void | |
7338 | renew_subscription( | |
7339 | cupsd_client_t *con, /* I - Client connection */ | |
7340 | int sub_id) /* I - Subscription ID */ | |
7341 | { | |
bd7854cb | 7342 | http_status_t status; /* Policy status */ |
7343 | cupsd_subscription_t *sub; /* Subscription */ | |
7344 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
7345 | ||
7346 | ||
7347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
7348 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
7349 | con, con->http.fd, sub_id); | |
7350 | ||
7351 | /* | |
7352 | * Is the subscription ID valid? | |
7353 | */ | |
7354 | ||
7355 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
7356 | { | |
7357 | /* | |
7358 | * Bad subscription ID... | |
7359 | */ | |
7360 | ||
7361 | send_ipp_status(con, IPP_NOT_FOUND, | |
7362 | _("notify-subscription-id %d no good!"), sub_id); | |
7363 | return; | |
7364 | } | |
7365 | ||
7366 | if (sub->job) | |
7367 | { | |
7368 | /* | |
7369 | * Job subscriptions cannot be renewed... | |
7370 | */ | |
7371 | ||
7372 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7373 | _("Job subscriptions cannot be renewed!")); | |
7374 | return; | |
7375 | } | |
7376 | ||
7377 | /* | |
7378 | * Check policy... | |
7379 | */ | |
7380 | ||
7381 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
7382 | DefaultPolicyPtr, | |
7383 | con, sub->owner)) != HTTP_OK) | |
7384 | { | |
7385 | send_http_error(con, status); | |
7386 | return; | |
7387 | } | |
7388 | ||
7389 | /* | |
7390 | * Renew the subscription... | |
7391 | */ | |
7392 | ||
7393 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
7394 | IPP_TAG_INTEGER); | |
7395 | ||
7396 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
7397 | ||
7398 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
7399 | { | |
7400 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7401 | "renew_subscription: Limiting notify-lease-duration to " | |
7402 | "%d seconds.", | |
7403 | MaxLeaseDuration); | |
7404 | sub->lease = MaxLeaseDuration; | |
7405 | } | |
7406 | ||
7407 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
7408 | ||
7409 | cupsdSaveAllSubscriptions(); | |
7410 | ||
7411 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 7412 | } |
7413 | ||
7414 | ||
7415 | /* | |
7416 | * 'restart_job()' - Restart an old print job. | |
7417 | */ | |
7418 | ||
7419 | static void | |
7420 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
7421 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
7422 | { | |
7423 | ipp_attribute_t *attr; /* Current attribute */ | |
7424 | int jobid; /* Job ID */ | |
7425 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7426 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7427 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7428 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7429 | int port; /* Port portion of URI */ | |
7430 | cupsd_job_t *job; /* Job information */ | |
7431 | ||
7432 | ||
7433 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
7434 | con->http.fd, uri->values[0].string.text); | |
7435 | ||
7436 | /* | |
7437 | * See if we have a job URI or a printer URI... | |
7438 | */ | |
7439 | ||
7440 | if (!strcmp(uri->name, "printer-uri")) | |
7441 | { | |
7442 | /* | |
7443 | * Got a printer URI; see if we also have a job-id attribute... | |
7444 | */ | |
7445 | ||
fa73b229 | 7446 | if ((attr = ippFindAttribute(con->request, "job-id", |
7447 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7448 | { |
7449 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7450 | _("Got a printer-uri attribute but no job-id!")); | |
7451 | return; | |
7452 | } | |
7453 | ||
7454 | jobid = attr->values[0].integer; | |
7455 | } | |
7456 | else | |
7457 | { | |
7458 | /* | |
7459 | * Got a job URI; parse it to get the job ID... | |
7460 | */ | |
7461 | ||
a4d04587 | 7462 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7463 | sizeof(method), username, sizeof(username), host, | |
7464 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7465 | |
fa73b229 | 7466 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 7467 | { |
7468 | /* | |
7469 | * Not a valid URI! | |
7470 | */ | |
7471 | ||
7472 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7473 | _("Bad job-uri attribute \"%s\"!"), | |
7474 | uri->values[0].string.text); | |
7475 | return; | |
7476 | } | |
7477 | ||
7478 | jobid = atoi(resource + 6); | |
7479 | } | |
7480 | ||
7481 | /* | |
7482 | * See if the job exists... | |
7483 | */ | |
7484 | ||
7485 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7486 | { | |
7487 | /* | |
7488 | * Nope - return a "not found" error... | |
7489 | */ | |
7490 | ||
7491 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7492 | return; | |
7493 | } | |
7494 | ||
7495 | /* | |
7496 | * See if job is in any of the "completed" states... | |
7497 | */ | |
7498 | ||
bd7854cb | 7499 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 7500 | { |
7501 | /* | |
7502 | * Nope - return a "not possible" error... | |
7503 | */ | |
7504 | ||
7505 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"), | |
7506 | jobid); | |
7507 | return; | |
7508 | } | |
7509 | ||
7510 | /* | |
7511 | * See if we have retained the job files... | |
7512 | */ | |
7513 | ||
bd7854cb | 7514 | cupsdLoadJob(job); |
7515 | ||
7516 | if (!job->attrs ||job->num_files == 0) | |
ef416fc2 | 7517 | { |
7518 | /* | |
7519 | * Nope - return a "not possible" error... | |
7520 | */ | |
7521 | ||
7522 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7523 | _("Job #%d cannot be restarted - no files!"), jobid); | |
7524 | return; | |
7525 | } | |
7526 | ||
7527 | /* | |
7528 | * See if the job is owned by the requesting user... | |
7529 | */ | |
7530 | ||
7531 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7532 | { | |
bd7854cb | 7533 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7534 | return; |
7535 | } | |
7536 | ||
7537 | /* | |
7538 | * Restart the job and return... | |
7539 | */ | |
7540 | ||
7541 | cupsdRestartJob(job); | |
7542 | ||
7543 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was restarted by \"%s\".", jobid, | |
7544 | username); | |
7545 | ||
7546 | con->response->request.status.status_code = IPP_OK; | |
7547 | } | |
7548 | ||
7549 | ||
7550 | /* | |
7551 | * 'save_auth_info()' - Save authentication information for a job. | |
7552 | */ | |
7553 | ||
7554 | static void | |
7555 | save_auth_info(cupsd_client_t *con, /* I - Client connection */ | |
7556 | cupsd_job_t *job) /* I - Job */ | |
7557 | { | |
7558 | int i; /* Looping var */ | |
7559 | char filename[1024]; /* Job authentication filename */ | |
7560 | cups_file_t *fp; /* Job authentication file */ | |
7561 | char line[1024]; /* Line for file */ | |
7562 | ||
7563 | ||
7564 | /* | |
7565 | * This function saves the in-memory authentication information for | |
7566 | * a job so that it can be used to authenticate with a remote host. | |
7567 | * The information is stored in a file that is readable only by the | |
7568 | * root user. The username and password are Base-64 encoded, each | |
7569 | * on a separate line, followed by random number (up to 1024) of | |
7570 | * newlines to limit the amount of information that is exposed. | |
7571 | * | |
7572 | * Because of the potential for exposing of authentication information, | |
7573 | * this functionality is only enabled when running cupsd as root. | |
7574 | * | |
7575 | * This caching only works for the Basic and BasicDigest authentication | |
7576 | * types. Digest authentication cannot be cached this way, and in | |
7577 | * the future Kerberos authentication may make all of this obsolete. | |
7578 | * | |
7579 | * Authentication information is saved whenever an authenticated | |
7580 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
7581 | * performed. | |
7582 | * | |
7583 | * This information is deleted after a job is completed or canceled, | |
7584 | * so reprints may require subsequent re-authentication. | |
7585 | */ | |
7586 | ||
7587 | if (RunUser) | |
7588 | return; | |
7589 | ||
7590 | /* | |
7591 | * Create the authentication file and change permissions... | |
7592 | */ | |
7593 | ||
7594 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
7595 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
7596 | { | |
7597 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
7598 | "Unable to save authentication info to \"%s\" - %s", | |
7599 | filename, strerror(errno)); | |
7600 | return; | |
7601 | } | |
7602 | ||
7603 | fchown(cupsFileNumber(fp), 0, 0); | |
7604 | fchmod(cupsFileNumber(fp), 0400); | |
7605 | ||
7606 | /* | |
7607 | * Write the authenticated username... | |
7608 | */ | |
7609 | ||
7610 | httpEncode64_2(line, sizeof(line), con->username, strlen(con->username)); | |
7611 | cupsFilePrintf(fp, "%s\n", line); | |
7612 | ||
7613 | /* | |
7614 | * Write the authenticated password... | |
7615 | */ | |
7616 | ||
7617 | httpEncode64_2(line, sizeof(line), con->password, strlen(con->password)); | |
7618 | cupsFilePrintf(fp, "%s\n", line); | |
7619 | ||
7620 | /* | |
7621 | * Write a random number of newlines to the end of the file... | |
7622 | */ | |
7623 | ||
7624 | for (i = (rand() % 1024); i >= 0; i --) | |
7625 | cupsFilePutChar(fp, '\n'); | |
7626 | ||
7627 | /* | |
7628 | * Close the file and return... | |
7629 | */ | |
7630 | ||
7631 | cupsFileClose(fp); | |
7632 | } | |
7633 | ||
7634 | ||
7635 | /* | |
7636 | * 'send_document()' - Send a file to a printer or class. | |
7637 | */ | |
7638 | ||
7639 | static void | |
7640 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
7641 | ipp_attribute_t *uri) /* I - Printer URI */ | |
7642 | { | |
7643 | ipp_attribute_t *attr; /* Current attribute */ | |
7644 | ipp_attribute_t *format; /* Document-format attribute */ | |
7645 | int jobid; /* Job ID number */ | |
7646 | cupsd_job_t *job; /* Current job */ | |
7647 | char job_uri[HTTP_MAX_URI], | |
7648 | /* Job URI */ | |
7649 | method[HTTP_MAX_URI], | |
7650 | /* Method portion of URI */ | |
7651 | username[HTTP_MAX_URI], | |
7652 | /* Username portion of URI */ | |
7653 | host[HTTP_MAX_URI], | |
7654 | /* Host portion of URI */ | |
7655 | resource[HTTP_MAX_URI]; | |
7656 | /* Resource portion of URI */ | |
7657 | int port; /* Port portion of URI */ | |
7658 | mime_type_t *filetype; /* Type of file */ | |
7659 | char super[MIME_MAX_SUPER], | |
7660 | /* Supertype of file */ | |
7661 | type[MIME_MAX_TYPE], | |
7662 | /* Subtype of file */ | |
7663 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
7664 | /* Textual name of mime type */ | |
7665 | char filename[1024]; /* Job filename */ | |
7666 | cupsd_printer_t *printer; /* Current printer */ | |
7667 | struct stat fileinfo; /* File information */ | |
7668 | int kbytes; /* Size of file */ | |
7669 | int compression; /* Type of compression */ | |
7670 | ||
7671 | ||
7672 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
7673 | con->http.fd, uri->values[0].string.text); | |
7674 | ||
7675 | /* | |
7676 | * See if we have a job URI or a printer URI... | |
7677 | */ | |
7678 | ||
7679 | if (!strcmp(uri->name, "printer-uri")) | |
7680 | { | |
7681 | /* | |
7682 | * Got a printer URI; see if we also have a job-id attribute... | |
7683 | */ | |
7684 | ||
fa73b229 | 7685 | if ((attr = ippFindAttribute(con->request, "job-id", |
7686 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7687 | { |
7688 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7689 | _("Got a printer-uri attribute but no job-id!")); | |
7690 | return; | |
7691 | } | |
7692 | ||
7693 | jobid = attr->values[0].integer; | |
7694 | } | |
7695 | else | |
7696 | { | |
7697 | /* | |
7698 | * Got a job URI; parse it to get the job ID... | |
7699 | */ | |
7700 | ||
a4d04587 | 7701 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7702 | sizeof(method), username, sizeof(username), host, | |
7703 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7704 | |
7705 | if (strncmp(resource, "/jobs/", 6)) | |
7706 | { | |
7707 | /* | |
7708 | * Not a valid URI! | |
7709 | */ | |
7710 | ||
7711 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7712 | _("Bad job-uri attribute \"%s\"!"), | |
7713 | uri->values[0].string.text); | |
7714 | return; | |
7715 | } | |
7716 | ||
7717 | jobid = atoi(resource + 6); | |
7718 | } | |
7719 | ||
7720 | /* | |
7721 | * See if the job exists... | |
7722 | */ | |
7723 | ||
7724 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7725 | { | |
7726 | /* | |
7727 | * Nope - return a "not found" error... | |
7728 | */ | |
7729 | ||
7730 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7731 | return; | |
7732 | } | |
7733 | ||
f301802f | 7734 | printer = cupsdFindDest(job->dest); |
7735 | ||
ef416fc2 | 7736 | /* |
7737 | * See if the job is owned by the requesting user... | |
7738 | */ | |
7739 | ||
7740 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7741 | { | |
bd7854cb | 7742 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7743 | return; |
7744 | } | |
7745 | ||
7746 | /* | |
7747 | * OK, see if the client is sending the document compressed - CUPS | |
7748 | * only supports "none" and "gzip". | |
7749 | */ | |
7750 | ||
7751 | compression = CUPS_FILE_NONE; | |
7752 | ||
fa73b229 | 7753 | if ((attr = ippFindAttribute(con->request, "compression", |
7754 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 7755 | { |
7756 | if (strcmp(attr->values[0].string.text, "none") | |
7757 | #ifdef HAVE_LIBZ | |
7758 | && strcmp(attr->values[0].string.text, "gzip") | |
7759 | #endif /* HAVE_LIBZ */ | |
7760 | ) | |
7761 | { | |
7762 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"), | |
7763 | attr->values[0].string.text); | |
7764 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
7765 | "compression", NULL, attr->values[0].string.text); | |
7766 | return; | |
7767 | } | |
7768 | ||
7769 | #ifdef HAVE_LIBZ | |
7770 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
7771 | compression = CUPS_FILE_GZIP; | |
7772 | #endif /* HAVE_LIBZ */ | |
7773 | } | |
7774 | ||
7775 | /* | |
7776 | * Do we have a file to print? | |
7777 | */ | |
7778 | ||
7779 | if (!con->filename) | |
7780 | { | |
7781 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
7782 | return; | |
7783 | } | |
7784 | ||
7785 | /* | |
7786 | * Is it a format we support? | |
7787 | */ | |
7788 | ||
7789 | if ((format = ippFindAttribute(con->request, "document-format", | |
7790 | IPP_TAG_MIMETYPE)) != NULL) | |
7791 | { | |
7792 | /* | |
7793 | * Grab format from client... | |
7794 | */ | |
7795 | ||
7796 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) | |
7797 | { | |
7798 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
7799 | format->values[0].string.text); | |
7800 | return; | |
7801 | } | |
7802 | } | |
7803 | else | |
7804 | { | |
7805 | /* | |
7806 | * No document format attribute? Auto-type it! | |
7807 | */ | |
7808 | ||
7809 | strcpy(super, "application"); | |
7810 | strcpy(type, "octet-stream"); | |
7811 | } | |
7812 | ||
fa73b229 | 7813 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
ef416fc2 | 7814 | { |
7815 | /* | |
7816 | * Auto-type the file... | |
7817 | */ | |
7818 | ||
bd7854cb | 7819 | ipp_attribute_t *doc_name; /* document-name attribute */ |
7820 | ||
7821 | ||
ef416fc2 | 7822 | cupsdLogMessage(CUPSD_LOG_DEBUG, "send_document: auto-typing file..."); |
7823 | ||
bd7854cb | 7824 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
7825 | filetype = mimeFileType(MimeDatabase, con->filename, | |
7826 | doc_name ? doc_name->values[0].string.text : NULL, | |
7827 | &compression); | |
ef416fc2 | 7828 | |
f7faf1f5 | 7829 | if (filetype) |
ed486911 | 7830 | { |
f7faf1f5 | 7831 | /* |
7832 | * Replace the document-format attribute value with the auto-typed one. | |
7833 | */ | |
ed486911 | 7834 | |
f7faf1f5 | 7835 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
7836 | filetype->type); | |
7837 | ||
7838 | if (format) | |
7839 | { | |
7840 | _cupsStrFree(format->values[0].string.text); | |
7841 | format->values[0].string.text = _cupsStrAlloc(mimetype); | |
7842 | } | |
7843 | else | |
7844 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
7845 | "document-format", NULL, mimetype); | |
ed486911 | 7846 | } |
7847 | else | |
f7faf1f5 | 7848 | filetype = mimeType(MimeDatabase, super, type); |
ed486911 | 7849 | } |
f7faf1f5 | 7850 | else |
7851 | filetype = mimeType(MimeDatabase, super, type); | |
7852 | ||
7853 | if (!filetype) | |
ef416fc2 | 7854 | { |
7855 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
7856 | _("Unsupported format \'%s/%s\'!"), super, type); | |
7857 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7858 | "Hint: Do you have the raw file printing rules enabled?"); | |
7859 | ||
7860 | if (format) | |
7861 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
7862 | "document-format", NULL, format->values[0].string.text); | |
7863 | ||
7864 | return; | |
7865 | } | |
7866 | ||
80ca4592 | 7867 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) |
7868 | { | |
7869 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
7870 | filetype->type); | |
7871 | ||
7872 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
7873 | _("Unsupported format \'%s\'!"), mimetype); | |
7874 | ||
7875 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
7876 | "document-format", NULL, mimetype); | |
7877 | ||
7878 | return; | |
7879 | } | |
7880 | ||
ef416fc2 | 7881 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
7882 | "send_document: request file type is %s/%s.", | |
7883 | filetype->super, filetype->type); | |
7884 | ||
7885 | /* | |
7886 | * Add the file to the job... | |
7887 | */ | |
7888 | ||
bd7854cb | 7889 | cupsdLoadJob(job); |
7890 | ||
ef416fc2 | 7891 | if (add_file(con, job, filetype, compression)) |
7892 | return; | |
7893 | ||
ef416fc2 | 7894 | if (stat(con->filename, &fileinfo)) |
7895 | kbytes = 0; | |
7896 | else | |
7897 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
7898 | ||
7899 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
7900 | ||
fa73b229 | 7901 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
7902 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 7903 | attr->values[0].integer += kbytes; |
7904 | ||
7905 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
7906 | job->num_files); | |
7907 | rename(con->filename, filename); | |
7908 | ||
7909 | cupsdClearString(&con->filename); | |
7910 | ||
7911 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7912 | "File of type %s/%s queued in job #%d by \"%s\".", | |
7913 | filetype->super, filetype->type, job->id, job->username); | |
7914 | ||
7915 | /* | |
7916 | * Start the job if this is the last document... | |
7917 | */ | |
7918 | ||
fa73b229 | 7919 | if ((attr = ippFindAttribute(con->request, "last-document", |
7920 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 7921 | attr->values[0].boolean) |
7922 | { | |
7923 | /* | |
7924 | * See if we need to add the ending sheet... | |
7925 | */ | |
7926 | ||
fa73b229 | 7927 | if (printer && |
ef416fc2 | 7928 | !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && |
fa73b229 | 7929 | (attr = ippFindAttribute(job->attrs, "job-sheets", |
7930 | IPP_TAG_ZERO)) != NULL && | |
ef416fc2 | 7931 | attr->num_values > 1) |
7932 | { | |
7933 | /* | |
7934 | * Yes... | |
7935 | */ | |
7936 | ||
7937 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7938 | "Adding end banner page \"%s\" to job %d.", | |
7939 | attr->values[1].string.text, job->id); | |
7940 | ||
7941 | kbytes = copy_banner(con, job, attr->values[1].string.text); | |
7942 | ||
7943 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
7944 | } | |
7945 | ||
bd7854cb | 7946 | if (job->state_value == IPP_JOB_STOPPED) |
7947 | { | |
ef416fc2 | 7948 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 7949 | job->state_value = IPP_JOB_PENDING; |
7950 | } | |
7951 | else if (job->state_value == IPP_JOB_HELD) | |
ef416fc2 | 7952 | { |
fa73b229 | 7953 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7954 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7955 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7956 | ||
fa73b229 | 7957 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
bd7854cb | 7958 | { |
ef416fc2 | 7959 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 7960 | job->state_value = IPP_JOB_PENDING; |
7961 | } | |
ef416fc2 | 7962 | } |
7963 | ||
7964 | cupsdSaveJob(job); | |
7965 | ||
7966 | /* | |
7967 | * Start the job if possible... Since cupsdCheckJobs() can cancel a | |
7968 | * job if it doesn't print, we need to re-find the job afterwards... | |
7969 | */ | |
7970 | ||
7971 | jobid = job->id; | |
7972 | ||
7973 | cupsdCheckJobs(); | |
7974 | ||
7975 | job = cupsdFindJob(jobid); | |
7976 | } | |
7977 | else | |
7978 | { | |
fa73b229 | 7979 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7980 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7981 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7982 | ||
fa73b229 | 7983 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
ef416fc2 | 7984 | { |
7985 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 7986 | job->state_value = IPP_JOB_HELD; |
ef416fc2 | 7987 | job->hold_until = time(NULL) + 60; |
7988 | cupsdSaveJob(job); | |
7989 | } | |
7990 | } | |
7991 | ||
7992 | /* | |
7993 | * Fill in the response info... | |
7994 | */ | |
7995 | ||
7996 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
7997 | LocalPort, jobid); | |
7998 | ||
7999 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
8000 | job_uri); | |
8001 | ||
8002 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
8003 | ||
8004 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
bd7854cb | 8005 | job ? job->state_value : IPP_JOB_CANCELLED); |
ef416fc2 | 8006 | add_job_state_reasons(con, job); |
8007 | ||
8008 | con->response->request.status.status_code = IPP_OK; | |
8009 | } | |
8010 | ||
8011 | ||
8012 | /* | |
8013 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
8014 | */ | |
8015 | ||
8016 | static void | |
8017 | send_http_error(cupsd_client_t *con, /* I - Client connection */ | |
8018 | http_status_t status) /* I - HTTP status code */ | |
8019 | { | |
8020 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s", | |
8021 | ippOpString(con->request->request.op.operation_id), | |
8022 | httpStatus(status)); | |
8023 | ||
8024 | cupsdSendError(con, status); | |
8025 | ||
8026 | ippDelete(con->response); | |
8027 | con->response = NULL; | |
8028 | ||
8029 | return; | |
8030 | } | |
8031 | ||
8032 | ||
8033 | /* | |
8034 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
8035 | */ | |
8036 | ||
8037 | static void | |
8038 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
8039 | ipp_status_t status, /* I - IPP status code */ | |
8040 | const char *message, /* I - Status message */ | |
8041 | ...) /* I - Additional args as needed */ | |
8042 | { | |
8043 | va_list ap; /* Pointer to additional args */ | |
8044 | char formatted[1024]; /* Formatted errror message */ | |
8045 | ||
8046 | ||
8047 | if (message) | |
8048 | { | |
8049 | va_start(ap, message); | |
8050 | vsnprintf(formatted, sizeof(formatted), | |
8051 | _cupsLangString(con->language, message), ap); | |
8052 | va_end(ap); | |
8053 | ||
8054 | cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO, | |
8055 | "%s %s: %s", | |
8056 | ippOpString(con->request->request.op.operation_id), | |
8057 | ippErrorString(status), formatted); | |
8058 | } | |
8059 | else | |
8060 | cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO, | |
8061 | "%s %s", | |
8062 | ippOpString(con->request->request.op.operation_id), | |
8063 | ippErrorString(status)); | |
8064 | ||
8065 | con->response->request.status.status_code = status; | |
8066 | ||
fa73b229 | 8067 | if (ippFindAttribute(con->response, "attributes-charset", |
8068 | IPP_TAG_ZERO) == NULL) | |
ef416fc2 | 8069 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
8070 | "attributes-charset", NULL, DefaultCharset); | |
8071 | ||
8072 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
8073 | IPP_TAG_ZERO) == NULL) | |
8074 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
8075 | "attributes-natural-language", NULL, DefaultLanguage); | |
8076 | ||
8077 | if (message) | |
8078 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
8079 | "status-message", NULL, formatted); | |
8080 | } | |
8081 | ||
8082 | ||
8083 | /* | |
8084 | * 'set_default()' - Set the default destination... | |
8085 | */ | |
8086 | ||
8087 | static void | |
8088 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
8089 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8090 | { | |
8091 | http_status_t status; /* Policy status */ | |
8092 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 8093 | char method[HTTP_MAX_URI], |
8094 | /* Method portion of URI */ | |
8095 | username[HTTP_MAX_URI], | |
8096 | /* Username portion of URI */ | |
8097 | host[HTTP_MAX_URI], | |
8098 | /* Host portion of URI */ | |
8099 | resource[HTTP_MAX_URI]; | |
8100 | /* Resource portion of URI */ | |
8101 | int port; /* Port portion of URI */ | |
8102 | const char *name; /* Printer name */ | |
ef416fc2 | 8103 | cupsd_printer_t *printer; /* Printer */ |
8104 | ||
8105 | ||
8106 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
8107 | con->http.fd, uri->values[0].string.text); | |
8108 | ||
8109 | /* | |
8110 | * Is the destination valid? | |
8111 | */ | |
8112 | ||
f7faf1f5 | 8113 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8114 | sizeof(method), username, sizeof(username), host, | |
8115 | sizeof(host), &port, resource, sizeof(resource)); | |
8116 | ||
8117 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 8118 | { |
8119 | /* | |
8120 | * Bad URI... | |
8121 | */ | |
8122 | ||
8123 | send_ipp_status(con, IPP_NOT_FOUND, | |
8124 | _("The printer or class was not found.")); | |
8125 | return; | |
8126 | } | |
8127 | ||
8128 | /* | |
8129 | * Check policy... | |
8130 | */ | |
8131 | ||
8132 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
8133 | { | |
8134 | send_http_error(con, status); | |
8135 | return; | |
8136 | } | |
8137 | ||
8138 | /* | |
8139 | * Set it as the default... | |
8140 | */ | |
8141 | ||
8142 | DefaultPrinter = printer; | |
8143 | ||
8144 | cupsdSaveAllPrinters(); | |
8145 | cupsdSaveAllClasses(); | |
8146 | ||
8147 | cupsdWritePrintcap(); | |
8148 | ||
8149 | cupsdLogMessage(CUPSD_LOG_INFO, | |
f7faf1f5 | 8150 | "Default destination set to \"%s\" by \"%s\".", name, |
8151 | get_username(con)); | |
ef416fc2 | 8152 | |
8153 | /* | |
8154 | * Everything was ok, so return OK status... | |
8155 | */ | |
8156 | ||
8157 | con->response->request.status.status_code = IPP_OK; | |
8158 | } | |
8159 | ||
8160 | ||
8161 | /* | |
8162 | * 'set_job_attrs()' - Set job attributes. | |
8163 | */ | |
8164 | ||
8165 | static void | |
8166 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
8167 | ipp_attribute_t *uri) /* I - Job URI */ | |
8168 | { | |
8169 | ipp_attribute_t *attr, /* Current attribute */ | |
8170 | *attr2; /* Job attribute */ | |
8171 | int jobid; /* Job ID */ | |
8172 | cupsd_job_t *job; /* Current job */ | |
8173 | char method[HTTP_MAX_URI], | |
8174 | /* Method portion of URI */ | |
8175 | username[HTTP_MAX_URI], | |
8176 | /* Username portion of URI */ | |
8177 | host[HTTP_MAX_URI], | |
8178 | /* Host portion of URI */ | |
8179 | resource[HTTP_MAX_URI]; | |
8180 | /* Resource portion of URI */ | |
8181 | int port; /* Port portion of URI */ | |
8182 | ||
8183 | ||
8184 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
8185 | con->http.fd, uri->values[0].string.text); | |
8186 | ||
8187 | /* | |
8188 | * Start with "everything is OK" status... | |
8189 | */ | |
8190 | ||
8191 | con->response->request.status.status_code = IPP_OK; | |
8192 | ||
8193 | /* | |
8194 | * See if we have a job URI or a printer URI... | |
8195 | */ | |
8196 | ||
fa73b229 | 8197 | if (!strcmp(uri->name, "printer-uri")) |
ef416fc2 | 8198 | { |
8199 | /* | |
8200 | * Got a printer URI; see if we also have a job-id attribute... | |
8201 | */ | |
8202 | ||
fa73b229 | 8203 | if ((attr = ippFindAttribute(con->request, "job-id", |
8204 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8205 | { |
8206 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8207 | _("Got a printer-uri attribute but no job-id!")); | |
8208 | return; | |
8209 | } | |
8210 | ||
8211 | jobid = attr->values[0].integer; | |
8212 | } | |
8213 | else | |
8214 | { | |
8215 | /* | |
8216 | * Got a job URI; parse it to get the job ID... | |
8217 | */ | |
8218 | ||
a4d04587 | 8219 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8220 | sizeof(method), username, sizeof(username), host, | |
8221 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8222 | |
fa73b229 | 8223 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 8224 | { |
8225 | /* | |
8226 | * Not a valid URI! | |
8227 | */ | |
8228 | ||
8229 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8230 | _("Bad job-uri attribute \"%s\"!"), | |
8231 | uri->values[0].string.text); | |
8232 | return; | |
8233 | } | |
8234 | ||
8235 | jobid = atoi(resource + 6); | |
8236 | } | |
8237 | ||
8238 | /* | |
8239 | * See if the job exists... | |
8240 | */ | |
8241 | ||
8242 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8243 | { | |
8244 | /* | |
8245 | * Nope - return a "not found" error... | |
8246 | */ | |
8247 | ||
8248 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
8249 | return; | |
8250 | } | |
8251 | ||
8252 | /* | |
8253 | * See if the job has been completed... | |
8254 | */ | |
8255 | ||
bd7854cb | 8256 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 8257 | { |
8258 | /* | |
8259 | * Return a "not-possible" error... | |
8260 | */ | |
8261 | ||
8262 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8263 | _("Job #%d is finished and cannot be altered!"), jobid); | |
8264 | return; | |
8265 | } | |
8266 | ||
8267 | /* | |
8268 | * See if the job is owned by the requesting user... | |
8269 | */ | |
8270 | ||
8271 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8272 | { | |
bd7854cb | 8273 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 8274 | return; |
8275 | } | |
8276 | ||
8277 | /* | |
8278 | * See what the user wants to change. | |
8279 | */ | |
8280 | ||
bd7854cb | 8281 | cupsdLoadJob(job); |
8282 | ||
fa73b229 | 8283 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 8284 | { |
8285 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8286 | continue; | |
8287 | ||
8288 | if (!strcmp(attr->name, "attributes-charset") || | |
8289 | !strcmp(attr->name, "attributes-natural-language") || | |
8290 | !strcmp(attr->name, "document-compression") || | |
8291 | !strcmp(attr->name, "document-format") || | |
8292 | !strcmp(attr->name, "job-detailed-status-messages") || | |
8293 | !strcmp(attr->name, "job-document-access-errors") || | |
8294 | !strcmp(attr->name, "job-id") || | |
8295 | !strcmp(attr->name, "job-k-octets") || | |
8296 | !strcmp(attr->name, "job-originating-host-name") || | |
8297 | !strcmp(attr->name, "job-originating-user-name") || | |
8298 | !strcmp(attr->name, "job-printer-up-time") || | |
8299 | !strcmp(attr->name, "job-printer-uri") || | |
8300 | !strcmp(attr->name, "job-sheets") || | |
8301 | !strcmp(attr->name, "job-state-message") || | |
8302 | !strcmp(attr->name, "job-state-reasons") || | |
8303 | !strcmp(attr->name, "job-uri") || | |
8304 | !strcmp(attr->name, "number-of-documents") || | |
8305 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
8306 | !strcmp(attr->name, "output-device-assigned") || | |
8307 | !strncmp(attr->name, "date-time-at-", 13) || | |
8308 | !strncmp(attr->name, "job-impressions", 15) || | |
8309 | !strncmp(attr->name, "job-k-octets", 12) || | |
8310 | !strncmp(attr->name, "job-media-sheets", 16) || | |
8311 | !strncmp(attr->name, "time-at-", 8)) | |
8312 | { | |
8313 | /* | |
8314 | * Read-only attrs! | |
8315 | */ | |
8316 | ||
8317 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
8318 | _("%s cannot be changed."), attr->name); | |
8319 | ||
8320 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8321 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8322 | ||
8323 | continue; | |
8324 | } | |
8325 | ||
8326 | if (!strcmp(attr->name, "job-priority")) | |
8327 | { | |
8328 | /* | |
8329 | * Change the job priority... | |
8330 | */ | |
8331 | ||
8332 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8333 | { | |
8334 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!")); | |
8335 | ||
8336 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8337 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8338 | } | |
bd7854cb | 8339 | else if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 8340 | { |
8341 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8342 | _("Job is completed and cannot be changed.")); | |
8343 | return; | |
8344 | } | |
8345 | else if (con->response->request.status.status_code == IPP_OK) | |
8346 | cupsdSetJobPriority(job, attr->values[0].integer); | |
8347 | } | |
8348 | else if (!strcmp(attr->name, "job-state")) | |
8349 | { | |
8350 | /* | |
8351 | * Change the job state... | |
8352 | */ | |
8353 | ||
8354 | if (attr->value_tag != IPP_TAG_ENUM) | |
8355 | { | |
8356 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!")); | |
8357 | ||
8358 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8359 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8360 | } | |
8361 | else | |
8362 | { | |
8363 | switch (attr->values[0].integer) | |
8364 | { | |
8365 | case IPP_JOB_PENDING : | |
8366 | case IPP_JOB_HELD : | |
bd7854cb | 8367 | if (job->state_value > IPP_JOB_HELD) |
ef416fc2 | 8368 | { |
8369 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8370 | _("Job state cannot be changed.")); | |
8371 | return; | |
8372 | } | |
8373 | else if (con->response->request.status.status_code == IPP_OK) | |
bd7854cb | 8374 | { |
ef416fc2 | 8375 | job->state->values[0].integer = attr->values[0].integer; |
bd7854cb | 8376 | job->state_value = attr->values[0].integer; |
8377 | } | |
ef416fc2 | 8378 | break; |
8379 | ||
8380 | case IPP_JOB_PROCESSING : | |
8381 | case IPP_JOB_STOPPED : | |
bd7854cb | 8382 | if (job->state_value != attr->values[0].integer) |
ef416fc2 | 8383 | { |
8384 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8385 | _("Job state cannot be changed.")); | |
8386 | return; | |
8387 | } | |
8388 | break; | |
8389 | ||
8390 | case IPP_JOB_CANCELLED : | |
8391 | case IPP_JOB_ABORTED : | |
8392 | case IPP_JOB_COMPLETED : | |
bd7854cb | 8393 | if (job->state_value > IPP_JOB_PROCESSING) |
ef416fc2 | 8394 | { |
8395 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8396 | _("Job state cannot be changed.")); | |
8397 | return; | |
8398 | } | |
8399 | else if (con->response->request.status.status_code == IPP_OK) | |
8400 | { | |
8401 | cupsdCancelJob(job, 0); | |
8402 | ||
8403 | if (JobHistory) | |
8404 | { | |
8405 | job->state->values[0].integer = attr->values[0].integer; | |
bd7854cb | 8406 | job->state_value = attr->values[0].integer; |
ef416fc2 | 8407 | cupsdSaveJob(job); |
8408 | } | |
8409 | } | |
8410 | break; | |
8411 | } | |
8412 | } | |
8413 | } | |
8414 | else if (con->response->request.status.status_code != IPP_OK) | |
8415 | continue; | |
fa73b229 | 8416 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, |
8417 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 8418 | { |
8419 | /* | |
8420 | * Some other value; first free the old value... | |
8421 | */ | |
8422 | ||
8423 | if (job->attrs->prev) | |
8424 | job->attrs->prev->next = attr2->next; | |
8425 | else | |
8426 | job->attrs->attrs = attr2->next; | |
8427 | ||
8428 | if (job->attrs->last == attr2) | |
8429 | job->attrs->last = job->attrs->prev; | |
8430 | ||
757d2cad | 8431 | _ippFreeAttr(attr2); |
ef416fc2 | 8432 | |
8433 | /* | |
8434 | * Then copy the attribute... | |
8435 | */ | |
8436 | ||
8437 | copy_attribute(job->attrs, attr, 0); | |
8438 | ||
8439 | /* | |
8440 | * See if the job-name or job-hold-until is being changed. | |
8441 | */ | |
8442 | ||
8443 | if (!strcmp(attr->name, "job-hold-until")) | |
8444 | { | |
8445 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
8446 | ||
8447 | if (!strcmp(attr->values[0].string.text, "no-hold")) | |
8448 | cupsdReleaseJob(job); | |
8449 | else | |
8450 | cupsdHoldJob(job); | |
8451 | } | |
8452 | } | |
8453 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
8454 | { | |
8455 | /* | |
8456 | * Delete the attribute... | |
8457 | */ | |
8458 | ||
8459 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
8460 | IPP_TAG_ZERO)) != NULL) | |
8461 | { | |
8462 | if (job->attrs->prev) | |
8463 | job->attrs->prev->next = attr2->next; | |
8464 | else | |
8465 | job->attrs->attrs = attr2->next; | |
8466 | ||
8467 | if (attr2 == job->attrs->last) | |
8468 | job->attrs->last = job->attrs->prev; | |
8469 | ||
757d2cad | 8470 | _ippFreeAttr(attr2); |
ef416fc2 | 8471 | } |
8472 | } | |
8473 | else | |
8474 | { | |
8475 | /* | |
8476 | * Add new option by copying it... | |
8477 | */ | |
8478 | ||
8479 | copy_attribute(job->attrs, attr, 0); | |
8480 | } | |
8481 | } | |
8482 | ||
8483 | /* | |
8484 | * Save the job... | |
8485 | */ | |
8486 | ||
8487 | cupsdSaveJob(job); | |
8488 | ||
8489 | /* | |
8490 | * Start jobs if possible... | |
8491 | */ | |
8492 | ||
8493 | cupsdCheckJobs(); | |
8494 | } | |
8495 | ||
8496 | ||
b423cd4c | 8497 | /* |
8498 | * 'set_printer_defaults()' - Set printer default options from a request. | |
8499 | */ | |
8500 | ||
8501 | static void | |
8502 | set_printer_defaults( | |
8503 | cupsd_client_t *con, /* I - Client connection */ | |
8504 | cupsd_printer_t *printer) /* I - Printer */ | |
8505 | { | |
8506 | int i; /* Looping var */ | |
8507 | ipp_attribute_t *attr; /* Current attribute */ | |
8508 | int namelen; /* Length of attribute name */ | |
8509 | char name[256], /* New attribute name */ | |
8510 | value[256]; /* String version of integer attrs */ | |
8511 | ||
8512 | ||
8513 | for (attr = con->request->attrs; attr; attr = attr->next) | |
8514 | { | |
8515 | /* | |
8516 | * Skip non-printer attributes... | |
8517 | */ | |
8518 | ||
8519 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
8520 | continue; | |
8521 | ||
8522 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
8523 | ||
8524 | if (!strcmp(attr->name, "job-sheets-default")) | |
8525 | { | |
8526 | /* | |
8527 | * Only allow keywords and names... | |
8528 | */ | |
8529 | ||
8530 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
8531 | continue; | |
8532 | ||
8533 | /* | |
8534 | * Only allow job-sheets-default to be set when running without a | |
8535 | * system high classification level... | |
8536 | */ | |
8537 | ||
8538 | if (Classification) | |
8539 | continue; | |
8540 | ||
8541 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
8542 | ||
8543 | if (attr->num_values > 1) | |
8544 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
8545 | else | |
8546 | cupsdSetString(&printer->job_sheets[1], "none"); | |
8547 | } | |
8548 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
8549 | { | |
8550 | cupsdFreePrinterUsers(printer); | |
8551 | ||
8552 | printer->deny_users = 0; | |
8553 | ||
8554 | if (attr->value_tag == IPP_TAG_NAME && | |
8555 | (attr->num_values > 1 || | |
8556 | strcmp(attr->values[0].string.text, "all"))) | |
8557 | { | |
8558 | for (i = 0; i < attr->num_values; i ++) | |
8559 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
8560 | } | |
8561 | } | |
8562 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
8563 | { | |
8564 | cupsdFreePrinterUsers(printer); | |
8565 | ||
8566 | printer->deny_users = 1; | |
8567 | ||
8568 | if (attr->value_tag == IPP_TAG_NAME && | |
8569 | (attr->num_values > 1 || | |
8570 | strcmp(attr->values[0].string.text, "none"))) | |
8571 | { | |
8572 | for (i = 0; i < attr->num_values; i ++) | |
8573 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
8574 | } | |
8575 | } | |
8576 | else if (!strcmp(attr->name, "job-quota-period")) | |
8577 | { | |
8578 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8579 | continue; | |
8580 | ||
8581 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
8582 | attr->values[0].integer); | |
8583 | cupsdFreeQuotas(printer); | |
8584 | ||
8585 | printer->quota_period = attr->values[0].integer; | |
8586 | } | |
8587 | else if (!strcmp(attr->name, "job-k-limit")) | |
8588 | { | |
8589 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8590 | continue; | |
8591 | ||
8592 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
8593 | attr->values[0].integer); | |
8594 | cupsdFreeQuotas(printer); | |
8595 | ||
8596 | printer->k_limit = attr->values[0].integer; | |
8597 | } | |
8598 | else if (!strcmp(attr->name, "job-page-limit")) | |
8599 | { | |
8600 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8601 | continue; | |
8602 | ||
8603 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
8604 | attr->values[0].integer); | |
8605 | cupsdFreeQuotas(printer); | |
8606 | ||
8607 | printer->page_limit = attr->values[0].integer; | |
8608 | } | |
8609 | else if (!strcmp(attr->name, "printer-op-policy")) | |
8610 | { | |
8611 | cupsd_policy_t *p; /* Policy */ | |
8612 | ||
8613 | ||
8614 | if (attr->value_tag != IPP_TAG_NAME) | |
8615 | continue; | |
8616 | ||
8617 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
8618 | { | |
8619 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8620 | "Setting printer-op-policy to \"%s\"...", | |
8621 | attr->values[0].string.text); | |
8622 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
8623 | printer->op_policy_ptr = p; | |
8624 | } | |
8625 | else | |
8626 | { | |
8627 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8628 | _("Unknown printer-op-policy \"%s\"."), | |
8629 | attr->values[0].string.text); | |
8630 | return; | |
8631 | } | |
8632 | } | |
8633 | else if (!strcmp(attr->name, "printer-error-policy")) | |
8634 | { | |
8635 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
8636 | continue; | |
8637 | ||
8638 | if (strcmp(attr->values[0].string.text, "abort-job") && | |
8639 | strcmp(attr->values[0].string.text, "retry-job") && | |
8640 | strcmp(attr->values[0].string.text, "stop-printer")) | |
8641 | { | |
8642 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8643 | _("Unknown printer-error-policy \"%s\"."), | |
8644 | attr->values[0].string.text); | |
8645 | return; | |
8646 | } | |
8647 | ||
8648 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8649 | "Setting printer-error-policy to \"%s\"...", | |
8650 | attr->values[0].string.text); | |
8651 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
8652 | } | |
f7faf1f5 | 8653 | else if (!strcmp(attr->name, "document-format-default") || |
8654 | !strcmp(attr->name, "notify-lease-duration-default") || | |
f301802f | 8655 | !strcmp(attr->name, "notify-events-default")) |
b423cd4c | 8656 | continue; |
8657 | ||
8658 | /* | |
8659 | * Skip any other non-default attributes... | |
8660 | */ | |
8661 | ||
8662 | namelen = strlen(attr->name); | |
8663 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
8664 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
8665 | continue; | |
8666 | ||
8667 | /* | |
8668 | * OK, anything else must be a user-defined default... | |
8669 | */ | |
8670 | ||
8671 | strlcpy(name, attr->name, sizeof(name)); | |
8672 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
8673 | ||
8674 | switch (attr->value_tag) | |
8675 | { | |
8676 | case IPP_TAG_DELETEATTR : | |
8677 | printer->num_options = cupsRemoveOption(name, | |
8678 | printer->num_options, | |
8679 | &(printer->options)); | |
8680 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8681 | "Deleting %s", attr->name); | |
8682 | break; | |
8683 | ||
8684 | case IPP_TAG_NAME : | |
8685 | case IPP_TAG_KEYWORD : | |
8686 | case IPP_TAG_URI : | |
8687 | printer->num_options = cupsAddOption(name, | |
8688 | attr->values[0].string.text, | |
8689 | printer->num_options, | |
8690 | &(printer->options)); | |
8691 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8692 | "Setting %s to \"%s\"...", attr->name, | |
8693 | attr->values[0].string.text); | |
8694 | break; | |
8695 | ||
8696 | case IPP_TAG_BOOLEAN : | |
8697 | printer->num_options = cupsAddOption(name, | |
8698 | attr->values[0].boolean ? | |
8699 | "true" : "false", | |
8700 | printer->num_options, | |
8701 | &(printer->options)); | |
8702 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8703 | "Setting %s to %s...", attr->name, | |
8704 | attr->values[0].boolean ? "true" : "false"); | |
8705 | break; | |
8706 | ||
8707 | case IPP_TAG_INTEGER : | |
8708 | case IPP_TAG_ENUM : | |
8709 | sprintf(value, "%d", attr->values[0].integer); | |
8710 | printer->num_options = cupsAddOption(name, value, | |
8711 | printer->num_options, | |
8712 | &(printer->options)); | |
8713 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8714 | "Setting %s to %s...", attr->name, value); | |
8715 | break; | |
8716 | ||
8717 | case IPP_TAG_RANGE : | |
8718 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
8719 | attr->values[0].range.upper); | |
8720 | printer->num_options = cupsAddOption(name, value, | |
8721 | printer->num_options, | |
8722 | &(printer->options)); | |
8723 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8724 | "Setting %s to %s...", attr->name, value); | |
8725 | break; | |
8726 | ||
8727 | case IPP_TAG_RESOLUTION : | |
8728 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
8729 | attr->values[0].resolution.yres, | |
8730 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
8731 | "dpi" : "dpc"); | |
8732 | printer->num_options = cupsAddOption(name, value, | |
8733 | printer->num_options, | |
8734 | &(printer->options)); | |
8735 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8736 | "Setting %s to %s...", attr->name, value); | |
8737 | break; | |
8738 | ||
8739 | default : | |
8740 | /* Do nothing for other values */ | |
8741 | break; | |
8742 | } | |
8743 | } | |
8744 | } | |
8745 | ||
8746 | ||
ef416fc2 | 8747 | /* |
8748 | * 'start_printer()' - Start a printer. | |
8749 | */ | |
8750 | ||
8751 | static void | |
8752 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
8753 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8754 | { | |
8755 | http_status_t status; /* Policy status */ | |
8756 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 8757 | char method[HTTP_MAX_URI], |
8758 | /* Method portion of URI */ | |
8759 | username[HTTP_MAX_URI], | |
8760 | /* Username portion of URI */ | |
8761 | host[HTTP_MAX_URI], | |
8762 | /* Host portion of URI */ | |
8763 | resource[HTTP_MAX_URI]; | |
8764 | /* Resource portion of URI */ | |
8765 | int port; /* Port portion of URI */ | |
8766 | const char *name; /* Printer name */ | |
ef416fc2 | 8767 | cupsd_printer_t *printer; /* Printer data */ |
8768 | ||
8769 | ||
8770 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
8771 | con->http.fd, uri->values[0].string.text); | |
8772 | ||
8773 | /* | |
8774 | * Is the destination valid? | |
8775 | */ | |
8776 | ||
f7faf1f5 | 8777 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8778 | sizeof(method), username, sizeof(username), host, | |
8779 | sizeof(host), &port, resource, sizeof(resource)); | |
8780 | ||
8781 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 8782 | { |
8783 | /* | |
8784 | * Bad URI... | |
8785 | */ | |
8786 | ||
8787 | send_ipp_status(con, IPP_NOT_FOUND, | |
8788 | _("The printer or class was not found.")); | |
8789 | return; | |
8790 | } | |
8791 | ||
8792 | /* | |
8793 | * Check policy... | |
8794 | */ | |
8795 | ||
8796 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8797 | { | |
8798 | send_http_error(con, status); | |
8799 | return; | |
8800 | } | |
8801 | ||
8802 | /* | |
8803 | * Start the printer... | |
8804 | */ | |
8805 | ||
8806 | printer->state_message[0] = '\0'; | |
8807 | ||
8808 | cupsdStartPrinter(printer, 1); | |
8809 | ||
8810 | if (dtype & CUPS_PRINTER_CLASS) | |
e53920b9 | 8811 | { |
f7faf1f5 | 8812 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", name, |
8813 | get_username(con)); | |
e53920b9 | 8814 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
f7faf1f5 | 8815 | "Class \"%s\" started by \"%s\".", name, get_username(con)); |
e53920b9 | 8816 | } |
e00b005a | 8817 | else |
e53920b9 | 8818 | { |
f7faf1f5 | 8819 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", name, |
8820 | get_username(con)); | |
e53920b9 | 8821 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
f7faf1f5 | 8822 | "Printer \"%s\" started by \"%s\".", name, get_username(con)); |
e53920b9 | 8823 | } |
ef416fc2 | 8824 | |
8825 | cupsdCheckJobs(); | |
8826 | ||
8827 | /* | |
8828 | * Everything was ok, so return OK status... | |
8829 | */ | |
8830 | ||
8831 | con->response->request.status.status_code = IPP_OK; | |
8832 | } | |
8833 | ||
8834 | ||
8835 | /* | |
8836 | * 'stop_printer()' - Stop a printer. | |
8837 | */ | |
8838 | ||
8839 | static void | |
8840 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
8841 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8842 | { | |
8843 | http_status_t status; /* Policy status */ | |
8844 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 8845 | char method[HTTP_MAX_URI], |
8846 | /* Method portion of URI */ | |
8847 | username[HTTP_MAX_URI], | |
8848 | /* Username portion of URI */ | |
8849 | host[HTTP_MAX_URI], | |
8850 | /* Host portion of URI */ | |
8851 | resource[HTTP_MAX_URI]; | |
8852 | /* Resource portion of URI */ | |
8853 | int port; /* Port portion of URI */ | |
8854 | const char *name; /* Printer name */ | |
ef416fc2 | 8855 | cupsd_printer_t *printer; /* Printer data */ |
8856 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
8857 | ||
8858 | ||
8859 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
8860 | con->http.fd, uri->values[0].string.text); | |
8861 | ||
8862 | /* | |
8863 | * Is the destination valid? | |
8864 | */ | |
8865 | ||
f7faf1f5 | 8866 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8867 | sizeof(method), username, sizeof(username), host, | |
8868 | sizeof(host), &port, resource, sizeof(resource)); | |
8869 | ||
8870 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 8871 | { |
8872 | /* | |
8873 | * Bad URI... | |
8874 | */ | |
8875 | ||
8876 | send_ipp_status(con, IPP_NOT_FOUND, | |
8877 | _("The printer or class was not found.")); | |
8878 | return; | |
8879 | } | |
8880 | ||
8881 | /* | |
8882 | * Check policy... | |
8883 | */ | |
8884 | ||
8885 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8886 | { | |
8887 | send_http_error(con, status); | |
8888 | return; | |
8889 | } | |
8890 | ||
8891 | /* | |
8892 | * Stop the printer... | |
8893 | */ | |
8894 | ||
8895 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8896 | IPP_TAG_TEXT)) == NULL) | |
8897 | strcpy(printer->state_message, "Paused"); | |
8898 | else | |
8899 | { | |
8900 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8901 | sizeof(printer->state_message)); | |
8902 | } | |
8903 | ||
8904 | cupsdStopPrinter(printer, 1); | |
8905 | ||
8906 | if (dtype & CUPS_PRINTER_CLASS) | |
e53920b9 | 8907 | { |
f7faf1f5 | 8908 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", name, |
8909 | get_username(con)); | |
e53920b9 | 8910 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
f7faf1f5 | 8911 | "Class \"%s\" stopped by \"%s\".", name, get_username(con)); |
e53920b9 | 8912 | } |
ef416fc2 | 8913 | else |
e53920b9 | 8914 | { |
f7faf1f5 | 8915 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", name, |
8916 | get_username(con)); | |
e53920b9 | 8917 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
f7faf1f5 | 8918 | "Printer \"%s\" stopped by \"%s\".", name, get_username(con)); |
e53920b9 | 8919 | } |
ef416fc2 | 8920 | |
8921 | /* | |
8922 | * Everything was ok, so return OK status... | |
8923 | */ | |
8924 | ||
8925 | con->response->request.status.status_code = IPP_OK; | |
8926 | } | |
8927 | ||
8928 | ||
89d46774 | 8929 | /* |
8930 | * 'url_encode_attr()' - URL-encode a string attribute. | |
8931 | */ | |
8932 | ||
8933 | static void | |
8934 | url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */ | |
8935 | char *buffer,/* I - String buffer */ | |
8936 | int bufsize)/* I - Size of buffer */ | |
8937 | { | |
8938 | int i; /* Looping var */ | |
8939 | char *bufptr, /* Pointer into buffer */ | |
8940 | *bufend, /* End of buffer */ | |
8941 | *valptr; /* Pointer into value */ | |
8942 | ||
8943 | ||
8944 | strlcpy(buffer, attr->name, bufsize); | |
8945 | bufptr = buffer + strlen(buffer); | |
8946 | bufend = buffer + bufsize - 1; | |
8947 | ||
8948 | for (i = 0; i < attr->num_values; i ++) | |
8949 | { | |
8950 | if (bufptr >= bufend) | |
8951 | break; | |
8952 | ||
8953 | if (i) | |
8954 | *bufptr++ = ','; | |
8955 | else | |
8956 | *bufptr++ = '='; | |
8957 | ||
8958 | if (bufptr >= bufend) | |
8959 | break; | |
8960 | ||
8961 | *bufptr++ = '\''; | |
8962 | ||
8963 | for (valptr = attr->values[i].string.text; | |
8964 | *valptr && bufptr < bufend; | |
8965 | valptr ++) | |
8966 | if (*valptr == ' ') | |
8967 | { | |
8968 | if (bufptr >= (bufend - 2)) | |
8969 | break; | |
8970 | ||
8971 | *bufptr++ = '%'; | |
8972 | *bufptr++ = '2'; | |
8973 | *bufptr++ = '0'; | |
8974 | } | |
8975 | else if (*valptr == '\'' || *valptr == '\\') | |
8976 | { | |
8977 | *bufptr++ = '\\'; | |
8978 | *bufptr++ = *valptr; | |
8979 | } | |
8980 | else | |
8981 | *bufptr++ = *valptr; | |
8982 | ||
8983 | if (bufptr >= bufend) | |
8984 | break; | |
8985 | ||
8986 | *bufptr++ = '\''; | |
8987 | } | |
8988 | ||
8989 | *bufptr = '\0'; | |
8990 | } | |
8991 | ||
8992 | ||
ef416fc2 | 8993 | /* |
8994 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
8995 | */ | |
8996 | ||
8997 | static int /* O - 0 if not allowed, 1 if allowed */ | |
8998 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
8999 | const char *username) /* I - Username */ | |
9000 | { | |
9001 | int i; /* Looping var */ | |
9002 | struct passwd *pw; /* User password data */ | |
9003 | ||
9004 | ||
9005 | if (p->num_users == 0) | |
9006 | return (1); | |
9007 | ||
9008 | if (!strcmp(username, "root")) | |
9009 | return (1); | |
9010 | ||
9011 | pw = getpwnam(username); | |
9012 | endpwent(); | |
9013 | ||
9014 | for (i = 0; i < p->num_users; i ++) | |
9015 | { | |
9016 | if (p->users[i][0] == '@') | |
9017 | { | |
9018 | /* | |
9019 | * Check group membership... | |
9020 | */ | |
9021 | ||
9022 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
9023 | break; | |
9024 | } | |
9025 | else if (!strcasecmp(username, p->users[i])) | |
9026 | break; | |
9027 | } | |
9028 | ||
9029 | return ((i < p->num_users) != p->deny_users); | |
9030 | } | |
9031 | ||
9032 | ||
9033 | /* | |
9034 | * 'validate_job()' - Validate printer options and destination. | |
9035 | */ | |
9036 | ||
9037 | static void | |
9038 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
9039 | ipp_attribute_t *uri) /* I - Printer URI */ | |
9040 | { | |
9041 | http_status_t status; /* Policy status */ | |
9042 | ipp_attribute_t *attr; /* Current attribute */ | |
9043 | ipp_attribute_t *format; /* Document-format attribute */ | |
9044 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
f7faf1f5 | 9045 | char method[HTTP_MAX_URI], |
9046 | /* Method portion of URI */ | |
9047 | username[HTTP_MAX_URI], | |
9048 | /* Username portion of URI */ | |
9049 | host[HTTP_MAX_URI], | |
9050 | /* Host portion of URI */ | |
9051 | resource[HTTP_MAX_URI]; | |
9052 | /* Resource portion of URI */ | |
9053 | int port; /* Port portion of URI */ | |
ef416fc2 | 9054 | char super[MIME_MAX_SUPER], |
9055 | /* Supertype of file */ | |
9056 | type[MIME_MAX_TYPE]; | |
9057 | /* Subtype of file */ | |
9058 | cupsd_printer_t *printer; /* Printer */ | |
9059 | ||
9060 | ||
9061 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
9062 | con->http.fd, uri->values[0].string.text); | |
9063 | ||
9064 | /* | |
9065 | * OK, see if the client is sending the document compressed - CUPS | |
9066 | * doesn't support compression yet... | |
9067 | */ | |
9068 | ||
fa73b229 | 9069 | if ((attr = ippFindAttribute(con->request, "compression", |
9070 | IPP_TAG_KEYWORD)) != NULL && | |
9071 | !strcmp(attr->values[0].string.text, "none")) | |
ef416fc2 | 9072 | { |
9073 | send_ipp_status(con, IPP_ATTRIBUTES, | |
9074 | _("Unsupported compression attribute %s!"), | |
9075 | attr->values[0].string.text); | |
9076 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9077 | "compression", NULL, attr->values[0].string.text); | |
9078 | return; | |
9079 | } | |
9080 | ||
9081 | /* | |
9082 | * Is it a format we support? | |
9083 | */ | |
9084 | ||
9085 | if ((format = ippFindAttribute(con->request, "document-format", | |
9086 | IPP_TAG_MIMETYPE)) != NULL) | |
9087 | { | |
9088 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) | |
9089 | { | |
9090 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
9091 | format->values[0].string.text); | |
9092 | return; | |
9093 | } | |
9094 | ||
fa73b229 | 9095 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && |
9096 | !mimeType(MimeDatabase, super, type)) | |
ef416fc2 | 9097 | { |
9098 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9099 | "Hint: Do you have the raw file printing rules enabled?"); | |
9100 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9101 | _("Unsupported format \"%s\"!"), | |
9102 | format->values[0].string.text); | |
9103 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9104 | "document-format", NULL, format->values[0].string.text); | |
9105 | return; | |
9106 | } | |
9107 | } | |
9108 | ||
9109 | /* | |
9110 | * Is the destination valid? | |
9111 | */ | |
9112 | ||
f7faf1f5 | 9113 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9114 | sizeof(method), username, sizeof(username), host, | |
9115 | sizeof(host), &port, resource, sizeof(resource)); | |
9116 | ||
9117 | if (cupsdValidateDest(host, resource, &dtype, &printer) == NULL) | |
ef416fc2 | 9118 | { |
9119 | /* | |
9120 | * Bad URI... | |
9121 | */ | |
9122 | ||
9123 | send_ipp_status(con, IPP_NOT_FOUND, | |
9124 | _("The printer or class was not found.")); | |
9125 | return; | |
9126 | } | |
9127 | ||
9128 | /* | |
9129 | * Check policy... | |
9130 | */ | |
9131 | ||
9132 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
9133 | { | |
9134 | send_http_error(con, status); | |
9135 | return; | |
9136 | } | |
9137 | ||
9138 | /* | |
9139 | * Everything was ok, so return OK status... | |
9140 | */ | |
9141 | ||
9142 | con->response->request.status.status_code = IPP_OK; | |
9143 | } | |
9144 | ||
9145 | ||
9146 | /* | |
9147 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
9148 | */ | |
9149 | ||
9150 | static int /* O - 0 if name is no good, 1 if name is good */ | |
9151 | validate_name(const char *name) /* I - Name to check */ | |
9152 | { | |
9153 | const char *ptr; /* Pointer into name */ | |
9154 | ||
9155 | ||
9156 | /* | |
9157 | * Scan the whole name... | |
9158 | */ | |
9159 | ||
9160 | for (ptr = name; *ptr; ptr ++) | |
9161 | if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') | |
9162 | return (0); | |
9163 | ||
9164 | /* | |
9165 | * All the characters are good; validate the length, too... | |
9166 | */ | |
9167 | ||
9168 | return ((ptr - name) < 128); | |
9169 | } | |
9170 | ||
9171 | ||
9172 | /* | |
9173 | * 'validate_user()' - Validate the user for the request. | |
9174 | */ | |
9175 | ||
9176 | static int /* O - 1 if permitted, 0 otherwise */ | |
9177 | validate_user(cupsd_job_t *job, /* I - Job */ | |
9178 | cupsd_client_t *con, /* I - Client connection */ | |
9179 | const char *owner, /* I - Owner of job/resource */ | |
9180 | char *username, /* O - Authenticated username */ | |
9181 | int userlen) /* I - Length of username */ | |
9182 | { | |
ef416fc2 | 9183 | cupsd_printer_t *printer; /* Printer for job */ |
9184 | ||
9185 | ||
9186 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
9187 | "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, " | |
9188 | "userlen=%d)", | |
9189 | job ? job->id : 0, con->http.fd, owner ? owner : "(null)", | |
9190 | username, userlen); | |
9191 | ||
9192 | /* | |
9193 | * Validate input... | |
9194 | */ | |
9195 | ||
9196 | if (!con || !owner || !username || userlen <= 0) | |
9197 | return (0); | |
9198 | ||
9199 | /* | |
9200 | * Get the best authenticated username that is available. | |
9201 | */ | |
9202 | ||
e00b005a | 9203 | strlcpy(username, get_username(con), userlen); |
ef416fc2 | 9204 | |
9205 | /* | |
9206 | * Check the username against the owner... | |
9207 | */ | |
9208 | ||
80ca4592 | 9209 | printer = cupsdFindDest(job->dest); |
ef416fc2 | 9210 | |
9211 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
9212 | con, owner) == HTTP_OK); | |
9213 | } | |
9214 | ||
9215 | ||
9216 | /* | |
f7faf1f5 | 9217 | * End of "$Id: ipp.c 5686 2006-06-21 21:02:56Z mike $". |
ef416fc2 | 9218 | */ |