]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
e53920b9 | 2 | * "$Id: ipp.c 5383 2006-04-07 15:36:10Z 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. | |
91 | * user_allowed() - See if a user is allowed to print to a queue. | |
92 | * validate_job() - Validate printer options and destination. | |
93 | * validate_name() - Make sure the printer name only contains | |
94 | * valid chars. | |
95 | * validate_user() - Validate the user for the request. | |
96 | */ | |
97 | ||
98 | /* | |
99 | * Include necessary headers... | |
100 | */ | |
101 | ||
102 | #include "cupsd.h" | |
103 | ||
104 | #ifdef HAVE_LIBPAPER | |
105 | # include <paper.h> | |
106 | #endif /* HAVE_LIBPAPER */ | |
107 | ||
108 | ||
109 | /* | |
110 | * PPD default choice structure... | |
111 | */ | |
112 | ||
113 | typedef struct | |
114 | { | |
115 | char option[PPD_MAX_NAME]; /* Main keyword (option name) */ | |
116 | char choice[PPD_MAX_NAME]; /* Option keyword (choice name) */ | |
117 | } ppd_default_t; | |
118 | ||
119 | ||
120 | /* | |
121 | * Local functions... | |
122 | */ | |
123 | ||
124 | static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
125 | static void add_class(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 126 | static int add_file(cupsd_client_t *con, cupsd_job_t *job, |
127 | mime_type_t *filetype, int compression); | |
128 | static cupsd_job_t *add_job(cupsd_client_t *con, ipp_attribute_t *uri, | |
80ca4592 | 129 | cupsd_printer_t **dprinter, |
130 | mime_type_t *filetype); | |
ef416fc2 | 131 | static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job); |
132 | static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job); | |
bd7854cb | 133 | static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job); |
ef416fc2 | 134 | static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
b423cd4c | 135 | static void add_printer_state_reasons(cupsd_client_t *con, |
136 | cupsd_printer_t *p); | |
ef416fc2 | 137 | static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p); |
b423cd4c | 138 | static void apply_printer_defaults(cupsd_printer_t *printer, |
139 | cupsd_job_t *job); | |
ef416fc2 | 140 | static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri); |
141 | static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
142 | static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
143 | static void cancel_subscription(cupsd_client_t *con, int id); | |
144 | static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p); | |
145 | static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr, | |
146 | int quickcopy); | |
fa73b229 | 147 | static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra, |
ef416fc2 | 148 | ipp_tag_t group, int quickcopy); |
b423cd4c | 149 | static int copy_banner(cupsd_client_t *con, cupsd_job_t *job, |
150 | const char *name); | |
ef416fc2 | 151 | static int copy_file(const char *from, const char *to); |
b423cd4c | 152 | static int copy_model(cupsd_client_t *con, const char *from, |
153 | const char *to); | |
fa73b229 | 154 | static void copy_job_attrs(cupsd_client_t *con, |
155 | cupsd_job_t *job, | |
156 | cups_array_t *ra); | |
157 | static void copy_printer_attrs(cupsd_client_t *con, | |
158 | cupsd_printer_t *printer, | |
159 | cups_array_t *ra); | |
ef416fc2 | 160 | static void copy_subscription_attrs(cupsd_client_t *con, |
161 | cupsd_subscription_t *sub, | |
162 | cups_array_t *ra); | |
163 | static void create_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
164 | static cups_array_t *create_requested_array(ipp_t *request); | |
165 | static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri); | |
166 | static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
167 | static void get_default(cupsd_client_t *con); | |
168 | static void get_devices(cupsd_client_t *con); | |
169 | static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
170 | static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
bd7854cb | 171 | static void get_notifications(cupsd_client_t *con); |
ef416fc2 | 172 | static void get_ppds(cupsd_client_t *con); |
173 | static void get_printers(cupsd_client_t *con, int type); | |
174 | static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
175 | static void get_subscription_attrs(cupsd_client_t *con, int sub_id); | |
176 | static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri); | |
e00b005a | 177 | static const char *get_username(cupsd_client_t *con); |
ef416fc2 | 178 | static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri); |
179 | static void move_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
180 | static int ppd_add_default(const char *option, const char *choice, | |
181 | int num_defaults, ppd_default_t **defaults); | |
182 | static int ppd_parse_line(const char *line, char *option, int olen, | |
183 | char *choice, int clen); | |
184 | static void print_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
185 | static void read_ps_job_ticket(cupsd_client_t *con); | |
186 | static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri); | |
187 | static void release_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
188 | static void renew_subscription(cupsd_client_t *con, int sub_id); | |
189 | static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
190 | static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job); | |
191 | static void send_document(cupsd_client_t *con, ipp_attribute_t *uri); | |
192 | static void send_http_error(cupsd_client_t *con, http_status_t status); | |
193 | static void send_ipp_status(cupsd_client_t *con, ipp_status_t status, | |
194 | const char *message, ...) | |
195 | # ifdef __GNUC__ | |
196 | __attribute__ ((__format__ (__printf__, 3, 4))) | |
197 | # endif /* __GNUC__ */ | |
198 | ; | |
199 | static void set_default(cupsd_client_t *con, ipp_attribute_t *uri); | |
200 | static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri); | |
b423cd4c | 201 | static void set_printer_defaults(cupsd_client_t *con, |
202 | cupsd_printer_t *printer); | |
ef416fc2 | 203 | static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri); |
204 | static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri); | |
205 | static int user_allowed(cupsd_printer_t *p, const char *username); | |
206 | static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri); | |
207 | static int validate_name(const char *name); | |
208 | static int validate_user(cupsd_job_t *job, cupsd_client_t *con, | |
209 | const char *owner, char *username, | |
210 | int userlen); | |
211 | ||
212 | ||
213 | /* | |
214 | * 'cupsdProcessIPPRequest()' - Process an incoming IPP request... | |
215 | */ | |
216 | ||
217 | int /* O - 1 on success, 0 on failure */ | |
218 | cupsdProcessIPPRequest( | |
219 | cupsd_client_t *con) /* I - Client connection */ | |
220 | { | |
221 | ipp_tag_t group; /* Current group tag */ | |
222 | ipp_attribute_t *attr; /* Current attribute */ | |
223 | ipp_attribute_t *charset; /* Character set attribute */ | |
224 | ipp_attribute_t *language; /* Language attribute */ | |
225 | ipp_attribute_t *uri; /* Printer URI attribute */ | |
226 | ipp_attribute_t *username; /* requesting-user-name attr */ | |
227 | int sub_id; /* Subscription ID */ | |
228 | ||
229 | ||
230 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
231 | "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x", | |
232 | con, con->http.fd, con->request->request.op.operation_id); | |
233 | ||
234 | /* | |
235 | * First build an empty response message for this request... | |
236 | */ | |
237 | ||
238 | con->response = ippNew(); | |
239 | ||
240 | con->response->request.status.version[0] = con->request->request.op.version[0]; | |
241 | con->response->request.status.version[1] = con->request->request.op.version[1]; | |
242 | con->response->request.status.request_id = con->request->request.op.request_id; | |
243 | ||
244 | /* | |
245 | * Then validate the request header and required attributes... | |
246 | */ | |
247 | ||
248 | if (con->request->request.any.version[0] != 1) | |
249 | { | |
250 | /* | |
251 | * Return an error, since we only support IPP 1.x. | |
252 | */ | |
253 | ||
254 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
255 | "%04X %s Bad request version number %d.%d", | |
256 | IPP_VERSION_NOT_SUPPORTED, con->http.hostname, | |
257 | con->request->request.any.version[0], | |
258 | con->request->request.any.version[1]); | |
259 | ||
260 | send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED, | |
261 | _("Bad request version number %d.%d!"), | |
262 | con->request->request.any.version[0], | |
263 | con->request->request.any.version[1]); | |
264 | } | |
fa73b229 | 265 | else if (!con->request->attrs) |
ef416fc2 | 266 | { |
267 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
268 | "%04X %s No attributes in request", | |
269 | IPP_BAD_REQUEST, con->http.hostname); | |
270 | ||
271 | send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!")); | |
272 | } | |
273 | else | |
274 | { | |
275 | /* | |
276 | * Make sure that the attributes are provided in the correct order and | |
277 | * don't repeat groups... | |
278 | */ | |
279 | ||
280 | for (attr = con->request->attrs, group = attr->group_tag; | |
fa73b229 | 281 | attr; |
ef416fc2 | 282 | attr = attr->next) |
283 | if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO) | |
284 | { | |
285 | /* | |
286 | * Out of order; return an error... | |
287 | */ | |
288 | ||
289 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
290 | "%04X %s Attribute groups are out of order", | |
291 | IPP_BAD_REQUEST, con->http.hostname); | |
292 | ||
293 | send_ipp_status(con, IPP_BAD_REQUEST, | |
294 | _("Attribute groups are out of order (%x < %x)!"), | |
295 | attr->group_tag, group); | |
296 | break; | |
297 | } | |
298 | else | |
299 | group = attr->group_tag; | |
300 | ||
fa73b229 | 301 | if (!attr) |
ef416fc2 | 302 | { |
303 | /* | |
304 | * Then make sure that the first three attributes are: | |
305 | * | |
306 | * attributes-charset | |
307 | * attributes-natural-language | |
308 | * printer-uri/job-uri | |
309 | */ | |
310 | ||
311 | attr = con->request->attrs; | |
312 | if (attr && !strcmp(attr->name, "attributes-charset") && | |
fa73b229 | 313 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET) |
ef416fc2 | 314 | charset = attr; |
315 | else | |
316 | charset = NULL; | |
317 | ||
318 | if (attr) | |
319 | attr = attr->next; | |
320 | ||
321 | if (attr && !strcmp(attr->name, "attributes-natural-language") && | |
fa73b229 | 322 | (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE) |
ef416fc2 | 323 | language = attr; |
324 | else | |
325 | language = NULL; | |
326 | ||
fa73b229 | 327 | if ((attr = ippFindAttribute(con->request, "printer-uri", |
328 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 329 | uri = attr; |
fa73b229 | 330 | else if ((attr = ippFindAttribute(con->request, "job-uri", |
331 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 332 | uri = attr; |
333 | else | |
334 | uri = NULL; | |
335 | ||
336 | if (charset) | |
337 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
338 | "attributes-charset", NULL, charset->values[0].string.text); | |
339 | else | |
340 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
341 | "attributes-charset", NULL, DefaultCharset); | |
342 | ||
343 | if (language) | |
344 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
345 | "attributes-natural-language", NULL, | |
346 | language->values[0].string.text); | |
347 | else | |
348 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
349 | "attributes-natural-language", NULL, DefaultLanguage); | |
350 | ||
fa73b229 | 351 | if (!charset || !language || |
352 | (!uri && | |
ef416fc2 | 353 | con->request->request.op.operation_id != CUPS_GET_DEFAULT && |
354 | con->request->request.op.operation_id != CUPS_GET_PRINTERS && | |
355 | con->request->request.op.operation_id != CUPS_GET_CLASSES && | |
356 | con->request->request.op.operation_id != CUPS_GET_DEVICES && | |
357 | con->request->request.op.operation_id != CUPS_GET_PPDS)) | |
358 | { | |
359 | /* | |
360 | * Return an error, since attributes-charset, | |
361 | * attributes-natural-language, and printer-uri/job-uri are required | |
362 | * for all operations. | |
363 | */ | |
364 | ||
365 | if (!charset) | |
366 | { | |
367 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
368 | "Missing attributes-charset attribute!"); | |
369 | ||
370 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
371 | "%04X %s Missing attributes-charset attribute", | |
372 | IPP_BAD_REQUEST, con->http.hostname); | |
373 | } | |
374 | ||
375 | if (!language) | |
376 | { | |
377 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
378 | "Missing attributes-natural-language attribute!"); | |
379 | ||
380 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
381 | "%04X %s Missing attributes-natural-language attribute", | |
382 | IPP_BAD_REQUEST, con->http.hostname); | |
383 | } | |
384 | ||
385 | if (!uri) | |
386 | { | |
387 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
388 | "Missing printer-uri or job-uri attribute!"); | |
389 | ||
390 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
391 | "%04X %s Missing printer-uri or job-uri attribute", | |
392 | IPP_BAD_REQUEST, con->http.hostname); | |
393 | } | |
394 | ||
395 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow..."); | |
396 | ||
fa73b229 | 397 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 398 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
399 | "attr \"%s\": group_tag = %x, value_tag = %x", | |
400 | attr->name ? attr->name : "(null)", attr->group_tag, | |
401 | attr->value_tag); | |
402 | ||
403 | cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes..."); | |
404 | ||
405 | send_ipp_status(con, IPP_BAD_REQUEST, | |
406 | _("Missing required attributes!")); | |
407 | } | |
408 | else | |
409 | { | |
410 | /* | |
411 | * OK, all the checks pass so far; make sure requesting-user-name is | |
412 | * not "root" from a remote host... | |
413 | */ | |
414 | ||
415 | if ((username = ippFindAttribute(con->request, "requesting-user-name", | |
416 | IPP_TAG_NAME)) != NULL) | |
417 | { | |
418 | /* | |
419 | * Check for root user... | |
420 | */ | |
421 | ||
422 | if (!strcmp(username->values[0].string.text, "root") && | |
423 | strcasecmp(con->http.hostname, "localhost") && | |
424 | strcmp(con->username, "root")) | |
425 | { | |
426 | /* | |
427 | * Remote unauthenticated user masquerading as local root... | |
428 | */ | |
429 | ||
757d2cad | 430 | _cupsStrFree(username->values[0].string.text); |
431 | username->values[0].string.text = _cupsStrAlloc(RemoteRoot); | |
ef416fc2 | 432 | } |
433 | } | |
434 | ||
435 | if ((attr = ippFindAttribute(con->request, "notify-subscription-id", | |
436 | IPP_TAG_INTEGER)) != NULL) | |
437 | sub_id = attr->values[0].integer; | |
438 | else | |
439 | sub_id = 0; | |
440 | ||
441 | /* | |
442 | * Then try processing the operation... | |
443 | */ | |
444 | ||
445 | if (uri) | |
bd7854cb | 446 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", |
447 | ippOpString(con->request->request.op.operation_id), | |
448 | uri->values[0].string.text); | |
449 | else | |
450 | cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", | |
451 | ippOpString(con->request->request.op.operation_id)); | |
ef416fc2 | 452 | |
453 | switch (con->request->request.op.operation_id) | |
454 | { | |
455 | case IPP_PRINT_JOB : | |
456 | print_job(con, uri); | |
457 | break; | |
458 | ||
459 | case IPP_VALIDATE_JOB : | |
460 | validate_job(con, uri); | |
461 | break; | |
462 | ||
463 | case IPP_CREATE_JOB : | |
464 | create_job(con, uri); | |
465 | break; | |
466 | ||
467 | case IPP_SEND_DOCUMENT : | |
468 | send_document(con, uri); | |
469 | break; | |
470 | ||
471 | case IPP_CANCEL_JOB : | |
472 | cancel_job(con, uri); | |
473 | break; | |
474 | ||
475 | case IPP_GET_JOB_ATTRIBUTES : | |
476 | get_job_attrs(con, uri); | |
477 | break; | |
478 | ||
479 | case IPP_GET_JOBS : | |
480 | get_jobs(con, uri); | |
481 | break; | |
482 | ||
483 | case IPP_GET_PRINTER_ATTRIBUTES : | |
484 | get_printer_attrs(con, uri); | |
485 | break; | |
486 | ||
487 | case IPP_HOLD_JOB : | |
488 | hold_job(con, uri); | |
489 | break; | |
490 | ||
491 | case IPP_RELEASE_JOB : | |
492 | release_job(con, uri); | |
493 | break; | |
494 | ||
495 | case IPP_RESTART_JOB : | |
496 | restart_job(con, uri); | |
497 | break; | |
498 | ||
499 | case IPP_PAUSE_PRINTER : | |
500 | stop_printer(con, uri); | |
501 | break; | |
502 | ||
503 | case IPP_RESUME_PRINTER : | |
504 | start_printer(con, uri); | |
505 | break; | |
506 | ||
507 | case IPP_PURGE_JOBS : | |
508 | cancel_all_jobs(con, uri); | |
509 | break; | |
510 | ||
511 | case IPP_SET_JOB_ATTRIBUTES : | |
512 | set_job_attrs(con, uri); | |
513 | break; | |
514 | ||
515 | case CUPS_GET_DEFAULT : | |
516 | get_default(con); | |
517 | break; | |
518 | ||
519 | case CUPS_GET_PRINTERS : | |
520 | get_printers(con, 0); | |
521 | break; | |
522 | ||
523 | case CUPS_GET_CLASSES : | |
524 | get_printers(con, CUPS_PRINTER_CLASS); | |
525 | break; | |
526 | ||
527 | case CUPS_ADD_PRINTER : | |
528 | add_printer(con, uri); | |
529 | break; | |
530 | ||
531 | case CUPS_DELETE_PRINTER : | |
532 | delete_printer(con, uri); | |
533 | break; | |
534 | ||
535 | case CUPS_ADD_CLASS : | |
536 | add_class(con, uri); | |
537 | break; | |
538 | ||
539 | case CUPS_DELETE_CLASS : | |
540 | delete_printer(con, uri); | |
541 | break; | |
542 | ||
543 | case CUPS_ACCEPT_JOBS : | |
544 | case IPP_ENABLE_PRINTER : | |
545 | accept_jobs(con, uri); | |
546 | break; | |
547 | ||
548 | case CUPS_REJECT_JOBS : | |
549 | case IPP_DISABLE_PRINTER : | |
550 | reject_jobs(con, uri); | |
551 | break; | |
552 | ||
553 | case CUPS_SET_DEFAULT : | |
554 | set_default(con, uri); | |
555 | break; | |
556 | ||
557 | case CUPS_GET_DEVICES : | |
558 | get_devices(con); | |
559 | break; | |
560 | ||
561 | case CUPS_GET_PPDS : | |
562 | get_ppds(con); | |
563 | break; | |
564 | ||
565 | case CUPS_MOVE_JOB : | |
566 | move_job(con, uri); | |
567 | break; | |
568 | ||
569 | case CUPS_AUTHENTICATE_JOB : | |
570 | authenticate_job(con, uri); | |
571 | break; | |
572 | ||
573 | case IPP_CREATE_PRINTER_SUBSCRIPTION : | |
574 | case IPP_CREATE_JOB_SUBSCRIPTION : | |
575 | create_subscription(con, uri); | |
576 | break; | |
577 | ||
578 | case IPP_GET_SUBSCRIPTION_ATTRIBUTES : | |
579 | get_subscription_attrs(con, sub_id); | |
580 | break; | |
581 | ||
582 | case IPP_GET_SUBSCRIPTIONS : | |
583 | get_subscriptions(con, uri); | |
584 | break; | |
585 | ||
586 | case IPP_RENEW_SUBSCRIPTION : | |
587 | renew_subscription(con, sub_id); | |
588 | break; | |
589 | ||
590 | case IPP_CANCEL_SUBSCRIPTION : | |
591 | cancel_subscription(con, sub_id); | |
592 | break; | |
593 | ||
594 | case IPP_GET_NOTIFICATIONS : | |
bd7854cb | 595 | get_notifications(con); |
ef416fc2 | 596 | break; |
597 | ||
598 | default : | |
599 | cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, | |
600 | "%04X %s Operation %04X (%s) not supported", | |
601 | IPP_OPERATION_NOT_SUPPORTED, con->http.hostname, | |
602 | con->request->request.op.operation_id, | |
603 | ippOpString(con->request->request.op.operation_id)); | |
604 | ||
605 | send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED, | |
606 | _("%s not supported!"), | |
607 | ippOpString(con->request->request.op.operation_id)); | |
608 | break; | |
609 | } | |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | if (con->response) | |
615 | { | |
616 | /* | |
617 | * Sending data from the scheduler... | |
618 | */ | |
619 | ||
620 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
621 | "cupsdProcessIPPRequest: %d status_code=%x (%s)", | |
622 | con->http.fd, con->response->request.status.status_code, | |
623 | ippErrorString(con->response->request.status.status_code)); | |
624 | ||
625 | if (cupsdSendHeader(con, HTTP_OK, "application/ipp")) | |
626 | { | |
627 | if (con->http.version == HTTP_1_1) | |
628 | { | |
629 | con->http.data_encoding = HTTP_ENCODE_CHUNKED; | |
630 | ||
631 | httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n"); | |
632 | } | |
633 | else | |
634 | { | |
635 | con->http.data_encoding = HTTP_ENCODE_LENGTH; | |
636 | con->http.data_remaining = ippLength(con->response); | |
637 | ||
638 | if (con->http.data_remaining < INT_MAX) | |
639 | con->http._data_remaining = con->http.data_remaining; | |
640 | else | |
641 | con->http._data_remaining = INT_MAX; | |
642 | ||
643 | httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n", | |
644 | CUPS_LLCAST con->http.data_remaining); | |
645 | } | |
646 | ||
647 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
648 | "cupsdProcessIPPRequest: Adding fd %d to OutputSet...", | |
649 | con->http.fd); | |
650 | ||
651 | FD_SET(con->http.fd, OutputSet); | |
652 | ||
653 | /* | |
654 | * Tell the caller the response header was sent successfully... | |
655 | */ | |
656 | ||
657 | return (1); | |
658 | } | |
659 | else | |
660 | { | |
661 | /* | |
662 | * Tell the caller the response header could not be sent... | |
663 | */ | |
664 | ||
665 | return (0); | |
666 | } | |
667 | } | |
668 | else | |
669 | { | |
670 | /* | |
671 | * Sending data from a subprocess like cups-deviced; tell the caller | |
672 | * everything is A-OK so far... | |
673 | */ | |
674 | ||
675 | return (1); | |
676 | } | |
677 | } | |
678 | ||
679 | ||
680 | /* | |
681 | * 'accept_jobs()' - Accept print jobs to a printer. | |
682 | */ | |
683 | ||
684 | static void | |
685 | accept_jobs(cupsd_client_t *con, /* I - Client connection */ | |
686 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
687 | { | |
688 | http_status_t status; /* Policy status */ | |
689 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
690 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
691 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
692 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
693 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
694 | int port; /* Port portion of URI */ | |
695 | const char *name; /* Printer name */ | |
696 | cupsd_printer_t *printer; /* Printer data */ | |
697 | ||
698 | ||
699 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con, | |
700 | con->http.fd, uri->values[0].string.text); | |
701 | ||
702 | /* | |
703 | * Is the destination valid? | |
704 | */ | |
705 | ||
a4d04587 | 706 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
707 | sizeof(method), username, sizeof(username), host, | |
708 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 709 | |
710 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
711 | { | |
712 | /* | |
713 | * Bad URI... | |
714 | */ | |
715 | ||
716 | send_ipp_status(con, IPP_NOT_FOUND, | |
717 | _("The printer or class was not found.")); | |
718 | return; | |
719 | } | |
720 | ||
721 | /* | |
722 | * Check policy... | |
723 | */ | |
724 | ||
725 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
726 | { | |
727 | send_http_error(con, status); | |
728 | return; | |
729 | } | |
730 | ||
731 | /* | |
732 | * Accept jobs sent to the printer... | |
733 | */ | |
734 | ||
735 | printer->accepting = 1; | |
736 | printer->state_message[0] = '\0'; | |
737 | ||
738 | cupsdAddPrinterHistory(printer); | |
739 | ||
740 | if (dtype & CUPS_PRINTER_CLASS) | |
741 | cupsdSaveAllClasses(); | |
742 | else | |
743 | cupsdSaveAllPrinters(); | |
744 | ||
745 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" now accepting jobs (\"%s\").", name, | |
e00b005a | 746 | get_username(con)); |
ef416fc2 | 747 | |
748 | /* | |
749 | * Everything was ok, so return OK status... | |
750 | */ | |
751 | ||
752 | con->response->request.status.status_code = IPP_OK; | |
753 | } | |
754 | ||
755 | ||
756 | /* | |
757 | * 'add_class()' - Add a class to the system. | |
758 | */ | |
759 | ||
760 | static void | |
761 | add_class(cupsd_client_t *con, /* I - Client connection */ | |
762 | ipp_attribute_t *uri) /* I - URI of class */ | |
763 | { | |
764 | http_status_t status; /* Policy status */ | |
765 | int i; /* Looping var */ | |
766 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
767 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
768 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
769 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
770 | int port; /* Port portion of URI */ | |
771 | cupsd_printer_t *pclass, /* Class */ | |
772 | *member; /* Member printer/class */ | |
773 | cups_ptype_t dtype; /* Destination type */ | |
774 | const char *dest; /* Printer or class name */ | |
775 | ipp_attribute_t *attr; /* Printer attribute */ | |
776 | int modify; /* Non-zero if we just modified */ | |
b423cd4c | 777 | char newname[IPP_MAX_NAME]; /* New class name */ |
e00b005a | 778 | int need_restart_job; /* Need to restart job? */ |
ef416fc2 | 779 | |
780 | ||
781 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con, | |
782 | con->http.fd, uri->values[0].string.text); | |
783 | ||
784 | /* | |
785 | * Do we have a valid URI? | |
786 | */ | |
787 | ||
a4d04587 | 788 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
789 | sizeof(method), username, sizeof(username), host, | |
790 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 791 | |
792 | ||
fa73b229 | 793 | if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9) |
ef416fc2 | 794 | { |
795 | /* | |
796 | * No, return an error... | |
797 | */ | |
798 | ||
799 | send_ipp_status(con, IPP_BAD_REQUEST, | |
800 | _("The printer-uri must be of the form " | |
801 | "\"ipp://HOSTNAME/classes/CLASSNAME\".")); | |
802 | return; | |
803 | } | |
804 | ||
805 | /* | |
806 | * Do we have a valid printer name? | |
807 | */ | |
808 | ||
809 | if (!validate_name(resource + 9)) | |
810 | { | |
811 | /* | |
812 | * No, return an error... | |
813 | */ | |
814 | ||
815 | send_ipp_status(con, IPP_BAD_REQUEST, | |
816 | _("The printer-uri \"%s\" contains invalid characters."), | |
817 | uri->values[0].string.text); | |
818 | return; | |
819 | } | |
820 | ||
821 | /* | |
822 | * Check policy... | |
823 | */ | |
824 | ||
825 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
826 | { | |
827 | send_http_error(con, status); | |
828 | return; | |
829 | } | |
830 | ||
831 | /* | |
832 | * See if the class already exists; if not, create a new class... | |
833 | */ | |
834 | ||
835 | if ((pclass = cupsdFindClass(resource + 9)) == NULL) | |
836 | { | |
837 | /* | |
838 | * Class doesn't exist; see if we have a printer of the same name... | |
839 | */ | |
840 | ||
841 | if ((pclass = cupsdFindPrinter(resource + 9)) != NULL && | |
842 | !(pclass->type & CUPS_PRINTER_REMOTE)) | |
843 | { | |
844 | /* | |
845 | * Yes, return an error... | |
846 | */ | |
847 | ||
848 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
849 | _("A printer named \"%s\" already exists!"), | |
850 | resource + 9); | |
851 | return; | |
852 | } | |
853 | ||
854 | /* | |
855 | * No, add the pclass... | |
856 | */ | |
857 | ||
858 | pclass = cupsdAddClass(resource + 9); | |
859 | modify = 0; | |
860 | } | |
861 | else if (pclass->type & CUPS_PRINTER_IMPLICIT) | |
862 | { | |
863 | /* | |
864 | * Rename the implicit class to "AnyClass" or remove it... | |
865 | */ | |
866 | ||
867 | if (ImplicitAnyClasses) | |
868 | { | |
b423cd4c | 869 | snprintf(newname, sizeof(newname), "Any%s", resource + 9); |
870 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 871 | } |
872 | else | |
873 | cupsdDeletePrinter(pclass, 1); | |
874 | ||
875 | /* | |
876 | * Add the class as a new local class... | |
877 | */ | |
878 | ||
879 | pclass = cupsdAddClass(resource + 9); | |
880 | modify = 0; | |
881 | } | |
882 | else if (pclass->type & CUPS_PRINTER_REMOTE) | |
883 | { | |
884 | /* | |
885 | * Rename the remote class to "Class"... | |
886 | */ | |
887 | ||
b423cd4c | 888 | snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname); |
889 | cupsdRenamePrinter(pclass, newname); | |
ef416fc2 | 890 | |
891 | /* | |
892 | * Add the class as a new local class... | |
893 | */ | |
894 | ||
895 | pclass = cupsdAddClass(resource + 9); | |
896 | modify = 0; | |
897 | } | |
898 | else | |
899 | modify = 1; | |
900 | ||
901 | /* | |
902 | * Look for attributes and copy them over as needed... | |
903 | */ | |
904 | ||
e00b005a | 905 | need_restart_job = 0; |
906 | ||
fa73b229 | 907 | if ((attr = ippFindAttribute(con->request, "printer-location", |
908 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 909 | cupsdSetString(&pclass->location, attr->values[0].string.text); |
910 | ||
fa73b229 | 911 | if ((attr = ippFindAttribute(con->request, "printer-info", |
912 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 913 | cupsdSetString(&pclass->info, attr->values[0].string.text); |
914 | ||
fa73b229 | 915 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
916 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 917 | { |
918 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
919 | pclass->name, attr->values[0].boolean, pclass->accepting); | |
920 | ||
921 | pclass->accepting = attr->values[0].boolean; | |
922 | cupsdAddPrinterHistory(pclass); | |
923 | } | |
924 | ||
fa73b229 | 925 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
926 | IPP_TAG_BOOLEAN)) != NULL) | |
ef416fc2 | 927 | { |
928 | if (pclass->shared && !attr->values[0].boolean) | |
929 | cupsdSendBrowseDelete(pclass); | |
930 | ||
931 | cupsdLogMessage(CUPSD_LOG_INFO, | |
932 | "Setting %s printer-is-shared to %d (was %d.)", | |
933 | pclass->name, attr->values[0].boolean, pclass->shared); | |
934 | ||
935 | pclass->shared = attr->values[0].boolean; | |
936 | } | |
937 | ||
fa73b229 | 938 | if ((attr = ippFindAttribute(con->request, "printer-state", |
939 | IPP_TAG_ENUM)) != NULL) | |
ef416fc2 | 940 | { |
941 | if (attr->values[0].integer != IPP_PRINTER_IDLE && | |
942 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
943 | { | |
944 | send_ipp_status(con, IPP_BAD_REQUEST, | |
945 | _("Attempt to set %s printer-state to bad value %d!"), | |
946 | pclass->name, attr->values[0].integer); | |
947 | return; | |
948 | } | |
949 | ||
950 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", pclass->name, | |
951 | attr->values[0].integer, pclass->state); | |
952 | ||
953 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
954 | cupsdStopPrinter(pclass, 0); | |
955 | else | |
e00b005a | 956 | { |
ef416fc2 | 957 | cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0); |
e00b005a | 958 | need_restart_job = 1; |
959 | } | |
ef416fc2 | 960 | } |
fa73b229 | 961 | if ((attr = ippFindAttribute(con->request, "printer-state-message", |
962 | IPP_TAG_TEXT)) != NULL) | |
ef416fc2 | 963 | { |
964 | strlcpy(pclass->state_message, attr->values[0].string.text, | |
965 | sizeof(pclass->state_message)); | |
966 | cupsdAddPrinterHistory(pclass); | |
967 | } | |
fa73b229 | 968 | if ((attr = ippFindAttribute(con->request, "member-uris", |
969 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 970 | { |
971 | /* | |
972 | * Clear the printer array as needed... | |
973 | */ | |
974 | ||
e00b005a | 975 | need_restart_job = 1; |
976 | ||
ef416fc2 | 977 | if (pclass->num_printers > 0) |
978 | { | |
979 | free(pclass->printers); | |
980 | pclass->num_printers = 0; | |
981 | } | |
982 | ||
983 | /* | |
984 | * Add each printer or class that is listed... | |
985 | */ | |
986 | ||
987 | for (i = 0; i < attr->num_values; i ++) | |
988 | { | |
989 | /* | |
990 | * Search for the printer or class URI... | |
991 | */ | |
992 | ||
a4d04587 | 993 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, method, |
994 | sizeof(method), username, sizeof(username), host, | |
995 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 996 | |
997 | if ((dest = cupsdValidateDest(host, resource, &dtype, &member)) == NULL) | |
998 | { | |
999 | /* | |
1000 | * Bad URI... | |
1001 | */ | |
1002 | ||
1003 | send_ipp_status(con, IPP_NOT_FOUND, | |
1004 | _("The printer or class was not found.")); | |
1005 | return; | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Add it to the class... | |
1010 | */ | |
1011 | ||
1012 | cupsdAddPrinterToClass(pclass, member); | |
1013 | } | |
1014 | } | |
1015 | ||
b423cd4c | 1016 | set_printer_defaults(con, pclass); |
1017 | ||
ef416fc2 | 1018 | /* |
1019 | * Update the printer class attributes and return... | |
1020 | */ | |
1021 | ||
1022 | cupsdSetPrinterAttrs(pclass); | |
1023 | cupsdSaveAllClasses(); | |
e00b005a | 1024 | |
1025 | if (need_restart_job && pclass->job) | |
1026 | { | |
1027 | cupsd_job_t *job; | |
1028 | ||
1029 | /* | |
1030 | * Stop the current job and then restart it below... | |
1031 | */ | |
1032 | ||
1033 | job = (cupsd_job_t *)pclass->job; | |
1034 | ||
1035 | cupsdStopJob(job, 1); | |
bd7854cb | 1036 | |
e00b005a | 1037 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 1038 | job->state_value = IPP_JOB_PENDING; |
e00b005a | 1039 | } |
1040 | ||
1041 | if (need_restart_job) | |
1042 | cupsdCheckJobs(); | |
ef416fc2 | 1043 | |
1044 | cupsdWritePrintcap(); | |
1045 | ||
1046 | if (modify) | |
1047 | { | |
1048 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL, | |
1049 | "Class \"%s\" modified by \"%s\".", pclass->name, | |
e00b005a | 1050 | get_username(con)); |
ef416fc2 | 1051 | |
1052 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".", | |
e00b005a | 1053 | pclass->name, get_username(con)); |
ef416fc2 | 1054 | } |
1055 | else | |
1056 | { | |
1057 | cupsdAddPrinterHistory(pclass); | |
1058 | ||
1059 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL, | |
1060 | "New class \"%s\" added by \"%s\".", pclass->name, | |
e00b005a | 1061 | get_username(con)); |
ef416fc2 | 1062 | |
1063 | cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".", | |
e00b005a | 1064 | pclass->name, get_username(con)); |
ef416fc2 | 1065 | } |
1066 | ||
1067 | con->response->request.status.status_code = IPP_OK; | |
1068 | } | |
1069 | ||
1070 | ||
1071 | /* | |
1072 | * 'add_file()' - Add a file to a job. | |
1073 | */ | |
1074 | ||
1075 | static int /* O - 0 on success, -1 on error */ | |
1076 | add_file(cupsd_client_t *con, /* I - Connection to client */ | |
1077 | cupsd_job_t *job, /* I - Job to add to */ | |
1078 | mime_type_t *filetype, /* I - Type of file */ | |
1079 | int compression) /* I - Compression */ | |
1080 | { | |
1081 | mime_type_t **filetypes; /* New filetypes array... */ | |
1082 | int *compressions; /* New compressions array... */ | |
1083 | ||
1084 | ||
1085 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1086 | "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)", | |
1087 | con, con->http.fd, job->id, filetype->super, filetype->type, | |
1088 | compression); | |
1089 | ||
1090 | /* | |
1091 | * Add the file to the job... | |
1092 | */ | |
1093 | ||
1094 | if (job->num_files == 0) | |
1095 | { | |
1096 | compressions = (int *)malloc(sizeof(int)); | |
1097 | filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *)); | |
1098 | } | |
1099 | else | |
1100 | { | |
1101 | compressions = (int *)realloc(job->compressions, | |
1102 | (job->num_files + 1) * sizeof(int)); | |
1103 | filetypes = (mime_type_t **)realloc(job->filetypes, | |
1104 | (job->num_files + 1) * | |
1105 | sizeof(mime_type_t *)); | |
1106 | } | |
1107 | ||
fa73b229 | 1108 | if (!compressions || !filetypes) |
ef416fc2 | 1109 | { |
1110 | cupsdCancelJob(job, 1); | |
1111 | ||
1112 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1113 | _("Unable to allocate memory for file types!")); | |
1114 | return (-1); | |
1115 | } | |
1116 | ||
1117 | job->compressions = compressions; | |
1118 | job->compressions[job->num_files] = compression; | |
1119 | job->filetypes = filetypes; | |
1120 | job->filetypes[job->num_files] = filetype; | |
1121 | ||
1122 | job->num_files ++; | |
1123 | ||
1124 | return (0); | |
1125 | } | |
1126 | ||
1127 | ||
1128 | /* | |
b423cd4c | 1129 | * 'add_job()' - Add a job to a print queue. |
ef416fc2 | 1130 | */ |
1131 | ||
b423cd4c | 1132 | static cupsd_job_t * /* O - Job object */ |
1133 | add_job(cupsd_client_t *con, /* I - Client connection */ | |
1134 | ipp_attribute_t *uri, /* I - printer-uri */ | |
80ca4592 | 1135 | cupsd_printer_t **dprinter, /* I - Destination printer */ |
1136 | mime_type_t *filetype) /* I - First print file type, if any */ | |
ef416fc2 | 1137 | { |
b423cd4c | 1138 | http_status_t status; /* Policy status */ |
1139 | ipp_attribute_t *attr; /* Current attribute */ | |
1140 | const char *dest; /* Destination */ | |
1141 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
1142 | const char *val; /* Default option value */ | |
1143 | int priority; /* Job priority */ | |
1144 | char *title; /* Job name/title */ | |
1145 | cupsd_job_t *job; /* Current job */ | |
1146 | char job_uri[HTTP_MAX_URI], /* Job URI */ | |
1147 | method[HTTP_MAX_URI], /* Method portion of URI */ | |
1148 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
1149 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
1150 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
1151 | int port; /* Port portion of URI */ | |
1152 | cupsd_printer_t *printer; /* Printer data */ | |
1153 | int kbytes; /* Size of print file */ | |
1154 | int i; /* Looping var */ | |
1155 | int lowerpagerange; /* Page range bound */ | |
ef416fc2 | 1156 | |
1157 | ||
b423cd4c | 1158 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %s)", con, |
1159 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 1160 | |
b423cd4c | 1161 | /* |
1162 | * Is the destination valid? | |
1163 | */ | |
1164 | ||
1165 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
1166 | sizeof(method), username, sizeof(username), host, | |
1167 | sizeof(host), &port, resource, sizeof(resource)); | |
1168 | ||
1169 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 1170 | { |
b423cd4c | 1171 | /* |
1172 | * Bad URI... | |
1173 | */ | |
ef416fc2 | 1174 | |
b423cd4c | 1175 | send_ipp_status(con, IPP_NOT_FOUND, |
1176 | _("The printer or class was not found.")); | |
1177 | return (NULL); | |
1178 | } | |
ef416fc2 | 1179 | |
b423cd4c | 1180 | if (dprinter) |
1181 | *dprinter = printer; | |
ef416fc2 | 1182 | |
b423cd4c | 1183 | /* |
1184 | * Check remote printing to non-shared printer... | |
1185 | */ | |
ef416fc2 | 1186 | |
b423cd4c | 1187 | if (!printer->shared && |
1188 | strcasecmp(con->http.hostname, "localhost") && | |
1189 | strcasecmp(con->http.hostname, ServerName)) | |
1190 | { | |
1191 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
1192 | _("The printer or class is not shared!")); | |
1193 | return (NULL); | |
1194 | } | |
ef416fc2 | 1195 | |
b423cd4c | 1196 | /* |
1197 | * Check policy... | |
1198 | */ | |
ef416fc2 | 1199 | |
b423cd4c | 1200 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
1201 | { | |
1202 | send_http_error(con, status); | |
1203 | return (NULL); | |
1204 | } | |
1205 | else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) && !con->username[0]) | |
1206 | { | |
1207 | send_http_error(con, HTTP_UNAUTHORIZED); | |
1208 | return (NULL); | |
1209 | } | |
ef416fc2 | 1210 | |
b423cd4c | 1211 | /* |
1212 | * See if the printer is accepting jobs... | |
1213 | */ | |
1214 | ||
1215 | if (!printer->accepting) | |
1216 | { | |
1217 | send_ipp_status(con, IPP_NOT_ACCEPTING, | |
1218 | _("Destination \"%s\" is not accepting jobs."), | |
1219 | dest); | |
1220 | return (NULL); | |
ef416fc2 | 1221 | } |
ef416fc2 | 1222 | |
b423cd4c | 1223 | /* |
80ca4592 | 1224 | * Validate job template attributes; for now just document-format, |
1225 | * copies, and page-ranges... | |
b423cd4c | 1226 | */ |
ef416fc2 | 1227 | |
80ca4592 | 1228 | if (filetype && printer->filetypes && |
1229 | !cupsArrayFind(printer->filetypes, filetype)) | |
1230 | { | |
1231 | char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
1232 | /* MIME media type string */ | |
1233 | ||
1234 | ||
1235 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
1236 | filetype->type); | |
1237 | ||
1238 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
1239 | _("Unsupported format \'%s\'!"), mimetype); | |
1240 | ||
1241 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
1242 | "document-format", NULL, mimetype); | |
1243 | ||
1244 | return (NULL); | |
1245 | } | |
1246 | ||
b423cd4c | 1247 | if ((attr = ippFindAttribute(con->request, "copies", |
1248 | IPP_TAG_INTEGER)) != NULL) | |
1249 | { | |
1250 | if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies) | |
1251 | { | |
1252 | send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."), | |
1253 | attr->values[0].integer); | |
1254 | ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, | |
1255 | "copies", attr->values[0].integer); | |
1256 | return (NULL); | |
1257 | } | |
1258 | } | |
ef416fc2 | 1259 | |
b423cd4c | 1260 | if ((attr = ippFindAttribute(con->request, "page-ranges", |
1261 | IPP_TAG_RANGE)) != NULL) | |
1262 | { | |
1263 | for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++) | |
1264 | { | |
1265 | if (attr->values[i].range.lower < lowerpagerange || | |
1266 | attr->values[i].range.lower > attr->values[i].range.upper) | |
1267 | { | |
1268 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1269 | _("Bad page-ranges values %d-%d."), | |
1270 | attr->values[i].range.lower, | |
1271 | attr->values[i].range.upper); | |
1272 | return (NULL); | |
1273 | } | |
ef416fc2 | 1274 | |
b423cd4c | 1275 | lowerpagerange = attr->values[i].range.upper + 1; |
1276 | } | |
1277 | } | |
ef416fc2 | 1278 | |
1279 | /* | |
b423cd4c | 1280 | * Make sure we aren't over our limit... |
ef416fc2 | 1281 | */ |
1282 | ||
b423cd4c | 1283 | if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs) |
1284 | cupsdCleanJobs(); | |
ef416fc2 | 1285 | |
b423cd4c | 1286 | if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs) |
1287 | { | |
1288 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
1289 | _("Too many active jobs.")); | |
1290 | return (NULL); | |
1291 | } | |
1292 | ||
1293 | if (!check_quotas(con, printer)) | |
1294 | { | |
1295 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached.")); | |
1296 | return (NULL); | |
1297 | } | |
ef416fc2 | 1298 | |
1299 | /* | |
b423cd4c | 1300 | * Create the job and set things up... |
ef416fc2 | 1301 | */ |
1302 | ||
b423cd4c | 1303 | if ((attr = ippFindAttribute(con->request, "job-priority", |
1304 | IPP_TAG_INTEGER)) != NULL) | |
1305 | priority = attr->values[0].integer; | |
1306 | else | |
ef416fc2 | 1307 | { |
b423cd4c | 1308 | if ((val = cupsGetOption("job-priority", printer->num_options, |
1309 | printer->options)) != NULL) | |
1310 | priority = atoi(val); | |
1311 | else | |
1312 | priority = 50; | |
ef416fc2 | 1313 | |
b423cd4c | 1314 | ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority", |
1315 | priority); | |
1316 | } | |
ef416fc2 | 1317 | |
b423cd4c | 1318 | if ((attr = ippFindAttribute(con->request, "job-name", |
1319 | IPP_TAG_NAME)) != NULL) | |
1320 | title = attr->values[0].string.text; | |
1321 | else | |
1322 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1323 | title = "Untitled"); | |
ef416fc2 | 1324 | |
b423cd4c | 1325 | if ((job = cupsdAddJob(priority, printer->name)) == NULL) |
1326 | { | |
1327 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
1328 | _("Unable to add job for destination \"%s\"!"), dest); | |
1329 | return (NULL); | |
1330 | } | |
ef416fc2 | 1331 | |
b423cd4c | 1332 | job->dtype = dtype; |
1333 | job->attrs = con->request; | |
1334 | con->request = NULL; | |
ef416fc2 | 1335 | |
b423cd4c | 1336 | add_job_uuid(con, job); |
1337 | apply_printer_defaults(printer, job); | |
ef416fc2 | 1338 | |
b423cd4c | 1339 | attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME); |
ef416fc2 | 1340 | |
b423cd4c | 1341 | if (con->username[0]) |
1342 | { | |
1343 | cupsdSetString(&job->username, con->username); | |
ef416fc2 | 1344 | |
1345 | if (attr) | |
b423cd4c | 1346 | cupsdSetString(&attr->values[0].string.text, con->username); |
1347 | ||
1348 | save_auth_info(con, job); | |
ef416fc2 | 1349 | } |
b423cd4c | 1350 | else if (attr) |
1351 | { | |
1352 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1353 | "add_job: requesting-user-name=\"%s\"", | |
1354 | attr->values[0].string.text); | |
ef416fc2 | 1355 | |
b423cd4c | 1356 | cupsdSetString(&job->username, attr->values[0].string.text); |
1357 | } | |
1358 | else | |
1359 | cupsdSetString(&job->username, "anonymous"); | |
ef416fc2 | 1360 | |
b423cd4c | 1361 | if (!attr) |
1362 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, | |
1363 | "job-originating-user-name", NULL, job->username); | |
1364 | else | |
1365 | { | |
1366 | attr->group_tag = IPP_TAG_JOB; | |
757d2cad | 1367 | _cupsStrFree(attr->name); |
1368 | attr->name = _cupsStrAlloc("job-originating-user-name"); | |
b423cd4c | 1369 | } |
ef416fc2 | 1370 | |
b423cd4c | 1371 | if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name", |
1372 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 1373 | { |
b423cd4c | 1374 | /* |
1375 | * Request contains a job-originating-host-name attribute; validate it... | |
1376 | */ | |
ef416fc2 | 1377 | |
b423cd4c | 1378 | if (attr->value_tag != IPP_TAG_NAME || |
1379 | attr->num_values != 1 || | |
1380 | strcmp(con->http.hostname, "localhost")) | |
ef416fc2 | 1381 | { |
1382 | /* | |
b423cd4c | 1383 | * Can't override the value if we aren't connected via localhost. |
1384 | * Also, we can only have 1 value and it must be a name value. | |
ef416fc2 | 1385 | */ |
1386 | ||
b423cd4c | 1387 | switch (attr->value_tag) |
1388 | { | |
1389 | case IPP_TAG_STRING : | |
1390 | case IPP_TAG_TEXTLANG : | |
1391 | case IPP_TAG_NAMELANG : | |
1392 | case IPP_TAG_TEXT : | |
1393 | case IPP_TAG_NAME : | |
1394 | case IPP_TAG_KEYWORD : | |
1395 | case IPP_TAG_URI : | |
1396 | case IPP_TAG_URISCHEME : | |
1397 | case IPP_TAG_CHARSET : | |
1398 | case IPP_TAG_LANGUAGE : | |
1399 | case IPP_TAG_MIMETYPE : | |
1400 | /* | |
1401 | * Free old strings... | |
1402 | */ | |
ef416fc2 | 1403 | |
b423cd4c | 1404 | for (i = 0; i < attr->num_values; i ++) |
1405 | { | |
757d2cad | 1406 | _cupsStrFree(attr->values[i].string.text); |
b423cd4c | 1407 | attr->values[i].string.text = NULL; |
1408 | if (attr->values[i].string.charset) | |
1409 | { | |
757d2cad | 1410 | _cupsStrFree(attr->values[i].string.charset); |
b423cd4c | 1411 | attr->values[i].string.charset = NULL; |
1412 | } | |
1413 | } | |
ef416fc2 | 1414 | |
b423cd4c | 1415 | default : |
1416 | break; | |
1417 | } | |
ef416fc2 | 1418 | |
b423cd4c | 1419 | /* |
1420 | * Use the default connection hostname instead... | |
1421 | */ | |
bd7854cb | 1422 | |
b423cd4c | 1423 | attr->value_tag = IPP_TAG_NAME; |
1424 | attr->num_values = 1; | |
757d2cad | 1425 | attr->values[0].string.text = _cupsStrAlloc(con->http.hostname); |
b423cd4c | 1426 | } |
bd7854cb | 1427 | |
b423cd4c | 1428 | attr->group_tag = IPP_TAG_JOB; |
1429 | } | |
1430 | else | |
1431 | { | |
1432 | /* | |
1433 | * No job-originating-host-name attribute, so use the hostname from | |
1434 | * the connection... | |
1435 | */ | |
bd7854cb | 1436 | |
b423cd4c | 1437 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, |
1438 | "job-originating-host-name", NULL, con->http.hostname); | |
1439 | } | |
bd7854cb | 1440 | |
b423cd4c | 1441 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", |
1442 | time(NULL)); | |
1443 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1444 | "time-at-processing", 0); | |
1445 | attr->value_tag = IPP_TAG_NOVALUE; | |
1446 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1447 | "time-at-completed", 0); | |
1448 | attr->value_tag = IPP_TAG_NOVALUE; | |
bd7854cb | 1449 | |
1450 | /* | |
b423cd4c | 1451 | * Add remaining job attributes... |
bd7854cb | 1452 | */ |
1453 | ||
b423cd4c | 1454 | ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
1455 | job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM, | |
1456 | "job-state", IPP_JOB_STOPPED); | |
1457 | job->state_value = job->state->values[0].integer; | |
1458 | job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1459 | "job-media-sheets-completed", 0); | |
1460 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL, | |
1461 | printer->uri); | |
1462 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, | |
1463 | title); | |
bd7854cb | 1464 | |
b423cd4c | 1465 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
1466 | IPP_TAG_INTEGER)) != NULL) | |
1467 | attr->values[0].integer = 0; | |
1468 | else | |
1469 | attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
1470 | "job-k-octets", 0); | |
bd7854cb | 1471 | |
b423cd4c | 1472 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
1473 | IPP_TAG_KEYWORD)) == NULL) | |
1474 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
1475 | if (!attr) | |
ef416fc2 | 1476 | { |
b423cd4c | 1477 | if ((val = cupsGetOption("job-hold-until", printer->num_options, |
1478 | printer->options)) == NULL) | |
1479 | val = "no-hold"; | |
ef416fc2 | 1480 | |
b423cd4c | 1481 | attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD, |
1482 | "job-hold-until", NULL, val); | |
ef416fc2 | 1483 | } |
b423cd4c | 1484 | if (attr && strcmp(attr->values[0].string.text, "no-hold") && |
1485 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 1486 | { |
1487 | /* | |
b423cd4c | 1488 | * Hold job until specified time... |
ef416fc2 | 1489 | */ |
1490 | ||
b423cd4c | 1491 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
e53920b9 | 1492 | |
1493 | job->state->values[0].integer = IPP_JOB_HELD; | |
1494 | job->state_value = IPP_JOB_HELD; | |
ef416fc2 | 1495 | } |
b423cd4c | 1496 | else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB) |
ef416fc2 | 1497 | { |
e53920b9 | 1498 | job->hold_until = time(NULL) + 60; |
b423cd4c | 1499 | job->state->values[0].integer = IPP_JOB_HELD; |
1500 | job->state_value = IPP_JOB_HELD; | |
1501 | } | |
1502 | else | |
1503 | { | |
1504 | job->state->values[0].integer = IPP_JOB_PENDING; | |
1505 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 1506 | } |
1507 | ||
b423cd4c | 1508 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
1509 | Classification) | |
ef416fc2 | 1510 | { |
1511 | /* | |
b423cd4c | 1512 | * Add job sheets options... |
ef416fc2 | 1513 | */ |
1514 | ||
b423cd4c | 1515 | if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1516 | IPP_TAG_ZERO)) == NULL) | |
ef416fc2 | 1517 | { |
b423cd4c | 1518 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1519 | "Adding default job-sheets values \"%s,%s\"...", | |
1520 | printer->job_sheets[0], printer->job_sheets[1]); | |
ef416fc2 | 1521 | |
b423cd4c | 1522 | attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets", |
1523 | 2, NULL, NULL); | |
757d2cad | 1524 | attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]); |
1525 | attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]); | |
ef416fc2 | 1526 | } |
1527 | ||
b423cd4c | 1528 | job->job_sheets = attr; |
ef416fc2 | 1529 | |
ef416fc2 | 1530 | /* |
b423cd4c | 1531 | * Enforce classification level if set... |
ef416fc2 | 1532 | */ |
1533 | ||
b423cd4c | 1534 | if (Classification) |
ef416fc2 | 1535 | { |
b423cd4c | 1536 | cupsdLogMessage(CUPSD_LOG_INFO, |
1537 | "Classification=\"%s\", ClassifyOverride=%d", | |
1538 | Classification ? Classification : "(null)", | |
1539 | ClassifyOverride); | |
ef416fc2 | 1540 | |
b423cd4c | 1541 | if (ClassifyOverride) |
1542 | { | |
1543 | if (!strcmp(attr->values[0].string.text, "none") && | |
1544 | (attr->num_values == 1 || | |
1545 | !strcmp(attr->values[1].string.text, "none"))) | |
1546 | { | |
1547 | /* | |
1548 | * Force the leading banner to have the classification on it... | |
1549 | */ | |
ef416fc2 | 1550 | |
b423cd4c | 1551 | cupsdSetString(&attr->values[0].string.text, Classification); |
e00b005a | 1552 | |
b423cd4c | 1553 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " |
1554 | "job-sheets=\"%s,none\", " | |
1555 | "job-originating-user-name=\"%s\"", | |
1556 | job->id, Classification, job->username); | |
1557 | } | |
1558 | else if (attr->num_values == 2 && | |
1559 | strcmp(attr->values[0].string.text, | |
1560 | attr->values[1].string.text) && | |
1561 | strcmp(attr->values[0].string.text, "none") && | |
1562 | strcmp(attr->values[1].string.text, "none")) | |
1563 | { | |
1564 | /* | |
1565 | * Can't put two different security markings on the same document! | |
1566 | */ | |
ef416fc2 | 1567 | |
b423cd4c | 1568 | cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text); |
ef416fc2 | 1569 | |
b423cd4c | 1570 | cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED " |
1571 | "job-sheets=\"%s,%s\", " | |
1572 | "job-originating-user-name=\"%s\"", | |
1573 | job->id, attr->values[0].string.text, | |
1574 | attr->values[1].string.text, job->username); | |
1575 | } | |
1576 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1577 | strcmp(attr->values[0].string.text, "none") && | |
1578 | (attr->num_values == 1 || | |
1579 | (strcmp(attr->values[1].string.text, Classification) && | |
1580 | strcmp(attr->values[1].string.text, "none")))) | |
1581 | { | |
1582 | if (attr->num_values == 1) | |
1583 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1584 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1585 | "job-sheets=\"%s\", " | |
1586 | "job-originating-user-name=\"%s\"", | |
1587 | job->id, attr->values[0].string.text, job->username); | |
1588 | else | |
1589 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1590 | "[Job %d] CLASSIFICATION OVERRIDDEN " | |
1591 | "job-sheets=\"%s,%s\",fffff " | |
1592 | "job-originating-user-name=\"%s\"", | |
1593 | job->id, attr->values[0].string.text, | |
1594 | attr->values[1].string.text, job->username); | |
1595 | } | |
1596 | } | |
1597 | else if (strcmp(attr->values[0].string.text, Classification) && | |
1598 | (attr->num_values == 1 || | |
1599 | strcmp(attr->values[1].string.text, Classification))) | |
ef416fc2 | 1600 | { |
1601 | /* | |
b423cd4c | 1602 | * Force the banner to have the classification on it... |
ef416fc2 | 1603 | */ |
1604 | ||
b423cd4c | 1605 | if (attr->num_values > 1 && |
1606 | !strcmp(attr->values[0].string.text, attr->values[1].string.text)) | |
1607 | { | |
1608 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
1609 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1610 | } | |
1611 | else | |
1612 | { | |
1613 | if (attr->num_values == 1 || | |
1614 | strcmp(attr->values[0].string.text, "none")) | |
1615 | cupsdSetString(&(attr->values[0].string.text), Classification); | |
ef416fc2 | 1616 | |
b423cd4c | 1617 | if (attr->num_values > 1 && |
1618 | strcmp(attr->values[1].string.text, "none")) | |
1619 | cupsdSetString(&(attr->values[1].string.text), Classification); | |
1620 | } | |
ef416fc2 | 1621 | |
b423cd4c | 1622 | if (attr->num_values > 1) |
1623 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1624 | "[Job %d] CLASSIFICATION FORCED " | |
1625 | "job-sheets=\"%s,%s\", " | |
1626 | "job-originating-user-name=\"%s\"", | |
1627 | job->id, attr->values[0].string.text, | |
1628 | attr->values[1].string.text, job->username); | |
1629 | else | |
1630 | cupsdLogMessage(CUPSD_LOG_NOTICE, | |
1631 | "[Job %d] CLASSIFICATION FORCED " | |
1632 | "job-sheets=\"%s\", " | |
1633 | "job-originating-user-name=\"%s\"", | |
1634 | job->id, Classification, job->username); | |
ef416fc2 | 1635 | } |
1636 | } | |
1637 | ||
b423cd4c | 1638 | /* |
1639 | * See if we need to add the starting sheet... | |
1640 | */ | |
ef416fc2 | 1641 | |
b423cd4c | 1642 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
ef416fc2 | 1643 | { |
b423cd4c | 1644 | cupsdLogMessage(CUPSD_LOG_INFO, |
1645 | "Adding start banner page \"%s\" to job %d.", | |
1646 | attr->values[0].string.text, job->id); | |
ef416fc2 | 1647 | |
b423cd4c | 1648 | kbytes = copy_banner(con, job, attr->values[0].string.text); |
ef416fc2 | 1649 | |
b423cd4c | 1650 | cupsdUpdateQuota(printer, job->username, 0, kbytes); |
1651 | } | |
ef416fc2 | 1652 | } |
b423cd4c | 1653 | else if ((attr = ippFindAttribute(job->attrs, "job-sheets", |
1654 | IPP_TAG_ZERO)) != NULL) | |
1655 | job->sheets = attr; | |
ef416fc2 | 1656 | |
b423cd4c | 1657 | /* |
1658 | * Fill in the response info... | |
1659 | */ | |
ef416fc2 | 1660 | |
b423cd4c | 1661 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, |
1662 | LocalPort, job->id); | |
ef416fc2 | 1663 | |
b423cd4c | 1664 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, |
1665 | job_uri); | |
ef416fc2 | 1666 | |
b423cd4c | 1667 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id); |
ef416fc2 | 1668 | |
b423cd4c | 1669 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", |
1670 | job->state_value); | |
1671 | add_job_state_reasons(con, job); | |
ef416fc2 | 1672 | |
b423cd4c | 1673 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 1674 | |
b423cd4c | 1675 | /* |
1676 | * Add any job subscriptions... | |
1677 | */ | |
ef416fc2 | 1678 | |
b423cd4c | 1679 | add_job_subscriptions(con, job); |
ef416fc2 | 1680 | |
b423cd4c | 1681 | /* |
1682 | * Set all but the first two attributes to the job attributes group... | |
1683 | */ | |
ef416fc2 | 1684 | |
b423cd4c | 1685 | for (attr = job->attrs->attrs->next->next; attr; attr = attr->next) |
1686 | attr->group_tag = IPP_TAG_JOB; | |
ef416fc2 | 1687 | |
1688 | /* | |
b423cd4c | 1689 | * Fire the "job created" event... |
ef416fc2 | 1690 | */ |
1691 | ||
b423cd4c | 1692 | cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created."); |
ef416fc2 | 1693 | |
1694 | /* | |
b423cd4c | 1695 | * Return the new job... |
ef416fc2 | 1696 | */ |
1697 | ||
b423cd4c | 1698 | return (job); |
1699 | } | |
ef416fc2 | 1700 | |
ef416fc2 | 1701 | |
b423cd4c | 1702 | /* |
1703 | * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based | |
1704 | * upon the job and printer state... | |
1705 | */ | |
ef416fc2 | 1706 | |
b423cd4c | 1707 | static void |
1708 | add_job_state_reasons( | |
1709 | cupsd_client_t *con, /* I - Client connection */ | |
1710 | cupsd_job_t *job) /* I - Job info */ | |
1711 | { | |
1712 | cupsd_printer_t *dest; /* Destination printer */ | |
ef416fc2 | 1713 | |
ef416fc2 | 1714 | |
b423cd4c | 1715 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)", |
1716 | con, con->http.fd, job ? job->id : 0); | |
ef416fc2 | 1717 | |
b423cd4c | 1718 | switch (job ? job->state_value : IPP_JOB_CANCELLED) |
ef416fc2 | 1719 | { |
b423cd4c | 1720 | case IPP_JOB_PENDING : |
80ca4592 | 1721 | dest = cupsdFindDest(job->dest); |
bd7854cb | 1722 | |
b423cd4c | 1723 | if (dest && dest->state == IPP_PRINTER_STOPPED) |
1724 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1725 | "job-state-reasons", NULL, "printer-stopped"); | |
1726 | else | |
1727 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1728 | "job-state-reasons", NULL, "none"); | |
1729 | break; | |
ef416fc2 | 1730 | |
b423cd4c | 1731 | case IPP_JOB_HELD : |
1732 | if (ippFindAttribute(job->attrs, "job-hold-until", | |
1733 | IPP_TAG_KEYWORD) != NULL || | |
1734 | ippFindAttribute(job->attrs, "job-hold-until", | |
1735 | IPP_TAG_NAME) != NULL) | |
1736 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1737 | "job-state-reasons", NULL, "job-hold-until-specified"); | |
1738 | else | |
1739 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1740 | "job-state-reasons", NULL, "job-incoming"); | |
1741 | break; | |
ef416fc2 | 1742 | |
b423cd4c | 1743 | case IPP_JOB_PROCESSING : |
1744 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1745 | "job-state-reasons", NULL, "job-printing"); | |
1746 | break; | |
ef416fc2 | 1747 | |
b423cd4c | 1748 | case IPP_JOB_STOPPED : |
1749 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1750 | "job-state-reasons", NULL, "job-stopped"); | |
1751 | break; | |
ef416fc2 | 1752 | |
b423cd4c | 1753 | case IPP_JOB_CANCELLED : |
1754 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1755 | "job-state-reasons", NULL, "job-canceled-by-user"); | |
1756 | break; | |
ef416fc2 | 1757 | |
b423cd4c | 1758 | case IPP_JOB_ABORTED : |
1759 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1760 | "job-state-reasons", NULL, "aborted-by-system"); | |
1761 | break; | |
ef416fc2 | 1762 | |
b423cd4c | 1763 | case IPP_JOB_COMPLETED : |
1764 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, | |
1765 | "job-state-reasons", NULL, "job-completed-successfully"); | |
1766 | break; | |
ef416fc2 | 1767 | } |
ef416fc2 | 1768 | } |
1769 | ||
1770 | ||
1771 | /* | |
b423cd4c | 1772 | * 'add_job_subscriptions()' - Add any subcriptions for a job. |
ef416fc2 | 1773 | */ |
1774 | ||
1775 | static void | |
b423cd4c | 1776 | add_job_subscriptions( |
1777 | cupsd_client_t *con, /* I - Client connection */ | |
1778 | cupsd_job_t *job) /* I - Newly created job */ | |
ef416fc2 | 1779 | { |
b423cd4c | 1780 | int i; /* Looping var */ |
1781 | ipp_attribute_t *prev, /* Previous attribute */ | |
1782 | *next, /* Next attribute */ | |
1783 | *attr; /* Current attribute */ | |
1784 | cupsd_subscription_t *sub; /* Subscription object */ | |
1785 | const char *recipient, /* notify-recipient-uri */ | |
1786 | *pullmethod; /* notify-pull-method */ | |
1787 | ipp_attribute_t *user_data; /* notify-user-data */ | |
1788 | int interval; /* notify-time-interval */ | |
1789 | unsigned mask; /* notify-events */ | |
ef416fc2 | 1790 | |
ef416fc2 | 1791 | |
b423cd4c | 1792 | /* |
1793 | * Find the first subscription group attribute; return if we have | |
1794 | * none... | |
1795 | */ | |
ef416fc2 | 1796 | |
b423cd4c | 1797 | for (attr = job->attrs->attrs, prev = NULL; |
1798 | attr; | |
1799 | prev = attr, attr = attr->next) | |
1800 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
1801 | break; | |
ef416fc2 | 1802 | |
b423cd4c | 1803 | if (!attr) |
1804 | return; | |
ef416fc2 | 1805 | |
b423cd4c | 1806 | /* |
1807 | * Process the subscription attributes in the request... | |
1808 | */ | |
ef416fc2 | 1809 | |
b423cd4c | 1810 | while (attr) |
1811 | { | |
1812 | recipient = NULL; | |
1813 | pullmethod = NULL; | |
1814 | user_data = NULL; | |
1815 | interval = 0; | |
1816 | mask = CUPSD_EVENT_NONE; | |
ef416fc2 | 1817 | |
b423cd4c | 1818 | while (attr && attr->group_tag != IPP_TAG_ZERO) |
1819 | { | |
1820 | if (!strcmp(attr->name, "notify-recipient") && | |
1821 | attr->value_tag == IPP_TAG_URI) | |
1822 | recipient = attr->values[0].string.text; | |
1823 | else if (!strcmp(attr->name, "notify-pull-method") && | |
1824 | attr->value_tag == IPP_TAG_KEYWORD) | |
1825 | pullmethod = attr->values[0].string.text; | |
1826 | else if (!strcmp(attr->name, "notify-charset") && | |
1827 | attr->value_tag == IPP_TAG_CHARSET && | |
1828 | strcmp(attr->values[0].string.text, "us-ascii") && | |
1829 | strcmp(attr->values[0].string.text, "utf-8")) | |
1830 | { | |
1831 | send_ipp_status(con, IPP_CHARSET, | |
1832 | _("Character set \"%s\" not supported!"), | |
1833 | attr->values[0].string.text); | |
1834 | return; | |
1835 | } | |
1836 | else if (!strcmp(attr->name, "notify-natural-language") && | |
1837 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
1838 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
1839 | { | |
1840 | send_ipp_status(con, IPP_CHARSET, | |
1841 | _("Language \"%s\" not supported!"), | |
1842 | attr->values[0].string.text); | |
1843 | return; | |
1844 | } | |
1845 | else if (!strcmp(attr->name, "notify-user-data") && | |
1846 | attr->value_tag == IPP_TAG_STRING) | |
1847 | { | |
1848 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
1849 | { | |
1850 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
1851 | _("The notify-user-data value is too large " | |
1852 | "(%d > 63 octets)!"), | |
1853 | attr->values[0].unknown.length); | |
1854 | return; | |
1855 | } | |
ef416fc2 | 1856 | |
b423cd4c | 1857 | user_data = attr; |
1858 | } | |
1859 | else if (!strcmp(attr->name, "notify-events") && | |
1860 | attr->value_tag == IPP_TAG_KEYWORD) | |
1861 | { | |
1862 | for (i = 0; i < attr->num_values; i ++) | |
1863 | mask |= cupsdEventValue(attr->values[i].string.text); | |
1864 | } | |
1865 | else if (!strcmp(attr->name, "notify-lease-duration")) | |
1866 | { | |
1867 | send_ipp_status(con, IPP_BAD_REQUEST, | |
1868 | _("The notify-lease-duration attribute cannot be " | |
1869 | "used with job subscriptions.")); | |
1870 | return; | |
1871 | } | |
1872 | else if (!strcmp(attr->name, "notify-time-interval") && | |
1873 | attr->value_tag == IPP_TAG_INTEGER) | |
1874 | interval = attr->values[0].integer; | |
ef416fc2 | 1875 | |
b423cd4c | 1876 | attr = attr->next; |
1877 | } | |
ef416fc2 | 1878 | |
b423cd4c | 1879 | if (!recipient && !pullmethod) |
1880 | break; | |
ef416fc2 | 1881 | |
b423cd4c | 1882 | if (mask == CUPSD_EVENT_NONE) |
1883 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
ef416fc2 | 1884 | |
b423cd4c | 1885 | sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient, |
1886 | 0); | |
ef416fc2 | 1887 | |
b423cd4c | 1888 | sub->interval = interval; |
ef416fc2 | 1889 | |
b423cd4c | 1890 | cupsdSetString(&sub->owner, job->username); |
ef416fc2 | 1891 | |
b423cd4c | 1892 | if (user_data) |
ef416fc2 | 1893 | { |
b423cd4c | 1894 | sub->user_data_len = user_data->values[0].unknown.length; |
1895 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
1896 | sub->user_data_len); | |
ef416fc2 | 1897 | } |
1898 | ||
b423cd4c | 1899 | ippAddSeparator(con->response); |
1900 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
1901 | "notify-subscription-id", sub->id); | |
ef416fc2 | 1902 | |
b423cd4c | 1903 | if (attr) |
1904 | attr = attr->next; | |
ef416fc2 | 1905 | } |
1906 | ||
b423cd4c | 1907 | cupsdSaveAllSubscriptions(); |
1908 | ||
ef416fc2 | 1909 | /* |
b423cd4c | 1910 | * Remove all of the subscription attributes from the job request... |
ef416fc2 | 1911 | */ |
1912 | ||
b423cd4c | 1913 | for (attr = job->attrs->attrs, prev = NULL; attr; attr = next) |
ef416fc2 | 1914 | { |
b423cd4c | 1915 | next = attr->next; |
ef416fc2 | 1916 | |
b423cd4c | 1917 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION || |
1918 | attr->group_tag == IPP_TAG_ZERO) | |
1919 | { | |
1920 | /* | |
1921 | * Free and remove this attribute... | |
1922 | */ | |
ef416fc2 | 1923 | |
757d2cad | 1924 | _ippFreeAttr(attr); |
ef416fc2 | 1925 | |
b423cd4c | 1926 | if (prev) |
1927 | prev->next = next; | |
1928 | else | |
1929 | job->attrs->attrs = next; | |
1930 | } | |
1931 | else | |
1932 | prev = attr; | |
ef416fc2 | 1933 | } |
1934 | ||
b423cd4c | 1935 | job->attrs->last = prev; |
1936 | job->attrs->current = prev; | |
1937 | } | |
ef416fc2 | 1938 | |
ef416fc2 | 1939 | |
b423cd4c | 1940 | /* |
1941 | * 'add_job_uuid()' - Add job-uuid attribute to a job. | |
1942 | * | |
1943 | * See RFC 4122 for the definition of UUIDs and the format. | |
1944 | */ | |
1945 | ||
1946 | static void | |
1947 | add_job_uuid(cupsd_client_t *con, /* I - Client connection */ | |
1948 | cupsd_job_t *job) /* I - Job */ | |
1949 | { | |
1950 | char uuid[1024]; /* job-uuid string */ | |
1951 | ipp_attribute_t *attr; /* job-uuid attribute */ | |
1952 | _cups_md5_state_t md5state; /* MD5 state */ | |
1953 | unsigned char md5sum[16]; /* MD5 digest/sum */ | |
ef416fc2 | 1954 | |
ef416fc2 | 1955 | |
1956 | /* | |
b423cd4c | 1957 | * First see if the job already has a job-uuid attribute; if so, return... |
ef416fc2 | 1958 | */ |
1959 | ||
b423cd4c | 1960 | if ((attr = ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI)) != NULL) |
1961 | return; | |
ef416fc2 | 1962 | |
1963 | /* | |
b423cd4c | 1964 | * No job-uuid attribute, so build a version 3 UUID with the local job |
1965 | * ID at the end; see RFC 4122 for details. Start with the MD5 sum of | |
1966 | * the ServerName, server name and port that the client connected to, | |
1967 | * and local job ID... | |
ef416fc2 | 1968 | */ |
1969 | ||
b423cd4c | 1970 | snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername, |
1971 | con->serverport, job->id); | |
ef416fc2 | 1972 | |
757d2cad | 1973 | _cupsMD5Init(&md5state); |
1974 | _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid)); | |
1975 | _cupsMD5Finish(&md5state, md5sum); | |
ef416fc2 | 1976 | |
1977 | /* | |
b423cd4c | 1978 | * Format the UUID URI using the MD5 sum and job ID. |
ef416fc2 | 1979 | */ |
1980 | ||
b423cd4c | 1981 | snprintf(uuid, sizeof(uuid), |
1982 | "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" | |
1983 | "%02x%02x%02x%02x%02x%02x", | |
1984 | md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], | |
1985 | (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, | |
1986 | md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], | |
1987 | md5sum[14], md5sum[15]); | |
ef416fc2 | 1988 | |
b423cd4c | 1989 | ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid); |
ef416fc2 | 1990 | } |
1991 | ||
1992 | ||
1993 | /* | |
b423cd4c | 1994 | * 'add_printer()' - Add a printer to the system. |
ef416fc2 | 1995 | */ |
1996 | ||
1997 | static void | |
b423cd4c | 1998 | add_printer(cupsd_client_t *con, /* I - Client connection */ |
1999 | ipp_attribute_t *uri) /* I - URI of printer */ | |
ef416fc2 | 2000 | { |
2001 | http_status_t status; /* Policy status */ | |
b423cd4c | 2002 | int i; /* Looping var */ |
ef416fc2 | 2003 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
b423cd4c | 2004 | username[HTTP_MAX_URI], /* Username portion of URI */ |
ef416fc2 | 2005 | host[HTTP_MAX_URI], /* Host portion of URI */ |
2006 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2007 | int port; /* Port portion of URI */ | |
b423cd4c | 2008 | cupsd_printer_t *printer; /* Printer/class */ |
2009 | ipp_attribute_t *attr; /* Printer attribute */ | |
2010 | cups_file_t *fp; /* Script/PPD file */ | |
2011 | char line[1024]; /* Line from file... */ | |
2012 | char srcfile[1024], /* Source Script/PPD file */ | |
2013 | dstfile[1024]; /* Destination Script/PPD file */ | |
2014 | int modify; /* Non-zero if we are modifying */ | |
2015 | char newname[IPP_MAX_NAME]; /* New printer name */ | |
2016 | int need_restart_job; /* Need to restart job? */ | |
ef416fc2 | 2017 | |
2018 | ||
b423cd4c | 2019 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con, |
ef416fc2 | 2020 | con->http.fd, uri->values[0].string.text); |
2021 | ||
2022 | /* | |
b423cd4c | 2023 | * Do we have a valid URI? |
ef416fc2 | 2024 | */ |
2025 | ||
b423cd4c | 2026 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2027 | sizeof(method), username, sizeof(username), host, | |
2028 | sizeof(host), &port, resource, sizeof(resource)); | |
2029 | ||
2030 | if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10) | |
ef416fc2 | 2031 | { |
b423cd4c | 2032 | /* |
2033 | * No, return an error... | |
2034 | */ | |
2035 | ||
ef416fc2 | 2036 | send_ipp_status(con, IPP_BAD_REQUEST, |
b423cd4c | 2037 | _("The printer-uri must be of the form " |
2038 | "\"ipp://HOSTNAME/printers/PRINTERNAME\".")); | |
ef416fc2 | 2039 | return; |
2040 | } | |
2041 | ||
2042 | /* | |
b423cd4c | 2043 | * Do we have a valid printer name? |
ef416fc2 | 2044 | */ |
2045 | ||
b423cd4c | 2046 | if (!validate_name(resource + 10)) |
ef416fc2 | 2047 | { |
b423cd4c | 2048 | /* |
2049 | * No, return an error... | |
2050 | */ | |
2051 | ||
2052 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2053 | _("The printer-uri \"%s\" contains invalid characters."), | |
2054 | uri->values[0].string.text); | |
2055 | return; | |
ef416fc2 | 2056 | } |
ef416fc2 | 2057 | |
2058 | /* | |
b423cd4c | 2059 | * Check policy... |
ef416fc2 | 2060 | */ |
2061 | ||
b423cd4c | 2062 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
2063 | { | |
2064 | send_http_error(con, status); | |
2065 | return; | |
2066 | } | |
ef416fc2 | 2067 | |
2068 | /* | |
b423cd4c | 2069 | * See if the printer already exists; if not, create a new printer... |
2070 | */ | |
ef416fc2 | 2071 | |
b423cd4c | 2072 | if ((printer = cupsdFindPrinter(resource + 10)) == NULL) |
ef416fc2 | 2073 | { |
2074 | /* | |
b423cd4c | 2075 | * Printer doesn't exist; see if we have a class of the same name... |
ef416fc2 | 2076 | */ |
2077 | ||
b423cd4c | 2078 | if ((printer = cupsdFindClass(resource + 10)) != NULL && |
2079 | !(printer->type & CUPS_PRINTER_REMOTE)) | |
ef416fc2 | 2080 | { |
b423cd4c | 2081 | /* |
2082 | * Yes, return an error... | |
2083 | */ | |
ef416fc2 | 2084 | |
b423cd4c | 2085 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2086 | _("A class named \"%s\" already exists!"), | |
2087 | resource + 10); | |
ef416fc2 | 2088 | return; |
2089 | } | |
2090 | ||
2091 | /* | |
b423cd4c | 2092 | * No, add the printer... |
ef416fc2 | 2093 | */ |
2094 | ||
b423cd4c | 2095 | printer = cupsdAddPrinter(resource + 10); |
2096 | modify = 0; | |
ef416fc2 | 2097 | } |
b423cd4c | 2098 | else if (printer->type & CUPS_PRINTER_IMPLICIT) |
ef416fc2 | 2099 | { |
2100 | /* | |
b423cd4c | 2101 | * Rename the implicit printer to "AnyPrinter" or delete it... |
ef416fc2 | 2102 | */ |
2103 | ||
b423cd4c | 2104 | if (ImplicitAnyClasses) |
ef416fc2 | 2105 | { |
b423cd4c | 2106 | snprintf(newname, sizeof(newname), "Any%s", resource + 10); |
2107 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2108 | } |
b423cd4c | 2109 | else |
2110 | cupsdDeletePrinter(printer, 1); | |
ef416fc2 | 2111 | |
2112 | /* | |
b423cd4c | 2113 | * Add the printer as a new local printer... |
ef416fc2 | 2114 | */ |
2115 | ||
b423cd4c | 2116 | printer = cupsdAddPrinter(resource + 10); |
2117 | modify = 0; | |
ef416fc2 | 2118 | } |
b423cd4c | 2119 | else if (printer->type & CUPS_PRINTER_REMOTE) |
2120 | { | |
2121 | /* | |
2122 | * Rename the remote printer to "Printer@server"... | |
2123 | */ | |
ef416fc2 | 2124 | |
b423cd4c | 2125 | snprintf(newname, sizeof(newname), "%s@%s", resource + 10, |
2126 | printer->hostname); | |
2127 | cupsdRenamePrinter(printer, newname); | |
ef416fc2 | 2128 | |
b423cd4c | 2129 | /* |
2130 | * Add the printer as a new local printer... | |
2131 | */ | |
ef416fc2 | 2132 | |
b423cd4c | 2133 | printer = cupsdAddPrinter(resource + 10); |
2134 | modify = 0; | |
2135 | } | |
2136 | else | |
2137 | modify = 1; | |
ef416fc2 | 2138 | |
b423cd4c | 2139 | /* |
2140 | * Look for attributes and copy them over as needed... | |
2141 | */ | |
ef416fc2 | 2142 | |
b423cd4c | 2143 | need_restart_job = 0; |
ef416fc2 | 2144 | |
b423cd4c | 2145 | if ((attr = ippFindAttribute(con->request, "printer-location", |
2146 | IPP_TAG_TEXT)) != NULL) | |
2147 | cupsdSetString(&printer->location, attr->values[0].string.text); | |
ef416fc2 | 2148 | |
b423cd4c | 2149 | if ((attr = ippFindAttribute(con->request, "printer-info", |
2150 | IPP_TAG_TEXT)) != NULL) | |
2151 | cupsdSetString(&printer->info, attr->values[0].string.text); | |
ef416fc2 | 2152 | |
b423cd4c | 2153 | if ((attr = ippFindAttribute(con->request, "device-uri", |
2154 | IPP_TAG_URI)) != NULL) | |
ef416fc2 | 2155 | { |
2156 | /* | |
b423cd4c | 2157 | * Do we have a valid device URI? |
ef416fc2 | 2158 | */ |
2159 | ||
b423cd4c | 2160 | need_restart_job = 1; |
ef416fc2 | 2161 | |
b423cd4c | 2162 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, method, |
2163 | sizeof(method), username, sizeof(username), host, | |
2164 | sizeof(host), &port, resource, sizeof(resource)); | |
2165 | ||
2166 | if (!strcmp(method, "file")) | |
ef416fc2 | 2167 | { |
2168 | /* | |
b423cd4c | 2169 | * See if the administrator has enabled file devices... |
ef416fc2 | 2170 | */ |
2171 | ||
b423cd4c | 2172 | if (!FileDevice && strcmp(resource, "/dev/null")) |
ef416fc2 | 2173 | { |
2174 | /* | |
b423cd4c | 2175 | * File devices are disabled and the URL is not file:/dev/null... |
ef416fc2 | 2176 | */ |
2177 | ||
b423cd4c | 2178 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2179 | _("File device URIs have been disabled! " | |
2180 | "To enable, see the FileDevice directive in " | |
2181 | "\"%s/cupsd.conf\"."), | |
2182 | ServerRoot); | |
ef416fc2 | 2183 | return; |
2184 | } | |
b423cd4c | 2185 | } |
2186 | else | |
2187 | { | |
ef416fc2 | 2188 | /* |
b423cd4c | 2189 | * See if the backend exists and is executable... |
ef416fc2 | 2190 | */ |
2191 | ||
b423cd4c | 2192 | snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, method); |
2193 | if (access(srcfile, X_OK)) | |
ef416fc2 | 2194 | { |
2195 | /* | |
b423cd4c | 2196 | * Could not find device in list! |
ef416fc2 | 2197 | */ |
ef416fc2 | 2198 | |
b423cd4c | 2199 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"), |
2200 | attr->values[0].string.text); | |
2201 | return; | |
ef416fc2 | 2202 | } |
2203 | } | |
ef416fc2 | 2204 | |
b423cd4c | 2205 | cupsdLogMessage(CUPSD_LOG_INFO, |
2206 | "Setting %s device-uri to \"%s\" (was \"%s\".)", | |
2207 | printer->name, | |
2208 | cupsdSanitizeURI(attr->values[0].string.text, line, | |
2209 | sizeof(line)), | |
2210 | cupsdSanitizeURI(printer->device_uri, resource, | |
2211 | sizeof(resource))); | |
ef416fc2 | 2212 | |
b423cd4c | 2213 | cupsdSetString(&printer->device_uri, attr->values[0].string.text); |
ef416fc2 | 2214 | } |
2215 | ||
b423cd4c | 2216 | if ((attr = ippFindAttribute(con->request, "port-monitor", |
2217 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 2218 | { |
b423cd4c | 2219 | ipp_attribute_t *supported; /* port-monitor-supported attribute */ |
ef416fc2 | 2220 | |
ef416fc2 | 2221 | |
b423cd4c | 2222 | need_restart_job = 1; |
ef416fc2 | 2223 | |
b423cd4c | 2224 | supported = ippFindAttribute(printer->attrs, "port-monitor-supported", |
2225 | IPP_TAG_KEYWORD); | |
2226 | for (i = 0; i < supported->num_values; i ++) | |
2227 | if (!strcmp(supported->values[i].string.text, | |
2228 | attr->values[0].string.text)) | |
2229 | break; | |
ef416fc2 | 2230 | |
b423cd4c | 2231 | if (i >= supported->num_values) |
2232 | { | |
2233 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"), | |
2234 | attr->values[0].string.text); | |
2235 | return; | |
2236 | } | |
ef416fc2 | 2237 | |
b423cd4c | 2238 | cupsdLogMessage(CUPSD_LOG_INFO, |
2239 | "Setting %s port-monitor to \"%s\" (was \"%s\".)", | |
2240 | printer->name, attr->values[0].string.text, | |
2241 | printer->port_monitor); | |
ef416fc2 | 2242 | |
b423cd4c | 2243 | if (strcmp(attr->values[0].string.text, "none")) |
2244 | cupsdSetString(&printer->port_monitor, attr->values[0].string.text); | |
2245 | else | |
2246 | cupsdClearString(&printer->port_monitor); | |
2247 | } | |
ef416fc2 | 2248 | |
b423cd4c | 2249 | if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", |
2250 | IPP_TAG_BOOLEAN)) != NULL) | |
2251 | { | |
2252 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2253 | "Setting %s printer-is-accepting-jobs to %d (was %d.)", | |
2254 | printer->name, attr->values[0].boolean, printer->accepting); | |
ef416fc2 | 2255 | |
b423cd4c | 2256 | printer->accepting = attr->values[0].boolean; |
2257 | cupsdAddPrinterHistory(printer); | |
2258 | } | |
bd7854cb | 2259 | |
b423cd4c | 2260 | if ((attr = ippFindAttribute(con->request, "printer-is-shared", |
2261 | IPP_TAG_BOOLEAN)) != NULL) | |
2262 | { | |
2263 | if (printer->shared && !attr->values[0].boolean) | |
2264 | cupsdSendBrowseDelete(printer); | |
bd7854cb | 2265 | |
b423cd4c | 2266 | cupsdLogMessage(CUPSD_LOG_INFO, |
2267 | "Setting %s printer-is-shared to %d (was %d.)", | |
2268 | printer->name, attr->values[0].boolean, printer->shared); | |
bd7854cb | 2269 | |
b423cd4c | 2270 | printer->shared = attr->values[0].boolean; |
2271 | } | |
bd7854cb | 2272 | |
b423cd4c | 2273 | if ((attr = ippFindAttribute(con->request, "printer-state", |
2274 | IPP_TAG_ENUM)) != NULL) | |
bd7854cb | 2275 | { |
b423cd4c | 2276 | if (attr->values[0].integer != IPP_PRINTER_IDLE && |
2277 | attr->values[0].integer != IPP_PRINTER_STOPPED) | |
2278 | { | |
2279 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"), | |
2280 | attr->values[0].integer); | |
2281 | return; | |
2282 | } | |
bd7854cb | 2283 | |
b423cd4c | 2284 | cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", printer->name, |
2285 | attr->values[0].integer, printer->state); | |
2286 | ||
2287 | if (attr->values[0].integer == IPP_PRINTER_STOPPED) | |
2288 | cupsdStopPrinter(printer, 0); | |
2289 | else | |
2290 | { | |
2291 | need_restart_job = 1; | |
2292 | cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0); | |
2293 | } | |
2294 | } | |
2295 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
2296 | IPP_TAG_TEXT)) != NULL) | |
2297 | { | |
2298 | strlcpy(printer->state_message, attr->values[0].string.text, | |
2299 | sizeof(printer->state_message)); | |
2300 | cupsdAddPrinterHistory(printer); | |
bd7854cb | 2301 | } |
2302 | ||
b423cd4c | 2303 | set_printer_defaults(con, printer); |
2304 | ||
bd7854cb | 2305 | /* |
b423cd4c | 2306 | * See if we have all required attributes... |
bd7854cb | 2307 | */ |
2308 | ||
b423cd4c | 2309 | if (!printer->device_uri) |
2310 | cupsdSetString(&printer->device_uri, "file:///dev/null"); | |
bd7854cb | 2311 | |
2312 | /* | |
b423cd4c | 2313 | * See if we have an interface script or PPD file attached to the request... |
bd7854cb | 2314 | */ |
2315 | ||
b423cd4c | 2316 | if (con->filename) |
2317 | { | |
2318 | need_restart_job = 1; | |
bd7854cb | 2319 | |
b423cd4c | 2320 | strlcpy(srcfile, con->filename, sizeof(srcfile)); |
ef416fc2 | 2321 | |
b423cd4c | 2322 | if ((fp = cupsFileOpen(srcfile, "rb"))) |
2323 | { | |
2324 | /* | |
2325 | * Yes; get the first line from it... | |
2326 | */ | |
ef416fc2 | 2327 | |
b423cd4c | 2328 | line[0] = '\0'; |
2329 | cupsFileGets(fp, line, sizeof(line)); | |
2330 | cupsFileClose(fp); | |
ef416fc2 | 2331 | |
b423cd4c | 2332 | /* |
2333 | * Then see what kind of file it is... | |
2334 | */ | |
ef416fc2 | 2335 | |
b423cd4c | 2336 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2337 | printer->name); | |
ef416fc2 | 2338 | |
b423cd4c | 2339 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2340 | { | |
2341 | /* | |
2342 | * The new file is a PPD file, so remove any old interface script | |
2343 | * that might be lying around... | |
2344 | */ | |
ef416fc2 | 2345 | |
b423cd4c | 2346 | unlink(dstfile); |
2347 | } | |
2348 | else | |
2349 | { | |
2350 | /* | |
2351 | * This must be an interface script, so move the file over to the | |
2352 | * interfaces directory and make it executable... | |
2353 | */ | |
ef416fc2 | 2354 | |
b423cd4c | 2355 | if (copy_file(srcfile, dstfile)) |
2356 | { | |
2357 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2358 | _("Unable to copy interface script - %s!"), | |
2359 | strerror(errno)); | |
2360 | return; | |
2361 | } | |
2362 | else | |
2363 | { | |
2364 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2365 | "Copied interface script successfully!"); | |
2366 | chmod(dstfile, 0755); | |
2367 | } | |
2368 | } | |
ef416fc2 | 2369 | |
b423cd4c | 2370 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2371 | printer->name); | |
ef416fc2 | 2372 | |
b423cd4c | 2373 | if (!strncmp(line, "*PPD-Adobe", 10)) |
2374 | { | |
2375 | /* | |
2376 | * The new file is a PPD file, so move the file over to the | |
2377 | * ppd directory and make it readable by all... | |
2378 | */ | |
ef416fc2 | 2379 | |
b423cd4c | 2380 | if (copy_file(srcfile, dstfile)) |
2381 | { | |
2382 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
2383 | _("Unable to copy PPD file - %s!"), | |
2384 | strerror(errno)); | |
2385 | return; | |
2386 | } | |
2387 | else | |
2388 | { | |
2389 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2390 | "Copied PPD file successfully!"); | |
2391 | chmod(dstfile, 0644); | |
2392 | } | |
2393 | } | |
2394 | else | |
2395 | { | |
2396 | /* | |
2397 | * This must be an interface script, so remove any old PPD file that | |
2398 | * may be lying around... | |
2399 | */ | |
ef416fc2 | 2400 | |
b423cd4c | 2401 | unlink(dstfile); |
2402 | } | |
2403 | } | |
2404 | } | |
2405 | else if ((attr = ippFindAttribute(con->request, "ppd-name", | |
2406 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 2407 | { |
b423cd4c | 2408 | need_restart_job = 1; |
ef416fc2 | 2409 | |
b423cd4c | 2410 | if (!strcmp(attr->values[0].string.text, "raw")) |
ef416fc2 | 2411 | { |
b423cd4c | 2412 | /* |
2413 | * Raw driver, remove any existing PPD or interface script files. | |
2414 | */ | |
ef416fc2 | 2415 | |
b423cd4c | 2416 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, |
2417 | printer->name); | |
2418 | unlink(dstfile); | |
ef416fc2 | 2419 | |
b423cd4c | 2420 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, |
2421 | printer->name); | |
2422 | unlink(dstfile); | |
2423 | } | |
2424 | else | |
ef416fc2 | 2425 | { |
b423cd4c | 2426 | /* |
2427 | * PPD model file... | |
2428 | */ | |
2429 | ||
2430 | snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot, | |
2431 | printer->name); | |
2432 | unlink(dstfile); | |
2433 | ||
2434 | snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, | |
2435 | printer->name); | |
2436 | ||
2437 | if (copy_model(con, attr->values[0].string.text, dstfile)) | |
2438 | { | |
2439 | send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!")); | |
2440 | return; | |
2441 | } | |
2442 | else | |
2443 | { | |
2444 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
2445 | "Copied PPD file successfully!"); | |
2446 | chmod(dstfile, 0644); | |
2447 | } | |
ef416fc2 | 2448 | } |
2449 | } | |
2450 | ||
2451 | /* | |
b423cd4c | 2452 | * Update the printer attributes and return... |
ef416fc2 | 2453 | */ |
2454 | ||
b423cd4c | 2455 | cupsdSetPrinterAttrs(printer); |
2456 | cupsdSaveAllPrinters(); | |
ef416fc2 | 2457 | |
b423cd4c | 2458 | if (need_restart_job && printer->job) |
ef416fc2 | 2459 | { |
b423cd4c | 2460 | cupsd_job_t *job; |
ef416fc2 | 2461 | |
b423cd4c | 2462 | /* |
2463 | * Stop the current job and then restart it below... | |
2464 | */ | |
ef416fc2 | 2465 | |
b423cd4c | 2466 | job = (cupsd_job_t *)printer->job; |
ef416fc2 | 2467 | |
b423cd4c | 2468 | cupsdStopJob(job, 1); |
2469 | ||
2470 | job->state->values[0].integer = IPP_JOB_PENDING; | |
2471 | job->state_value = IPP_JOB_PENDING; | |
ef416fc2 | 2472 | } |
2473 | ||
b423cd4c | 2474 | if (need_restart_job) |
2475 | cupsdCheckJobs(); | |
ef416fc2 | 2476 | |
b423cd4c | 2477 | cupsdWritePrintcap(); |
2478 | ||
2479 | if (modify) | |
ef416fc2 | 2480 | { |
b423cd4c | 2481 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
2482 | "Printer \"%s\" modified by \"%s\".", printer->name, | |
2483 | get_username(con)); | |
ef416fc2 | 2484 | |
b423cd4c | 2485 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".", |
2486 | printer->name, get_username(con)); | |
ef416fc2 | 2487 | } |
b423cd4c | 2488 | else |
2489 | { | |
2490 | cupsdAddPrinterHistory(printer); | |
ef416fc2 | 2491 | |
b423cd4c | 2492 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL, |
2493 | "New printer \"%s\" added by \"%s\".", printer->name, | |
2494 | get_username(con)); | |
ef416fc2 | 2495 | |
b423cd4c | 2496 | cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".", |
2497 | printer->name, get_username(con)); | |
2498 | } | |
2499 | ||
2500 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 2501 | } |
2502 | ||
2503 | ||
2504 | /* | |
b423cd4c | 2505 | * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute |
2506 | * based upon the printer state... | |
ef416fc2 | 2507 | */ |
2508 | ||
b423cd4c | 2509 | static void |
2510 | add_printer_state_reasons( | |
2511 | cupsd_client_t *con, /* I - Client connection */ | |
2512 | cupsd_printer_t *p) /* I - Printer info */ | |
ef416fc2 | 2513 | { |
b423cd4c | 2514 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
2515 | "add_printer_state_reasons(%p[%d], %p[%s])", | |
2516 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2517 | |
b423cd4c | 2518 | if (p->num_reasons == 0) |
2519 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2520 | "printer-state-reasons", NULL, | |
2521 | p->state == IPP_PRINTER_STOPPED ? "paused" : "none"); | |
2522 | else | |
2523 | ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, | |
2524 | "printer-state-reasons", p->num_reasons, NULL, | |
2525 | (const char * const *)p->reasons); | |
2526 | } | |
ef416fc2 | 2527 | |
ef416fc2 | 2528 | |
b423cd4c | 2529 | /* |
2530 | * 'add_queued_job_count()' - Add the "queued-job-count" attribute for | |
2531 | * the specified printer or class. | |
2532 | */ | |
ef416fc2 | 2533 | |
b423cd4c | 2534 | static void |
2535 | add_queued_job_count( | |
2536 | cupsd_client_t *con, /* I - Client connection */ | |
2537 | cupsd_printer_t *p) /* I - Printer or class */ | |
2538 | { | |
2539 | int count; /* Number of jobs on destination */ | |
ef416fc2 | 2540 | |
ef416fc2 | 2541 | |
b423cd4c | 2542 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])", |
2543 | con, con->http.fd, p, p->name); | |
ef416fc2 | 2544 | |
b423cd4c | 2545 | count = cupsdGetPrinterJobCount(p->name); |
ef416fc2 | 2546 | |
b423cd4c | 2547 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, |
2548 | "queued-job-count", count); | |
2549 | } | |
ef416fc2 | 2550 | |
ef416fc2 | 2551 | |
b423cd4c | 2552 | /* |
2553 | * 'apply_printer_defaults()' - Apply printer default options to a job. | |
2554 | */ | |
ef416fc2 | 2555 | |
b423cd4c | 2556 | static void |
2557 | apply_printer_defaults( | |
2558 | cupsd_printer_t *printer, /* I - Printer */ | |
2559 | cupsd_job_t *job) /* I - Job */ | |
2560 | { | |
2561 | int i, /* Looping var */ | |
2562 | num_options; /* Number of default options */ | |
2563 | cups_option_t *options, /* Default options */ | |
2564 | *option; /* Current option */ | |
ef416fc2 | 2565 | |
ef416fc2 | 2566 | |
b423cd4c | 2567 | /* |
2568 | * Collect all of the default options and add the missing ones to the | |
2569 | * job object... | |
2570 | */ | |
ef416fc2 | 2571 | |
b423cd4c | 2572 | for (i = printer->num_options, num_options = 0, option = printer->options; |
2573 | i > 0; | |
2574 | i --, option ++) | |
2575 | if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO)) | |
2576 | { | |
2577 | num_options = cupsAddOption(option->name, option->value, num_options, | |
2578 | &options); | |
2579 | } | |
ef416fc2 | 2580 | |
b423cd4c | 2581 | /* |
2582 | * Encode these options as attributes in the job object... | |
2583 | */ | |
ef416fc2 | 2584 | |
b423cd4c | 2585 | cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB); |
2586 | cupsFreeOptions(num_options, options); | |
2587 | } | |
2588 | ||
2589 | ||
2590 | /* | |
2591 | * 'authenticate_job()' - Set job authentication info. | |
2592 | */ | |
2593 | ||
2594 | static void | |
2595 | authenticate_job(cupsd_client_t *con, /* I - Client connection */ | |
2596 | ipp_attribute_t *uri) /* I - Job URI */ | |
2597 | { | |
2598 | ipp_attribute_t *attr; /* Job-id attribute */ | |
2599 | int jobid; /* Job ID */ | |
2600 | cupsd_job_t *job; /* Current job */ | |
2601 | char method[HTTP_MAX_URI], | |
2602 | /* Method portion of URI */ | |
2603 | username[HTTP_MAX_URI], | |
2604 | /* Username portion of URI */ | |
2605 | host[HTTP_MAX_URI], | |
2606 | /* Host portion of URI */ | |
2607 | resource[HTTP_MAX_URI]; | |
2608 | /* Resource portion of URI */ | |
2609 | int port; /* Port portion of URI */ | |
ef416fc2 | 2610 | |
ef416fc2 | 2611 | |
b423cd4c | 2612 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)", |
2613 | con, con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 2614 | |
b423cd4c | 2615 | /* |
2616 | * Start with "everything is OK" status... | |
2617 | */ | |
ef416fc2 | 2618 | |
b423cd4c | 2619 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 2620 | |
b423cd4c | 2621 | /* |
2622 | * See if we have a job URI or a printer URI... | |
2623 | */ | |
ef416fc2 | 2624 | |
b423cd4c | 2625 | if (!strcmp(uri->name, "printer-uri")) |
2626 | { | |
2627 | /* | |
2628 | * Got a printer URI; see if we also have a job-id attribute... | |
2629 | */ | |
ef416fc2 | 2630 | |
b423cd4c | 2631 | if ((attr = ippFindAttribute(con->request, "job-id", |
2632 | IPP_TAG_INTEGER)) == NULL) | |
2633 | { | |
2634 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2635 | _("Got a printer-uri attribute but no job-id!")); | |
2636 | return; | |
2637 | } | |
2638 | ||
2639 | jobid = attr->values[0].integer; | |
2640 | } | |
2641 | else | |
2642 | { | |
2643 | /* | |
2644 | * Got a job URI; parse it to get the job ID... | |
2645 | */ | |
ef416fc2 | 2646 | |
b423cd4c | 2647 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2648 | sizeof(method), username, sizeof(username), host, | |
2649 | sizeof(host), &port, resource, sizeof(resource)); | |
2650 | ||
2651 | if (strncmp(resource, "/jobs/", 6)) | |
2652 | { | |
2653 | /* | |
2654 | * Not a valid URI! | |
2655 | */ | |
ef416fc2 | 2656 | |
b423cd4c | 2657 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"), |
2658 | uri->values[0].string.text); | |
2659 | return; | |
2660 | } | |
ef416fc2 | 2661 | |
b423cd4c | 2662 | jobid = atoi(resource + 6); |
2663 | } | |
ef416fc2 | 2664 | |
b423cd4c | 2665 | /* |
2666 | * See if the job exists... | |
2667 | */ | |
ef416fc2 | 2668 | |
b423cd4c | 2669 | if ((job = cupsdFindJob(jobid)) == NULL) |
2670 | { | |
2671 | /* | |
2672 | * Nope - return a "not found" error... | |
2673 | */ | |
ef416fc2 | 2674 | |
b423cd4c | 2675 | send_ipp_status(con, IPP_NOT_FOUND, |
2676 | _("Job #%d does not exist!"), jobid); | |
ef416fc2 | 2677 | return; |
b423cd4c | 2678 | } |
ef416fc2 | 2679 | |
b423cd4c | 2680 | /* |
2681 | * See if the job has been completed... | |
2682 | */ | |
2683 | ||
2684 | if (job->state_value != IPP_JOB_HELD) | |
ef416fc2 | 2685 | { |
2686 | /* | |
b423cd4c | 2687 | * Return a "not-possible" error... |
ef416fc2 | 2688 | */ |
2689 | ||
b423cd4c | 2690 | send_ipp_status(con, IPP_NOT_POSSIBLE, |
2691 | _("Job #%d is not held for authentication!"), | |
2692 | jobid); | |
2693 | return; | |
2694 | } | |
ef416fc2 | 2695 | |
b423cd4c | 2696 | /* |
2697 | * See if we have already authenticated... | |
2698 | */ | |
2699 | ||
2700 | if (!con->username[0]) | |
2701 | { | |
2702 | send_ipp_status(con, IPP_NOT_AUTHORIZED, | |
2703 | _("No authentication information provided!")); | |
2704 | return; | |
2705 | } | |
2706 | ||
2707 | /* | |
2708 | * See if the job is owned by the requesting user... | |
2709 | */ | |
2710 | ||
2711 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
2712 | { | |
2713 | send_http_error(con, HTTP_UNAUTHORIZED); | |
2714 | return; | |
2715 | } | |
2716 | ||
2717 | /* | |
2718 | * Save the authentication information for this job... | |
2719 | */ | |
2720 | ||
2721 | save_auth_info(con, job); | |
2722 | ||
2723 | /* | |
2724 | * Reset the job-hold-until value to "no-hold"... | |
2725 | */ | |
2726 | ||
2727 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", | |
2728 | IPP_TAG_KEYWORD)) == NULL) | |
2729 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
2730 | ||
2731 | if (attr) | |
2732 | { | |
2733 | attr->value_tag = IPP_TAG_KEYWORD; | |
2734 | cupsdSetString(&(attr->values[0].string.text), "no-hold"); | |
ef416fc2 | 2735 | } |
b423cd4c | 2736 | |
2737 | /* | |
2738 | * Release the job and return... | |
2739 | */ | |
2740 | ||
2741 | cupsdReleaseJob(job); | |
2742 | ||
2743 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was authenticated by \"%s\".", jobid, | |
2744 | con->username); | |
ef416fc2 | 2745 | } |
2746 | ||
2747 | ||
2748 | /* | |
b423cd4c | 2749 | * 'cancel_all_jobs()' - Cancel all print jobs. |
ef416fc2 | 2750 | */ |
2751 | ||
b423cd4c | 2752 | static void |
2753 | cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */ | |
2754 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 2755 | { |
b423cd4c | 2756 | http_status_t status; /* Policy status */ |
2757 | const char *dest; /* Destination */ | |
2758 | cups_ptype_t dtype; /* Destination type */ | |
2759 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
2760 | userpass[HTTP_MAX_URI], /* Username portion of URI */ | |
2761 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2762 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2763 | int port; /* Port portion of URI */ | |
2764 | ipp_attribute_t *attr; /* Attribute in request */ | |
2765 | const char *username; /* Username */ | |
2766 | int purge; /* Purge? */ | |
2767 | cupsd_printer_t *printer; /* Printer */ | |
ef416fc2 | 2768 | |
2769 | ||
b423cd4c | 2770 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con, |
2771 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 2772 | |
2773 | /* | |
b423cd4c | 2774 | * See if we have a printer URI... |
ef416fc2 | 2775 | */ |
2776 | ||
b423cd4c | 2777 | if (strcmp(uri->name, "printer-uri")) |
2778 | { | |
2779 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2780 | _("The printer-uri attribute is required!")); | |
2781 | return; | |
2782 | } | |
ef416fc2 | 2783 | |
2784 | /* | |
b423cd4c | 2785 | * Get the username (if any) for the jobs we want to cancel (only if |
2786 | * "my-jobs" is specified... | |
ef416fc2 | 2787 | */ |
2788 | ||
b423cd4c | 2789 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
2790 | IPP_TAG_BOOLEAN)) != NULL && | |
2791 | attr->values[0].boolean) | |
ef416fc2 | 2792 | { |
b423cd4c | 2793 | if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
2794 | IPP_TAG_NAME)) != NULL) | |
2795 | username = attr->values[0].string.text; | |
2796 | else | |
2797 | { | |
2798 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2799 | _("Missing requesting-user-name attribute!")); | |
2800 | return; | |
2801 | } | |
ef416fc2 | 2802 | } |
b423cd4c | 2803 | else |
2804 | username = NULL; | |
ef416fc2 | 2805 | |
b423cd4c | 2806 | /* |
2807 | * Look for the "purge-jobs" attribute... | |
2808 | */ | |
2809 | ||
2810 | if ((attr = ippFindAttribute(con->request, "purge-jobs", | |
2811 | IPP_TAG_BOOLEAN)) != NULL) | |
2812 | purge = attr->values[0].boolean; | |
2813 | else | |
2814 | purge = 1; | |
ef416fc2 | 2815 | |
2816 | /* | |
b423cd4c | 2817 | * And if the destination is valid... |
ef416fc2 | 2818 | */ |
2819 | ||
b423cd4c | 2820 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2821 | sizeof(method), userpass, sizeof(userpass), host, | |
2822 | sizeof(host), &port, resource, sizeof(resource)); | |
2823 | ||
2824 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 2825 | { |
2826 | /* | |
b423cd4c | 2827 | * Bad URI? |
ef416fc2 | 2828 | */ |
2829 | ||
b423cd4c | 2830 | if ((!strncmp(resource, "/printers/", 10) && resource[10]) || |
2831 | (!strncmp(resource, "/classes/", 9) && resource[9])) | |
2832 | { | |
2833 | send_ipp_status(con, IPP_NOT_FOUND, | |
2834 | _("The printer or class was not found.")); | |
2835 | return; | |
2836 | } | |
2837 | else if (strcmp(resource, "/printers/")) | |
2838 | { | |
2839 | send_ipp_status(con, IPP_NOT_FOUND, | |
2840 | _("The printer-uri \"%s\" is not valid."), | |
2841 | uri->values[0].string.text); | |
2842 | return; | |
2843 | } | |
ef416fc2 | 2844 | |
b423cd4c | 2845 | /* |
2846 | * Check policy... | |
2847 | */ | |
2848 | ||
2849 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
2850 | { | |
2851 | send_http_error(con, status); | |
2852 | return; | |
2853 | } | |
ef416fc2 | 2854 | |
ef416fc2 | 2855 | /* |
b423cd4c | 2856 | * Cancel all jobs on all printers... |
ef416fc2 | 2857 | */ |
2858 | ||
b423cd4c | 2859 | cupsdCancelJobs(NULL, username, purge); |
ef416fc2 | 2860 | |
b423cd4c | 2861 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".", |
2862 | purge ? "purged" : "cancelled", get_username(con)); | |
ef416fc2 | 2863 | } |
b423cd4c | 2864 | else |
ef416fc2 | 2865 | { |
2866 | /* | |
b423cd4c | 2867 | * Check policy... |
ef416fc2 | 2868 | */ |
2869 | ||
b423cd4c | 2870 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
2871 | { | |
2872 | send_http_error(con, status); | |
2873 | return; | |
2874 | } | |
ef416fc2 | 2875 | |
b423cd4c | 2876 | /* |
2877 | * Cancel all of the jobs on the named printer... | |
2878 | */ | |
2879 | ||
2880 | cupsdCancelJobs(dest, username, purge); | |
2881 | ||
2882 | cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".", | |
2883 | dest, purge ? "purged" : "cancelled", get_username(con)); | |
ef416fc2 | 2884 | } |
2885 | ||
b423cd4c | 2886 | con->response->request.status.status_code = IPP_OK; |
2887 | } | |
2888 | ||
2889 | ||
2890 | /* | |
2891 | * 'cancel_job()' - Cancel a print job. | |
2892 | */ | |
2893 | ||
2894 | static void | |
2895 | cancel_job(cupsd_client_t *con, /* I - Client connection */ | |
2896 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
2897 | { | |
2898 | ipp_attribute_t *attr; /* Current attribute */ | |
2899 | int jobid; /* Job ID */ | |
2900 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
2901 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
2902 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
2903 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
2904 | int port; /* Port portion of URI */ | |
2905 | cupsd_job_t *job; /* Job information */ | |
2906 | const char *dest; /* Destination */ | |
2907 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
2908 | cupsd_printer_t *printer; /* Printer data */ | |
2909 | ||
2910 | ||
2911 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con, | |
2912 | con->http.fd, uri->values[0].string.text); | |
2913 | ||
ef416fc2 | 2914 | /* |
b423cd4c | 2915 | * See if we have a job URI or a printer URI... |
ef416fc2 | 2916 | */ |
2917 | ||
b423cd4c | 2918 | if (!strcmp(uri->name, "printer-uri")) |
2919 | { | |
2920 | /* | |
2921 | * Got a printer URI; see if we also have a job-id attribute... | |
2922 | */ | |
2923 | ||
2924 | if ((attr = ippFindAttribute(con->request, "job-id", | |
2925 | IPP_TAG_INTEGER)) == NULL) | |
2926 | { | |
2927 | send_ipp_status(con, IPP_BAD_REQUEST, | |
2928 | _("Got a printer-uri attribute but no job-id!")); | |
2929 | return; | |
2930 | } | |
2931 | ||
2932 | if ((jobid = attr->values[0].integer) == 0) | |
ef416fc2 | 2933 | { |
2934 | /* | |
b423cd4c | 2935 | * Find the current job on the specified printer... |
ef416fc2 | 2936 | */ |
2937 | ||
b423cd4c | 2938 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2939 | sizeof(method), username, sizeof(username), host, | |
2940 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 2941 | |
b423cd4c | 2942 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) |
ef416fc2 | 2943 | { |
2944 | /* | |
b423cd4c | 2945 | * Bad URI... |
ef416fc2 | 2946 | */ |
2947 | ||
b423cd4c | 2948 | send_ipp_status(con, IPP_NOT_FOUND, |
2949 | _("The printer or class was not found.")); | |
2950 | return; | |
ef416fc2 | 2951 | } |
2952 | ||
2953 | /* | |
b423cd4c | 2954 | * See if the printer is currently printing a job... |
ef416fc2 | 2955 | */ |
2956 | ||
b423cd4c | 2957 | if (printer->job) |
2958 | jobid = ((cupsd_job_t *)printer->job)->id; | |
ef416fc2 | 2959 | else |
ef416fc2 | 2960 | { |
2961 | /* | |
b423cd4c | 2962 | * No, see if there are any pending jobs... |
ef416fc2 | 2963 | */ |
b423cd4c | 2964 | |
2965 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
2966 | job; | |
2967 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
2968 | if (job->state_value <= IPP_JOB_PROCESSING && | |
2969 | !strcasecmp(job->dest, dest)) | |
2970 | break; | |
ef416fc2 | 2971 | |
b423cd4c | 2972 | if (job) |
2973 | jobid = job->id; | |
2974 | else | |
ef416fc2 | 2975 | { |
b423cd4c | 2976 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"), |
2977 | dest); | |
2978 | return; | |
2979 | } | |
2980 | } | |
2981 | } | |
2982 | } | |
2983 | else | |
2984 | { | |
2985 | /* | |
2986 | * Got a job URI; parse it to get the job ID... | |
2987 | */ | |
ef416fc2 | 2988 | |
b423cd4c | 2989 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
2990 | sizeof(method), username, sizeof(username), host, | |
2991 | sizeof(host), &port, resource, sizeof(resource)); | |
2992 | ||
2993 | if (strncmp(resource, "/jobs/", 6)) | |
2994 | { | |
2995 | /* | |
2996 | * Not a valid URI! | |
2997 | */ | |
ef416fc2 | 2998 | |
b423cd4c | 2999 | send_ipp_status(con, IPP_BAD_REQUEST, |
3000 | _("Bad job-uri attribute \"%s\"!"), | |
3001 | uri->values[0].string.text); | |
3002 | return; | |
3003 | } | |
3004 | ||
3005 | jobid = atoi(resource + 6); | |
3006 | } | |
3007 | ||
3008 | /* | |
3009 | * See if the job exists... | |
3010 | */ | |
3011 | ||
3012 | if ((job = cupsdFindJob(jobid)) == NULL) | |
3013 | { | |
3014 | /* | |
3015 | * Nope - return a "not found" error... | |
3016 | */ | |
3017 | ||
3018 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
3019 | return; | |
3020 | } | |
3021 | ||
3022 | /* | |
3023 | * See if the job is owned by the requesting user... | |
3024 | */ | |
3025 | ||
3026 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
3027 | { | |
3028 | send_http_error(con, HTTP_UNAUTHORIZED); | |
3029 | return; | |
3030 | } | |
3031 | ||
3032 | /* | |
3033 | * See if the job is already completed, cancelled, or aborted; if so, | |
3034 | * we can't cancel... | |
3035 | */ | |
3036 | ||
3037 | if (job->state_value >= IPP_JOB_CANCELLED) | |
3038 | { | |
e1d6a774 | 3039 | switch (job->state_value) |
3040 | { | |
3041 | case IPP_JOB_CANCELLED : | |
3042 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3043 | _("Job #%d is already cancelled - can\'t cancel."), | |
3044 | jobid); | |
3045 | break; | |
3046 | ||
3047 | case IPP_JOB_ABORTED : | |
3048 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3049 | _("Job #%d is already aborted - can\'t cancel."), | |
3050 | jobid); | |
3051 | break; | |
3052 | ||
3053 | default : | |
3054 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
3055 | _("Job #%d is already completed - can\'t cancel."), | |
3056 | jobid); | |
3057 | break; | |
3058 | } | |
3059 | ||
b423cd4c | 3060 | return; |
3061 | } | |
ef416fc2 | 3062 | |
b423cd4c | 3063 | /* |
3064 | * Cancel the job and return... | |
3065 | */ | |
ef416fc2 | 3066 | |
b423cd4c | 3067 | cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job, |
3068 | "Job cancelled by \"%s\".", username); | |
ef416fc2 | 3069 | |
b423cd4c | 3070 | cupsdCancelJob(job, 0); |
3071 | cupsdCheckJobs(); | |
ef416fc2 | 3072 | |
b423cd4c | 3073 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was cancelled by \"%s\".", jobid, |
3074 | username); | |
ef416fc2 | 3075 | |
b423cd4c | 3076 | con->response->request.status.status_code = IPP_OK; |
3077 | } | |
ef416fc2 | 3078 | |
ef416fc2 | 3079 | |
b423cd4c | 3080 | /* |
3081 | * 'cancel_subscription()' - Cancel a subscription. | |
3082 | */ | |
ef416fc2 | 3083 | |
b423cd4c | 3084 | static void |
3085 | cancel_subscription( | |
3086 | cupsd_client_t *con, /* I - Client connection */ | |
3087 | int sub_id) /* I - Subscription ID */ | |
3088 | { | |
3089 | http_status_t status; /* Policy status */ | |
3090 | cupsd_subscription_t *sub; /* Subscription */ | |
ef416fc2 | 3091 | |
ef416fc2 | 3092 | |
b423cd4c | 3093 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3094 | "cancel_subscription(con=%p[%d], sub_id=%d)", | |
3095 | con, con->http.fd, sub_id); | |
ef416fc2 | 3096 | |
b423cd4c | 3097 | /* |
3098 | * Is the subscription ID valid? | |
3099 | */ | |
ef416fc2 | 3100 | |
b423cd4c | 3101 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
3102 | { | |
3103 | /* | |
3104 | * Bad subscription ID... | |
3105 | */ | |
ef416fc2 | 3106 | |
b423cd4c | 3107 | send_ipp_status(con, IPP_NOT_FOUND, |
3108 | _("notify-subscription-id %d no good!"), sub_id); | |
3109 | return; | |
3110 | } | |
ef416fc2 | 3111 | |
b423cd4c | 3112 | /* |
3113 | * Check policy... | |
3114 | */ | |
ef416fc2 | 3115 | |
b423cd4c | 3116 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
3117 | DefaultPolicyPtr, | |
3118 | con, sub->owner)) != HTTP_OK) | |
3119 | { | |
3120 | send_http_error(con, status); | |
3121 | return; | |
3122 | } | |
ef416fc2 | 3123 | |
b423cd4c | 3124 | /* |
3125 | * Cancel the subscription... | |
3126 | */ | |
ef416fc2 | 3127 | |
b423cd4c | 3128 | cupsdDeleteSubscription(sub, 1); |
ef416fc2 | 3129 | |
b423cd4c | 3130 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 3131 | } |
3132 | ||
3133 | ||
3134 | /* | |
b423cd4c | 3135 | * 'check_quotas()' - Check quotas for a printer and user. |
ef416fc2 | 3136 | */ |
3137 | ||
b423cd4c | 3138 | static int /* O - 1 if OK, 0 if not */ |
3139 | check_quotas(cupsd_client_t *con, /* I - Client connection */ | |
3140 | cupsd_printer_t *p) /* I - Printer or class */ | |
ef416fc2 | 3141 | { |
b423cd4c | 3142 | int i; /* Looping var */ |
3143 | char username[33]; /* Username */ | |
3144 | cupsd_quota_t *q; /* Quota data */ | |
3145 | struct passwd *pw; /* User password data */ | |
ef416fc2 | 3146 | |
3147 | ||
b423cd4c | 3148 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])", |
3149 | con, con->http.fd, p, p->name); | |
ef416fc2 | 3150 | |
3151 | /* | |
b423cd4c | 3152 | * Check input... |
ef416fc2 | 3153 | */ |
3154 | ||
b423cd4c | 3155 | if (!con || !p) |
3156 | return (0); | |
ef416fc2 | 3157 | |
b423cd4c | 3158 | /* |
3159 | * Figure out who is printing... | |
3160 | */ | |
3161 | ||
3162 | strlcpy(username, get_username(con), sizeof(username)); | |
3163 | ||
3164 | /* | |
3165 | * Check global active job limits for printers and users... | |
3166 | */ | |
3167 | ||
3168 | if (MaxJobsPerPrinter) | |
ef416fc2 | 3169 | { |
b423cd4c | 3170 | /* |
3171 | * Check if there are too many pending jobs on this printer... | |
3172 | */ | |
3173 | ||
3174 | if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter) | |
3175 | { | |
3176 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...", | |
3177 | p->name); | |
3178 | return (0); | |
3179 | } | |
3180 | } | |
3181 | ||
3182 | if (MaxJobsPerUser) | |
3183 | { | |
3184 | /* | |
3185 | * Check if there are too many pending jobs for this user... | |
3186 | */ | |
3187 | ||
3188 | if (cupsdGetUserJobCount(username) >= MaxJobsPerUser) | |
3189 | { | |
3190 | cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...", | |
3191 | username); | |
3192 | return (0); | |
3193 | } | |
ef416fc2 | 3194 | } |
3195 | ||
3196 | /* | |
b423cd4c | 3197 | * Check against users... |
ef416fc2 | 3198 | */ |
3199 | ||
b423cd4c | 3200 | if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0) |
3201 | return (1); | |
3202 | ||
3203 | if (p->num_users) | |
3204 | { | |
3205 | pw = getpwnam(username); | |
3206 | endpwent(); | |
3207 | ||
3208 | for (i = 0; i < p->num_users; i ++) | |
3209 | if (p->users[i][0] == '@') | |
3210 | { | |
3211 | /* | |
3212 | * Check group membership... | |
3213 | */ | |
3214 | ||
3215 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
3216 | break; | |
3217 | } | |
3218 | else if (!strcasecmp(username, p->users[i])) | |
3219 | break; | |
3220 | ||
3221 | if ((i < p->num_users) == p->deny_users) | |
ef416fc2 | 3222 | { |
b423cd4c | 3223 | cupsdLogMessage(CUPSD_LOG_INFO, |
3224 | "Denying user \"%s\" access to printer \"%s\"...", | |
3225 | username, p->name); | |
3226 | return (0); | |
ef416fc2 | 3227 | } |
b423cd4c | 3228 | } |
ef416fc2 | 3229 | |
3230 | /* | |
b423cd4c | 3231 | * Check quotas... |
ef416fc2 | 3232 | */ |
3233 | ||
b423cd4c | 3234 | if (p->k_limit || p->page_limit) |
3235 | { | |
3236 | if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL) | |
3237 | { | |
3238 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3239 | "Unable to allocate quota data for user \"%s\"!", | |
3240 | username); | |
3241 | return (0); | |
3242 | } | |
ef416fc2 | 3243 | |
b423cd4c | 3244 | if ((q->k_count >= p->k_limit && p->k_limit) || |
3245 | (q->page_count >= p->page_limit && p->page_limit)) | |
3246 | { | |
3247 | cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...", | |
3248 | username); | |
3249 | return (0); | |
3250 | } | |
3251 | } | |
3252 | ||
3253 | /* | |
3254 | * If we have gotten this far, we're done! | |
3255 | */ | |
3256 | ||
3257 | return (1); | |
ef416fc2 | 3258 | } |
3259 | ||
3260 | ||
3261 | /* | |
b423cd4c | 3262 | * 'copy_attribute()' - Copy a single attribute. |
ef416fc2 | 3263 | */ |
3264 | ||
b423cd4c | 3265 | static ipp_attribute_t * /* O - New attribute */ |
3266 | copy_attribute( | |
3267 | ipp_t *to, /* O - Destination request/response */ | |
3268 | ipp_attribute_t *attr, /* I - Attribute to copy */ | |
3269 | int quickcopy) /* I - Do a quick copy? */ | |
ef416fc2 | 3270 | { |
b423cd4c | 3271 | int i; /* Looping var */ |
3272 | ipp_attribute_t *toattr; /* Destination attribute */ | |
ef416fc2 | 3273 | |
3274 | ||
3275 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
b423cd4c | 3276 | "copy_attribute(%p, %p[%s,%x,%x])", to, attr, |
3277 | attr->name ? attr->name : "(null)", attr->group_tag, | |
3278 | attr->value_tag); | |
ef416fc2 | 3279 | |
b423cd4c | 3280 | switch (attr->value_tag & ~IPP_TAG_COPY) |
3281 | { | |
3282 | case IPP_TAG_ZERO : | |
3283 | toattr = ippAddSeparator(to); | |
3284 | break; | |
ef416fc2 | 3285 | |
b423cd4c | 3286 | case IPP_TAG_INTEGER : |
3287 | case IPP_TAG_ENUM : | |
3288 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
3289 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 3290 | |
b423cd4c | 3291 | for (i = 0; i < attr->num_values; i ++) |
3292 | toattr->values[i].integer = attr->values[i].integer; | |
3293 | break; | |
ef416fc2 | 3294 | |
b423cd4c | 3295 | case IPP_TAG_BOOLEAN : |
3296 | toattr = ippAddBooleans(to, attr->group_tag, attr->name, | |
3297 | attr->num_values, NULL); | |
ef416fc2 | 3298 | |
b423cd4c | 3299 | for (i = 0; i < attr->num_values; i ++) |
3300 | toattr->values[i].boolean = attr->values[i].boolean; | |
3301 | break; | |
ef416fc2 | 3302 | |
b423cd4c | 3303 | case IPP_TAG_STRING : |
3304 | case IPP_TAG_TEXT : | |
3305 | case IPP_TAG_NAME : | |
3306 | case IPP_TAG_KEYWORD : | |
3307 | case IPP_TAG_URI : | |
3308 | case IPP_TAG_URISCHEME : | |
3309 | case IPP_TAG_CHARSET : | |
3310 | case IPP_TAG_LANGUAGE : | |
3311 | case IPP_TAG_MIMETYPE : | |
3312 | toattr = ippAddStrings(to, attr->group_tag, | |
3313 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
3314 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 3315 | |
b423cd4c | 3316 | if (quickcopy) |
3317 | { | |
3318 | for (i = 0; i < attr->num_values; i ++) | |
3319 | toattr->values[i].string.text = attr->values[i].string.text; | |
3320 | } | |
3321 | else | |
3322 | { | |
3323 | for (i = 0; i < attr->num_values; i ++) | |
757d2cad | 3324 | toattr->values[i].string.text = _cupsStrAlloc(attr->values[i].string.text); |
b423cd4c | 3325 | } |
3326 | break; | |
ef416fc2 | 3327 | |
b423cd4c | 3328 | case IPP_TAG_DATE : |
3329 | toattr = ippAddDate(to, attr->group_tag, attr->name, | |
3330 | attr->values[0].date); | |
3331 | break; | |
ef416fc2 | 3332 | |
b423cd4c | 3333 | case IPP_TAG_RESOLUTION : |
3334 | toattr = ippAddResolutions(to, attr->group_tag, attr->name, | |
3335 | attr->num_values, IPP_RES_PER_INCH, | |
3336 | NULL, NULL); | |
ef416fc2 | 3337 | |
b423cd4c | 3338 | for (i = 0; i < attr->num_values; i ++) |
3339 | { | |
3340 | toattr->values[i].resolution.xres = attr->values[i].resolution.xres; | |
3341 | toattr->values[i].resolution.yres = attr->values[i].resolution.yres; | |
3342 | toattr->values[i].resolution.units = attr->values[i].resolution.units; | |
3343 | } | |
3344 | break; | |
ef416fc2 | 3345 | |
b423cd4c | 3346 | case IPP_TAG_RANGE : |
3347 | toattr = ippAddRanges(to, attr->group_tag, attr->name, | |
3348 | attr->num_values, NULL, NULL); | |
ef416fc2 | 3349 | |
b423cd4c | 3350 | for (i = 0; i < attr->num_values; i ++) |
3351 | { | |
3352 | toattr->values[i].range.lower = attr->values[i].range.lower; | |
3353 | toattr->values[i].range.upper = attr->values[i].range.upper; | |
3354 | } | |
3355 | break; | |
ef416fc2 | 3356 | |
b423cd4c | 3357 | case IPP_TAG_TEXTLANG : |
3358 | case IPP_TAG_NAMELANG : | |
3359 | toattr = ippAddStrings(to, attr->group_tag, | |
3360 | (ipp_tag_t)(attr->value_tag | quickcopy), | |
3361 | attr->name, attr->num_values, NULL, NULL); | |
ef416fc2 | 3362 | |
b423cd4c | 3363 | if (quickcopy) |
3364 | { | |
3365 | for (i = 0; i < attr->num_values; i ++) | |
3366 | { | |
3367 | toattr->values[i].string.charset = attr->values[i].string.charset; | |
3368 | toattr->values[i].string.text = attr->values[i].string.text; | |
3369 | } | |
3370 | } | |
3371 | else | |
3372 | { | |
3373 | for (i = 0; i < attr->num_values; i ++) | |
3374 | { | |
3375 | if (!i) | |
3376 | toattr->values[i].string.charset = | |
757d2cad | 3377 | _cupsStrAlloc(attr->values[i].string.charset); |
b423cd4c | 3378 | else |
3379 | toattr->values[i].string.charset = | |
3380 | toattr->values[0].string.charset; | |
ef416fc2 | 3381 | |
757d2cad | 3382 | toattr->values[i].string.text = _cupsStrAlloc(attr->values[i].string.text); |
b423cd4c | 3383 | } |
3384 | } | |
3385 | break; | |
ef416fc2 | 3386 | |
b423cd4c | 3387 | case IPP_TAG_BEGIN_COLLECTION : |
3388 | toattr = ippAddCollections(to, attr->group_tag, attr->name, | |
3389 | attr->num_values, NULL); | |
ef416fc2 | 3390 | |
b423cd4c | 3391 | for (i = 0; i < attr->num_values; i ++) |
3392 | { | |
3393 | toattr->values[i].collection = ippNew(); | |
3394 | copy_attrs(toattr->values[i].collection, attr->values[i].collection, | |
3395 | NULL, IPP_TAG_ZERO, 0); | |
3396 | } | |
ef416fc2 | 3397 | break; |
ef416fc2 | 3398 | |
b423cd4c | 3399 | default : |
3400 | toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag, | |
3401 | attr->name, attr->num_values, NULL); | |
ef416fc2 | 3402 | |
b423cd4c | 3403 | for (i = 0; i < attr->num_values; i ++) |
3404 | { | |
3405 | toattr->values[i].unknown.length = attr->values[i].unknown.length; | |
ef416fc2 | 3406 | |
b423cd4c | 3407 | if (toattr->values[i].unknown.length > 0) |
3408 | { | |
3409 | if ((toattr->values[i].unknown.data = | |
3410 | malloc(toattr->values[i].unknown.length)) == NULL) | |
3411 | toattr->values[i].unknown.length = 0; | |
3412 | else | |
3413 | memcpy(toattr->values[i].unknown.data, | |
3414 | attr->values[i].unknown.data, | |
3415 | toattr->values[i].unknown.length); | |
3416 | } | |
3417 | } | |
3418 | break; /* anti-compiler-warning-code */ | |
3419 | } | |
ef416fc2 | 3420 | |
b423cd4c | 3421 | return (toattr); |
3422 | } | |
3423 | ||
3424 | ||
3425 | /* | |
3426 | * 'copy_attrs()' - Copy attributes from one request to another. | |
3427 | */ | |
3428 | ||
3429 | static void | |
3430 | copy_attrs(ipp_t *to, /* I - Destination request */ | |
3431 | ipp_t *from, /* I - Source request */ | |
3432 | cups_array_t *ra, /* I - Requested attributes */ | |
3433 | ipp_tag_t group, /* I - Group to copy */ | |
3434 | int quickcopy) /* I - Do a quick copy? */ | |
3435 | { | |
3436 | ipp_attribute_t *fromattr; /* Source attribute */ | |
ef416fc2 | 3437 | |
ef416fc2 | 3438 | |
b423cd4c | 3439 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3440 | "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)", | |
3441 | to, from, ra, group, quickcopy); | |
ef416fc2 | 3442 | |
b423cd4c | 3443 | if (!to || !from) |
3444 | return; | |
e00b005a | 3445 | |
b423cd4c | 3446 | for (fromattr = from->attrs; fromattr; fromattr = fromattr->next) |
ef416fc2 | 3447 | { |
3448 | /* | |
b423cd4c | 3449 | * Filter attributes as needed... |
ef416fc2 | 3450 | */ |
3451 | ||
b423cd4c | 3452 | if (group != IPP_TAG_ZERO && fromattr->group_tag != group && |
3453 | fromattr->group_tag != IPP_TAG_ZERO && !fromattr->name) | |
3454 | continue; | |
3455 | ||
3456 | if (!ra || cupsArrayFind(ra, fromattr->name)) | |
3457 | copy_attribute(to, fromattr, quickcopy); | |
ef416fc2 | 3458 | } |
b423cd4c | 3459 | } |
ef416fc2 | 3460 | |
ef416fc2 | 3461 | |
b423cd4c | 3462 | /* |
3463 | * 'copy_banner()' - Copy a banner file to the requests directory for the | |
3464 | * specified job. | |
3465 | */ | |
ef416fc2 | 3466 | |
b423cd4c | 3467 | static int /* O - Size of banner file in kbytes */ |
3468 | copy_banner(cupsd_client_t *con, /* I - Client connection */ | |
3469 | cupsd_job_t *job, /* I - Job information */ | |
3470 | const char *name) /* I - Name of banner */ | |
3471 | { | |
3472 | int i; /* Looping var */ | |
3473 | int kbytes; /* Size of banner file in kbytes */ | |
3474 | char filename[1024]; /* Job filename */ | |
3475 | cupsd_banner_t *banner; /* Pointer to banner */ | |
3476 | cups_file_t *in; /* Input file */ | |
3477 | cups_file_t *out; /* Output file */ | |
3478 | int ch; /* Character from file */ | |
3479 | char attrname[255], /* Name of attribute */ | |
3480 | *s; /* Pointer into name */ | |
3481 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 3482 | |
ef416fc2 | 3483 | |
b423cd4c | 3484 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner(%p[%d], %p[%d], %s)", |
3485 | con, con->http.fd, job, job->id, name ? name : "(null)"); | |
ef416fc2 | 3486 | |
b423cd4c | 3487 | /* |
3488 | * Find the banner; return if not found or "none"... | |
3489 | */ | |
ef416fc2 | 3490 | |
b423cd4c | 3491 | if (!name || !strcmp(name, "none") || |
3492 | (banner = cupsdFindBanner(name)) == NULL) | |
3493 | return (0); | |
ef416fc2 | 3494 | |
b423cd4c | 3495 | /* |
3496 | * Open the banner and job files... | |
3497 | */ | |
ef416fc2 | 3498 | |
b423cd4c | 3499 | if (add_file(con, job, banner->filetype, 0)) |
3500 | return (0); | |
ef416fc2 | 3501 | |
b423cd4c | 3502 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, |
3503 | job->num_files); | |
3504 | if ((out = cupsFileOpen(filename, "w")) == NULL) | |
3505 | { | |
3506 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3507 | "copy_banner: Unable to create banner job file %s - %s", | |
3508 | filename, strerror(errno)); | |
3509 | job->num_files --; | |
3510 | return (0); | |
3511 | } | |
3512 | ||
3513 | fchmod(cupsFileNumber(out), 0640); | |
3514 | fchown(cupsFileNumber(out), RunUser, Group); | |
ef416fc2 | 3515 | |
3516 | /* | |
b423cd4c | 3517 | * Try the localized banner file under the subdirectory... |
ef416fc2 | 3518 | */ |
3519 | ||
80ca4592 | 3520 | strlcpy(attrname, job->attrs->attrs->next->values[0].string.text, |
b423cd4c | 3521 | sizeof(attrname)); |
3522 | if (strlen(attrname) > 2 && attrname[2] == '-') | |
ef416fc2 | 3523 | { |
3524 | /* | |
b423cd4c | 3525 | * Convert ll-cc to ll_CC... |
ef416fc2 | 3526 | */ |
3527 | ||
b423cd4c | 3528 | attrname[2] = '_'; |
3529 | attrname[3] = toupper(attrname[3] & 255); | |
3530 | attrname[4] = toupper(attrname[4] & 255); | |
3531 | } | |
ef416fc2 | 3532 | |
b423cd4c | 3533 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
3534 | attrname, name); | |
ef416fc2 | 3535 | |
b423cd4c | 3536 | if (access(filename, 0) && strlen(attrname) > 2) |
ef416fc2 | 3537 | { |
3538 | /* | |
b423cd4c | 3539 | * Wasn't able to find "ll_CC" locale file; try the non-national |
3540 | * localization banner directory. | |
ef416fc2 | 3541 | */ |
3542 | ||
b423cd4c | 3543 | attrname[2] = '\0'; |
ef416fc2 | 3544 | |
b423cd4c | 3545 | snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir, |
3546 | attrname, name); | |
ef416fc2 | 3547 | } |
b423cd4c | 3548 | |
3549 | if (access(filename, 0)) | |
ef416fc2 | 3550 | { |
3551 | /* | |
b423cd4c | 3552 | * Use the non-localized banner file. |
ef416fc2 | 3553 | */ |
3554 | ||
b423cd4c | 3555 | snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name); |
3556 | } | |
ef416fc2 | 3557 | |
b423cd4c | 3558 | if ((in = cupsFileOpen(filename, "r")) == NULL) |
3559 | { | |
3560 | cupsFileClose(out); | |
3561 | unlink(filename); | |
3562 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3563 | "copy_banner: Unable to open banner template file %s - %s", | |
3564 | filename, strerror(errno)); | |
3565 | job->num_files --; | |
3566 | return (0); | |
ef416fc2 | 3567 | } |
3568 | ||
3569 | /* | |
b423cd4c | 3570 | * Parse the file to the end... |
ef416fc2 | 3571 | */ |
3572 | ||
b423cd4c | 3573 | while ((ch = cupsFileGetChar(in)) != EOF) |
3574 | if (ch == '{') | |
3575 | { | |
3576 | /* | |
3577 | * Get an attribute name... | |
3578 | */ | |
ef416fc2 | 3579 | |
b423cd4c | 3580 | for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;) |
3581 | if (!isalpha(ch & 255) && ch != '-' && ch != '?') | |
3582 | break; | |
3583 | else if (s < (attrname + sizeof(attrname) - 1)) | |
3584 | *s++ = ch; | |
3585 | else | |
3586 | break; | |
ef416fc2 | 3587 | |
b423cd4c | 3588 | *s = '\0'; |
3589 | ||
3590 | if (ch != '}') | |
3591 | { | |
3592 | /* | |
3593 | * Ignore { followed by stuff that is not an attribute name... | |
3594 | */ | |
3595 | ||
3596 | cupsFilePrintf(out, "{%s%c", attrname, ch); | |
3597 | continue; | |
3598 | } | |
ef416fc2 | 3599 | |
ef416fc2 | 3600 | /* |
b423cd4c | 3601 | * See if it is defined... |
ef416fc2 | 3602 | */ |
3603 | ||
b423cd4c | 3604 | if (attrname[0] == '?') |
3605 | s = attrname + 1; | |
3606 | else | |
3607 | s = attrname; | |
ef416fc2 | 3608 | |
b423cd4c | 3609 | if (!strcmp(s, "printer-name")) |
3610 | { | |
3611 | cupsFilePuts(out, job->dest); | |
3612 | continue; | |
ef416fc2 | 3613 | } |
b423cd4c | 3614 | else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL) |
3615 | { | |
3616 | /* | |
3617 | * See if we have a leading question mark... | |
3618 | */ | |
ef416fc2 | 3619 | |
b423cd4c | 3620 | if (attrname[0] != '?') |
3621 | { | |
3622 | /* | |
3623 | * Nope, write to file as-is; probably a PostScript procedure... | |
3624 | */ | |
ef416fc2 | 3625 | |
b423cd4c | 3626 | cupsFilePrintf(out, "{%s}", attrname); |
3627 | } | |
ef416fc2 | 3628 | |
b423cd4c | 3629 | continue; |
3630 | } | |
ef416fc2 | 3631 | |
b423cd4c | 3632 | /* |
3633 | * Output value(s)... | |
3634 | */ | |
ef416fc2 | 3635 | |
b423cd4c | 3636 | for (i = 0; i < attr->num_values; i ++) |
3637 | { | |
3638 | if (i) | |
3639 | cupsFilePutChar(out, ','); | |
ef416fc2 | 3640 | |
b423cd4c | 3641 | switch (attr->value_tag) |
3642 | { | |
3643 | case IPP_TAG_INTEGER : | |
3644 | case IPP_TAG_ENUM : | |
3645 | if (!strncmp(s, "time-at-", 8)) | |
3646 | cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer)); | |
3647 | else | |
3648 | cupsFilePrintf(out, "%d", attr->values[i].integer); | |
3649 | break; | |
ef416fc2 | 3650 | |
b423cd4c | 3651 | case IPP_TAG_BOOLEAN : |
3652 | cupsFilePrintf(out, "%d", attr->values[i].boolean); | |
3653 | break; | |
ef416fc2 | 3654 | |
b423cd4c | 3655 | case IPP_TAG_NOVALUE : |
3656 | cupsFilePuts(out, "novalue"); | |
3657 | break; | |
ef416fc2 | 3658 | |
b423cd4c | 3659 | case IPP_TAG_RANGE : |
3660 | cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower, | |
3661 | attr->values[i].range.upper); | |
3662 | break; | |
fa73b229 | 3663 | |
b423cd4c | 3664 | case IPP_TAG_RESOLUTION : |
3665 | cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres, | |
3666 | attr->values[i].resolution.yres, | |
3667 | attr->values[i].resolution.units == IPP_RES_PER_INCH ? | |
3668 | "dpi" : "dpc"); | |
3669 | break; | |
fa73b229 | 3670 | |
b423cd4c | 3671 | case IPP_TAG_URI : |
3672 | case IPP_TAG_STRING : | |
3673 | case IPP_TAG_TEXT : | |
3674 | case IPP_TAG_NAME : | |
3675 | case IPP_TAG_KEYWORD : | |
3676 | case IPP_TAG_CHARSET : | |
3677 | case IPP_TAG_LANGUAGE : | |
3678 | if (!strcasecmp(banner->filetype->type, "postscript")) | |
3679 | { | |
3680 | /* | |
3681 | * Need to quote strings for PS banners... | |
3682 | */ | |
fa73b229 | 3683 | |
b423cd4c | 3684 | const char *p; |
fa73b229 | 3685 | |
b423cd4c | 3686 | for (p = attr->values[i].string.text; *p; p ++) |
3687 | { | |
3688 | if (*p == '(' || *p == ')' || *p == '\\') | |
3689 | { | |
3690 | cupsFilePutChar(out, '\\'); | |
3691 | cupsFilePutChar(out, *p); | |
3692 | } | |
3693 | else if (*p < 32 || *p > 126) | |
3694 | cupsFilePrintf(out, "\\%03o", *p & 255); | |
3695 | else | |
3696 | cupsFilePutChar(out, *p); | |
3697 | } | |
3698 | } | |
3699 | else | |
3700 | cupsFilePuts(out, attr->values[i].string.text); | |
3701 | break; | |
fa73b229 | 3702 | |
b423cd4c | 3703 | default : |
3704 | break; /* anti-compiler-warning-code */ | |
3705 | } | |
3706 | } | |
3707 | } | |
3708 | else if (ch == '\\') /* Quoted char */ | |
3709 | { | |
3710 | ch = cupsFileGetChar(in); | |
fa73b229 | 3711 | |
b423cd4c | 3712 | if (ch != '{') /* Only do special handling for \{ */ |
3713 | cupsFilePutChar(out, '\\'); | |
fa73b229 | 3714 | |
b423cd4c | 3715 | cupsFilePutChar(out, ch); |
3716 | } | |
3717 | else | |
3718 | cupsFilePutChar(out, ch); | |
fa73b229 | 3719 | |
b423cd4c | 3720 | cupsFileClose(in); |
fa73b229 | 3721 | |
b423cd4c | 3722 | kbytes = (cupsFileTell(out) + 1023) / 1024; |
fa73b229 | 3723 | |
b423cd4c | 3724 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
3725 | IPP_TAG_INTEGER)) != NULL) | |
3726 | attr->values[0].integer += kbytes; | |
3727 | ||
3728 | cupsFileClose(out); | |
3729 | ||
3730 | return (kbytes); | |
fa73b229 | 3731 | } |
3732 | ||
3733 | ||
3734 | /* | |
b423cd4c | 3735 | * 'copy_file()' - Copy a PPD file or interface script... |
fa73b229 | 3736 | */ |
3737 | ||
b423cd4c | 3738 | static int /* O - 0 = success, -1 = error */ |
3739 | copy_file(const char *from, /* I - Source file */ | |
3740 | const char *to) /* I - Destination file */ | |
fa73b229 | 3741 | { |
b423cd4c | 3742 | cups_file_t *src, /* Source file */ |
3743 | *dst; /* Destination file */ | |
3744 | int bytes; /* Bytes to read/write */ | |
3745 | char buffer[2048]; /* Copy buffer */ | |
fa73b229 | 3746 | |
3747 | ||
b423cd4c | 3748 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to); |
3749 | ||
fa73b229 | 3750 | /* |
b423cd4c | 3751 | * Open the source and destination file for a copy... |
fa73b229 | 3752 | */ |
3753 | ||
b423cd4c | 3754 | if ((src = cupsFileOpen(from, "rb")) == NULL) |
3755 | return (-1); | |
fa73b229 | 3756 | |
b423cd4c | 3757 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
3758 | { | |
3759 | cupsFileClose(src); | |
3760 | return (-1); | |
3761 | } | |
fa73b229 | 3762 | |
b423cd4c | 3763 | /* |
3764 | * Copy the source file to the destination... | |
3765 | */ | |
fa73b229 | 3766 | |
b423cd4c | 3767 | while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0) |
3768 | if (cupsFileWrite(dst, buffer, bytes) < bytes) | |
3769 | { | |
3770 | cupsFileClose(src); | |
3771 | cupsFileClose(dst); | |
3772 | return (-1); | |
3773 | } | |
fa73b229 | 3774 | |
b423cd4c | 3775 | /* |
3776 | * Close both files and return... | |
3777 | */ | |
fa73b229 | 3778 | |
b423cd4c | 3779 | cupsFileClose(src); |
fa73b229 | 3780 | |
b423cd4c | 3781 | return (cupsFileClose(dst)); |
3782 | } | |
fa73b229 | 3783 | |
fa73b229 | 3784 | |
b423cd4c | 3785 | /* |
3786 | * 'copy_model()' - Copy a PPD model file, substituting default values | |
3787 | * as needed... | |
3788 | */ | |
e00b005a | 3789 | |
b423cd4c | 3790 | static int /* O - 0 = success, -1 = error */ |
3791 | copy_model(cupsd_client_t *con, /* I - Client connection */ | |
3792 | const char *from, /* I - Source file */ | |
3793 | const char *to) /* I - Destination file */ | |
3794 | { | |
3795 | fd_set *input; /* select() input set */ | |
3796 | struct timeval timeout; /* select() timeout */ | |
3797 | int maxfd; /* Maximum file descriptor for select() */ | |
3798 | char tempfile[1024]; /* Temporary PPD file */ | |
3799 | int tempfd; /* Temporary PPD file descriptor */ | |
3800 | int temppid; /* Process ID of cups-driverd */ | |
3801 | int temppipe[2]; /* Temporary pipes */ | |
3802 | char *argv[4], /* Command-line arguments */ | |
3803 | *envp[MAX_ENV]; /* Environment */ | |
3804 | cups_file_t *src, /* Source file */ | |
3805 | *dst; /* Destination file */ | |
3806 | int bytes, /* Bytes from pipe */ | |
3807 | total; /* Total bytes from pipe */ | |
3808 | char buffer[2048], /* Copy buffer */ | |
3809 | *ptr; /* Pointer into buffer */ | |
3810 | int i; /* Looping var */ | |
3811 | char option[PPD_MAX_NAME], /* Option name */ | |
3812 | choice[PPD_MAX_NAME]; /* Choice name */ | |
3813 | int num_defaults; /* Number of default options */ | |
3814 | ppd_default_t *defaults; /* Default options */ | |
3815 | char cups_protocol[PPD_MAX_LINE]; | |
3816 | /* cupsProtocol attribute */ | |
3817 | int have_letter, /* Have Letter size */ | |
3818 | have_a4; /* Have A4 size */ | |
3819 | #ifdef HAVE_LIBPAPER | |
3820 | char *paper_result; /* Paper size name from libpaper */ | |
3821 | char system_paper[64]; /* Paper size name buffer */ | |
3822 | #endif /* HAVE_LIBPAPER */ | |
e00b005a | 3823 | |
e00b005a | 3824 | |
b423cd4c | 3825 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
3826 | "copy_model(con=%p, from=\"%s\", to=\"%s\")", | |
3827 | con, from, to); | |
e00b005a | 3828 | |
b423cd4c | 3829 | /* |
3830 | * Run cups-driverd to get the PPD file... | |
3831 | */ | |
e00b005a | 3832 | |
b423cd4c | 3833 | argv[0] = "cups-driverd"; |
3834 | argv[1] = "cat"; | |
3835 | argv[2] = (char *)from; | |
3836 | argv[3] = NULL; | |
e00b005a | 3837 | |
b423cd4c | 3838 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
e00b005a | 3839 | |
b423cd4c | 3840 | snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin); |
3841 | snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd); | |
3842 | tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600); | |
3843 | if (tempfd < 0) | |
3844 | return (-1); | |
3845 | ||
3846 | cupsdOpenPipe(temppipe); | |
3847 | ||
3848 | if ((input = calloc(1, SetSize)) == NULL) | |
3849 | { | |
3850 | close(tempfd); | |
3851 | unlink(tempfile); | |
3852 | ||
3853 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3854 | "copy_model: Unable to allocate %d bytes for select()...", | |
3855 | SetSize); | |
3856 | return (-1); | |
e00b005a | 3857 | } |
3858 | ||
b423cd4c | 3859 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
3860 | "copy_model: Running \"cups-driverd cat %s\"...", from); | |
fa73b229 | 3861 | |
b423cd4c | 3862 | if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1], |
3863 | -1, 0, &temppid)) | |
fa73b229 | 3864 | { |
b423cd4c | 3865 | free(input); |
3866 | close(tempfd); | |
3867 | unlink(tempfile); | |
3868 | return (-1); | |
fa73b229 | 3869 | } |
3870 | ||
b423cd4c | 3871 | close(temppipe[1]); |
fa73b229 | 3872 | |
b423cd4c | 3873 | /* |
3874 | * Wait up to 30 seconds for the PPD file to be copied... | |
3875 | */ | |
fa73b229 | 3876 | |
b423cd4c | 3877 | total = 0; |
fa73b229 | 3878 | |
b423cd4c | 3879 | if (temppipe[0] > CGIPipes[0]) |
3880 | maxfd = temppipe[0] + 1; | |
3881 | else | |
3882 | maxfd = CGIPipes[0] + 1; | |
ef416fc2 | 3883 | |
b423cd4c | 3884 | for (;;) |
3885 | { | |
3886 | /* | |
3887 | * See if we have data ready... | |
3888 | */ | |
ef416fc2 | 3889 | |
b423cd4c | 3890 | bytes = 0; |
ef416fc2 | 3891 | |
b423cd4c | 3892 | FD_SET(temppipe[0], input); |
3893 | FD_SET(CGIPipes[0], input); | |
ef416fc2 | 3894 | |
b423cd4c | 3895 | timeout.tv_sec = 30; |
3896 | timeout.tv_usec = 0; | |
3897 | ||
3898 | if ((i = select(maxfd, input, NULL, NULL, &timeout)) < 0) | |
3899 | { | |
3900 | if (errno == EINTR) | |
3901 | continue; | |
3902 | else | |
3903 | break; | |
3904 | } | |
3905 | else if (i == 0) | |
ef416fc2 | 3906 | { |
3907 | /* | |
b423cd4c | 3908 | * We have timed out... |
ef416fc2 | 3909 | */ |
3910 | ||
b423cd4c | 3911 | break; |
ef416fc2 | 3912 | } |
b423cd4c | 3913 | |
3914 | if (FD_ISSET(temppipe[0], input)) | |
ef416fc2 | 3915 | { |
3916 | /* | |
b423cd4c | 3917 | * Read the PPD file from the pipe, and write it to the PPD file. |
ef416fc2 | 3918 | */ |
3919 | ||
b423cd4c | 3920 | if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0) |
3921 | { | |
3922 | if (write(tempfd, buffer, bytes) < bytes) | |
3923 | break; | |
ef416fc2 | 3924 | |
b423cd4c | 3925 | total += bytes; |
3926 | } | |
3927 | else | |
3928 | break; | |
ef416fc2 | 3929 | } |
b423cd4c | 3930 | |
3931 | if (FD_ISSET(CGIPipes[0], input)) | |
3932 | cupsdUpdateCGI(); | |
ef416fc2 | 3933 | } |
3934 | ||
b423cd4c | 3935 | close(temppipe[0]); |
3936 | close(tempfd); | |
ef416fc2 | 3937 | |
b423cd4c | 3938 | free(input); |
ef416fc2 | 3939 | |
b423cd4c | 3940 | if (!total) |
ef416fc2 | 3941 | { |
b423cd4c | 3942 | /* |
3943 | * No data from cups-deviced... | |
3944 | */ | |
3945 | ||
3946 | cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!"); | |
3947 | unlink(tempfile); | |
3948 | return (-1); | |
ef416fc2 | 3949 | } |
3950 | ||
b423cd4c | 3951 | /* |
3952 | * Read the source file and see what page sizes are supported... | |
3953 | */ | |
fa73b229 | 3954 | |
b423cd4c | 3955 | if ((src = cupsFileOpen(tempfile, "rb")) == NULL) |
3956 | { | |
3957 | unlink(tempfile); | |
3958 | return (-1); | |
3959 | } | |
fa73b229 | 3960 | |
b423cd4c | 3961 | have_letter = 0; |
3962 | have_a4 = 0; | |
fa73b229 | 3963 | |
b423cd4c | 3964 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
3965 | if (!strncmp(buffer, "*PageSize ", 10)) | |
3966 | { | |
3967 | /* | |
3968 | * Strip UI text and command data from the end of the line... | |
3969 | */ | |
fa73b229 | 3970 | |
b423cd4c | 3971 | if ((ptr = strchr(buffer + 10, '/')) != NULL) |
3972 | *ptr = '\0'; | |
3973 | if ((ptr = strchr(buffer + 10, ':')) != NULL) | |
3974 | *ptr = '\0'; | |
ef416fc2 | 3975 | |
b423cd4c | 3976 | for (ptr = buffer + 10; isspace(*ptr); ptr ++); |
ef416fc2 | 3977 | |
b423cd4c | 3978 | /* |
3979 | * Look for Letter and A4 page sizes... | |
3980 | */ | |
ef416fc2 | 3981 | |
b423cd4c | 3982 | if (!strcmp(ptr, "Letter")) |
3983 | have_letter = 1; | |
ef416fc2 | 3984 | |
b423cd4c | 3985 | if (!strcmp(ptr, "A4")) |
3986 | have_a4 = 1; | |
3987 | } | |
ef416fc2 | 3988 | |
b423cd4c | 3989 | cupsFileRewind(src); |
ef416fc2 | 3990 | |
3991 | /* | |
b423cd4c | 3992 | * Open the destination (if possible) and set the default options... |
ef416fc2 | 3993 | */ |
3994 | ||
b423cd4c | 3995 | num_defaults = 0; |
3996 | defaults = NULL; | |
3997 | cups_protocol[0] = '\0'; | |
ef416fc2 | 3998 | |
b423cd4c | 3999 | if ((dst = cupsFileOpen(to, "rb")) != NULL) |
ef416fc2 | 4000 | { |
4001 | /* | |
b423cd4c | 4002 | * Read all of the default lines from the old PPD... |
ef416fc2 | 4003 | */ |
4004 | ||
b423cd4c | 4005 | while (cupsFileGets(dst, buffer, sizeof(buffer))) |
4006 | if (!strncmp(buffer, "*Default", 8)) | |
4007 | { | |
4008 | /* | |
4009 | * Add the default option... | |
4010 | */ | |
ef416fc2 | 4011 | |
b423cd4c | 4012 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4013 | choice, sizeof(choice))) | |
4014 | num_defaults = ppd_add_default(option, choice, num_defaults, | |
4015 | &defaults); | |
4016 | } | |
4017 | else if (!strncmp(buffer, "*cupsProtocol:", 14)) | |
4018 | strlcpy(cups_protocol, buffer, sizeof(cups_protocol)); | |
ef416fc2 | 4019 | |
b423cd4c | 4020 | cupsFileClose(dst); |
ef416fc2 | 4021 | } |
b423cd4c | 4022 | #ifdef HAVE_LIBPAPER |
4023 | else if ((paper_result = systempapername()) != NULL) | |
4024 | { | |
4025 | /* | |
4026 | * Set the default media sizes from the systemwide default... | |
4027 | */ | |
ef416fc2 | 4028 | |
b423cd4c | 4029 | strlcpy(system_paper, paper_result, sizeof(system_paper)); |
4030 | system_paper[0] = toupper(system_paper[0] & 255); | |
ef416fc2 | 4031 | |
b423cd4c | 4032 | if ((!strcmp(system_paper, "Letter") && have_letter) || |
4033 | (!strcmp(system_paper, "A4") && have_a4)) | |
4034 | { | |
4035 | num_defaults = ppd_add_default("PageSize", system_paper, | |
4036 | num_defaults, &defaults); | |
4037 | num_defaults = ppd_add_default("PageRegion", system_paper, | |
4038 | num_defaults, &defaults); | |
4039 | num_defaults = ppd_add_default("PaperDimension", system_paper, | |
4040 | num_defaults, &defaults); | |
4041 | num_defaults = ppd_add_default("ImageableArea", system_paper, | |
4042 | num_defaults, &defaults); | |
4043 | } | |
ef416fc2 | 4044 | } |
b423cd4c | 4045 | #endif /* HAVE_LIBPAPER */ |
4046 | else | |
ef416fc2 | 4047 | { |
b423cd4c | 4048 | /* |
4049 | * Add the default media sizes... | |
4050 | * | |
4051 | * Note: These values are generally not valid for large-format devices | |
4052 | * like plotters, however it is probably safe to say that those | |
4053 | * users will configure the media size after initially adding | |
4054 | * the device anyways... | |
4055 | */ | |
4056 | ||
4057 | if (!DefaultLanguage || | |
4058 | !strcasecmp(DefaultLanguage, "C") || | |
4059 | !strcasecmp(DefaultLanguage, "POSIX") || | |
4060 | !strcasecmp(DefaultLanguage, "en") || | |
4061 | !strncasecmp(DefaultLanguage, "en_US", 5) || | |
4062 | !strncasecmp(DefaultLanguage, "en_CA", 5) || | |
4063 | !strncasecmp(DefaultLanguage, "fr_CA", 5)) | |
4064 | { | |
4065 | /* | |
4066 | * These are the only locales that will default to "letter" size... | |
4067 | */ | |
4068 | ||
4069 | if (have_letter) | |
4070 | { | |
4071 | num_defaults = ppd_add_default("PageSize", "Letter", num_defaults, | |
4072 | &defaults); | |
4073 | num_defaults = ppd_add_default("PageRegion", "Letter", num_defaults, | |
4074 | &defaults); | |
4075 | num_defaults = ppd_add_default("PaperDimension", "Letter", num_defaults, | |
4076 | &defaults); | |
4077 | num_defaults = ppd_add_default("ImageableArea", "Letter", num_defaults, | |
4078 | &defaults); | |
4079 | } | |
4080 | } | |
4081 | else if (have_a4) | |
4082 | { | |
4083 | /* | |
4084 | * The rest default to "a4" size... | |
4085 | */ | |
4086 | ||
4087 | num_defaults = ppd_add_default("PageSize", "A4", num_defaults, | |
4088 | &defaults); | |
4089 | num_defaults = ppd_add_default("PageRegion", "A4", num_defaults, | |
4090 | &defaults); | |
4091 | num_defaults = ppd_add_default("PaperDimension", "A4", num_defaults, | |
4092 | &defaults); | |
4093 | num_defaults = ppd_add_default("ImageableArea", "A4", num_defaults, | |
4094 | &defaults); | |
4095 | } | |
ef416fc2 | 4096 | } |
4097 | ||
4098 | /* | |
b423cd4c | 4099 | * Open the destination file for a copy... |
ef416fc2 | 4100 | */ |
4101 | ||
b423cd4c | 4102 | if ((dst = cupsFileOpen(to, "wb")) == NULL) |
ef416fc2 | 4103 | { |
b423cd4c | 4104 | if (num_defaults > 0) |
4105 | free(defaults); | |
4106 | ||
4107 | cupsFileClose(src); | |
4108 | unlink(tempfile); | |
4109 | return (-1); | |
ef416fc2 | 4110 | } |
4111 | ||
4112 | /* | |
b423cd4c | 4113 | * Copy the source file to the destination... |
ef416fc2 | 4114 | */ |
4115 | ||
b423cd4c | 4116 | while (cupsFileGets(src, buffer, sizeof(buffer))) |
ef416fc2 | 4117 | { |
b423cd4c | 4118 | if (!strncmp(buffer, "*Default", 8)) |
ef416fc2 | 4119 | { |
b423cd4c | 4120 | /* |
4121 | * Check for an previous default option choice... | |
4122 | */ | |
ef416fc2 | 4123 | |
b423cd4c | 4124 | if (!ppd_parse_line(buffer, option, sizeof(option), |
4125 | choice, sizeof(choice))) | |
ef416fc2 | 4126 | { |
b423cd4c | 4127 | for (i = 0; i < num_defaults; i ++) |
4128 | if (!strcmp(option, defaults[i].option)) | |
4129 | { | |
4130 | /* | |
4131 | * Substitute the previous choice... | |
4132 | */ | |
ef416fc2 | 4133 | |
b423cd4c | 4134 | snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, |
4135 | defaults[i].choice); | |
4136 | break; | |
4137 | } | |
4138 | } | |
ef416fc2 | 4139 | } |
b423cd4c | 4140 | |
4141 | cupsFilePrintf(dst, "%s\n", buffer); | |
ef416fc2 | 4142 | } |
4143 | ||
b423cd4c | 4144 | if (cups_protocol[0]) |
4145 | cupsFilePrintf(dst, "%s\n", cups_protocol); | |
4146 | ||
4147 | if (num_defaults > 0) | |
4148 | free(defaults); | |
4149 | ||
ef416fc2 | 4150 | /* |
b423cd4c | 4151 | * Close both files and return... |
ef416fc2 | 4152 | */ |
4153 | ||
b423cd4c | 4154 | cupsFileClose(src); |
4155 | ||
4156 | unlink(tempfile); | |
4157 | ||
4158 | return (cupsFileClose(dst)); | |
4159 | } | |
4160 | ||
4161 | ||
4162 | /* | |
4163 | * 'copy_job_attrs()' - Copy job attributes. | |
4164 | */ | |
ef416fc2 | 4165 | |
b423cd4c | 4166 | static void |
4167 | copy_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
4168 | cupsd_job_t *job, /* I - Job */ | |
4169 | cups_array_t *ra) /* I - Requested attributes array */ | |
4170 | { | |
4171 | char job_uri[HTTP_MAX_URI]; /* Job URI */ | |
ef416fc2 | 4172 | |
ef416fc2 | 4173 | |
4174 | /* | |
b423cd4c | 4175 | * Send the requested attributes for each job... |
ef416fc2 | 4176 | */ |
4177 | ||
b423cd4c | 4178 | httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, |
4179 | con->servername, con->serverport, "/jobs/%d", | |
4180 | job->id); | |
ef416fc2 | 4181 | |
b423cd4c | 4182 | if (!ra || cupsArrayFind(ra, "job-more-info")) |
4183 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
4184 | "job-more-info", NULL, job_uri); | |
ef416fc2 | 4185 | |
b423cd4c | 4186 | if (job->state_value > IPP_JOB_PROCESSING && |
4187 | (!ra || cupsArrayFind(ra, "job-preserved"))) | |
4188 | ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved", | |
4189 | job->num_files > 0); | |
ef416fc2 | 4190 | |
b423cd4c | 4191 | if (!ra || cupsArrayFind(ra, "job-printer-up-time")) |
4192 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, | |
4193 | "job-printer-up-time", time(NULL)); | |
ef416fc2 | 4194 | |
b423cd4c | 4195 | if (!ra || cupsArrayFind(ra, "job-state-reasons")) |
4196 | add_job_state_reasons(con, job); | |
bd7854cb | 4197 | |
b423cd4c | 4198 | if (!ra || cupsArrayFind(ra, "job-uri")) |
4199 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, | |
4200 | "job-uri", NULL, job_uri); | |
ef416fc2 | 4201 | |
b423cd4c | 4202 | copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0); |
4203 | } | |
ef416fc2 | 4204 | |
ef416fc2 | 4205 | |
b423cd4c | 4206 | /* |
4207 | * 'copy_printer_attrs()' - Copy printer attributes. | |
4208 | */ | |
ef416fc2 | 4209 | |
b423cd4c | 4210 | static void |
4211 | copy_printer_attrs( | |
4212 | cupsd_client_t *con, /* I - Client connection */ | |
4213 | cupsd_printer_t *printer, /* I - Printer */ | |
4214 | cups_array_t *ra) /* I - Requested attributes array */ | |
4215 | { | |
4216 | char printer_uri[HTTP_MAX_URI]; | |
4217 | /* Printer URI */ | |
4218 | time_t curtime; /* Current time */ | |
4219 | int i; /* Looping var */ | |
4220 | ipp_attribute_t *history; /* History collection */ | |
ef416fc2 | 4221 | |
ef416fc2 | 4222 | |
b423cd4c | 4223 | /* |
4224 | * Copy the printer attributes to the response using requested-attributes | |
4225 | * and document-format attributes that may be provided by the client. | |
4226 | */ | |
ef416fc2 | 4227 | |
b423cd4c | 4228 | curtime = time(NULL); |
ef416fc2 | 4229 | |
b423cd4c | 4230 | #ifdef __APPLE__ |
4231 | if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) && | |
4232 | printer->recoverable) | |
4233 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
4234 | "com.apple.print.recoverable-message", NULL, | |
4235 | printer->recoverable); | |
4236 | #endif /* __APPLE__ */ | |
ef416fc2 | 4237 | |
b423cd4c | 4238 | if (!ra || cupsArrayFind(ra, "printer-current-time")) |
4239 | ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", | |
4240 | ippTimeToDate(curtime)); | |
ef416fc2 | 4241 | |
b423cd4c | 4242 | if (!ra || cupsArrayFind(ra, "printer-error-policy")) |
4243 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
4244 | "printer-error-policy", NULL, printer->error_policy); | |
ef416fc2 | 4245 | |
b423cd4c | 4246 | if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs")) |
4247 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", | |
4248 | printer->accepting); | |
ef416fc2 | 4249 | |
b423cd4c | 4250 | if (!ra || cupsArrayFind(ra, "printer-is-shared")) |
4251 | ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", | |
4252 | printer->shared); | |
ef416fc2 | 4253 | |
b423cd4c | 4254 | if (!ra || cupsArrayFind(ra, "printer-op-policy")) |
4255 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
4256 | "printer-op-policy", NULL, printer->op_policy); | |
4257 | ||
4258 | if (!ra || cupsArrayFind(ra, "printer-state")) | |
4259 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
4260 | printer->state); | |
4261 | ||
4262 | if (!ra || cupsArrayFind(ra, "printer-state-change-time")) | |
4263 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
4264 | "printer-state-change-time", printer->state_time); | |
4265 | ||
4266 | if (MaxPrinterHistory > 0 && printer->num_history > 0 && | |
4267 | cupsArrayFind(ra, "printer-state-history")) | |
ef416fc2 | 4268 | { |
4269 | /* | |
b423cd4c | 4270 | * Printer history is only sent if specifically requested, so that |
4271 | * older CUPS/IPP clients won't barf on the collection attributes. | |
ef416fc2 | 4272 | */ |
4273 | ||
b423cd4c | 4274 | history = ippAddCollections(con->response, IPP_TAG_PRINTER, |
4275 | "printer-state-history", | |
4276 | printer->num_history, NULL); | |
4277 | ||
4278 | for (i = 0; i < printer->num_history; i ++) | |
4279 | copy_attrs(history->values[i].collection = ippNew(), printer->history[i], | |
4280 | NULL, IPP_TAG_ZERO, 0); | |
ef416fc2 | 4281 | } |
4282 | ||
b423cd4c | 4283 | if (!ra || cupsArrayFind(ra, "printer-state-message")) |
4284 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, | |
4285 | "printer-state-message", NULL, printer->state_message); | |
ef416fc2 | 4286 | |
b423cd4c | 4287 | if (!ra || cupsArrayFind(ra, "printer-state-reasons")) |
4288 | add_printer_state_reasons(con, printer); | |
ef416fc2 | 4289 | |
b423cd4c | 4290 | if (!ra || cupsArrayFind(ra, "printer-type")) |
4291 | { | |
4292 | int type; /* printer-type value */ | |
ef416fc2 | 4293 | |
ef416fc2 | 4294 | |
ef416fc2 | 4295 | /* |
b423cd4c | 4296 | * Add the CUPS-specific printer-type attribute... |
ef416fc2 | 4297 | */ |
4298 | ||
b423cd4c | 4299 | type = printer->type; |
4300 | ||
4301 | if (printer == DefaultPrinter) | |
4302 | type |= CUPS_PRINTER_DEFAULT; | |
4303 | ||
4304 | if (!printer->accepting) | |
4305 | type |= CUPS_PRINTER_REJECTING; | |
4306 | ||
4307 | if (!printer->shared) | |
4308 | type |= CUPS_PRINTER_NOT_SHARED; | |
4309 | ||
4310 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", | |
4311 | type); | |
ef416fc2 | 4312 | } |
ef416fc2 | 4313 | |
b423cd4c | 4314 | if (!ra || cupsArrayFind(ra, "printer-up-time")) |
4315 | ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, | |
4316 | "printer-up-time", curtime); | |
ef416fc2 | 4317 | |
b423cd4c | 4318 | if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) && |
4319 | !ippFindAttribute(printer->attrs, "printer-uri-supported", | |
4320 | IPP_TAG_URI)) | |
ef416fc2 | 4321 | { |
b423cd4c | 4322 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), |
4323 | "ipp", NULL, con->servername, con->serverport, | |
4324 | "/printers/%s", printer->name); | |
4325 | ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, | |
4326 | "printer-uri-supported", NULL, printer_uri); | |
4327 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", | |
4328 | printer_uri); | |
4329 | } | |
ef416fc2 | 4330 | |
b423cd4c | 4331 | if (!ra || cupsArrayFind(ra, "queued-job-count")) |
4332 | add_queued_job_count(con, printer); | |
ef416fc2 | 4333 | |
b423cd4c | 4334 | copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0); |
4335 | copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY); | |
4336 | } | |
ef416fc2 | 4337 | |
ef416fc2 | 4338 | |
b423cd4c | 4339 | /* |
4340 | * 'copy_subscription_attrs()' - Copy subscription attributes. | |
4341 | */ | |
ef416fc2 | 4342 | |
b423cd4c | 4343 | static void |
4344 | copy_subscription_attrs( | |
4345 | cupsd_client_t *con, /* I - Client connection */ | |
4346 | cupsd_subscription_t *sub, /* I - Subscription */ | |
4347 | cups_array_t *ra) /* I - Requested attributes array */ | |
4348 | { | |
4349 | ipp_attribute_t *attr; /* Current attribute */ | |
4350 | char printer_uri[HTTP_MAX_URI]; | |
4351 | /* Printer URI */ | |
4352 | int count; /* Number of events */ | |
4353 | unsigned mask; /* Current event mask */ | |
4354 | const char *name; /* Current event name */ | |
ef416fc2 | 4355 | |
ef416fc2 | 4356 | |
b423cd4c | 4357 | /* |
4358 | * Copy the subscription attributes to the response using the | |
4359 | * requested-attributes attribute that may be provided by the client. | |
4360 | */ | |
ef416fc2 | 4361 | |
b423cd4c | 4362 | if (!ra || cupsArrayFind(ra, "notify-events")) |
4363 | { | |
4364 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
4365 | { | |
4366 | /* | |
4367 | * Simple event list... | |
4368 | */ | |
ef416fc2 | 4369 | |
b423cd4c | 4370 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, |
4371 | IPP_TAG_KEYWORD | IPP_TAG_COPY, | |
4372 | "notify-events", NULL, name); | |
4373 | } | |
4374 | else | |
4375 | { | |
4376 | /* | |
4377 | * Complex event list... | |
4378 | */ | |
ef416fc2 | 4379 | |
b423cd4c | 4380 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
4381 | if (sub->mask & mask) | |
4382 | count ++; | |
ef416fc2 | 4383 | |
b423cd4c | 4384 | attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, |
4385 | IPP_TAG_KEYWORD | IPP_TAG_COPY, | |
4386 | "notify-events", count, NULL, NULL); | |
ef416fc2 | 4387 | |
b423cd4c | 4388 | for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1) |
4389 | if (sub->mask & mask) | |
4390 | { | |
4391 | attr->values[count].string.text = | |
4392 | (char *)cupsdEventName((cupsd_eventmask_t)mask); | |
ef416fc2 | 4393 | |
b423cd4c | 4394 | count ++; |
4395 | } | |
ef416fc2 | 4396 | } |
b423cd4c | 4397 | } |
ef416fc2 | 4398 | |
b423cd4c | 4399 | if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id"))) |
4400 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4401 | "notify-job-id", sub->job->id); | |
ef416fc2 | 4402 | |
b423cd4c | 4403 | if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))) |
4404 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4405 | "notify-lease-duration", sub->lease); | |
ef416fc2 | 4406 | |
b423cd4c | 4407 | if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri"))) |
4408 | { | |
4409 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), | |
4410 | "ipp", NULL, con->servername, con->serverport, | |
4411 | "/printers/%s", sub->dest->name); | |
4412 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
4413 | "notify-printer-uri", NULL, printer_uri); | |
ef416fc2 | 4414 | } |
ef416fc2 | 4415 | |
b423cd4c | 4416 | if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))) |
4417 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | |
4418 | "notify-recipient-uri", NULL, sub->recipient); | |
4419 | else if (!ra || cupsArrayFind(ra, "notify-pull-method")) | |
4420 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | |
4421 | "notify-pull-method", NULL, "ippget"); | |
ef416fc2 | 4422 | |
b423cd4c | 4423 | if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")) |
4424 | ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME, | |
4425 | "notify-subscriber-user-name", NULL, sub->owner); | |
ef416fc2 | 4426 | |
b423cd4c | 4427 | if (!ra || cupsArrayFind(ra, "notify-subscription-id")) |
4428 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4429 | "notify-subscription-id", sub->id); | |
ef416fc2 | 4430 | |
b423cd4c | 4431 | if (!ra || cupsArrayFind(ra, "notify-time-interval")) |
4432 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4433 | "notify-time-interval", sub->interval); | |
ef416fc2 | 4434 | |
b423cd4c | 4435 | if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data"))) |
4436 | ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data", | |
4437 | sub->user_data, sub->user_data_len); | |
4438 | } | |
ef416fc2 | 4439 | |
ef416fc2 | 4440 | |
b423cd4c | 4441 | /* |
4442 | * 'create_job()' - Print a file to a printer or class. | |
4443 | */ | |
4444 | ||
4445 | static void | |
4446 | create_job(cupsd_client_t *con, /* I - Client connection */ | |
4447 | ipp_attribute_t *uri) /* I - Printer URI */ | |
4448 | { | |
4449 | cupsd_job_t *job; /* New job */ | |
4450 | ||
ef416fc2 | 4451 | |
b423cd4c | 4452 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con, |
4453 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 4454 | |
4455 | /* | |
b423cd4c | 4456 | * Create the job object... |
ef416fc2 | 4457 | */ |
4458 | ||
80ca4592 | 4459 | if ((job = add_job(con, uri, NULL, NULL)) == NULL) |
b423cd4c | 4460 | return; |
ef416fc2 | 4461 | |
4462 | /* | |
4463 | * Save and log the job... | |
4464 | */ | |
4465 | ||
4466 | cupsdSaveJob(job); | |
4467 | ||
4468 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d created on \"%s\" by \"%s\".", | |
4469 | job->id, job->dest, job->username); | |
ef416fc2 | 4470 | } |
4471 | ||
4472 | ||
4473 | /* | |
4474 | * 'create_requested_array()' - Create an array for the requested-attributes. | |
4475 | */ | |
4476 | ||
4477 | static cups_array_t * /* O - Array of attributes or NULL */ | |
4478 | create_requested_array(ipp_t *request) /* I - IPP request */ | |
4479 | { | |
4480 | int i; /* Looping var */ | |
4481 | ipp_attribute_t *requested; /* requested-attributes attribute */ | |
4482 | cups_array_t *ra; /* Requested attributes array */ | |
4483 | char *value; /* Current value */ | |
4484 | ||
4485 | ||
4486 | /* | |
4487 | * Get the requested-attributes attribute, and return NULL if we don't | |
4488 | * have one... | |
4489 | */ | |
4490 | ||
4491 | if ((requested = ippFindAttribute(request, "requested-attributes", | |
4492 | IPP_TAG_KEYWORD)) == NULL) | |
4493 | return (NULL); | |
4494 | ||
4495 | /* | |
4496 | * If the attribute contains a single "all" keyword, return NULL... | |
4497 | */ | |
4498 | ||
4499 | if (requested->num_values == 1 && | |
4500 | !strcmp(requested->values[0].string.text, "all")) | |
4501 | return (NULL); | |
4502 | ||
4503 | /* | |
4504 | * Create an array using "strcmp" as the comparison function... | |
4505 | */ | |
4506 | ||
4507 | ra = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
4508 | ||
4509 | for (i = 0; i < requested->num_values; i ++) | |
4510 | { | |
4511 | value = requested->values[i].string.text; | |
4512 | ||
4513 | if (!strcmp(value, "job-template")) | |
4514 | { | |
4515 | cupsArrayAdd(ra, "copies"); | |
4516 | cupsArrayAdd(ra, "copies-default"); | |
4517 | cupsArrayAdd(ra, "copies-supported"); | |
4518 | cupsArrayAdd(ra, "finishings"); | |
4519 | cupsArrayAdd(ra, "finishings-default"); | |
4520 | cupsArrayAdd(ra, "finishings-supported"); | |
4521 | cupsArrayAdd(ra, "job-hold-until"); | |
4522 | cupsArrayAdd(ra, "job-hold-until-default"); | |
4523 | cupsArrayAdd(ra, "job-hold-until-supported"); | |
4524 | cupsArrayAdd(ra, "job-priority"); | |
4525 | cupsArrayAdd(ra, "job-priority-default"); | |
4526 | cupsArrayAdd(ra, "job-priority-supported"); | |
4527 | cupsArrayAdd(ra, "job-sheets"); | |
4528 | cupsArrayAdd(ra, "job-sheets-default"); | |
4529 | cupsArrayAdd(ra, "job-sheets-supported"); | |
4530 | cupsArrayAdd(ra, "media"); | |
4531 | cupsArrayAdd(ra, "media-default"); | |
4532 | cupsArrayAdd(ra, "media-supported"); | |
4533 | cupsArrayAdd(ra, "multiple-document-handling"); | |
4534 | cupsArrayAdd(ra, "multiple-document-handling-default"); | |
4535 | cupsArrayAdd(ra, "multiple-document-handling-supported"); | |
4536 | cupsArrayAdd(ra, "number-up"); | |
4537 | cupsArrayAdd(ra, "number-up-default"); | |
4538 | cupsArrayAdd(ra, "number-up-supported"); | |
4539 | cupsArrayAdd(ra, "orientation-requested"); | |
4540 | cupsArrayAdd(ra, "orientation-requested-default"); | |
4541 | cupsArrayAdd(ra, "orientation-requested-supported"); | |
4542 | cupsArrayAdd(ra, "page-ranges"); | |
4543 | cupsArrayAdd(ra, "page-ranges-supported"); | |
4544 | cupsArrayAdd(ra, "printer-resolution"); | |
4545 | cupsArrayAdd(ra, "printer-resolution-default"); | |
4546 | cupsArrayAdd(ra, "printer-resolution-supported"); | |
4547 | cupsArrayAdd(ra, "print-quality"); | |
4548 | cupsArrayAdd(ra, "print-quality-default"); | |
4549 | cupsArrayAdd(ra, "print-quality-supported"); | |
4550 | cupsArrayAdd(ra, "sides"); | |
4551 | cupsArrayAdd(ra, "sides-default"); | |
4552 | cupsArrayAdd(ra, "sides-supported"); | |
4553 | } | |
4554 | else if (!strcmp(value, "job-description")) | |
4555 | { | |
4556 | cupsArrayAdd(ra, "date-time-at-completed"); | |
4557 | cupsArrayAdd(ra, "date-time-at-creation"); | |
4558 | cupsArrayAdd(ra, "date-time-at-processing"); | |
4559 | cupsArrayAdd(ra, "job-detailed-status-message"); | |
4560 | cupsArrayAdd(ra, "job-document-access-errors"); | |
4561 | cupsArrayAdd(ra, "job-id"); | |
4562 | cupsArrayAdd(ra, "job-impressions"); | |
4563 | cupsArrayAdd(ra, "job-impressions-completed"); | |
4564 | cupsArrayAdd(ra, "job-k-octets"); | |
4565 | cupsArrayAdd(ra, "job-k-octets-processed"); | |
4566 | cupsArrayAdd(ra, "job-media-sheets"); | |
4567 | cupsArrayAdd(ra, "job-media-sheets-completed"); | |
4568 | cupsArrayAdd(ra, "job-message-from-operator"); | |
4569 | cupsArrayAdd(ra, "job-more-info"); | |
4570 | cupsArrayAdd(ra, "job-name"); | |
4571 | cupsArrayAdd(ra, "job-originating-user-name"); | |
4572 | cupsArrayAdd(ra, "job-printer-up-time"); | |
4573 | cupsArrayAdd(ra, "job-printer-uri"); | |
4574 | cupsArrayAdd(ra, "job-state"); | |
4575 | cupsArrayAdd(ra, "job-state-message"); | |
4576 | cupsArrayAdd(ra, "job-state-reasons"); | |
4577 | cupsArrayAdd(ra, "job-uri"); | |
4578 | cupsArrayAdd(ra, "number-of-documents"); | |
4579 | cupsArrayAdd(ra, "number-of-intervening-jobs"); | |
4580 | cupsArrayAdd(ra, "output-device-assigned"); | |
4581 | cupsArrayAdd(ra, "time-at-completed"); | |
4582 | cupsArrayAdd(ra, "time-at-creation"); | |
4583 | cupsArrayAdd(ra, "time-at-processing"); | |
4584 | } | |
4585 | else if (!strcmp(value, "printer-description")) | |
4586 | { | |
4587 | cupsArrayAdd(ra, "charset-configured"); | |
4588 | cupsArrayAdd(ra, "charset-supported"); | |
4589 | cupsArrayAdd(ra, "color-supported"); | |
4590 | cupsArrayAdd(ra, "compression-supported"); | |
4591 | cupsArrayAdd(ra, "document-format-default"); | |
4592 | cupsArrayAdd(ra, "document-format-supported"); | |
4593 | cupsArrayAdd(ra, "generated-natural-language-supported"); | |
4594 | cupsArrayAdd(ra, "ipp-versions-supported"); | |
4595 | cupsArrayAdd(ra, "job-impressions-supported"); | |
4596 | cupsArrayAdd(ra, "job-k-octets-supported"); | |
4597 | cupsArrayAdd(ra, "job-media-sheets-supported"); | |
4598 | cupsArrayAdd(ra, "multiple-document-jobs-supported"); | |
4599 | cupsArrayAdd(ra, "multiple-operation-time-out"); | |
4600 | cupsArrayAdd(ra, "natural-language-configured"); | |
4601 | cupsArrayAdd(ra, "notify-attributes-supported"); | |
4602 | cupsArrayAdd(ra, "notify-lease-duration-default"); | |
4603 | cupsArrayAdd(ra, "notify-lease-duration-supported"); | |
4604 | cupsArrayAdd(ra, "notify-max-events-supported"); | |
4605 | cupsArrayAdd(ra, "notify-notify-events-default"); | |
4606 | cupsArrayAdd(ra, "notify-notify-events-supported"); | |
4607 | cupsArrayAdd(ra, "notify-pull-method-supported"); | |
4608 | cupsArrayAdd(ra, "notify-schemes-supported"); | |
4609 | cupsArrayAdd(ra, "operations-supported"); | |
4610 | cupsArrayAdd(ra, "pages-per-minute"); | |
4611 | cupsArrayAdd(ra, "pages-per-minute-color"); | |
4612 | cupsArrayAdd(ra, "pdl-override-supported"); | |
4613 | cupsArrayAdd(ra, "printer-current-time"); | |
4614 | cupsArrayAdd(ra, "printer-driver-installer"); | |
4615 | cupsArrayAdd(ra, "printer-info"); | |
4616 | cupsArrayAdd(ra, "printer-is-accepting-jobs"); | |
4617 | cupsArrayAdd(ra, "printer-location"); | |
4618 | cupsArrayAdd(ra, "printer-make-and-model"); | |
4619 | cupsArrayAdd(ra, "printer-message-from-operator"); | |
4620 | cupsArrayAdd(ra, "printer-more-info"); | |
4621 | cupsArrayAdd(ra, "printer-more-info-manufacturer"); | |
4622 | cupsArrayAdd(ra, "printer-name"); | |
4623 | cupsArrayAdd(ra, "printer-state"); | |
4624 | cupsArrayAdd(ra, "printer-state-message"); | |
4625 | cupsArrayAdd(ra, "printer-state-reasons"); | |
4626 | cupsArrayAdd(ra, "printer-up-time"); | |
4627 | cupsArrayAdd(ra, "printer-uri-supported"); | |
4628 | cupsArrayAdd(ra, "queued-job-count"); | |
4629 | cupsArrayAdd(ra, "reference-uri-schemes-supported"); | |
4630 | cupsArrayAdd(ra, "uri-authentication-supported"); | |
4631 | cupsArrayAdd(ra, "uri-security-supported"); | |
4632 | } | |
4633 | else if (!strcmp(value, "subscription-template")) | |
4634 | { | |
4635 | cupsArrayAdd(ra, "notify-attributes"); | |
4636 | cupsArrayAdd(ra, "notify-charset"); | |
4637 | cupsArrayAdd(ra, "notify-events"); | |
4638 | cupsArrayAdd(ra, "notify-lease-duration"); | |
4639 | cupsArrayAdd(ra, "notify-natural-language"); | |
4640 | cupsArrayAdd(ra, "notify-pull-method"); | |
4641 | cupsArrayAdd(ra, "notify-recipient-uri"); | |
4642 | cupsArrayAdd(ra, "notify-time-interval"); | |
4643 | cupsArrayAdd(ra, "notify-user-data"); | |
4644 | } | |
4645 | else | |
4646 | cupsArrayAdd(ra, value); | |
4647 | } | |
4648 | ||
4649 | return (ra); | |
4650 | } | |
4651 | ||
4652 | ||
4653 | /* | |
4654 | * 'create_subscription()' - Create a notification subscription. | |
4655 | */ | |
4656 | ||
4657 | static void | |
4658 | create_subscription( | |
4659 | cupsd_client_t *con, /* I - Client connection */ | |
4660 | ipp_attribute_t *uri) /* I - Printer URI */ | |
4661 | { | |
4662 | http_status_t status; /* Policy status */ | |
4663 | int i; /* Looping var */ | |
4664 | ipp_attribute_t *attr; /* Current attribute */ | |
4665 | const char *dest; /* Destination */ | |
4666 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
4667 | char method[HTTP_MAX_URI], | |
4668 | /* Method portion of URI */ | |
4669 | userpass[HTTP_MAX_URI], | |
4670 | /* Username portion of URI */ | |
4671 | host[HTTP_MAX_URI], | |
4672 | /* Host portion of URI */ | |
4673 | resource[HTTP_MAX_URI]; | |
4674 | /* Resource portion of URI */ | |
4675 | int port; /* Port portion of URI */ | |
4676 | cupsd_printer_t *printer; /* Printer/class */ | |
4677 | cupsd_job_t *job; /* Job */ | |
4678 | int jobid; /* Job ID */ | |
4679 | cupsd_subscription_t *sub; /* Subscription object */ | |
4680 | const char *username, /* requesting-user-name or authenticated username */ | |
4681 | *recipient, /* notify-recipient-uri */ | |
4682 | *pullmethod; /* notify-pull-method */ | |
4683 | ipp_attribute_t *user_data; /* notify-user-data */ | |
4684 | int interval, /* notify-time-interval */ | |
4685 | lease; /* notify-lease-duration */ | |
4686 | unsigned mask; /* notify-events */ | |
4687 | ||
4688 | ||
bd7854cb | 4689 | #ifdef DEBUG |
4690 | for (attr = con->request->attrs; attr; attr = attr->next) | |
4691 | { | |
4692 | if (attr->group_tag != IPP_TAG_ZERO) | |
4693 | cupsdLogMessage(CUPSD_LOG_DEBUG, "g%04x v%04x %s", attr->group_tag, | |
4694 | attr->value_tag, attr->name); | |
4695 | else | |
4696 | cupsdLogMessage(CUPSD_LOG_DEBUG, "----SEP----"); | |
4697 | } | |
4698 | #endif /* DEBUG */ | |
4699 | ||
ef416fc2 | 4700 | /* |
4701 | * Is the destination valid? | |
4702 | */ | |
4703 | ||
fa73b229 | 4704 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
4705 | "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")", | |
4706 | con, con->http.fd, uri->values[0].string.text); | |
4707 | ||
a4d04587 | 4708 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
4709 | sizeof(method), userpass, sizeof(userpass), host, | |
4710 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 4711 | |
4712 | if (!strcmp(resource, "/")) | |
4713 | { | |
4714 | dest = NULL; | |
4715 | dtype = (cups_ptype_t)0; | |
4716 | printer = NULL; | |
4717 | } | |
4718 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) | |
4719 | { | |
4720 | dest = NULL; | |
4721 | dtype = (cups_ptype_t)0; | |
4722 | printer = NULL; | |
4723 | } | |
4724 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) | |
4725 | { | |
4726 | dest = NULL; | |
4727 | dtype = CUPS_PRINTER_CLASS; | |
4728 | printer = NULL; | |
4729 | } | |
4730 | else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
4731 | { | |
4732 | /* | |
4733 | * Bad URI... | |
4734 | */ | |
4735 | ||
4736 | send_ipp_status(con, IPP_NOT_FOUND, | |
4737 | _("The printer or class was not found.")); | |
4738 | return; | |
4739 | } | |
4740 | ||
4741 | /* | |
4742 | * Check policy... | |
4743 | */ | |
4744 | ||
4745 | if (printer) | |
4746 | { | |
4747 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
4748 | { | |
4749 | send_http_error(con, status); | |
4750 | return; | |
4751 | } | |
4752 | } | |
4753 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
4754 | { | |
4755 | send_http_error(con, status); | |
4756 | return; | |
4757 | } | |
4758 | ||
4759 | /* | |
4760 | * Get the user that is requesting the subscription... | |
4761 | */ | |
4762 | ||
e00b005a | 4763 | username = get_username(con); |
ef416fc2 | 4764 | |
4765 | /* | |
4766 | * Find the first subscription group attribute; return if we have | |
4767 | * none... | |
4768 | */ | |
4769 | ||
4770 | for (attr = con->request->attrs; attr; attr = attr->next) | |
4771 | if (attr->group_tag == IPP_TAG_SUBSCRIPTION) | |
4772 | break; | |
4773 | ||
4774 | if (!attr) | |
4775 | { | |
4776 | send_ipp_status(con, IPP_BAD_REQUEST, | |
4777 | _("No subscription attributes in request!")); | |
4778 | return; | |
4779 | } | |
4780 | ||
4781 | /* | |
4782 | * Process the subscription attributes in the request... | |
4783 | */ | |
4784 | ||
fa73b229 | 4785 | con->response->request.status.status_code = IPP_BAD_REQUEST; |
4786 | ||
ef416fc2 | 4787 | while (attr) |
4788 | { | |
4789 | recipient = NULL; | |
4790 | pullmethod = NULL; | |
4791 | user_data = NULL; | |
4792 | interval = 0; | |
4793 | lease = DefaultLeaseDuration; | |
4794 | jobid = 0; | |
4795 | mask = CUPSD_EVENT_NONE; | |
4796 | ||
4797 | while (attr && attr->group_tag != IPP_TAG_ZERO) | |
4798 | { | |
4799 | if (!strcmp(attr->name, "notify-recipient") && | |
4800 | attr->value_tag == IPP_TAG_URI) | |
4801 | recipient = attr->values[0].string.text; | |
4802 | else if (!strcmp(attr->name, "notify-pull-method") && | |
4803 | attr->value_tag == IPP_TAG_KEYWORD) | |
4804 | pullmethod = attr->values[0].string.text; | |
4805 | else if (!strcmp(attr->name, "notify-charset") && | |
4806 | attr->value_tag == IPP_TAG_CHARSET && | |
4807 | strcmp(attr->values[0].string.text, "us-ascii") && | |
4808 | strcmp(attr->values[0].string.text, "utf-8")) | |
4809 | { | |
4810 | send_ipp_status(con, IPP_CHARSET, | |
4811 | _("Character set \"%s\" not supported!"), | |
4812 | attr->values[0].string.text); | |
4813 | return; | |
4814 | } | |
4815 | else if (!strcmp(attr->name, "notify-natural-language") && | |
4816 | (attr->value_tag != IPP_TAG_LANGUAGE || | |
4817 | strcmp(attr->values[0].string.text, DefaultLanguage))) | |
4818 | { | |
4819 | send_ipp_status(con, IPP_CHARSET, | |
4820 | _("Language \"%s\" not supported!"), | |
4821 | attr->values[0].string.text); | |
4822 | return; | |
4823 | } | |
4824 | else if (!strcmp(attr->name, "notify-user-data") && | |
4825 | attr->value_tag == IPP_TAG_STRING) | |
4826 | { | |
4827 | if (attr->num_values > 1 || attr->values[0].unknown.length > 63) | |
4828 | { | |
4829 | send_ipp_status(con, IPP_REQUEST_VALUE, | |
4830 | _("The notify-user-data value is too large " | |
4831 | "(%d > 63 octets)!"), | |
4832 | attr->values[0].unknown.length); | |
4833 | return; | |
4834 | } | |
4835 | ||
4836 | user_data = attr; | |
4837 | } | |
4838 | else if (!strcmp(attr->name, "notify-events") && | |
4839 | attr->value_tag == IPP_TAG_KEYWORD) | |
4840 | { | |
4841 | for (i = 0; i < attr->num_values; i ++) | |
4842 | mask |= cupsdEventValue(attr->values[i].string.text); | |
4843 | } | |
4844 | else if (!strcmp(attr->name, "notify-lease-duration") && | |
4845 | attr->value_tag == IPP_TAG_INTEGER) | |
4846 | lease = attr->values[0].integer; | |
4847 | else if (!strcmp(attr->name, "notify-time-interval") && | |
4848 | attr->value_tag == IPP_TAG_INTEGER) | |
4849 | interval = attr->values[0].integer; | |
4850 | else if (!strcmp(attr->name, "notify-job-id") && | |
4851 | attr->value_tag == IPP_TAG_INTEGER) | |
4852 | jobid = attr->values[0].integer; | |
4853 | ||
4854 | attr = attr->next; | |
4855 | } | |
4856 | ||
bd7854cb | 4857 | if (recipient) |
4858 | cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient); | |
4859 | if (pullmethod) | |
4860 | cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod); | |
4861 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease); | |
4862 | cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval); | |
fa73b229 | 4863 | |
ef416fc2 | 4864 | if (!recipient && !pullmethod) |
4865 | break; | |
4866 | ||
4867 | if (mask == CUPSD_EVENT_NONE) | |
4868 | { | |
4869 | if (jobid) | |
4870 | mask = CUPSD_EVENT_JOB_COMPLETED; | |
4871 | else if (printer) | |
4872 | mask = CUPSD_EVENT_PRINTER_STATE_CHANGED; | |
4873 | else | |
4874 | { | |
4875 | send_ipp_status(con, IPP_BAD_REQUEST, | |
4876 | _("notify-events not specified!")); | |
4877 | return; | |
4878 | } | |
4879 | } | |
4880 | ||
bd7854cb | 4881 | if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration)) |
ef416fc2 | 4882 | { |
4883 | cupsdLogMessage(CUPSD_LOG_INFO, | |
4884 | "create_subscription: Limiting notify-lease-duration to " | |
4885 | "%d seconds.", | |
4886 | MaxLeaseDuration); | |
4887 | lease = MaxLeaseDuration; | |
4888 | } | |
4889 | ||
4890 | if (jobid) | |
4891 | { | |
4892 | if ((job = cupsdFindJob(jobid)) == NULL) | |
4893 | { | |
4894 | send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid); | |
4895 | return; | |
4896 | } | |
4897 | } | |
4898 | else | |
4899 | job = NULL; | |
4900 | ||
4901 | sub = cupsdAddSubscription(mask, printer, job, recipient, 0); | |
4902 | ||
fa73b229 | 4903 | if (job) |
4904 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d", | |
4905 | sub->id, job->id); | |
4906 | else if (printer) | |
4907 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
4908 | "Added subscription %d for printer \"%s\"", | |
4909 | sub->id, printer->name); | |
4910 | else | |
4911 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server", | |
4912 | sub->id); | |
4913 | ||
ef416fc2 | 4914 | sub->interval = interval; |
4915 | sub->lease = lease; | |
bd7854cb | 4916 | sub->expire = lease ? time(NULL) + lease : 0; |
ef416fc2 | 4917 | |
4918 | cupsdSetString(&sub->owner, username); | |
4919 | ||
4920 | if (user_data) | |
4921 | { | |
4922 | sub->user_data_len = user_data->values[0].unknown.length; | |
4923 | memcpy(sub->user_data, user_data->values[0].unknown.data, | |
4924 | sub->user_data_len); | |
4925 | } | |
4926 | ||
4927 | ippAddSeparator(con->response); | |
4928 | ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
4929 | "notify-subscription-id", sub->id); | |
4930 | ||
fa73b229 | 4931 | con->response->request.status.status_code = IPP_OK; |
4932 | ||
ef416fc2 | 4933 | if (attr) |
4934 | attr = attr->next; | |
4935 | } | |
4936 | ||
4937 | cupsdSaveAllSubscriptions(); | |
4938 | ||
ef416fc2 | 4939 | } |
4940 | ||
4941 | ||
4942 | /* | |
4943 | * 'delete_printer()' - Remove a printer or class from the system. | |
4944 | */ | |
4945 | ||
4946 | static void | |
4947 | delete_printer(cupsd_client_t *con, /* I - Client connection */ | |
4948 | ipp_attribute_t *uri) /* I - URI of printer or class */ | |
4949 | { | |
4950 | http_status_t status; /* Policy status */ | |
4951 | const char *dest; /* Destination */ | |
4952 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
4953 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
4954 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
4955 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
4956 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
4957 | int port; /* Port portion of URI */ | |
4958 | cupsd_printer_t *printer; /* Printer/class */ | |
4959 | char filename[1024]; /* Script/PPD filename */ | |
4960 | ||
4961 | ||
4962 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con, | |
4963 | con->http.fd, uri->values[0].string.text); | |
4964 | ||
4965 | /* | |
4966 | * Do we have a valid URI? | |
4967 | */ | |
4968 | ||
a4d04587 | 4969 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
4970 | sizeof(method), username, sizeof(username), host, | |
4971 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 4972 | |
4973 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
4974 | { | |
4975 | /* | |
4976 | * Bad URI... | |
4977 | */ | |
4978 | ||
4979 | send_ipp_status(con, IPP_NOT_FOUND, | |
4980 | _("The printer or class was not found.")); | |
4981 | return; | |
4982 | } | |
4983 | ||
4984 | /* | |
4985 | * Check policy... | |
4986 | */ | |
4987 | ||
4988 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
4989 | { | |
4990 | send_http_error(con, status); | |
4991 | return; | |
4992 | } | |
4993 | ||
4994 | /* | |
4995 | * Remove old jobs... | |
4996 | */ | |
4997 | ||
4998 | cupsdCancelJobs(dest, NULL, 1); | |
4999 | ||
5000 | /* | |
5001 | * Remove old subscriptions and send a "deleted printer" event... | |
5002 | */ | |
5003 | ||
5004 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL, | |
5005 | "%s \"%s\" deleted by \"%s\".", | |
5006 | (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
e00b005a | 5007 | dest, get_username(con)); |
ef416fc2 | 5008 | |
5009 | cupsdExpireSubscriptions(printer, NULL); | |
5010 | ||
5011 | /* | |
5012 | * Remove any old PPD or script files... | |
5013 | */ | |
5014 | ||
5015 | snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, dest); | |
5016 | unlink(filename); | |
5017 | ||
5018 | snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest); | |
5019 | unlink(filename); | |
5020 | ||
5021 | if (dtype & CUPS_PRINTER_CLASS) | |
5022 | { | |
5023 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", dest, | |
e00b005a | 5024 | get_username(con)); |
ef416fc2 | 5025 | |
5026 | cupsdDeletePrinter(printer, 0); | |
5027 | cupsdSaveAllClasses(); | |
5028 | } | |
5029 | else | |
5030 | { | |
5031 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", dest, | |
e00b005a | 5032 | get_username(con)); |
ef416fc2 | 5033 | |
5034 | cupsdDeletePrinter(printer, 0); | |
5035 | cupsdSaveAllPrinters(); | |
5036 | } | |
5037 | ||
5038 | cupsdWritePrintcap(); | |
5039 | ||
5040 | /* | |
5041 | * Return with no errors... | |
5042 | */ | |
5043 | ||
5044 | con->response->request.status.status_code = IPP_OK; | |
5045 | } | |
5046 | ||
5047 | ||
5048 | /* | |
5049 | * 'get_default()' - Get the default destination. | |
5050 | */ | |
5051 | ||
5052 | static void | |
5053 | get_default(cupsd_client_t *con) /* I - Client connection */ | |
5054 | { | |
fa73b229 | 5055 | http_status_t status; /* Policy status */ |
5056 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5057 | |
5058 | ||
5059 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd); | |
5060 | ||
5061 | /* | |
5062 | * Check policy... | |
5063 | */ | |
5064 | ||
5065 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5066 | { | |
5067 | send_http_error(con, status); | |
5068 | return; | |
5069 | } | |
5070 | ||
fa73b229 | 5071 | if (DefaultPrinter) |
ef416fc2 | 5072 | { |
fa73b229 | 5073 | ra = create_requested_array(con->request); |
ef416fc2 | 5074 | |
fa73b229 | 5075 | copy_printer_attrs(con, DefaultPrinter, ra); |
ef416fc2 | 5076 | |
fa73b229 | 5077 | cupsArrayDelete(ra); |
ef416fc2 | 5078 | |
fa73b229 | 5079 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 5080 | } |
5081 | else | |
5082 | send_ipp_status(con, IPP_NOT_FOUND, _("No default printer")); | |
5083 | } | |
5084 | ||
5085 | ||
5086 | /* | |
5087 | * 'get_devices()' - Get the list of available devices on the local system. | |
5088 | */ | |
5089 | ||
5090 | static void | |
5091 | get_devices(cupsd_client_t *con) /* I - Client connection */ | |
5092 | { | |
5093 | http_status_t status; /* Policy status */ | |
5094 | int i; /* Looping var */ | |
5095 | ipp_attribute_t *limit, /* Limit attribute */ | |
5096 | *requested; /* requested-attributes attribute */ | |
5097 | char command[1024], /* cups-deviced command */ | |
5098 | options[1024], /* Options to pass to command */ | |
5099 | attrs[1024], /* String for requested attributes */ | |
5100 | *aptr; /* Pointer into string */ | |
5101 | int alen; /* Length of attribute value */ | |
5102 | ||
5103 | ||
5104 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd); | |
5105 | ||
5106 | /* | |
5107 | * Check policy... | |
5108 | */ | |
5109 | ||
5110 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5111 | { | |
5112 | send_http_error(con, status); | |
5113 | return; | |
5114 | } | |
5115 | ||
5116 | /* | |
5117 | * Run cups-deviced command with the given options... | |
5118 | */ | |
5119 | ||
5120 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
5121 | requested = ippFindAttribute(con->request, "requested-attributes", | |
5122 | IPP_TAG_KEYWORD); | |
5123 | ||
5124 | if (requested) | |
5125 | { | |
5126 | for (i = 0, aptr = attrs; i < requested->num_values; i ++) | |
5127 | { | |
5128 | /* | |
5129 | * Check that we have enough room... | |
5130 | */ | |
5131 | ||
5132 | alen = strlen(requested->values[i].string.text); | |
5133 | if (alen > (sizeof(attrs) - (aptr - attrs) - 2)) | |
5134 | break; | |
5135 | ||
5136 | /* | |
5137 | * Put commas between values... | |
5138 | */ | |
5139 | ||
5140 | if (i) | |
5141 | *aptr++ = ','; | |
5142 | ||
5143 | /* | |
5144 | * Add the value to the end of the string... | |
5145 | */ | |
5146 | ||
5147 | strcpy(aptr, requested->values[i].string.text); | |
5148 | aptr += alen; | |
5149 | } | |
5150 | ||
5151 | /* | |
5152 | * If we have more attribute names than will fit, default to "all"... | |
5153 | */ | |
5154 | ||
5155 | if (i < requested->num_values) | |
5156 | strcpy(attrs, "all"); | |
5157 | } | |
5158 | else | |
5159 | strcpy(attrs, "all"); | |
5160 | ||
5161 | snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin); | |
5162 | snprintf(options, sizeof(options), | |
b423cd4c | 5163 | "%d+%d+%d+requested-attributes=%s", |
ef416fc2 | 5164 | con->request->request.op.request_id, |
e1d6a774 | 5165 | limit ? limit->values[0].integer : 0, (int)User, |
ef416fc2 | 5166 | attrs); |
5167 | ||
5168 | if (cupsdSendCommand(con, command, options, 1)) | |
5169 | { | |
5170 | /* | |
5171 | * Command started successfully, don't send an IPP response here... | |
5172 | */ | |
5173 | ||
5174 | ippDelete(con->response); | |
5175 | con->response = NULL; | |
5176 | } | |
5177 | else | |
5178 | { | |
5179 | /* | |
5180 | * Command failed, return "internal error" so the user knows something | |
5181 | * went wrong... | |
5182 | */ | |
5183 | ||
5184 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
5185 | _("cups-deviced failed to execute.")); | |
5186 | } | |
5187 | } | |
5188 | ||
5189 | ||
5190 | /* | |
fa73b229 | 5191 | * 'get_job_attrs()' - Get job attributes. |
ef416fc2 | 5192 | */ |
5193 | ||
5194 | static void | |
fa73b229 | 5195 | get_job_attrs(cupsd_client_t *con, /* I - Client connection */ |
5196 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 5197 | { |
5198 | http_status_t status; /* Policy status */ | |
fa73b229 | 5199 | ipp_attribute_t *attr; /* Current attribute */ |
5200 | int jobid; /* Job ID */ | |
5201 | cupsd_job_t *job; /* Current job */ | |
ef416fc2 | 5202 | char method[HTTP_MAX_URI], /* Method portion of URI */ |
5203 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
5204 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
5205 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
5206 | int port; /* Port portion of URI */ | |
fa73b229 | 5207 | cups_array_t *ra; /* Requested attributes array */ |
ef416fc2 | 5208 | |
5209 | ||
fa73b229 | 5210 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con, |
5211 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5212 | |
5213 | /* | |
fa73b229 | 5214 | * See if we have a job URI or a printer URI... |
5215 | */ | |
5216 | ||
5217 | if (!strcmp(uri->name, "printer-uri")) | |
5218 | { | |
5219 | /* | |
5220 | * Got a printer URI; see if we also have a job-id attribute... | |
5221 | */ | |
5222 | ||
5223 | if ((attr = ippFindAttribute(con->request, "job-id", | |
5224 | IPP_TAG_INTEGER)) == NULL) | |
5225 | { | |
5226 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5227 | _("Got a printer-uri attribute but no job-id!")); | |
5228 | return; | |
5229 | } | |
5230 | ||
5231 | jobid = attr->values[0].integer; | |
5232 | } | |
5233 | else | |
5234 | { | |
5235 | /* | |
5236 | * Got a job URI; parse it to get the job ID... | |
5237 | */ | |
5238 | ||
a4d04587 | 5239 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5240 | sizeof(method), username, sizeof(username), host, | |
5241 | sizeof(host), &port, resource, sizeof(resource)); | |
fa73b229 | 5242 | |
5243 | if (strncmp(resource, "/jobs/", 6)) | |
5244 | { | |
5245 | /* | |
5246 | * Not a valid URI! | |
5247 | */ | |
5248 | ||
5249 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5250 | _("Bad job-uri attribute \"%s\"!"), | |
5251 | uri->values[0].string.text); | |
5252 | return; | |
5253 | } | |
5254 | ||
5255 | jobid = atoi(resource + 6); | |
5256 | } | |
5257 | ||
5258 | /* | |
5259 | * See if the job exists... | |
5260 | */ | |
5261 | ||
5262 | if ((job = cupsdFindJob(jobid)) == NULL) | |
5263 | { | |
5264 | /* | |
5265 | * Nope - return a "not found" error... | |
5266 | */ | |
5267 | ||
5268 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
5269 | return; | |
5270 | } | |
5271 | ||
5272 | /* | |
5273 | * Check policy... | |
5274 | */ | |
5275 | ||
5276 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5277 | { | |
5278 | send_http_error(con, status); | |
5279 | return; | |
5280 | } | |
5281 | ||
5282 | /* | |
5283 | * Copy attributes... | |
5284 | */ | |
5285 | ||
bd7854cb | 5286 | cupsdLoadJob(job); |
5287 | ||
fa73b229 | 5288 | ra = create_requested_array(con->request); |
5289 | copy_job_attrs(con, job, ra); | |
5290 | cupsArrayDelete(ra); | |
5291 | ||
5292 | con->response->request.status.status_code = IPP_OK; | |
5293 | } | |
5294 | ||
5295 | ||
5296 | /* | |
5297 | * 'get_jobs()' - Get a list of jobs for the specified printer. | |
5298 | */ | |
5299 | ||
5300 | static void | |
5301 | get_jobs(cupsd_client_t *con, /* I - Client connection */ | |
5302 | ipp_attribute_t *uri) /* I - Printer URI */ | |
5303 | { | |
5304 | http_status_t status; /* Policy status */ | |
5305 | ipp_attribute_t *attr; /* Current attribute */ | |
5306 | const char *dest; /* Destination */ | |
5307 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
5308 | cups_ptype_t dmask; /* Destination type mask */ | |
5309 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
5310 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
5311 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
5312 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
5313 | int port; /* Port portion of URI */ | |
5314 | int completed; /* Completed jobs? */ | |
5315 | int first_job_id; /* First job ID */ | |
5316 | int limit; /* Maximum number of jobs to return */ | |
5317 | int count; /* Number of jobs that match */ | |
5318 | cupsd_job_t *job; /* Current job pointer */ | |
5319 | cupsd_printer_t *printer; /* Printer */ | |
5320 | cups_array_t *list; /* Which job list... */ | |
5321 | cups_array_t *ra; /* Requested attributes array */ | |
5322 | ||
5323 | ||
5324 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd, | |
5325 | uri->values[0].string.text); | |
5326 | ||
5327 | /* | |
5328 | * Is the destination valid? | |
ef416fc2 | 5329 | */ |
5330 | ||
a4d04587 | 5331 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5332 | sizeof(method), username, sizeof(username), host, | |
5333 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 5334 | |
fa73b229 | 5335 | if (!strcmp(resource, "/") || |
5336 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6)) | |
ef416fc2 | 5337 | { |
5338 | dest = NULL; | |
5339 | dtype = (cups_ptype_t)0; | |
5340 | dmask = (cups_ptype_t)0; | |
5341 | printer = NULL; | |
5342 | } | |
fa73b229 | 5343 | else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) |
ef416fc2 | 5344 | { |
5345 | dest = NULL; | |
5346 | dtype = (cups_ptype_t)0; | |
5347 | dmask = CUPS_PRINTER_CLASS; | |
5348 | printer = NULL; | |
5349 | } | |
fa73b229 | 5350 | else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9) |
ef416fc2 | 5351 | { |
5352 | dest = NULL; | |
5353 | dtype = CUPS_PRINTER_CLASS; | |
5354 | dmask = CUPS_PRINTER_CLASS; | |
5355 | printer = NULL; | |
5356 | } | |
5357 | else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
5358 | { | |
5359 | /* | |
5360 | * Bad URI... | |
5361 | */ | |
5362 | ||
5363 | send_ipp_status(con, IPP_NOT_FOUND, | |
5364 | _("The printer or class was not found.")); | |
5365 | return; | |
5366 | } | |
5367 | else | |
5368 | dmask = CUPS_PRINTER_CLASS; | |
5369 | ||
5370 | /* | |
5371 | * Check policy... | |
5372 | */ | |
5373 | ||
5374 | if (printer) | |
5375 | { | |
5376 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
5377 | { | |
5378 | send_http_error(con, status); | |
5379 | return; | |
5380 | } | |
5381 | } | |
5382 | else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
5383 | { | |
5384 | send_http_error(con, status); | |
5385 | return; | |
5386 | } | |
5387 | ||
5388 | /* | |
5389 | * See if the "which-jobs" attribute have been specified... | |
5390 | */ | |
5391 | ||
fa73b229 | 5392 | if ((attr = ippFindAttribute(con->request, "which-jobs", |
5393 | IPP_TAG_KEYWORD)) != NULL && | |
ef416fc2 | 5394 | !strcmp(attr->values[0].string.text, "completed")) |
5395 | { | |
5396 | completed = 1; | |
5397 | list = Jobs; | |
5398 | } | |
5399 | else if (attr && !strcmp(attr->values[0].string.text, "all")) | |
5400 | { | |
5401 | completed = 0; | |
5402 | list = Jobs; | |
5403 | } | |
5404 | else | |
5405 | { | |
5406 | completed = 0; | |
5407 | list = ActiveJobs; | |
5408 | } | |
5409 | ||
5410 | /* | |
5411 | * See if they want to limit the number of jobs reported... | |
5412 | */ | |
5413 | ||
fa73b229 | 5414 | if ((attr = ippFindAttribute(con->request, "limit", |
5415 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 5416 | limit = attr->values[0].integer; |
5417 | else | |
5418 | limit = 1000000; | |
5419 | ||
5420 | if ((attr = ippFindAttribute(con->request, "first-job-id", | |
5421 | IPP_TAG_INTEGER)) != NULL) | |
5422 | first_job_id = attr->values[0].integer; | |
5423 | else | |
5424 | first_job_id = 1; | |
5425 | ||
5426 | /* | |
5427 | * See if we only want to see jobs for a specific user... | |
5428 | */ | |
5429 | ||
fa73b229 | 5430 | if ((attr = ippFindAttribute(con->request, "my-jobs", |
5431 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 5432 | attr->values[0].boolean) |
e00b005a | 5433 | strlcpy(username, get_username(con), sizeof(username)); |
ef416fc2 | 5434 | else |
5435 | username[0] = '\0'; | |
5436 | ||
fa73b229 | 5437 | ra = create_requested_array(con->request); |
ef416fc2 | 5438 | |
5439 | /* | |
5440 | * OK, build a list of jobs for this printer... | |
5441 | */ | |
5442 | ||
5443 | for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list); | |
5444 | count < limit && job; | |
5445 | job = (cupsd_job_t *)cupsArrayNext(list)) | |
5446 | { | |
5447 | /* | |
5448 | * Filter out jobs that don't match... | |
5449 | */ | |
5450 | ||
5451 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id); | |
5452 | ||
fa73b229 | 5453 | if ((dest && strcmp(job->dest, dest)) && |
5454 | (!job->printer || !dest || strcmp(job->printer->name, dest))) | |
ef416fc2 | 5455 | continue; |
5456 | if ((job->dtype & dmask) != dtype && | |
fa73b229 | 5457 | (!job->printer || (job->printer->type & dmask) != dtype)) |
ef416fc2 | 5458 | continue; |
fa73b229 | 5459 | if (username[0] && strcasecmp(username, job->username)) |
ef416fc2 | 5460 | continue; |
5461 | ||
bd7854cb | 5462 | if (completed && job->state_value <= IPP_JOB_STOPPED) |
ef416fc2 | 5463 | continue; |
5464 | ||
5465 | if (job->id < first_job_id) | |
5466 | continue; | |
5467 | ||
bd7854cb | 5468 | cupsdLoadJob(job); |
5469 | ||
5470 | if (!job->attrs) | |
5471 | continue; | |
5472 | ||
fa73b229 | 5473 | if (count > 0) |
5474 | ippAddSeparator(con->response); | |
5475 | ||
ef416fc2 | 5476 | count ++; |
5477 | ||
5478 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count); | |
5479 | ||
fa73b229 | 5480 | copy_job_attrs(con, job, ra); |
ef416fc2 | 5481 | } |
5482 | ||
fa73b229 | 5483 | cupsArrayDelete(ra); |
ef416fc2 | 5484 | |
fa73b229 | 5485 | con->response->request.status.status_code = IPP_OK; |
ef416fc2 | 5486 | } |
5487 | ||
5488 | ||
5489 | /* | |
5490 | * 'get_notifications()' - Get events for a subscription. | |
5491 | */ | |
5492 | ||
5493 | static void | |
bd7854cb | 5494 | get_notifications(cupsd_client_t *con) /* I - Client connection */ |
ef416fc2 | 5495 | { |
bd7854cb | 5496 | int i, j; /* Looping vars */ |
5497 | http_status_t status; /* Policy status */ | |
5498 | cupsd_subscription_t *sub; /* Subscription */ | |
5499 | ipp_attribute_t *ids, /* notify-subscription-ids */ | |
5500 | *sequences; /* notify-sequence-numbers */ | |
5501 | int min_seq; /* Minimum sequence number */ | |
5502 | int interval; /* Poll interval */ | |
5503 | ||
5504 | ||
5505 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_subscription_attrs(con=%p[%d])", | |
5506 | con, con->http.fd); | |
5507 | ||
5508 | /* | |
5509 | * Get subscription attributes... | |
5510 | */ | |
5511 | ||
5512 | ids = ippFindAttribute(con->request, "notify-subscription-ids", | |
5513 | IPP_TAG_INTEGER); | |
5514 | sequences = ippFindAttribute(con->request, "notify-sequence-numbers", | |
5515 | IPP_TAG_INTEGER); | |
5516 | ||
5517 | if (!ids) | |
5518 | { | |
5519 | send_ipp_status(con, IPP_BAD_REQUEST, | |
5520 | _("Missing notify-subscription-ids attribute!")); | |
5521 | return; | |
5522 | } | |
5523 | ||
5524 | /* | |
5525 | * Are the subscription IDs valid? | |
5526 | */ | |
5527 | ||
5528 | for (i = 0, interval = 60; i < ids->num_values; i ++) | |
5529 | { | |
5530 | if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL) | |
5531 | { | |
5532 | /* | |
5533 | * Bad subscription ID... | |
5534 | */ | |
5535 | ||
5536 | send_ipp_status(con, IPP_NOT_FOUND, | |
5537 | _("notify-subscription-id %d no good!"), | |
5538 | ids->values[i].integer); | |
5539 | return; | |
5540 | } | |
5541 | ||
5542 | /* | |
5543 | * Check policy... | |
5544 | */ | |
5545 | ||
5546 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
5547 | DefaultPolicyPtr, | |
5548 | con, sub->owner)) != HTTP_OK) | |
5549 | { | |
5550 | send_http_error(con, status); | |
5551 | return; | |
5552 | } | |
5553 | ||
5554 | /* | |
5555 | * Check the subscription type and update the interval accordingly. | |
5556 | */ | |
5557 | ||
5558 | if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING && | |
5559 | interval > 10) | |
5560 | interval = 10; | |
5561 | else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED) | |
5562 | interval = 0; | |
5563 | else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING && | |
5564 | interval > 30) | |
5565 | interval = 30; | |
5566 | } | |
5567 | ||
5568 | /* | |
5569 | * Tell the client to poll again in N seconds... | |
5570 | */ | |
5571 | ||
5572 | if (interval > 0) | |
5573 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
5574 | "notify-get-interval", interval); | |
5575 | ||
5576 | ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
5577 | "printer-up-time", time(NULL)); | |
5578 | ||
5579 | /* | |
5580 | * Copy the subscription event attributes to the response. | |
5581 | */ | |
5582 | ||
5583 | con->response->request.status.status_code = | |
5584 | interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE; | |
5585 | ||
5586 | for (i = 0; i < ids->num_values; i ++) | |
5587 | { | |
5588 | /* | |
5589 | * Get the subscription and sequence number... | |
5590 | */ | |
5591 | ||
5592 | sub = cupsdFindSubscription(ids->values[i].integer); | |
5593 | ||
5594 | if (sequences && i < sequences->num_values) | |
5595 | min_seq = sequences->values[i].integer; | |
5596 | else | |
5597 | min_seq = 1; | |
5598 | ||
5599 | /* | |
5600 | * If we don't have any new events, nothing to do here... | |
5601 | */ | |
5602 | ||
5603 | if (min_seq > (sub->first_event_id + sub->num_events)) | |
5604 | continue; | |
5605 | ||
5606 | /* | |
5607 | * Otherwise copy all of the new events... | |
5608 | */ | |
5609 | ||
5610 | if (sub->first_event_id > min_seq) | |
5611 | j = 0; | |
5612 | else | |
5613 | j = min_seq - sub->first_event_id; | |
5614 | ||
b423cd4c | 5615 | for (; j < sub->num_events; j ++) |
5616 | { | |
5617 | ippAddSeparator(con->response); | |
ef416fc2 | 5618 | |
b423cd4c | 5619 | copy_attrs(con->response, sub->events[j]->attrs, NULL, |
5620 | IPP_TAG_EVENT_NOTIFICATION, 0); | |
5621 | } | |
5622 | } | |
5623 | } | |
ef416fc2 | 5624 | |
ef416fc2 | 5625 | |
b423cd4c | 5626 | /* |
5627 | * 'get_ppds()' - Get the list of PPD files on the local system. | |
5628 | */ | |
ef416fc2 | 5629 | |
b423cd4c | 5630 | static void |
5631 | get_ppds(cupsd_client_t *con) /* I - Client connection */ | |
5632 | { | |
5633 | http_status_t status; /* Policy status */ | |
5634 | int i; /* Looping var */ | |
5635 | ipp_attribute_t *limit, /* Limit attribute */ | |
5636 | *make, /* ppd-make attribute */ | |
5637 | *requested; /* requested-attributes attribute */ | |
5638 | char command[1024], /* cups-deviced command */ | |
5639 | options[1024], /* Options to pass to command */ | |
5640 | attrs[1024], /* String for requested attributes */ | |
5641 | *aptr; /* Pointer into string */ | |
5642 | int alen; /* Length of attribute value */ | |
ef416fc2 | 5643 | |
ef416fc2 | 5644 | |
b423cd4c | 5645 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd); |
ef416fc2 | 5646 | |
5647 | /* | |
b423cd4c | 5648 | * Check policy... |
ef416fc2 | 5649 | */ |
5650 | ||
b423cd4c | 5651 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5652 | { |
b423cd4c | 5653 | send_http_error(con, status); |
5654 | return; | |
ef416fc2 | 5655 | } |
ef416fc2 | 5656 | |
b423cd4c | 5657 | /* |
5658 | * Run cups-driverd command with the given options... | |
5659 | */ | |
5660 | ||
5661 | limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER); | |
5662 | make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT); | |
5663 | requested = ippFindAttribute(con->request, "requested-attributes", | |
5664 | IPP_TAG_KEYWORD); | |
5665 | ||
5666 | if (requested) | |
fa73b229 | 5667 | { |
b423cd4c | 5668 | for (i = 0, aptr = attrs; i < requested->num_values; i ++) |
ef416fc2 | 5669 | { |
5670 | /* | |
b423cd4c | 5671 | * Check that we have enough room... |
ef416fc2 | 5672 | */ |
5673 | ||
b423cd4c | 5674 | alen = strlen(requested->values[i].string.text); |
5675 | if (alen > (sizeof(attrs) - (aptr - attrs) - 2)) | |
5676 | break; | |
ef416fc2 | 5677 | |
5678 | /* | |
b423cd4c | 5679 | * Put commas between values... |
ef416fc2 | 5680 | */ |
5681 | ||
b423cd4c | 5682 | if (i) |
5683 | *aptr++ = ','; | |
ef416fc2 | 5684 | |
5685 | /* | |
b423cd4c | 5686 | * Add the value to the end of the string... |
ef416fc2 | 5687 | */ |
5688 | ||
b423cd4c | 5689 | strcpy(aptr, requested->values[i].string.text); |
5690 | aptr += alen; | |
5691 | } | |
ef416fc2 | 5692 | |
b423cd4c | 5693 | /* |
5694 | * If we have more attribute names than will fit, default to "all"... | |
5695 | */ | |
ef416fc2 | 5696 | |
b423cd4c | 5697 | if (i < requested->num_values) |
5698 | strcpy(attrs, "all"); | |
fa73b229 | 5699 | } |
b423cd4c | 5700 | else |
5701 | strcpy(attrs, "all"); | |
fa73b229 | 5702 | |
b423cd4c | 5703 | snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin); |
5704 | snprintf(options, sizeof(options), | |
5705 | "list+%d+%d+requested-attributes=%s%s%s", | |
5706 | con->request->request.op.request_id, | |
5707 | limit ? limit->values[0].integer : 0, | |
5708 | attrs, | |
5709 | make ? "%20ppd-make=" : "", | |
5710 | make ? make->values[0].string.text : ""); | |
ef416fc2 | 5711 | |
b423cd4c | 5712 | if (cupsdSendCommand(con, command, options, 0)) |
5713 | { | |
5714 | /* | |
5715 | * Command started successfully, don't send an IPP response here... | |
5716 | */ | |
5717 | ||
5718 | ippDelete(con->response); | |
5719 | con->response = NULL; | |
5720 | } | |
5721 | else | |
5722 | { | |
5723 | /* | |
5724 | * Command failed, return "internal error" so the user knows something | |
5725 | * went wrong... | |
5726 | */ | |
5727 | ||
5728 | send_ipp_status(con, IPP_INTERNAL_ERROR, | |
5729 | _("cups-driverd failed to execute.")); | |
5730 | } | |
ef416fc2 | 5731 | } |
5732 | ||
5733 | ||
5734 | /* | |
b423cd4c | 5735 | * 'get_printer_attrs()' - Get printer attributes. |
ef416fc2 | 5736 | */ |
5737 | ||
5738 | static void | |
b423cd4c | 5739 | get_printer_attrs(cupsd_client_t *con, /* I - Client connection */ |
5740 | ipp_attribute_t *uri) /* I - Printer URI */ | |
ef416fc2 | 5741 | { |
5742 | http_status_t status; /* Policy status */ | |
b423cd4c | 5743 | const char *dest; /* Destination */ |
5744 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
5745 | char method[HTTP_MAX_URI], | |
5746 | /* Method portion of URI */ | |
5747 | username[HTTP_MAX_URI], | |
5748 | /* Username portion of URI */ | |
5749 | host[HTTP_MAX_URI], | |
5750 | /* Host portion of URI */ | |
5751 | resource[HTTP_MAX_URI]; | |
5752 | /* Resource portion of URI */ | |
5753 | int port; /* Port portion of URI */ | |
5754 | cupsd_printer_t *printer; /* Printer/class */ | |
ef416fc2 | 5755 | cups_array_t *ra; /* Requested attributes array */ |
5756 | ||
5757 | ||
b423cd4c | 5758 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con, |
5759 | con->http.fd, uri->values[0].string.text); | |
ef416fc2 | 5760 | |
5761 | /* | |
b423cd4c | 5762 | * Is the destination valid? |
ef416fc2 | 5763 | */ |
5764 | ||
b423cd4c | 5765 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
5766 | sizeof(method), username, sizeof(username), host, | |
5767 | sizeof(host), &port, resource, sizeof(resource)); | |
5768 | ||
5769 | if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
ef416fc2 | 5770 | { |
5771 | /* | |
b423cd4c | 5772 | * Bad URI... |
ef416fc2 | 5773 | */ |
5774 | ||
5775 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 5776 | _("The printer or class was not found.")); |
ef416fc2 | 5777 | return; |
5778 | } | |
5779 | ||
5780 | /* | |
5781 | * Check policy... | |
5782 | */ | |
5783 | ||
b423cd4c | 5784 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5785 | { |
5786 | send_http_error(con, status); | |
5787 | return; | |
5788 | } | |
5789 | ||
5790 | /* | |
b423cd4c | 5791 | * Send the attributes... |
ef416fc2 | 5792 | */ |
5793 | ||
5794 | ra = create_requested_array(con->request); | |
5795 | ||
b423cd4c | 5796 | copy_printer_attrs(con, printer, ra); |
ef416fc2 | 5797 | |
5798 | cupsArrayDelete(ra); | |
5799 | ||
5800 | con->response->request.status.status_code = IPP_OK; | |
5801 | } | |
5802 | ||
5803 | ||
5804 | /* | |
b423cd4c | 5805 | * 'get_printers()' - Get a list of printers or classes. |
ef416fc2 | 5806 | */ |
5807 | ||
5808 | static void | |
b423cd4c | 5809 | get_printers(cupsd_client_t *con, /* I - Client connection */ |
5810 | int type) /* I - 0 or CUPS_PRINTER_CLASS */ | |
ef416fc2 | 5811 | { |
b423cd4c | 5812 | http_status_t status; /* Policy status */ |
5813 | ipp_attribute_t *attr; /* Current attribute */ | |
5814 | int limit; /* Maximum number of printers to return */ | |
5815 | int count; /* Number of printers that match */ | |
5816 | cupsd_printer_t *printer; /* Current printer pointer */ | |
5817 | int printer_type, /* printer-type attribute */ | |
5818 | printer_mask; /* printer-type-mask attribute */ | |
5819 | char *location; /* Location string */ | |
5820 | const char *username; /* Current user */ | |
5821 | char *first_printer_name; /* first-printer-name attribute */ | |
5822 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5823 | |
ef416fc2 | 5824 | |
b423cd4c | 5825 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con, |
5826 | con->http.fd, type); | |
ef416fc2 | 5827 | |
5828 | /* | |
5829 | * Check policy... | |
5830 | */ | |
5831 | ||
b423cd4c | 5832 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) |
ef416fc2 | 5833 | { |
5834 | send_http_error(con, status); | |
5835 | return; | |
5836 | } | |
5837 | ||
5838 | /* | |
b423cd4c | 5839 | * Check for printers... |
ef416fc2 | 5840 | */ |
5841 | ||
b423cd4c | 5842 | if (!Printers || !cupsArrayCount(Printers)) |
5843 | { | |
5844 | send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added.")); | |
5845 | return; | |
5846 | } | |
5847 | ||
5848 | /* | |
5849 | * See if they want to limit the number of printers reported... | |
5850 | */ | |
ef416fc2 | 5851 | |
fa73b229 | 5852 | if ((attr = ippFindAttribute(con->request, "limit", |
5853 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 5854 | limit = attr->values[0].integer; |
5855 | else | |
b423cd4c | 5856 | limit = 10000000; |
5857 | ||
5858 | if ((attr = ippFindAttribute(con->request, "first-printer-name", | |
5859 | IPP_TAG_NAME)) != NULL) | |
5860 | first_printer_name = attr->values[0].string.text; | |
5861 | else | |
5862 | first_printer_name = NULL; | |
ef416fc2 | 5863 | |
5864 | /* | |
b423cd4c | 5865 | * Support filtering... |
ef416fc2 | 5866 | */ |
5867 | ||
b423cd4c | 5868 | if ((attr = ippFindAttribute(con->request, "printer-type", |
5869 | IPP_TAG_ENUM)) != NULL) | |
5870 | printer_type = attr->values[0].integer; | |
ef416fc2 | 5871 | else |
b423cd4c | 5872 | printer_type = 0; |
ef416fc2 | 5873 | |
b423cd4c | 5874 | if ((attr = ippFindAttribute(con->request, "printer-type-mask", |
5875 | IPP_TAG_ENUM)) != NULL) | |
5876 | printer_mask = attr->values[0].integer; | |
ef416fc2 | 5877 | else |
b423cd4c | 5878 | printer_mask = 0; |
e00b005a | 5879 | |
b423cd4c | 5880 | if ((attr = ippFindAttribute(con->request, "printer-location", |
5881 | IPP_TAG_TEXT)) != NULL) | |
5882 | location = attr->values[0].string.text; | |
5883 | else | |
5884 | location = NULL; | |
e00b005a | 5885 | |
5886 | if (con->username[0]) | |
b423cd4c | 5887 | username = con->username; |
e00b005a | 5888 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", |
5889 | IPP_TAG_NAME)) != NULL) | |
b423cd4c | 5890 | username = attr->values[0].string.text; |
e00b005a | 5891 | else |
b423cd4c | 5892 | username = NULL; |
ef416fc2 | 5893 | |
b423cd4c | 5894 | ra = create_requested_array(con->request); |
ef416fc2 | 5895 | |
5896 | /* | |
b423cd4c | 5897 | * OK, build a list of printers for this printer... |
ef416fc2 | 5898 | */ |
5899 | ||
b423cd4c | 5900 | if (first_printer_name) |
ef416fc2 | 5901 | { |
b423cd4c | 5902 | if ((printer = cupsdFindDest(first_printer_name)) == NULL) |
5903 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 5904 | } |
5905 | else | |
b423cd4c | 5906 | printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
ef416fc2 | 5907 | |
b423cd4c | 5908 | for (count = 0; |
5909 | count < limit && printer; | |
5910 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
5911 | { | |
5912 | if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) && | |
5913 | (printer->type & printer_mask) == printer_type && | |
5914 | (!location || !printer->location || | |
5915 | !strcasecmp(printer->location, location))) | |
ef416fc2 | 5916 | { |
5917 | /* | |
b423cd4c | 5918 | * If HideImplicitMembers is enabled, see if this printer or class |
5919 | * is a member of an implicit class... | |
ef416fc2 | 5920 | */ |
5921 | ||
b423cd4c | 5922 | if (ImplicitClasses && HideImplicitMembers && |
5923 | printer->in_implicit_class) | |
5924 | continue; | |
ef416fc2 | 5925 | |
b423cd4c | 5926 | /* |
5927 | * If a username is specified, see if it is allowed or denied | |
5928 | * access... | |
5929 | */ | |
ef416fc2 | 5930 | |
b423cd4c | 5931 | if (printer->num_users && username && !user_allowed(printer, username)) |
5932 | continue; | |
ef416fc2 | 5933 | |
b423cd4c | 5934 | /* |
5935 | * Add the group separator as needed... | |
5936 | */ | |
ef416fc2 | 5937 | |
b423cd4c | 5938 | if (count > 0) |
5939 | ippAddSeparator(con->response); | |
ef416fc2 | 5940 | |
b423cd4c | 5941 | count ++; |
ef416fc2 | 5942 | |
b423cd4c | 5943 | /* |
5944 | * Send the attributes... | |
5945 | */ | |
ef416fc2 | 5946 | |
b423cd4c | 5947 | copy_printer_attrs(con, printer, ra); |
5948 | } | |
ef416fc2 | 5949 | } |
5950 | ||
b423cd4c | 5951 | cupsArrayDelete(ra); |
ef416fc2 | 5952 | |
5953 | con->response->request.status.status_code = IPP_OK; | |
5954 | } | |
5955 | ||
5956 | ||
5957 | /* | |
b423cd4c | 5958 | * 'get_subscription_attrs()' - Get subscription attributes. |
ef416fc2 | 5959 | */ |
5960 | ||
5961 | static void | |
b423cd4c | 5962 | get_subscription_attrs( |
5963 | cupsd_client_t *con, /* I - Client connection */ | |
5964 | int sub_id) /* I - Subscription ID */ | |
ef416fc2 | 5965 | { |
b423cd4c | 5966 | http_status_t status; /* Policy status */ |
5967 | cupsd_subscription_t *sub; /* Subscription */ | |
5968 | cups_array_t *ra; /* Requested attributes array */ | |
ef416fc2 | 5969 | |
5970 | ||
b423cd4c | 5971 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
5972 | "get_subscription_attrs(con=%p[%d], sub_id=%d)", | |
5973 | con, con->http.fd, sub_id); | |
ef416fc2 | 5974 | |
fa73b229 | 5975 | /* |
b423cd4c | 5976 | * Is the subscription ID valid? |
fa73b229 | 5977 | */ |
5978 | ||
b423cd4c | 5979 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) |
fa73b229 | 5980 | { |
5981 | /* | |
b423cd4c | 5982 | * Bad subscription ID... |
fa73b229 | 5983 | */ |
5984 | ||
5985 | send_ipp_status(con, IPP_NOT_FOUND, | |
b423cd4c | 5986 | _("notify-subscription-id %d no good!"), sub_id); |
fa73b229 | 5987 | return; |
5988 | } | |
5989 | ||
5990 | /* | |
5991 | * Check policy... | |
5992 | */ | |
5993 | ||
b423cd4c | 5994 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : |
5995 | DefaultPolicyPtr, | |
5996 | con, sub->owner)) != HTTP_OK) | |
fa73b229 | 5997 | { |
5998 | send_http_error(con, status); | |
5999 | return; | |
6000 | } | |
6001 | ||
ef416fc2 | 6002 | /* |
b423cd4c | 6003 | * Copy the subscription attributes to the response using the |
6004 | * requested-attributes attribute that may be provided by the client. | |
ef416fc2 | 6005 | */ |
6006 | ||
b423cd4c | 6007 | ra = create_requested_array(con->request); |
fa73b229 | 6008 | |
b423cd4c | 6009 | copy_subscription_attrs(con, sub, ra); |
ef416fc2 | 6010 | |
b423cd4c | 6011 | cupsArrayDelete(ra); |
fa73b229 | 6012 | |
b423cd4c | 6013 | con->response->request.status.status_code = IPP_OK; |
6014 | } | |
fa73b229 | 6015 | |
fa73b229 | 6016 | |
b423cd4c | 6017 | /* |
6018 | * 'get_subscriptions()' - Get subscriptions. | |
6019 | */ | |
ef416fc2 | 6020 | |
b423cd4c | 6021 | static void |
6022 | get_subscriptions(cupsd_client_t *con, /* I - Client connection */ | |
6023 | ipp_attribute_t *uri) /* I - Printer/job URI */ | |
6024 | { | |
6025 | http_status_t status; /* Policy status */ | |
6026 | int count; /* Number of subscriptions */ | |
6027 | int limit; /* Limit */ | |
6028 | cupsd_subscription_t *sub; /* Subscription */ | |
6029 | cups_array_t *ra; /* Requested attributes array */ | |
6030 | ipp_attribute_t *attr; /* Attribute */ | |
6031 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
6032 | char method[HTTP_MAX_URI], | |
6033 | /* Method portion of URI */ | |
6034 | username[HTTP_MAX_URI], | |
6035 | /* Username portion of URI */ | |
6036 | host[HTTP_MAX_URI], | |
6037 | /* Host portion of URI */ | |
6038 | resource[HTTP_MAX_URI]; | |
6039 | /* Resource portion of URI */ | |
6040 | int port; /* Port portion of URI */ | |
6041 | cupsd_job_t *job; /* Job pointer */ | |
6042 | cupsd_printer_t *printer; /* Printer */ | |
fa73b229 | 6043 | |
fa73b229 | 6044 | |
b423cd4c | 6045 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
6046 | "get_subscriptions(con=%p[%d], uri=%s)", | |
6047 | con, con->http.fd, uri->values[0].string.text); | |
6048 | ||
6049 | /* | |
6050 | * Is the destination valid? | |
6051 | */ | |
6052 | ||
6053 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, | |
6054 | sizeof(method), username, sizeof(username), host, | |
6055 | sizeof(host), &port, resource, sizeof(resource)); | |
6056 | ||
6057 | if (!strcmp(resource, "/") || | |
6058 | (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) || | |
6059 | (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) || | |
6060 | (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)) | |
6061 | { | |
6062 | printer = NULL; | |
6063 | job = NULL; | |
ef416fc2 | 6064 | } |
b423cd4c | 6065 | else if (!strncmp(resource, "/jobs/", 6) && resource[6]) |
ef416fc2 | 6066 | { |
b423cd4c | 6067 | printer = NULL; |
6068 | job = cupsdFindJob(atoi(resource + 6)); | |
ef416fc2 | 6069 | |
b423cd4c | 6070 | if (!job) |
ef416fc2 | 6071 | { |
b423cd4c | 6072 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"), |
6073 | resource + 6); | |
ef416fc2 | 6074 | return; |
6075 | } | |
b423cd4c | 6076 | } |
6077 | else if (!cupsdValidateDest(host, resource, &dtype, &printer)) | |
6078 | { | |
fa73b229 | 6079 | /* |
b423cd4c | 6080 | * Bad URI... |
fa73b229 | 6081 | */ |
6082 | ||
b423cd4c | 6083 | send_ipp_status(con, IPP_NOT_FOUND, |
6084 | _("The printer or class was not found.")); | |
6085 | return; | |
6086 | } | |
6087 | else if ((attr = ippFindAttribute(con->request, "notify-job-id", | |
6088 | IPP_TAG_INTEGER)) != NULL) | |
6089 | { | |
6090 | job = cupsdFindJob(attr->values[0].integer); | |
ef416fc2 | 6091 | |
b423cd4c | 6092 | if (!job) |
fa73b229 | 6093 | { |
b423cd4c | 6094 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), |
6095 | attr->values[0].integer); | |
fa73b229 | 6096 | return; |
6097 | } | |
ef416fc2 | 6098 | } |
b423cd4c | 6099 | else |
6100 | job = NULL; | |
ef416fc2 | 6101 | |
6102 | /* | |
b423cd4c | 6103 | * Check policy... |
ef416fc2 | 6104 | */ |
6105 | ||
b423cd4c | 6106 | if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr : |
6107 | DefaultPolicyPtr, | |
6108 | con, NULL)) != HTTP_OK) | |
ef416fc2 | 6109 | { |
b423cd4c | 6110 | send_http_error(con, status); |
6111 | return; | |
6112 | } | |
ef416fc2 | 6113 | |
b423cd4c | 6114 | /* |
6115 | * Copy the subscription attributes to the response using the | |
6116 | * requested-attributes attribute that may be provided by the client. | |
6117 | */ | |
ef416fc2 | 6118 | |
b423cd4c | 6119 | ra = create_requested_array(con->request); |
ef416fc2 | 6120 | |
b423cd4c | 6121 | if ((attr = ippFindAttribute(con->request, "limit", |
6122 | IPP_TAG_INTEGER)) != NULL) | |
6123 | limit = attr->values[0].integer; | |
6124 | else | |
6125 | limit = 0; | |
ef416fc2 | 6126 | |
b423cd4c | 6127 | /* |
6128 | * See if we only want to see subscriptions for a specific user... | |
6129 | */ | |
ef416fc2 | 6130 | |
b423cd4c | 6131 | if ((attr = ippFindAttribute(con->request, "my-subscriptions", |
6132 | IPP_TAG_BOOLEAN)) != NULL && | |
6133 | attr->values[0].boolean) | |
6134 | strlcpy(username, get_username(con), sizeof(username)); | |
fa73b229 | 6135 | else |
b423cd4c | 6136 | username[0] = '\0'; |
ef416fc2 | 6137 | |
b423cd4c | 6138 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0; |
6139 | sub; | |
6140 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
6141 | if ((!printer || sub->dest == printer) && (!job || sub->job == job) && | |
6142 | (!username[0] || !strcasecmp(username, sub->owner))) | |
fa73b229 | 6143 | { |
b423cd4c | 6144 | ippAddSeparator(con->response); |
6145 | copy_subscription_attrs(con, sub, ra); | |
ef416fc2 | 6146 | |
b423cd4c | 6147 | count ++; |
6148 | if (limit && count >= limit) | |
6149 | break; | |
6150 | } | |
ef416fc2 | 6151 | |
b423cd4c | 6152 | cupsArrayDelete(ra); |
fa73b229 | 6153 | |
b423cd4c | 6154 | if (count) |
6155 | con->response->request.status.status_code = IPP_OK; | |
6156 | else | |
6157 | send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found.")); | |
6158 | } | |
ef416fc2 | 6159 | |
ef416fc2 | 6160 | |
b423cd4c | 6161 | /* |
6162 | * 'get_username()' - Get the username associated with a request. | |
6163 | */ | |
ef416fc2 | 6164 | |
b423cd4c | 6165 | static const char * /* O - Username */ |
6166 | get_username(cupsd_client_t *con) /* I - Connection */ | |
6167 | { | |
6168 | ipp_attribute_t *attr; /* Attribute */ | |
ef416fc2 | 6169 | |
ef416fc2 | 6170 | |
b423cd4c | 6171 | if (con->username[0]) |
6172 | return (con->username); | |
6173 | else if ((attr = ippFindAttribute(con->request, "requesting-user-name", | |
6174 | IPP_TAG_NAME)) != NULL) | |
6175 | return (attr->values[0].string.text); | |
6176 | else | |
6177 | return ("anonymous"); | |
ef416fc2 | 6178 | } |
6179 | ||
6180 | ||
6181 | /* | |
b423cd4c | 6182 | * 'hold_job()' - Hold a print job. |
ef416fc2 | 6183 | */ |
6184 | ||
b423cd4c | 6185 | static void |
6186 | hold_job(cupsd_client_t *con, /* I - Client connection */ | |
6187 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
ef416fc2 | 6188 | { |
b423cd4c | 6189 | ipp_attribute_t *attr, /* Current job-hold-until */ |
6190 | *newattr; /* New job-hold-until */ | |
6191 | int jobid; /* Job ID */ | |
6192 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
6193 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
6194 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
6195 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
6196 | int port; /* Port portion of URI */ | |
6197 | cupsd_job_t *job; /* Job information */ | |
6198 | ||
ef416fc2 | 6199 | |
b423cd4c | 6200 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd, |
6201 | uri->values[0].string.text); | |
ef416fc2 | 6202 | |
6203 | /* | |
b423cd4c | 6204 | * See if we have a job URI or a printer URI... |
ef416fc2 | 6205 | */ |
6206 | ||
b423cd4c | 6207 | if (!strcmp(uri->name, "printer-uri")) |
6208 | { | |
6209 | /* | |
6210 | * Got a printer URI; see if we also have a job-id attribute... | |
6211 | */ | |
ef416fc2 | 6212 | |
b423cd4c | 6213 | if ((attr = ippFindAttribute(con->request, "job-id", |
6214 | IPP_TAG_INTEGER)) == NULL) | |
6215 | { | |
6216 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6217 | _("Got a printer-uri attribute but no job-id!")); | |
6218 | return; | |
6219 | } | |
ef416fc2 | 6220 | |
b423cd4c | 6221 | jobid = attr->values[0].integer; |
6222 | } | |
ef416fc2 | 6223 | else |
ef416fc2 | 6224 | { |
b423cd4c | 6225 | /* |
6226 | * Got a job URI; parse it to get the job ID... | |
6227 | */ | |
ef416fc2 | 6228 | |
b423cd4c | 6229 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6230 | sizeof(method), username, sizeof(username), host, | |
6231 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 6232 | |
b423cd4c | 6233 | if (strncmp(resource, "/jobs/", 6)) |
6234 | { | |
6235 | /* | |
6236 | * Not a valid URI! | |
6237 | */ | |
ef416fc2 | 6238 | |
b423cd4c | 6239 | send_ipp_status(con, IPP_BAD_REQUEST, |
6240 | _("Bad job-uri attribute \"%s\"!"), | |
6241 | uri->values[0].string.text); | |
6242 | return; | |
6243 | } | |
ef416fc2 | 6244 | |
b423cd4c | 6245 | jobid = atoi(resource + 6); |
6246 | } | |
ef416fc2 | 6247 | |
ef416fc2 | 6248 | /* |
b423cd4c | 6249 | * See if the job exists... |
ef416fc2 | 6250 | */ |
6251 | ||
b423cd4c | 6252 | if ((job = cupsdFindJob(jobid)) == NULL) |
6253 | { | |
6254 | /* | |
6255 | * Nope - return a "not found" error... | |
6256 | */ | |
6257 | ||
6258 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
6259 | return; | |
6260 | } | |
ef416fc2 | 6261 | |
6262 | /* | |
b423cd4c | 6263 | * See if the job is owned by the requesting user... |
ef416fc2 | 6264 | */ |
6265 | ||
b423cd4c | 6266 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6267 | { | |
6268 | send_http_error(con, HTTP_UNAUTHORIZED); | |
6269 | return; | |
6270 | } | |
ef416fc2 | 6271 | |
6272 | /* | |
b423cd4c | 6273 | * Hold the job and return... |
ef416fc2 | 6274 | */ |
6275 | ||
b423cd4c | 6276 | cupsdHoldJob(job); |
ef416fc2 | 6277 | |
b423cd4c | 6278 | if ((newattr = ippFindAttribute(con->request, "job-hold-until", |
6279 | IPP_TAG_KEYWORD)) == NULL) | |
6280 | newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 6281 | |
b423cd4c | 6282 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
6283 | IPP_TAG_KEYWORD)) == NULL) | |
6284 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); | |
ef416fc2 | 6285 | |
b423cd4c | 6286 | if (attr) |
6287 | { | |
6288 | /* | |
6289 | * Free the old hold value and copy the new one over... | |
6290 | */ | |
ef416fc2 | 6291 | |
757d2cad | 6292 | _cupsStrFree(attr->values[0].string.text); |
ef416fc2 | 6293 | |
b423cd4c | 6294 | if (newattr) |
ef416fc2 | 6295 | { |
b423cd4c | 6296 | attr->value_tag = newattr->value_tag; |
6297 | attr->values[0].string.text = | |
757d2cad | 6298 | _cupsStrAlloc(newattr->values[0].string.text); |
b423cd4c | 6299 | } |
6300 | else | |
6301 | { | |
6302 | attr->value_tag = IPP_TAG_KEYWORD; | |
757d2cad | 6303 | attr->values[0].string.text = _cupsStrAlloc("indefinite"); |
ef416fc2 | 6304 | } |
6305 | ||
b423cd4c | 6306 | /* |
6307 | * Hold job until specified time... | |
6308 | */ | |
ef416fc2 | 6309 | |
b423cd4c | 6310 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); |
6311 | } | |
ef416fc2 | 6312 | |
b423cd4c | 6313 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was held by \"%s\".", jobid, |
6314 | username); | |
6315 | ||
6316 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 6317 | } |
6318 | ||
6319 | ||
6320 | /* | |
b423cd4c | 6321 | * 'move_job()' - Move a job to a new destination. |
ef416fc2 | 6322 | */ |
6323 | ||
6324 | static void | |
b423cd4c | 6325 | move_job(cupsd_client_t *con, /* I - Client connection */ |
6326 | ipp_attribute_t *uri) /* I - Job URI */ | |
ef416fc2 | 6327 | { |
6328 | http_status_t status; /* Policy status */ | |
6329 | ipp_attribute_t *attr; /* Current attribute */ | |
b423cd4c | 6330 | int jobid; /* Job ID */ |
ef416fc2 | 6331 | cupsd_job_t *job; /* Current job */ |
b423cd4c | 6332 | const char *src, /* Source printer/class */ |
6333 | *dest; /* Destination */ | |
6334 | cups_ptype_t stype, /* Source type (printer or class) */ | |
6335 | dtype; /* Destination type (printer or class) */ | |
6336 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
ef416fc2 | 6337 | username[HTTP_MAX_URI], /* Username portion of URI */ |
6338 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
b423cd4c | 6339 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ |
ef416fc2 | 6340 | int port; /* Port portion of URI */ |
b423cd4c | 6341 | cupsd_printer_t *sprinter, /* Source printer */ |
6342 | *dprinter; /* Destination printer */ | |
ef416fc2 | 6343 | |
6344 | ||
b423cd4c | 6345 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd, |
ef416fc2 | 6346 | uri->values[0].string.text); |
6347 | ||
6348 | /* | |
b423cd4c | 6349 | * Get the new printer or class... |
ef416fc2 | 6350 | */ |
6351 | ||
b423cd4c | 6352 | if ((attr = ippFindAttribute(con->request, "job-printer-uri", |
6353 | IPP_TAG_URI)) == NULL) | |
ef416fc2 | 6354 | { |
b423cd4c | 6355 | /* |
6356 | * Need job-printer-uri... | |
6357 | */ | |
ef416fc2 | 6358 | |
b423cd4c | 6359 | send_ipp_status(con, IPP_BAD_REQUEST, |
6360 | _("job-printer-uri attribute missing!")); | |
6361 | return; | |
ef416fc2 | 6362 | } |
b423cd4c | 6363 | |
6364 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, method, | |
6365 | sizeof(method), username, sizeof(username), host, | |
6366 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 6367 | |
b423cd4c | 6368 | if ((dest = cupsdValidateDest(host, resource, &dtype, &dprinter)) == NULL) |
ef416fc2 | 6369 | { |
b423cd4c | 6370 | /* |
6371 | * Bad URI... | |
6372 | */ | |
ef416fc2 | 6373 | |
b423cd4c | 6374 | send_ipp_status(con, IPP_NOT_FOUND, |
6375 | _("The printer or class was not found.")); | |
6376 | return; | |
ef416fc2 | 6377 | } |
6378 | ||
6379 | /* | |
b423cd4c | 6380 | * Check policy... |
ef416fc2 | 6381 | */ |
6382 | ||
b423cd4c | 6383 | if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con, NULL)) != HTTP_OK) |
ef416fc2 | 6384 | { |
b423cd4c | 6385 | send_http_error(con, status); |
ef416fc2 | 6386 | return; |
6387 | } | |
6388 | ||
6389 | /* | |
b423cd4c | 6390 | * See if we have a job URI or a printer URI... |
ef416fc2 | 6391 | */ |
6392 | ||
b423cd4c | 6393 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
6394 | sizeof(method), username, sizeof(username), host, | |
6395 | sizeof(host), &port, resource, sizeof(resource)); | |
6396 | ||
6397 | if (!strcmp(uri->name, "printer-uri")) | |
ef416fc2 | 6398 | { |
6399 | /* | |
b423cd4c | 6400 | * Got a printer URI; see if we also have a job-id attribute... |
ef416fc2 | 6401 | */ |
6402 | ||
b423cd4c | 6403 | if ((attr = ippFindAttribute(con->request, "job-id", |
6404 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 6405 | { |
b423cd4c | 6406 | /* |
6407 | * Move all jobs... | |
6408 | */ | |
6409 | ||
6410 | if ((src = cupsdValidateDest(host, resource, &stype, &sprinter)) == NULL) | |
6411 | { | |
6412 | /* | |
6413 | * Bad URI... | |
6414 | */ | |
6415 | ||
6416 | send_ipp_status(con, IPP_NOT_FOUND, | |
6417 | _("The printer or class was not found.")); | |
6418 | return; | |
6419 | } | |
6420 | ||
6421 | job = NULL; | |
6422 | } | |
6423 | else | |
6424 | { | |
6425 | /* | |
6426 | * Otherwise, just move a single job... | |
6427 | */ | |
6428 | ||
6429 | if ((job = cupsdFindJob(attr->values[0].integer)) == NULL) | |
6430 | { | |
6431 | /* | |
6432 | * Nope - return a "not found" error... | |
6433 | */ | |
6434 | ||
6435 | send_ipp_status(con, IPP_NOT_FOUND, | |
6436 | _("Job #%d does not exist!"), attr->values[0].integer); | |
6437 | return; | |
6438 | } | |
6439 | else | |
6440 | { | |
6441 | /* | |
6442 | * Job found, initialize source pointers... | |
6443 | */ | |
6444 | ||
6445 | src = NULL; | |
6446 | sprinter = NULL; | |
6447 | } | |
ef416fc2 | 6448 | } |
6449 | } | |
6450 | else | |
6451 | { | |
6452 | /* | |
b423cd4c | 6453 | * Got a job URI; parse it to get the job ID... |
ef416fc2 | 6454 | */ |
6455 | ||
b423cd4c | 6456 | if (strncmp(resource, "/jobs/", 6)) |
6457 | { | |
6458 | /* | |
6459 | * Not a valid URI! | |
6460 | */ | |
6461 | ||
6462 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6463 | _("Bad job-uri attribute \"%s\"!"), | |
6464 | uri->values[0].string.text); | |
6465 | return; | |
6466 | } | |
ef416fc2 | 6467 | |
ef416fc2 | 6468 | /* |
b423cd4c | 6469 | * See if the job exists... |
ef416fc2 | 6470 | */ |
6471 | ||
b423cd4c | 6472 | jobid = atoi(resource + 6); |
ef416fc2 | 6473 | |
b423cd4c | 6474 | if ((job = cupsdFindJob(jobid)) == NULL) |
ef416fc2 | 6475 | { |
6476 | /* | |
b423cd4c | 6477 | * Nope - return a "not found" error... |
ef416fc2 | 6478 | */ |
6479 | ||
b423cd4c | 6480 | send_ipp_status(con, IPP_NOT_FOUND, |
6481 | _("Job #%d does not exist!"), jobid); | |
6482 | return; | |
ef416fc2 | 6483 | } |
6484 | else | |
b423cd4c | 6485 | { |
6486 | /* | |
6487 | * Job found, initialize source pointers... | |
6488 | */ | |
ef416fc2 | 6489 | |
b423cd4c | 6490 | src = NULL; |
6491 | sprinter = NULL; | |
6492 | } | |
ef416fc2 | 6493 | } |
6494 | ||
ef416fc2 | 6495 | /* |
b423cd4c | 6496 | * Now move the job or jobs... |
ef416fc2 | 6497 | */ |
6498 | ||
b423cd4c | 6499 | if (job) |
6500 | { | |
6501 | /* | |
6502 | * See if the job has been completed... | |
6503 | */ | |
6504 | ||
6505 | if (job->state_value > IPP_JOB_STOPPED) | |
6506 | { | |
6507 | /* | |
6508 | * Return a "not-possible" error... | |
6509 | */ | |
6510 | ||
6511 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
6512 | _("Job #%d is finished and cannot be altered!"), | |
6513 | job->id); | |
6514 | return; | |
6515 | } | |
ef416fc2 | 6516 | |
b423cd4c | 6517 | /* |
6518 | * See if the job is owned by the requesting user... | |
6519 | */ | |
ef416fc2 | 6520 | |
b423cd4c | 6521 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6522 | { | |
6523 | send_http_error(con, HTTP_UNAUTHORIZED); | |
6524 | return; | |
6525 | } | |
ef416fc2 | 6526 | |
ef416fc2 | 6527 | /* |
b423cd4c | 6528 | * Move the job to a different printer or class... |
ef416fc2 | 6529 | */ |
6530 | ||
e53920b9 | 6531 | cupsdMoveJob(job, dprinter); |
ef416fc2 | 6532 | } |
b423cd4c | 6533 | else |
6534 | { | |
6535 | /* | |
6536 | * Got the source printer, now look through the jobs... | |
6537 | */ | |
ef416fc2 | 6538 | |
b423cd4c | 6539 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); |
6540 | job; | |
6541 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
6542 | { | |
6543 | /* | |
6544 | * See if the job is pointing at the source printer or has not been | |
6545 | * completed... | |
6546 | */ | |
ef416fc2 | 6547 | |
b423cd4c | 6548 | if (strcasecmp(job->dest, src) || |
6549 | job->state_value > IPP_JOB_STOPPED) | |
6550 | continue; | |
ef416fc2 | 6551 | |
b423cd4c | 6552 | /* |
6553 | * See if the job can be moved by the requesting user... | |
6554 | */ | |
ef416fc2 | 6555 | |
b423cd4c | 6556 | if (!validate_user(job, con, job->username, username, sizeof(username))) |
6557 | continue; | |
ef416fc2 | 6558 | |
b423cd4c | 6559 | /* |
6560 | * Move the job to a different printer or class... | |
6561 | */ | |
ef416fc2 | 6562 | |
e53920b9 | 6563 | cupsdMoveJob(job, dprinter); |
b423cd4c | 6564 | } |
ef416fc2 | 6565 | } |
6566 | ||
6567 | /* | |
b423cd4c | 6568 | * Start jobs if possible... |
ef416fc2 | 6569 | */ |
6570 | ||
b423cd4c | 6571 | cupsdCheckJobs(); |
ef416fc2 | 6572 | |
6573 | /* | |
b423cd4c | 6574 | * Return with "everything is OK" status... |
ef416fc2 | 6575 | */ |
6576 | ||
b423cd4c | 6577 | con->response->request.status.status_code = IPP_OK; |
6578 | } | |
ef416fc2 | 6579 | |
ef416fc2 | 6580 | |
b423cd4c | 6581 | /* |
6582 | * 'ppd_add_default()' - Add a PPD default choice. | |
6583 | */ | |
ef416fc2 | 6584 | |
b423cd4c | 6585 | static int /* O - Number of defaults */ |
6586 | ppd_add_default( | |
6587 | const char *option, /* I - Option name */ | |
6588 | const char *choice, /* I - Choice name */ | |
6589 | int num_defaults, /* I - Number of defaults */ | |
6590 | ppd_default_t **defaults) /* IO - Defaults */ | |
6591 | { | |
6592 | int i; /* Looping var */ | |
6593 | ppd_default_t *temp; /* Temporary defaults array */ | |
ef416fc2 | 6594 | |
bd7854cb | 6595 | |
ef416fc2 | 6596 | /* |
b423cd4c | 6597 | * First check if the option already has a default value; the PPD spec |
6598 | * says that the first one is used... | |
ef416fc2 | 6599 | */ |
6600 | ||
b423cd4c | 6601 | for (i = 0, temp = *defaults; i < num_defaults; i ++) |
6602 | if (!strcmp(option, temp[i].option)) | |
6603 | return (num_defaults); | |
ef416fc2 | 6604 | |
b423cd4c | 6605 | /* |
6606 | * Now add the option... | |
6607 | */ | |
ef416fc2 | 6608 | |
b423cd4c | 6609 | if (num_defaults == 0) |
6610 | temp = malloc(sizeof(ppd_default_t)); | |
ef416fc2 | 6611 | else |
b423cd4c | 6612 | temp = realloc(*defaults, (num_defaults + 1) * sizeof(ppd_default_t)); |
ef416fc2 | 6613 | |
b423cd4c | 6614 | if (!temp) |
ef416fc2 | 6615 | { |
b423cd4c | 6616 | cupsdLogMessage(CUPSD_LOG_ERROR, "ppd_add_default: Unable to add default value for \"%s\" - %s", |
6617 | option, strerror(errno)); | |
6618 | return (num_defaults); | |
ef416fc2 | 6619 | } |
6620 | ||
b423cd4c | 6621 | *defaults = temp; |
6622 | temp += num_defaults; | |
ef416fc2 | 6623 | |
b423cd4c | 6624 | strlcpy(temp->option, option, sizeof(temp->option)); |
6625 | strlcpy(temp->choice, choice, sizeof(temp->choice)); | |
ef416fc2 | 6626 | |
b423cd4c | 6627 | return (num_defaults + 1); |
6628 | } | |
ef416fc2 | 6629 | |
ef416fc2 | 6630 | |
b423cd4c | 6631 | /* |
6632 | * 'ppd_parse_line()' - Parse a PPD default line. | |
6633 | */ | |
ef416fc2 | 6634 | |
b423cd4c | 6635 | static int /* O - 0 on success, -1 on failure */ |
6636 | ppd_parse_line(const char *line, /* I - Line */ | |
6637 | char *option, /* O - Option name */ | |
6638 | int olen, /* I - Size of option name */ | |
6639 | char *choice, /* O - Choice name */ | |
6640 | int clen) /* I - Size of choice name */ | |
6641 | { | |
6642 | /* | |
6643 | * Verify this is a default option line... | |
6644 | */ | |
ef416fc2 | 6645 | |
b423cd4c | 6646 | if (strncmp(line, "*Default", 8)) |
6647 | return (-1); | |
ef416fc2 | 6648 | |
b423cd4c | 6649 | /* |
6650 | * Read the option name... | |
6651 | */ | |
6652 | ||
6653 | for (line += 8, olen --; isalnum(*line & 255); line ++) | |
6654 | if (olen > 0) | |
6655 | { | |
6656 | *option++ = *line; | |
6657 | olen --; | |
ef416fc2 | 6658 | } |
6659 | ||
b423cd4c | 6660 | *option = '\0'; |
ef416fc2 | 6661 | |
b423cd4c | 6662 | /* |
6663 | * Skip everything else up to the colon (:)... | |
6664 | */ | |
ef416fc2 | 6665 | |
b423cd4c | 6666 | while (*line && *line != ':') |
6667 | line ++; | |
ef416fc2 | 6668 | |
b423cd4c | 6669 | if (!*line) |
6670 | return (-1); | |
ef416fc2 | 6671 | |
b423cd4c | 6672 | line ++; |
ef416fc2 | 6673 | |
b423cd4c | 6674 | /* |
6675 | * Now grab the option choice, skipping leading whitespace... | |
6676 | */ | |
ef416fc2 | 6677 | |
b423cd4c | 6678 | while (isspace(*line & 255)) |
6679 | line ++; | |
ef416fc2 | 6680 | |
b423cd4c | 6681 | for (clen --; isalnum(*line & 255); line ++) |
6682 | if (clen > 0) | |
6683 | { | |
6684 | *choice++ = *line; | |
6685 | clen --; | |
6686 | } | |
ef416fc2 | 6687 | |
b423cd4c | 6688 | *choice = '\0'; |
ef416fc2 | 6689 | |
b423cd4c | 6690 | /* |
6691 | * Return with no errors... | |
6692 | */ | |
ef416fc2 | 6693 | |
b423cd4c | 6694 | return (0); |
6695 | } | |
ef416fc2 | 6696 | |
ef416fc2 | 6697 | |
b423cd4c | 6698 | /* |
6699 | * 'print_job()' - Print a file to a printer or class. | |
6700 | */ | |
ef416fc2 | 6701 | |
b423cd4c | 6702 | static void |
6703 | print_job(cupsd_client_t *con, /* I - Client connection */ | |
6704 | ipp_attribute_t *uri) /* I - Printer URI */ | |
6705 | { | |
6706 | ipp_attribute_t *attr; /* Current attribute */ | |
6707 | ipp_attribute_t *format; /* Document-format attribute */ | |
6708 | cupsd_job_t *job; /* New job */ | |
6709 | char filename[1024]; /* Job filename */ | |
6710 | mime_type_t *filetype; /* Type of file */ | |
6711 | char super[MIME_MAX_SUPER], /* Supertype of file */ | |
6712 | type[MIME_MAX_TYPE], /* Subtype of file */ | |
6713 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
6714 | /* Textual name of mime type */ | |
6715 | cupsd_printer_t *printer; /* Printer data */ | |
6716 | struct stat fileinfo; /* File information */ | |
6717 | int kbytes; /* Size of file */ | |
6718 | int compression; /* Document compression */ | |
ef416fc2 | 6719 | |
ef416fc2 | 6720 | |
b423cd4c | 6721 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd, |
6722 | uri->values[0].string.text); | |
ef416fc2 | 6723 | |
b423cd4c | 6724 | /* |
6725 | * Validate print file attributes, for now just document-format and | |
6726 | * compression (CUPS only supports "none" and "gzip")... | |
6727 | */ | |
ef416fc2 | 6728 | |
b423cd4c | 6729 | compression = CUPS_FILE_NONE; |
ef416fc2 | 6730 | |
b423cd4c | 6731 | if ((attr = ippFindAttribute(con->request, "compression", |
6732 | IPP_TAG_KEYWORD)) != NULL) | |
6733 | { | |
6734 | if (strcmp(attr->values[0].string.text, "none") | |
6735 | #ifdef HAVE_LIBZ | |
6736 | && strcmp(attr->values[0].string.text, "gzip") | |
6737 | #endif /* HAVE_LIBZ */ | |
6738 | ) | |
6739 | { | |
6740 | send_ipp_status(con, IPP_ATTRIBUTES, | |
6741 | _("Unsupported compression \"%s\"!"), | |
6742 | attr->values[0].string.text); | |
6743 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
6744 | "compression", NULL, attr->values[0].string.text); | |
6745 | return; | |
6746 | } | |
ef416fc2 | 6747 | |
b423cd4c | 6748 | #ifdef HAVE_LIBZ |
6749 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
6750 | compression = CUPS_FILE_GZIP; | |
6751 | #endif /* HAVE_LIBZ */ | |
6752 | } | |
ef416fc2 | 6753 | |
b423cd4c | 6754 | /* |
6755 | * Do we have a file to print? | |
6756 | */ | |
ef416fc2 | 6757 | |
b423cd4c | 6758 | if (!con->filename) |
6759 | { | |
6760 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
6761 | return; | |
6762 | } | |
ef416fc2 | 6763 | |
b423cd4c | 6764 | /* |
6765 | * Is it a format we support? | |
6766 | */ | |
ef416fc2 | 6767 | |
b423cd4c | 6768 | if ((format = ippFindAttribute(con->request, "document-format", |
6769 | IPP_TAG_MIMETYPE)) != NULL) | |
6770 | { | |
6771 | /* | |
6772 | * Grab format from client... | |
6773 | */ | |
6774 | ||
6775 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) | |
6776 | { | |
6777 | send_ipp_status(con, IPP_BAD_REQUEST, | |
6778 | _("Could not scan type \"%s\"!"), | |
6779 | format->values[0].string.text); | |
6780 | return; | |
ef416fc2 | 6781 | } |
b423cd4c | 6782 | } |
6783 | else | |
6784 | { | |
6785 | /* | |
6786 | * No document format attribute? Auto-type it! | |
6787 | */ | |
6788 | ||
6789 | strcpy(super, "application"); | |
6790 | strcpy(type, "octet-stream"); | |
6791 | } | |
ef416fc2 | 6792 | |
b423cd4c | 6793 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
6794 | { | |
ef416fc2 | 6795 | /* |
b423cd4c | 6796 | * Auto-type the file... |
ef416fc2 | 6797 | */ |
6798 | ||
b423cd4c | 6799 | ipp_attribute_t *doc_name; /* document-name attribute */ |
6800 | ||
6801 | ||
6802 | cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: auto-typing file..."); | |
6803 | ||
6804 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); | |
6805 | filetype = mimeFileType(MimeDatabase, con->filename, | |
6806 | doc_name ? doc_name->values[0].string.text : NULL, | |
6807 | &compression); | |
6808 | ||
6809 | if (filetype) | |
ef416fc2 | 6810 | { |
b423cd4c | 6811 | /* |
6812 | * Replace the document-format attribute value with the auto-typed one. | |
6813 | */ | |
ef416fc2 | 6814 | |
b423cd4c | 6815 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, |
6816 | filetype->type); | |
ef416fc2 | 6817 | |
b423cd4c | 6818 | if (format) |
6819 | { | |
757d2cad | 6820 | _cupsStrFree(format->values[0].string.text); |
b423cd4c | 6821 | |
757d2cad | 6822 | format->values[0].string.text = _cupsStrAlloc(mimetype); |
b423cd4c | 6823 | } |
6824 | else | |
6825 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
6826 | "document-format", NULL, mimetype); | |
ef416fc2 | 6827 | } |
b423cd4c | 6828 | else |
6829 | filetype = mimeType(MimeDatabase, super, type); | |
ef416fc2 | 6830 | } |
b423cd4c | 6831 | else |
6832 | filetype = mimeType(MimeDatabase, super, type); | |
6833 | ||
6834 | if (!filetype) | |
6835 | { | |
6836 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
6837 | _("Unsupported format \'%s/%s\'!"), super, type); | |
6838 | cupsdLogMessage(CUPSD_LOG_INFO, | |
6839 | "Hint: Do you have the raw file printing rules enabled?"); | |
6840 | ||
6841 | if (format) | |
6842 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
6843 | "document-format", NULL, format->values[0].string.text); | |
6844 | ||
6845 | return; | |
6846 | } | |
6847 | ||
6848 | cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: request file type is %s/%s.", | |
80ca4592 | 6849 | filetype->super, filetype->type); |
b423cd4c | 6850 | |
6851 | /* | |
6852 | * Read any embedded job ticket info from PS files... | |
6853 | */ | |
6854 | ||
6855 | if (!strcasecmp(filetype->super, "application") && | |
6856 | !strcasecmp(filetype->type, "postscript")) | |
6857 | read_ps_job_ticket(con); | |
6858 | ||
6859 | /* | |
6860 | * Create the job object... | |
6861 | */ | |
6862 | ||
80ca4592 | 6863 | if ((job = add_job(con, uri, &printer, filetype)) == NULL) |
b423cd4c | 6864 | return; |
6865 | ||
6866 | /* | |
6867 | * Update quota data... | |
6868 | */ | |
6869 | ||
6870 | if (stat(con->filename, &fileinfo)) | |
6871 | kbytes = 0; | |
6872 | else | |
6873 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
6874 | ||
6875 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
6876 | ||
6877 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
6878 | IPP_TAG_INTEGER)) != NULL) | |
6879 | attr->values[0].integer += kbytes; | |
6880 | ||
ef416fc2 | 6881 | /* |
6882 | * Add the job file... | |
6883 | */ | |
6884 | ||
6885 | if (add_file(con, job, filetype, compression)) | |
6886 | return; | |
6887 | ||
6888 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
6889 | job->num_files); | |
6890 | rename(con->filename, filename); | |
6891 | cupsdClearString(&con->filename); | |
6892 | ||
6893 | /* | |
6894 | * See if we need to add the ending sheet... | |
6895 | */ | |
6896 | ||
6897 | if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && | |
6898 | attr->num_values > 1) | |
6899 | { | |
6900 | /* | |
6901 | * Yes... | |
6902 | */ | |
6903 | ||
6904 | cupsdLogMessage(CUPSD_LOG_INFO, "Adding end banner page \"%s\" to job %d.", | |
6905 | attr->values[1].string.text, job->id); | |
6906 | ||
6907 | kbytes = copy_banner(con, job, attr->values[1].string.text); | |
6908 | ||
6909 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
6910 | } | |
6911 | ||
ef416fc2 | 6912 | /* |
6913 | * Log and save the job... | |
6914 | */ | |
6915 | ||
6916 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d queued on \"%s\" by \"%s\".", job->id, | |
6917 | job->dest, job->username); | |
6918 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Job %d hold_until = %d", job->id, | |
6919 | (int)job->hold_until); | |
6920 | ||
6921 | cupsdSaveJob(job); | |
6922 | ||
ef416fc2 | 6923 | /* |
6924 | * Start the job if possible... | |
6925 | */ | |
6926 | ||
6927 | cupsdCheckJobs(); | |
6928 | } | |
6929 | ||
6930 | ||
6931 | /* | |
6932 | * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file. | |
6933 | * | |
6934 | * This function only gets called when printing a single PostScript | |
6935 | * file using the Print-Job operation. It doesn't work for Create-Job + | |
6936 | * Send-File, since the job attributes need to be set at job creation | |
6937 | * time for banners to work. The embedded PS job ticket stuff is here | |
6938 | * only to allow the Windows printer driver for CUPS to pass in JCL | |
6939 | * options and IPP attributes which otherwise would be lost. | |
6940 | * | |
6941 | * The format of a PS job ticket is simple: | |
6942 | * | |
6943 | * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN | |
6944 | * | |
6945 | * %cupsJobTicket: attr1=value1 | |
6946 | * %cupsJobTicket: attr2=value2 | |
6947 | * ... | |
6948 | * %cupsJobTicket: attrN=valueN | |
6949 | * | |
6950 | * Job ticket lines must appear immediately after the first line that | |
6951 | * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop | |
6952 | * looking for job ticket info when it finds a line that does not begin | |
6953 | * with "%cupsJobTicket:". | |
6954 | * | |
6955 | * The maximum length of a job ticket line, including the prefix, is | |
6956 | * 255 characters to conform with the Adobe DSC. | |
6957 | * | |
6958 | * Read-only attributes are rejected with a notice to the error log in | |
6959 | * case a malicious user tries anything. Since the job ticket is read | |
6960 | * prior to attribute validation in print_job(), job ticket attributes | |
6961 | * will go through the same validation as IPP attributes... | |
6962 | */ | |
6963 | ||
6964 | static void | |
6965 | read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */ | |
6966 | { | |
6967 | cups_file_t *fp; /* File to read from */ | |
6968 | char line[256]; /* Line data */ | |
6969 | int num_options; /* Number of options */ | |
6970 | cups_option_t *options; /* Options */ | |
6971 | ipp_t *ticket; /* New attributes */ | |
6972 | ipp_attribute_t *attr, /* Current attribute */ | |
6973 | *attr2, /* Job attribute */ | |
6974 | *prev2; /* Previous job attribute */ | |
6975 | ||
6976 | ||
6977 | /* | |
6978 | * First open the print file... | |
6979 | */ | |
6980 | ||
6981 | if ((fp = cupsFileOpen(con->filename, "rb")) == NULL) | |
6982 | { | |
6983 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6984 | "read_ps_job_ticket: Unable to open PostScript print file " | |
6985 | "- %s", | |
6986 | strerror(errno)); | |
6987 | return; | |
6988 | } | |
6989 | ||
6990 | /* | |
6991 | * Skip the first line... | |
6992 | */ | |
6993 | ||
6994 | if (cupsFileGets(fp, line, sizeof(line)) == NULL) | |
6995 | { | |
6996 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
6997 | "read_ps_job_ticket: Unable to read from PostScript print " | |
6998 | "file - %s", | |
6999 | strerror(errno)); | |
7000 | cupsFileClose(fp); | |
7001 | return; | |
7002 | } | |
7003 | ||
fa73b229 | 7004 | if (strncmp(line, "%!PS-Adobe-", 11)) |
ef416fc2 | 7005 | { |
7006 | /* | |
7007 | * Not a DSC-compliant file, so no job ticket info will be available... | |
7008 | */ | |
7009 | ||
7010 | cupsFileClose(fp); | |
7011 | return; | |
7012 | } | |
7013 | ||
7014 | /* | |
7015 | * Read job ticket info from the file... | |
7016 | */ | |
7017 | ||
7018 | num_options = 0; | |
7019 | options = NULL; | |
7020 | ||
fa73b229 | 7021 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 7022 | { |
7023 | /* | |
7024 | * Stop at the first non-ticket line... | |
7025 | */ | |
7026 | ||
fa73b229 | 7027 | if (strncmp(line, "%cupsJobTicket:", 15)) |
ef416fc2 | 7028 | break; |
7029 | ||
7030 | /* | |
7031 | * Add the options to the option array... | |
7032 | */ | |
7033 | ||
7034 | num_options = cupsParseOptions(line + 15, num_options, &options); | |
7035 | } | |
7036 | ||
7037 | /* | |
7038 | * Done with the file; see if we have any options... | |
7039 | */ | |
7040 | ||
7041 | cupsFileClose(fp); | |
7042 | ||
7043 | if (num_options == 0) | |
7044 | return; | |
7045 | ||
7046 | /* | |
7047 | * OK, convert the options to an attribute list, and apply them to | |
7048 | * the request... | |
7049 | */ | |
7050 | ||
7051 | ticket = ippNew(); | |
7052 | cupsEncodeOptions(ticket, num_options, options); | |
7053 | ||
7054 | /* | |
7055 | * See what the user wants to change. | |
7056 | */ | |
7057 | ||
fa73b229 | 7058 | for (attr = ticket->attrs; attr; attr = attr->next) |
ef416fc2 | 7059 | { |
7060 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
7061 | continue; | |
7062 | ||
7063 | if (!strcmp(attr->name, "job-originating-host-name") || | |
7064 | !strcmp(attr->name, "job-originating-user-name") || | |
7065 | !strcmp(attr->name, "job-media-sheets-completed") || | |
7066 | !strcmp(attr->name, "job-k-octets") || | |
7067 | !strcmp(attr->name, "job-id") || | |
7068 | !strncmp(attr->name, "job-state", 9) || | |
7069 | !strncmp(attr->name, "time-at-", 8)) | |
7070 | continue; /* Read-only attrs */ | |
7071 | ||
fa73b229 | 7072 | if ((attr2 = ippFindAttribute(con->request, attr->name, |
7073 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 7074 | { |
7075 | /* | |
7076 | * Some other value; first free the old value... | |
7077 | */ | |
7078 | ||
7079 | if (con->request->attrs == attr2) | |
7080 | { | |
7081 | con->request->attrs = attr2->next; | |
7082 | prev2 = NULL; | |
7083 | } | |
7084 | else | |
7085 | { | |
fa73b229 | 7086 | for (prev2 = con->request->attrs; prev2; prev2 = prev2->next) |
ef416fc2 | 7087 | if (prev2->next == attr2) |
7088 | { | |
7089 | prev2->next = attr2->next; | |
7090 | break; | |
7091 | } | |
7092 | } | |
7093 | ||
7094 | if (con->request->last == attr2) | |
7095 | con->request->last = prev2; | |
7096 | ||
757d2cad | 7097 | _ippFreeAttr(attr2); |
ef416fc2 | 7098 | } |
7099 | ||
7100 | /* | |
7101 | * Add new option by copying it... | |
7102 | */ | |
7103 | ||
7104 | copy_attribute(con->request, attr, 0); | |
7105 | } | |
7106 | ||
7107 | /* | |
7108 | * Then free the attribute list and option array... | |
7109 | */ | |
7110 | ||
7111 | ippDelete(ticket); | |
7112 | cupsFreeOptions(num_options, options); | |
7113 | } | |
7114 | ||
7115 | ||
7116 | /* | |
7117 | * 'reject_jobs()' - Reject print jobs to a printer. | |
7118 | */ | |
7119 | ||
7120 | static void | |
7121 | reject_jobs(cupsd_client_t *con, /* I - Client connection */ | |
7122 | ipp_attribute_t *uri) /* I - Printer or class URI */ | |
7123 | { | |
7124 | http_status_t status; /* Policy status */ | |
7125 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
7126 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7127 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7128 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7129 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7130 | int port; /* Port portion of URI */ | |
7131 | const char *name; /* Printer name */ | |
7132 | cupsd_printer_t *printer; /* Printer data */ | |
7133 | ipp_attribute_t *attr; /* printer-state-message text */ | |
7134 | ||
7135 | ||
7136 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con, | |
7137 | con->http.fd, uri->values[0].string.text); | |
7138 | ||
7139 | /* | |
7140 | * Is the destination valid? | |
7141 | */ | |
7142 | ||
a4d04587 | 7143 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7144 | sizeof(method), username, sizeof(username), host, | |
7145 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7146 | |
7147 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
7148 | { | |
7149 | /* | |
7150 | * Bad URI... | |
7151 | */ | |
7152 | ||
7153 | send_ipp_status(con, IPP_NOT_FOUND, | |
7154 | _("The printer or class was not found.")); | |
7155 | return; | |
7156 | } | |
7157 | ||
7158 | /* | |
7159 | * Check policy... | |
7160 | */ | |
7161 | ||
7162 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
7163 | { | |
7164 | send_http_error(con, status); | |
7165 | return; | |
7166 | } | |
7167 | ||
7168 | /* | |
7169 | * Reject jobs sent to the printer... | |
7170 | */ | |
7171 | ||
7172 | printer->accepting = 0; | |
7173 | ||
7174 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
7175 | IPP_TAG_TEXT)) == NULL) | |
7176 | strcpy(printer->state_message, "Rejecting Jobs"); | |
7177 | else | |
7178 | strlcpy(printer->state_message, attr->values[0].string.text, | |
7179 | sizeof(printer->state_message)); | |
7180 | ||
7181 | cupsdAddPrinterHistory(printer); | |
7182 | ||
7183 | if (dtype & CUPS_PRINTER_CLASS) | |
7184 | { | |
7185 | cupsdSaveAllClasses(); | |
7186 | ||
7187 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").", | |
e00b005a | 7188 | name, get_username(con)); |
ef416fc2 | 7189 | } |
7190 | else | |
7191 | { | |
7192 | cupsdSaveAllPrinters(); | |
7193 | ||
7194 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").", | |
e00b005a | 7195 | name, get_username(con)); |
ef416fc2 | 7196 | } |
7197 | ||
7198 | /* | |
7199 | * Everything was ok, so return OK status... | |
7200 | */ | |
7201 | ||
7202 | con->response->request.status.status_code = IPP_OK; | |
7203 | } | |
7204 | ||
7205 | ||
7206 | /* | |
7207 | * 'release_job()' - Release a held print job. | |
7208 | */ | |
7209 | ||
7210 | static void | |
7211 | release_job(cupsd_client_t *con, /* I - Client connection */ | |
7212 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
7213 | { | |
7214 | ipp_attribute_t *attr; /* Current attribute */ | |
7215 | int jobid; /* Job ID */ | |
7216 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7217 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7218 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7219 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7220 | int port; /* Port portion of URI */ | |
7221 | cupsd_job_t *job; /* Job information */ | |
7222 | ||
7223 | ||
7224 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con, | |
7225 | con->http.fd, uri->values[0].string.text); | |
7226 | ||
7227 | /* | |
7228 | * See if we have a job URI or a printer URI... | |
7229 | */ | |
7230 | ||
7231 | if (!strcmp(uri->name, "printer-uri")) | |
7232 | { | |
7233 | /* | |
7234 | * Got a printer URI; see if we also have a job-id attribute... | |
7235 | */ | |
7236 | ||
fa73b229 | 7237 | if ((attr = ippFindAttribute(con->request, "job-id", |
7238 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7239 | { |
7240 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7241 | _("Got a printer-uri attribute but no job-id!")); | |
7242 | return; | |
7243 | } | |
7244 | ||
7245 | jobid = attr->values[0].integer; | |
7246 | } | |
7247 | else | |
7248 | { | |
7249 | /* | |
7250 | * Got a job URI; parse it to get the job ID... | |
7251 | */ | |
7252 | ||
a4d04587 | 7253 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7254 | sizeof(method), username, sizeof(username), host, | |
7255 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7256 | |
7257 | if (strncmp(resource, "/jobs/", 6)) | |
7258 | { | |
7259 | /* | |
7260 | * Not a valid URI! | |
7261 | */ | |
7262 | ||
7263 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7264 | _("Bad job-uri attribute \"%s\"!"), | |
7265 | uri->values[0].string.text); | |
7266 | return; | |
7267 | } | |
7268 | ||
7269 | jobid = atoi(resource + 6); | |
7270 | } | |
7271 | ||
7272 | /* | |
7273 | * See if the job exists... | |
7274 | */ | |
7275 | ||
7276 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7277 | { | |
7278 | /* | |
7279 | * Nope - return a "not found" error... | |
7280 | */ | |
7281 | ||
7282 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7283 | return; | |
7284 | } | |
7285 | ||
7286 | /* | |
7287 | * See if job is "held"... | |
7288 | */ | |
7289 | ||
bd7854cb | 7290 | if (job->state_value != IPP_JOB_HELD) |
ef416fc2 | 7291 | { |
7292 | /* | |
7293 | * Nope - return a "not possible" error... | |
7294 | */ | |
7295 | ||
7296 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid); | |
7297 | return; | |
7298 | } | |
7299 | ||
7300 | /* | |
7301 | * See if the job is owned by the requesting user... | |
7302 | */ | |
7303 | ||
7304 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7305 | { | |
bd7854cb | 7306 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7307 | return; |
7308 | } | |
7309 | ||
7310 | /* | |
7311 | * Reset the job-hold-until value to "no-hold"... | |
7312 | */ | |
7313 | ||
fa73b229 | 7314 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7315 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7316 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7317 | ||
fa73b229 | 7318 | if (attr) |
ef416fc2 | 7319 | { |
757d2cad | 7320 | _cupsStrFree(attr->values[0].string.text); |
4400e98d | 7321 | |
ef416fc2 | 7322 | attr->value_tag = IPP_TAG_KEYWORD; |
757d2cad | 7323 | attr->values[0].string.text = _cupsStrAlloc("no-hold"); |
ef416fc2 | 7324 | } |
7325 | ||
7326 | /* | |
7327 | * Release the job and return... | |
7328 | */ | |
7329 | ||
7330 | cupsdReleaseJob(job); | |
7331 | ||
7332 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was released by \"%s\".", jobid, | |
7333 | username); | |
7334 | ||
7335 | con->response->request.status.status_code = IPP_OK; | |
7336 | } | |
7337 | ||
7338 | ||
7339 | /* | |
7340 | * 'renew_subscription()' - Renew an existing subscription... | |
7341 | */ | |
7342 | ||
7343 | static void | |
7344 | renew_subscription( | |
7345 | cupsd_client_t *con, /* I - Client connection */ | |
7346 | int sub_id) /* I - Subscription ID */ | |
7347 | { | |
bd7854cb | 7348 | http_status_t status; /* Policy status */ |
7349 | cupsd_subscription_t *sub; /* Subscription */ | |
7350 | ipp_attribute_t *lease; /* notify-lease-duration */ | |
7351 | ||
7352 | ||
7353 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
7354 | "renew_subscription(con=%p[%d], sub_id=%d)", | |
7355 | con, con->http.fd, sub_id); | |
7356 | ||
7357 | /* | |
7358 | * Is the subscription ID valid? | |
7359 | */ | |
7360 | ||
7361 | if ((sub = cupsdFindSubscription(sub_id)) == NULL) | |
7362 | { | |
7363 | /* | |
7364 | * Bad subscription ID... | |
7365 | */ | |
7366 | ||
7367 | send_ipp_status(con, IPP_NOT_FOUND, | |
7368 | _("notify-subscription-id %d no good!"), sub_id); | |
7369 | return; | |
7370 | } | |
7371 | ||
7372 | if (sub->job) | |
7373 | { | |
7374 | /* | |
7375 | * Job subscriptions cannot be renewed... | |
7376 | */ | |
7377 | ||
7378 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7379 | _("Job subscriptions cannot be renewed!")); | |
7380 | return; | |
7381 | } | |
7382 | ||
7383 | /* | |
7384 | * Check policy... | |
7385 | */ | |
7386 | ||
7387 | if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr : | |
7388 | DefaultPolicyPtr, | |
7389 | con, sub->owner)) != HTTP_OK) | |
7390 | { | |
7391 | send_http_error(con, status); | |
7392 | return; | |
7393 | } | |
7394 | ||
7395 | /* | |
7396 | * Renew the subscription... | |
7397 | */ | |
7398 | ||
7399 | lease = ippFindAttribute(con->request, "notify-lease-duration", | |
7400 | IPP_TAG_INTEGER); | |
7401 | ||
7402 | sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration; | |
7403 | ||
7404 | if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration)) | |
7405 | { | |
7406 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7407 | "renew_subscription: Limiting notify-lease-duration to " | |
7408 | "%d seconds.", | |
7409 | MaxLeaseDuration); | |
7410 | sub->lease = MaxLeaseDuration; | |
7411 | } | |
7412 | ||
7413 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
7414 | ||
7415 | cupsdSaveAllSubscriptions(); | |
7416 | ||
7417 | con->response->request.status.status_code = IPP_OK; | |
ef416fc2 | 7418 | } |
7419 | ||
7420 | ||
7421 | /* | |
7422 | * 'restart_job()' - Restart an old print job. | |
7423 | */ | |
7424 | ||
7425 | static void | |
7426 | restart_job(cupsd_client_t *con, /* I - Client connection */ | |
7427 | ipp_attribute_t *uri) /* I - Job or Printer URI */ | |
7428 | { | |
7429 | ipp_attribute_t *attr; /* Current attribute */ | |
7430 | int jobid; /* Job ID */ | |
7431 | char method[HTTP_MAX_URI], /* Method portion of URI */ | |
7432 | username[HTTP_MAX_URI], /* Username portion of URI */ | |
7433 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
7434 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
7435 | int port; /* Port portion of URI */ | |
7436 | cupsd_job_t *job; /* Job information */ | |
7437 | ||
7438 | ||
7439 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con, | |
7440 | con->http.fd, uri->values[0].string.text); | |
7441 | ||
7442 | /* | |
7443 | * See if we have a job URI or a printer URI... | |
7444 | */ | |
7445 | ||
7446 | if (!strcmp(uri->name, "printer-uri")) | |
7447 | { | |
7448 | /* | |
7449 | * Got a printer URI; see if we also have a job-id attribute... | |
7450 | */ | |
7451 | ||
fa73b229 | 7452 | if ((attr = ippFindAttribute(con->request, "job-id", |
7453 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7454 | { |
7455 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7456 | _("Got a printer-uri attribute but no job-id!")); | |
7457 | return; | |
7458 | } | |
7459 | ||
7460 | jobid = attr->values[0].integer; | |
7461 | } | |
7462 | else | |
7463 | { | |
7464 | /* | |
7465 | * Got a job URI; parse it to get the job ID... | |
7466 | */ | |
7467 | ||
a4d04587 | 7468 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7469 | sizeof(method), username, sizeof(username), host, | |
7470 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7471 | |
fa73b229 | 7472 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 7473 | { |
7474 | /* | |
7475 | * Not a valid URI! | |
7476 | */ | |
7477 | ||
7478 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7479 | _("Bad job-uri attribute \"%s\"!"), | |
7480 | uri->values[0].string.text); | |
7481 | return; | |
7482 | } | |
7483 | ||
7484 | jobid = atoi(resource + 6); | |
7485 | } | |
7486 | ||
7487 | /* | |
7488 | * See if the job exists... | |
7489 | */ | |
7490 | ||
7491 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7492 | { | |
7493 | /* | |
7494 | * Nope - return a "not found" error... | |
7495 | */ | |
7496 | ||
7497 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7498 | return; | |
7499 | } | |
7500 | ||
7501 | /* | |
7502 | * See if job is in any of the "completed" states... | |
7503 | */ | |
7504 | ||
bd7854cb | 7505 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 7506 | { |
7507 | /* | |
7508 | * Nope - return a "not possible" error... | |
7509 | */ | |
7510 | ||
7511 | send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"), | |
7512 | jobid); | |
7513 | return; | |
7514 | } | |
7515 | ||
7516 | /* | |
7517 | * See if we have retained the job files... | |
7518 | */ | |
7519 | ||
bd7854cb | 7520 | cupsdLoadJob(job); |
7521 | ||
7522 | if (!job->attrs ||job->num_files == 0) | |
ef416fc2 | 7523 | { |
7524 | /* | |
7525 | * Nope - return a "not possible" error... | |
7526 | */ | |
7527 | ||
7528 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
7529 | _("Job #%d cannot be restarted - no files!"), jobid); | |
7530 | return; | |
7531 | } | |
7532 | ||
7533 | /* | |
7534 | * See if the job is owned by the requesting user... | |
7535 | */ | |
7536 | ||
7537 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7538 | { | |
bd7854cb | 7539 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7540 | return; |
7541 | } | |
7542 | ||
7543 | /* | |
7544 | * Restart the job and return... | |
7545 | */ | |
7546 | ||
7547 | cupsdRestartJob(job); | |
7548 | ||
7549 | cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was restarted by \"%s\".", jobid, | |
7550 | username); | |
7551 | ||
7552 | con->response->request.status.status_code = IPP_OK; | |
7553 | } | |
7554 | ||
7555 | ||
7556 | /* | |
7557 | * 'save_auth_info()' - Save authentication information for a job. | |
7558 | */ | |
7559 | ||
7560 | static void | |
7561 | save_auth_info(cupsd_client_t *con, /* I - Client connection */ | |
7562 | cupsd_job_t *job) /* I - Job */ | |
7563 | { | |
7564 | int i; /* Looping var */ | |
7565 | char filename[1024]; /* Job authentication filename */ | |
7566 | cups_file_t *fp; /* Job authentication file */ | |
7567 | char line[1024]; /* Line for file */ | |
7568 | ||
7569 | ||
7570 | /* | |
7571 | * This function saves the in-memory authentication information for | |
7572 | * a job so that it can be used to authenticate with a remote host. | |
7573 | * The information is stored in a file that is readable only by the | |
7574 | * root user. The username and password are Base-64 encoded, each | |
7575 | * on a separate line, followed by random number (up to 1024) of | |
7576 | * newlines to limit the amount of information that is exposed. | |
7577 | * | |
7578 | * Because of the potential for exposing of authentication information, | |
7579 | * this functionality is only enabled when running cupsd as root. | |
7580 | * | |
7581 | * This caching only works for the Basic and BasicDigest authentication | |
7582 | * types. Digest authentication cannot be cached this way, and in | |
7583 | * the future Kerberos authentication may make all of this obsolete. | |
7584 | * | |
7585 | * Authentication information is saved whenever an authenticated | |
7586 | * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is | |
7587 | * performed. | |
7588 | * | |
7589 | * This information is deleted after a job is completed or canceled, | |
7590 | * so reprints may require subsequent re-authentication. | |
7591 | */ | |
7592 | ||
7593 | if (RunUser) | |
7594 | return; | |
7595 | ||
7596 | /* | |
7597 | * Create the authentication file and change permissions... | |
7598 | */ | |
7599 | ||
7600 | snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id); | |
7601 | if ((fp = cupsFileOpen(filename, "w")) == NULL) | |
7602 | { | |
7603 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
7604 | "Unable to save authentication info to \"%s\" - %s", | |
7605 | filename, strerror(errno)); | |
7606 | return; | |
7607 | } | |
7608 | ||
7609 | fchown(cupsFileNumber(fp), 0, 0); | |
7610 | fchmod(cupsFileNumber(fp), 0400); | |
7611 | ||
7612 | /* | |
7613 | * Write the authenticated username... | |
7614 | */ | |
7615 | ||
7616 | httpEncode64_2(line, sizeof(line), con->username, strlen(con->username)); | |
7617 | cupsFilePrintf(fp, "%s\n", line); | |
7618 | ||
7619 | /* | |
7620 | * Write the authenticated password... | |
7621 | */ | |
7622 | ||
7623 | httpEncode64_2(line, sizeof(line), con->password, strlen(con->password)); | |
7624 | cupsFilePrintf(fp, "%s\n", line); | |
7625 | ||
7626 | /* | |
7627 | * Write a random number of newlines to the end of the file... | |
7628 | */ | |
7629 | ||
7630 | for (i = (rand() % 1024); i >= 0; i --) | |
7631 | cupsFilePutChar(fp, '\n'); | |
7632 | ||
7633 | /* | |
7634 | * Close the file and return... | |
7635 | */ | |
7636 | ||
7637 | cupsFileClose(fp); | |
7638 | } | |
7639 | ||
7640 | ||
7641 | /* | |
7642 | * 'send_document()' - Send a file to a printer or class. | |
7643 | */ | |
7644 | ||
7645 | static void | |
7646 | send_document(cupsd_client_t *con, /* I - Client connection */ | |
7647 | ipp_attribute_t *uri) /* I - Printer URI */ | |
7648 | { | |
7649 | ipp_attribute_t *attr; /* Current attribute */ | |
7650 | ipp_attribute_t *format; /* Document-format attribute */ | |
7651 | int jobid; /* Job ID number */ | |
7652 | cupsd_job_t *job; /* Current job */ | |
7653 | char job_uri[HTTP_MAX_URI], | |
7654 | /* Job URI */ | |
7655 | method[HTTP_MAX_URI], | |
7656 | /* Method portion of URI */ | |
7657 | username[HTTP_MAX_URI], | |
7658 | /* Username portion of URI */ | |
7659 | host[HTTP_MAX_URI], | |
7660 | /* Host portion of URI */ | |
7661 | resource[HTTP_MAX_URI]; | |
7662 | /* Resource portion of URI */ | |
7663 | int port; /* Port portion of URI */ | |
7664 | mime_type_t *filetype; /* Type of file */ | |
7665 | char super[MIME_MAX_SUPER], | |
7666 | /* Supertype of file */ | |
7667 | type[MIME_MAX_TYPE], | |
7668 | /* Subtype of file */ | |
7669 | mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2]; | |
7670 | /* Textual name of mime type */ | |
7671 | char filename[1024]; /* Job filename */ | |
7672 | cupsd_printer_t *printer; /* Current printer */ | |
7673 | struct stat fileinfo; /* File information */ | |
7674 | int kbytes; /* Size of file */ | |
7675 | int compression; /* Type of compression */ | |
7676 | ||
7677 | ||
7678 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con, | |
7679 | con->http.fd, uri->values[0].string.text); | |
7680 | ||
7681 | /* | |
7682 | * See if we have a job URI or a printer URI... | |
7683 | */ | |
7684 | ||
7685 | if (!strcmp(uri->name, "printer-uri")) | |
7686 | { | |
7687 | /* | |
7688 | * Got a printer URI; see if we also have a job-id attribute... | |
7689 | */ | |
7690 | ||
fa73b229 | 7691 | if ((attr = ippFindAttribute(con->request, "job-id", |
7692 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 7693 | { |
7694 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7695 | _("Got a printer-uri attribute but no job-id!")); | |
7696 | return; | |
7697 | } | |
7698 | ||
7699 | jobid = attr->values[0].integer; | |
7700 | } | |
7701 | else | |
7702 | { | |
7703 | /* | |
7704 | * Got a job URI; parse it to get the job ID... | |
7705 | */ | |
7706 | ||
a4d04587 | 7707 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
7708 | sizeof(method), username, sizeof(username), host, | |
7709 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 7710 | |
7711 | if (strncmp(resource, "/jobs/", 6)) | |
7712 | { | |
7713 | /* | |
7714 | * Not a valid URI! | |
7715 | */ | |
7716 | ||
7717 | send_ipp_status(con, IPP_BAD_REQUEST, | |
7718 | _("Bad job-uri attribute \"%s\"!"), | |
7719 | uri->values[0].string.text); | |
7720 | return; | |
7721 | } | |
7722 | ||
7723 | jobid = atoi(resource + 6); | |
7724 | } | |
7725 | ||
7726 | /* | |
7727 | * See if the job exists... | |
7728 | */ | |
7729 | ||
7730 | if ((job = cupsdFindJob(jobid)) == NULL) | |
7731 | { | |
7732 | /* | |
7733 | * Nope - return a "not found" error... | |
7734 | */ | |
7735 | ||
7736 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
7737 | return; | |
7738 | } | |
7739 | ||
7740 | /* | |
7741 | * See if the job is owned by the requesting user... | |
7742 | */ | |
7743 | ||
7744 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
7745 | { | |
bd7854cb | 7746 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 7747 | return; |
7748 | } | |
7749 | ||
7750 | /* | |
7751 | * OK, see if the client is sending the document compressed - CUPS | |
7752 | * only supports "none" and "gzip". | |
7753 | */ | |
7754 | ||
7755 | compression = CUPS_FILE_NONE; | |
7756 | ||
fa73b229 | 7757 | if ((attr = ippFindAttribute(con->request, "compression", |
7758 | IPP_TAG_KEYWORD)) != NULL) | |
ef416fc2 | 7759 | { |
7760 | if (strcmp(attr->values[0].string.text, "none") | |
7761 | #ifdef HAVE_LIBZ | |
7762 | && strcmp(attr->values[0].string.text, "gzip") | |
7763 | #endif /* HAVE_LIBZ */ | |
7764 | ) | |
7765 | { | |
7766 | send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"), | |
7767 | attr->values[0].string.text); | |
7768 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
7769 | "compression", NULL, attr->values[0].string.text); | |
7770 | return; | |
7771 | } | |
7772 | ||
7773 | #ifdef HAVE_LIBZ | |
7774 | if (!strcmp(attr->values[0].string.text, "gzip")) | |
7775 | compression = CUPS_FILE_GZIP; | |
7776 | #endif /* HAVE_LIBZ */ | |
7777 | } | |
7778 | ||
7779 | /* | |
7780 | * Do we have a file to print? | |
7781 | */ | |
7782 | ||
7783 | if (!con->filename) | |
7784 | { | |
7785 | send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!")); | |
7786 | return; | |
7787 | } | |
7788 | ||
7789 | /* | |
7790 | * Is it a format we support? | |
7791 | */ | |
7792 | ||
7793 | if ((format = ippFindAttribute(con->request, "document-format", | |
7794 | IPP_TAG_MIMETYPE)) != NULL) | |
7795 | { | |
7796 | /* | |
7797 | * Grab format from client... | |
7798 | */ | |
7799 | ||
7800 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) | |
7801 | { | |
7802 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
7803 | format->values[0].string.text); | |
7804 | return; | |
7805 | } | |
7806 | } | |
7807 | else | |
7808 | { | |
7809 | /* | |
7810 | * No document format attribute? Auto-type it! | |
7811 | */ | |
7812 | ||
7813 | strcpy(super, "application"); | |
7814 | strcpy(type, "octet-stream"); | |
7815 | } | |
7816 | ||
fa73b229 | 7817 | if (!strcmp(super, "application") && !strcmp(type, "octet-stream")) |
ef416fc2 | 7818 | { |
7819 | /* | |
7820 | * Auto-type the file... | |
7821 | */ | |
7822 | ||
bd7854cb | 7823 | ipp_attribute_t *doc_name; /* document-name attribute */ |
7824 | ||
7825 | ||
ef416fc2 | 7826 | cupsdLogMessage(CUPSD_LOG_DEBUG, "send_document: auto-typing file..."); |
7827 | ||
bd7854cb | 7828 | doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME); |
7829 | filetype = mimeFileType(MimeDatabase, con->filename, | |
7830 | doc_name ? doc_name->values[0].string.text : NULL, | |
7831 | &compression); | |
ef416fc2 | 7832 | |
fa73b229 | 7833 | if (filetype) |
ef416fc2 | 7834 | { |
7835 | /* | |
7836 | * Replace the document-format attribute value with the auto-typed one. | |
7837 | */ | |
7838 | ||
7839 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
7840 | filetype->type); | |
7841 | ||
fa73b229 | 7842 | if (format) |
ef416fc2 | 7843 | { |
757d2cad | 7844 | _cupsStrFree(format->values[0].string.text); |
7845 | format->values[0].string.text = _cupsStrAlloc(mimetype); | |
ef416fc2 | 7846 | } |
7847 | else | |
7848 | ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, | |
7849 | "document-format", NULL, mimetype); | |
7850 | } | |
7851 | else | |
7852 | filetype = mimeType(MimeDatabase, super, type); | |
7853 | } | |
7854 | else | |
7855 | filetype = mimeType(MimeDatabase, super, type); | |
7856 | ||
fa73b229 | 7857 | if (!filetype) |
ef416fc2 | 7858 | { |
7859 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
7860 | _("Unsupported format \'%s/%s\'!"), super, type); | |
7861 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7862 | "Hint: Do you have the raw file printing rules enabled?"); | |
7863 | ||
7864 | if (format) | |
7865 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
7866 | "document-format", NULL, format->values[0].string.text); | |
7867 | ||
7868 | return; | |
7869 | } | |
7870 | ||
80ca4592 | 7871 | printer = cupsdFindDest(job->dest); |
7872 | ||
7873 | if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype)) | |
7874 | { | |
7875 | snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, | |
7876 | filetype->type); | |
7877 | ||
7878 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
7879 | _("Unsupported format \'%s\'!"), mimetype); | |
7880 | ||
7881 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
7882 | "document-format", NULL, mimetype); | |
7883 | ||
7884 | return; | |
7885 | } | |
7886 | ||
ef416fc2 | 7887 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
7888 | "send_document: request file type is %s/%s.", | |
7889 | filetype->super, filetype->type); | |
7890 | ||
7891 | /* | |
7892 | * Add the file to the job... | |
7893 | */ | |
7894 | ||
bd7854cb | 7895 | cupsdLoadJob(job); |
7896 | ||
ef416fc2 | 7897 | if (add_file(con, job, filetype, compression)) |
7898 | return; | |
7899 | ||
ef416fc2 | 7900 | if (stat(con->filename, &fileinfo)) |
7901 | kbytes = 0; | |
7902 | else | |
7903 | kbytes = (fileinfo.st_size + 1023) / 1024; | |
7904 | ||
7905 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
7906 | ||
fa73b229 | 7907 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", |
7908 | IPP_TAG_INTEGER)) != NULL) | |
ef416fc2 | 7909 | attr->values[0].integer += kbytes; |
7910 | ||
7911 | snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, | |
7912 | job->num_files); | |
7913 | rename(con->filename, filename); | |
7914 | ||
7915 | cupsdClearString(&con->filename); | |
7916 | ||
7917 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7918 | "File of type %s/%s queued in job #%d by \"%s\".", | |
7919 | filetype->super, filetype->type, job->id, job->username); | |
7920 | ||
7921 | /* | |
7922 | * Start the job if this is the last document... | |
7923 | */ | |
7924 | ||
fa73b229 | 7925 | if ((attr = ippFindAttribute(con->request, "last-document", |
7926 | IPP_TAG_BOOLEAN)) != NULL && | |
ef416fc2 | 7927 | attr->values[0].boolean) |
7928 | { | |
7929 | /* | |
7930 | * See if we need to add the ending sheet... | |
7931 | */ | |
7932 | ||
fa73b229 | 7933 | if (printer && |
ef416fc2 | 7934 | !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && |
fa73b229 | 7935 | (attr = ippFindAttribute(job->attrs, "job-sheets", |
7936 | IPP_TAG_ZERO)) != NULL && | |
ef416fc2 | 7937 | attr->num_values > 1) |
7938 | { | |
7939 | /* | |
7940 | * Yes... | |
7941 | */ | |
7942 | ||
7943 | cupsdLogMessage(CUPSD_LOG_INFO, | |
7944 | "Adding end banner page \"%s\" to job %d.", | |
7945 | attr->values[1].string.text, job->id); | |
7946 | ||
7947 | kbytes = copy_banner(con, job, attr->values[1].string.text); | |
7948 | ||
7949 | cupsdUpdateQuota(printer, job->username, 0, kbytes); | |
7950 | } | |
7951 | ||
bd7854cb | 7952 | if (job->state_value == IPP_JOB_STOPPED) |
7953 | { | |
ef416fc2 | 7954 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 7955 | job->state_value = IPP_JOB_PENDING; |
7956 | } | |
7957 | else if (job->state_value == IPP_JOB_HELD) | |
ef416fc2 | 7958 | { |
fa73b229 | 7959 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7960 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7961 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7962 | ||
fa73b229 | 7963 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
bd7854cb | 7964 | { |
ef416fc2 | 7965 | job->state->values[0].integer = IPP_JOB_PENDING; |
bd7854cb | 7966 | job->state_value = IPP_JOB_PENDING; |
7967 | } | |
ef416fc2 | 7968 | } |
7969 | ||
7970 | cupsdSaveJob(job); | |
7971 | ||
7972 | /* | |
7973 | * Start the job if possible... Since cupsdCheckJobs() can cancel a | |
7974 | * job if it doesn't print, we need to re-find the job afterwards... | |
7975 | */ | |
7976 | ||
7977 | jobid = job->id; | |
7978 | ||
7979 | cupsdCheckJobs(); | |
7980 | ||
7981 | job = cupsdFindJob(jobid); | |
7982 | } | |
7983 | else | |
7984 | { | |
fa73b229 | 7985 | if ((attr = ippFindAttribute(job->attrs, "job-hold-until", |
7986 | IPP_TAG_KEYWORD)) == NULL) | |
ef416fc2 | 7987 | attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME); |
7988 | ||
fa73b229 | 7989 | if (!attr || !strcmp(attr->values[0].string.text, "no-hold")) |
ef416fc2 | 7990 | { |
7991 | job->state->values[0].integer = IPP_JOB_HELD; | |
bd7854cb | 7992 | job->state_value = IPP_JOB_HELD; |
ef416fc2 | 7993 | job->hold_until = time(NULL) + 60; |
7994 | cupsdSaveJob(job); | |
7995 | } | |
7996 | } | |
7997 | ||
7998 | /* | |
7999 | * Fill in the response info... | |
8000 | */ | |
8001 | ||
8002 | snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName, | |
8003 | LocalPort, jobid); | |
8004 | ||
8005 | ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, | |
8006 | job_uri); | |
8007 | ||
8008 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid); | |
8009 | ||
8010 | ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", | |
bd7854cb | 8011 | job ? job->state_value : IPP_JOB_CANCELLED); |
ef416fc2 | 8012 | add_job_state_reasons(con, job); |
8013 | ||
8014 | con->response->request.status.status_code = IPP_OK; | |
8015 | } | |
8016 | ||
8017 | ||
8018 | /* | |
8019 | * 'send_http_error()' - Send a HTTP error back to the IPP client. | |
8020 | */ | |
8021 | ||
8022 | static void | |
8023 | send_http_error(cupsd_client_t *con, /* I - Client connection */ | |
8024 | http_status_t status) /* I - HTTP status code */ | |
8025 | { | |
8026 | cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s", | |
8027 | ippOpString(con->request->request.op.operation_id), | |
8028 | httpStatus(status)); | |
8029 | ||
8030 | cupsdSendError(con, status); | |
8031 | ||
8032 | ippDelete(con->response); | |
8033 | con->response = NULL; | |
8034 | ||
8035 | return; | |
8036 | } | |
8037 | ||
8038 | ||
8039 | /* | |
8040 | * 'send_ipp_status()' - Send a status back to the IPP client. | |
8041 | */ | |
8042 | ||
8043 | static void | |
8044 | send_ipp_status(cupsd_client_t *con, /* I - Client connection */ | |
8045 | ipp_status_t status, /* I - IPP status code */ | |
8046 | const char *message, /* I - Status message */ | |
8047 | ...) /* I - Additional args as needed */ | |
8048 | { | |
8049 | va_list ap; /* Pointer to additional args */ | |
8050 | char formatted[1024]; /* Formatted errror message */ | |
8051 | ||
8052 | ||
8053 | if (message) | |
8054 | { | |
8055 | va_start(ap, message); | |
8056 | vsnprintf(formatted, sizeof(formatted), | |
8057 | _cupsLangString(con->language, message), ap); | |
8058 | va_end(ap); | |
8059 | ||
8060 | cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO, | |
8061 | "%s %s: %s", | |
8062 | ippOpString(con->request->request.op.operation_id), | |
8063 | ippErrorString(status), formatted); | |
8064 | } | |
8065 | else | |
8066 | cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO, | |
8067 | "%s %s", | |
8068 | ippOpString(con->request->request.op.operation_id), | |
8069 | ippErrorString(status)); | |
8070 | ||
8071 | con->response->request.status.status_code = status; | |
8072 | ||
fa73b229 | 8073 | if (ippFindAttribute(con->response, "attributes-charset", |
8074 | IPP_TAG_ZERO) == NULL) | |
ef416fc2 | 8075 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
8076 | "attributes-charset", NULL, DefaultCharset); | |
8077 | ||
8078 | if (ippFindAttribute(con->response, "attributes-natural-language", | |
8079 | IPP_TAG_ZERO) == NULL) | |
8080 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
8081 | "attributes-natural-language", NULL, DefaultLanguage); | |
8082 | ||
8083 | if (message) | |
8084 | ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
8085 | "status-message", NULL, formatted); | |
8086 | } | |
8087 | ||
8088 | ||
8089 | /* | |
8090 | * 'set_default()' - Set the default destination... | |
8091 | */ | |
8092 | ||
8093 | static void | |
8094 | set_default(cupsd_client_t *con, /* I - Client connection */ | |
8095 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8096 | { | |
8097 | http_status_t status; /* Policy status */ | |
8098 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
8099 | char method[HTTP_MAX_URI], | |
8100 | /* Method portion of URI */ | |
8101 | username[HTTP_MAX_URI], | |
8102 | /* Username portion of URI */ | |
8103 | host[HTTP_MAX_URI], | |
8104 | /* Host portion of URI */ | |
8105 | resource[HTTP_MAX_URI]; | |
8106 | /* Resource portion of URI */ | |
8107 | int port; /* Port portion of URI */ | |
8108 | const char *name; /* Printer name */ | |
8109 | cupsd_printer_t *printer; /* Printer */ | |
8110 | ||
8111 | ||
8112 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con, | |
8113 | con->http.fd, uri->values[0].string.text); | |
8114 | ||
8115 | /* | |
8116 | * Is the destination valid? | |
8117 | */ | |
8118 | ||
a4d04587 | 8119 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8120 | sizeof(method), username, sizeof(username), host, | |
8121 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8122 | |
8123 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
8124 | { | |
8125 | /* | |
8126 | * Bad URI... | |
8127 | */ | |
8128 | ||
8129 | send_ipp_status(con, IPP_NOT_FOUND, | |
8130 | _("The printer or class was not found.")); | |
8131 | return; | |
8132 | } | |
8133 | ||
8134 | /* | |
8135 | * Check policy... | |
8136 | */ | |
8137 | ||
8138 | if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK) | |
8139 | { | |
8140 | send_http_error(con, status); | |
8141 | return; | |
8142 | } | |
8143 | ||
8144 | /* | |
8145 | * Set it as the default... | |
8146 | */ | |
8147 | ||
8148 | DefaultPrinter = printer; | |
8149 | ||
8150 | cupsdSaveAllPrinters(); | |
8151 | cupsdSaveAllClasses(); | |
8152 | ||
8153 | cupsdWritePrintcap(); | |
8154 | ||
8155 | cupsdLogMessage(CUPSD_LOG_INFO, | |
8156 | "Default destination set to \"%s\" by \"%s\".", name, | |
e00b005a | 8157 | get_username(con)); |
ef416fc2 | 8158 | |
8159 | /* | |
8160 | * Everything was ok, so return OK status... | |
8161 | */ | |
8162 | ||
8163 | con->response->request.status.status_code = IPP_OK; | |
8164 | } | |
8165 | ||
8166 | ||
8167 | /* | |
8168 | * 'set_job_attrs()' - Set job attributes. | |
8169 | */ | |
8170 | ||
8171 | static void | |
8172 | set_job_attrs(cupsd_client_t *con, /* I - Client connection */ | |
8173 | ipp_attribute_t *uri) /* I - Job URI */ | |
8174 | { | |
8175 | ipp_attribute_t *attr, /* Current attribute */ | |
8176 | *attr2; /* Job attribute */ | |
8177 | int jobid; /* Job ID */ | |
8178 | cupsd_job_t *job; /* Current job */ | |
8179 | char method[HTTP_MAX_URI], | |
8180 | /* Method portion of URI */ | |
8181 | username[HTTP_MAX_URI], | |
8182 | /* Username portion of URI */ | |
8183 | host[HTTP_MAX_URI], | |
8184 | /* Host portion of URI */ | |
8185 | resource[HTTP_MAX_URI]; | |
8186 | /* Resource portion of URI */ | |
8187 | int port; /* Port portion of URI */ | |
8188 | ||
8189 | ||
8190 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con, | |
8191 | con->http.fd, uri->values[0].string.text); | |
8192 | ||
8193 | /* | |
8194 | * Start with "everything is OK" status... | |
8195 | */ | |
8196 | ||
8197 | con->response->request.status.status_code = IPP_OK; | |
8198 | ||
8199 | /* | |
8200 | * See if we have a job URI or a printer URI... | |
8201 | */ | |
8202 | ||
fa73b229 | 8203 | if (!strcmp(uri->name, "printer-uri")) |
ef416fc2 | 8204 | { |
8205 | /* | |
8206 | * Got a printer URI; see if we also have a job-id attribute... | |
8207 | */ | |
8208 | ||
fa73b229 | 8209 | if ((attr = ippFindAttribute(con->request, "job-id", |
8210 | IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 8211 | { |
8212 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8213 | _("Got a printer-uri attribute but no job-id!")); | |
8214 | return; | |
8215 | } | |
8216 | ||
8217 | jobid = attr->values[0].integer; | |
8218 | } | |
8219 | else | |
8220 | { | |
8221 | /* | |
8222 | * Got a job URI; parse it to get the job ID... | |
8223 | */ | |
8224 | ||
a4d04587 | 8225 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8226 | sizeof(method), username, sizeof(username), host, | |
8227 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8228 | |
fa73b229 | 8229 | if (strncmp(resource, "/jobs/", 6)) |
ef416fc2 | 8230 | { |
8231 | /* | |
8232 | * Not a valid URI! | |
8233 | */ | |
8234 | ||
8235 | send_ipp_status(con, IPP_BAD_REQUEST, | |
8236 | _("Bad job-uri attribute \"%s\"!"), | |
8237 | uri->values[0].string.text); | |
8238 | return; | |
8239 | } | |
8240 | ||
8241 | jobid = atoi(resource + 6); | |
8242 | } | |
8243 | ||
8244 | /* | |
8245 | * See if the job exists... | |
8246 | */ | |
8247 | ||
8248 | if ((job = cupsdFindJob(jobid)) == NULL) | |
8249 | { | |
8250 | /* | |
8251 | * Nope - return a "not found" error... | |
8252 | */ | |
8253 | ||
8254 | send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid); | |
8255 | return; | |
8256 | } | |
8257 | ||
8258 | /* | |
8259 | * See if the job has been completed... | |
8260 | */ | |
8261 | ||
bd7854cb | 8262 | if (job->state_value > IPP_JOB_STOPPED) |
ef416fc2 | 8263 | { |
8264 | /* | |
8265 | * Return a "not-possible" error... | |
8266 | */ | |
8267 | ||
8268 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8269 | _("Job #%d is finished and cannot be altered!"), jobid); | |
8270 | return; | |
8271 | } | |
8272 | ||
8273 | /* | |
8274 | * See if the job is owned by the requesting user... | |
8275 | */ | |
8276 | ||
8277 | if (!validate_user(job, con, job->username, username, sizeof(username))) | |
8278 | { | |
bd7854cb | 8279 | send_http_error(con, HTTP_UNAUTHORIZED); |
ef416fc2 | 8280 | return; |
8281 | } | |
8282 | ||
8283 | /* | |
8284 | * See what the user wants to change. | |
8285 | */ | |
8286 | ||
bd7854cb | 8287 | cupsdLoadJob(job); |
8288 | ||
fa73b229 | 8289 | for (attr = con->request->attrs; attr; attr = attr->next) |
ef416fc2 | 8290 | { |
8291 | if (attr->group_tag != IPP_TAG_JOB || !attr->name) | |
8292 | continue; | |
8293 | ||
8294 | if (!strcmp(attr->name, "attributes-charset") || | |
8295 | !strcmp(attr->name, "attributes-natural-language") || | |
8296 | !strcmp(attr->name, "document-compression") || | |
8297 | !strcmp(attr->name, "document-format") || | |
8298 | !strcmp(attr->name, "job-detailed-status-messages") || | |
8299 | !strcmp(attr->name, "job-document-access-errors") || | |
8300 | !strcmp(attr->name, "job-id") || | |
8301 | !strcmp(attr->name, "job-k-octets") || | |
8302 | !strcmp(attr->name, "job-originating-host-name") || | |
8303 | !strcmp(attr->name, "job-originating-user-name") || | |
8304 | !strcmp(attr->name, "job-printer-up-time") || | |
8305 | !strcmp(attr->name, "job-printer-uri") || | |
8306 | !strcmp(attr->name, "job-sheets") || | |
8307 | !strcmp(attr->name, "job-state-message") || | |
8308 | !strcmp(attr->name, "job-state-reasons") || | |
8309 | !strcmp(attr->name, "job-uri") || | |
8310 | !strcmp(attr->name, "number-of-documents") || | |
8311 | !strcmp(attr->name, "number-of-intervening-jobs") || | |
8312 | !strcmp(attr->name, "output-device-assigned") || | |
8313 | !strncmp(attr->name, "date-time-at-", 13) || | |
8314 | !strncmp(attr->name, "job-impressions", 15) || | |
8315 | !strncmp(attr->name, "job-k-octets", 12) || | |
8316 | !strncmp(attr->name, "job-media-sheets", 16) || | |
8317 | !strncmp(attr->name, "time-at-", 8)) | |
8318 | { | |
8319 | /* | |
8320 | * Read-only attrs! | |
8321 | */ | |
8322 | ||
8323 | send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE, | |
8324 | _("%s cannot be changed."), attr->name); | |
8325 | ||
8326 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8327 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8328 | ||
8329 | continue; | |
8330 | } | |
8331 | ||
8332 | if (!strcmp(attr->name, "job-priority")) | |
8333 | { | |
8334 | /* | |
8335 | * Change the job priority... | |
8336 | */ | |
8337 | ||
8338 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8339 | { | |
8340 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!")); | |
8341 | ||
8342 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8343 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8344 | } | |
bd7854cb | 8345 | else if (job->state_value >= IPP_JOB_PROCESSING) |
ef416fc2 | 8346 | { |
8347 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8348 | _("Job is completed and cannot be changed.")); | |
8349 | return; | |
8350 | } | |
8351 | else if (con->response->request.status.status_code == IPP_OK) | |
8352 | cupsdSetJobPriority(job, attr->values[0].integer); | |
8353 | } | |
8354 | else if (!strcmp(attr->name, "job-state")) | |
8355 | { | |
8356 | /* | |
8357 | * Change the job state... | |
8358 | */ | |
8359 | ||
8360 | if (attr->value_tag != IPP_TAG_ENUM) | |
8361 | { | |
8362 | send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!")); | |
8363 | ||
8364 | if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL) | |
8365 | attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP; | |
8366 | } | |
8367 | else | |
8368 | { | |
8369 | switch (attr->values[0].integer) | |
8370 | { | |
8371 | case IPP_JOB_PENDING : | |
8372 | case IPP_JOB_HELD : | |
bd7854cb | 8373 | if (job->state_value > IPP_JOB_HELD) |
ef416fc2 | 8374 | { |
8375 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8376 | _("Job state cannot be changed.")); | |
8377 | return; | |
8378 | } | |
8379 | else if (con->response->request.status.status_code == IPP_OK) | |
bd7854cb | 8380 | { |
ef416fc2 | 8381 | job->state->values[0].integer = attr->values[0].integer; |
bd7854cb | 8382 | job->state_value = attr->values[0].integer; |
8383 | } | |
ef416fc2 | 8384 | break; |
8385 | ||
8386 | case IPP_JOB_PROCESSING : | |
8387 | case IPP_JOB_STOPPED : | |
bd7854cb | 8388 | if (job->state_value != attr->values[0].integer) |
ef416fc2 | 8389 | { |
8390 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8391 | _("Job state cannot be changed.")); | |
8392 | return; | |
8393 | } | |
8394 | break; | |
8395 | ||
8396 | case IPP_JOB_CANCELLED : | |
8397 | case IPP_JOB_ABORTED : | |
8398 | case IPP_JOB_COMPLETED : | |
bd7854cb | 8399 | if (job->state_value > IPP_JOB_PROCESSING) |
ef416fc2 | 8400 | { |
8401 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8402 | _("Job state cannot be changed.")); | |
8403 | return; | |
8404 | } | |
8405 | else if (con->response->request.status.status_code == IPP_OK) | |
8406 | { | |
8407 | cupsdCancelJob(job, 0); | |
8408 | ||
8409 | if (JobHistory) | |
8410 | { | |
8411 | job->state->values[0].integer = attr->values[0].integer; | |
bd7854cb | 8412 | job->state_value = attr->values[0].integer; |
ef416fc2 | 8413 | cupsdSaveJob(job); |
8414 | } | |
8415 | } | |
8416 | break; | |
8417 | } | |
8418 | } | |
8419 | } | |
8420 | else if (con->response->request.status.status_code != IPP_OK) | |
8421 | continue; | |
fa73b229 | 8422 | else if ((attr2 = ippFindAttribute(job->attrs, attr->name, |
8423 | IPP_TAG_ZERO)) != NULL) | |
ef416fc2 | 8424 | { |
8425 | /* | |
8426 | * Some other value; first free the old value... | |
8427 | */ | |
8428 | ||
8429 | if (job->attrs->prev) | |
8430 | job->attrs->prev->next = attr2->next; | |
8431 | else | |
8432 | job->attrs->attrs = attr2->next; | |
8433 | ||
8434 | if (job->attrs->last == attr2) | |
8435 | job->attrs->last = job->attrs->prev; | |
8436 | ||
757d2cad | 8437 | _ippFreeAttr(attr2); |
ef416fc2 | 8438 | |
8439 | /* | |
8440 | * Then copy the attribute... | |
8441 | */ | |
8442 | ||
8443 | copy_attribute(job->attrs, attr, 0); | |
8444 | ||
8445 | /* | |
8446 | * See if the job-name or job-hold-until is being changed. | |
8447 | */ | |
8448 | ||
8449 | if (!strcmp(attr->name, "job-hold-until")) | |
8450 | { | |
8451 | cupsdSetJobHoldUntil(job, attr->values[0].string.text); | |
8452 | ||
8453 | if (!strcmp(attr->values[0].string.text, "no-hold")) | |
8454 | cupsdReleaseJob(job); | |
8455 | else | |
8456 | cupsdHoldJob(job); | |
8457 | } | |
8458 | } | |
8459 | else if (attr->value_tag == IPP_TAG_DELETEATTR) | |
8460 | { | |
8461 | /* | |
8462 | * Delete the attribute... | |
8463 | */ | |
8464 | ||
8465 | if ((attr2 = ippFindAttribute(job->attrs, attr->name, | |
8466 | IPP_TAG_ZERO)) != NULL) | |
8467 | { | |
8468 | if (job->attrs->prev) | |
8469 | job->attrs->prev->next = attr2->next; | |
8470 | else | |
8471 | job->attrs->attrs = attr2->next; | |
8472 | ||
8473 | if (attr2 == job->attrs->last) | |
8474 | job->attrs->last = job->attrs->prev; | |
8475 | ||
757d2cad | 8476 | _ippFreeAttr(attr2); |
ef416fc2 | 8477 | } |
8478 | } | |
8479 | else | |
8480 | { | |
8481 | /* | |
8482 | * Add new option by copying it... | |
8483 | */ | |
8484 | ||
8485 | copy_attribute(job->attrs, attr, 0); | |
8486 | } | |
8487 | } | |
8488 | ||
8489 | /* | |
8490 | * Save the job... | |
8491 | */ | |
8492 | ||
8493 | cupsdSaveJob(job); | |
8494 | ||
8495 | /* | |
8496 | * Start jobs if possible... | |
8497 | */ | |
8498 | ||
8499 | cupsdCheckJobs(); | |
8500 | } | |
8501 | ||
8502 | ||
b423cd4c | 8503 | /* |
8504 | * 'set_printer_defaults()' - Set printer default options from a request. | |
8505 | */ | |
8506 | ||
8507 | static void | |
8508 | set_printer_defaults( | |
8509 | cupsd_client_t *con, /* I - Client connection */ | |
8510 | cupsd_printer_t *printer) /* I - Printer */ | |
8511 | { | |
8512 | int i; /* Looping var */ | |
8513 | ipp_attribute_t *attr; /* Current attribute */ | |
8514 | int namelen; /* Length of attribute name */ | |
8515 | char name[256], /* New attribute name */ | |
8516 | value[256]; /* String version of integer attrs */ | |
8517 | ||
8518 | ||
8519 | for (attr = con->request->attrs; attr; attr = attr->next) | |
8520 | { | |
8521 | /* | |
8522 | * Skip non-printer attributes... | |
8523 | */ | |
8524 | ||
8525 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
8526 | continue; | |
8527 | ||
8528 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name); | |
8529 | ||
8530 | if (!strcmp(attr->name, "job-sheets-default")) | |
8531 | { | |
8532 | /* | |
8533 | * Only allow keywords and names... | |
8534 | */ | |
8535 | ||
8536 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
8537 | continue; | |
8538 | ||
8539 | /* | |
8540 | * Only allow job-sheets-default to be set when running without a | |
8541 | * system high classification level... | |
8542 | */ | |
8543 | ||
8544 | if (Classification) | |
8545 | continue; | |
8546 | ||
8547 | cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text); | |
8548 | ||
8549 | if (attr->num_values > 1) | |
8550 | cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text); | |
8551 | else | |
8552 | cupsdSetString(&printer->job_sheets[1], "none"); | |
8553 | } | |
8554 | else if (!strcmp(attr->name, "requesting-user-name-allowed")) | |
8555 | { | |
8556 | cupsdFreePrinterUsers(printer); | |
8557 | ||
8558 | printer->deny_users = 0; | |
8559 | ||
8560 | if (attr->value_tag == IPP_TAG_NAME && | |
8561 | (attr->num_values > 1 || | |
8562 | strcmp(attr->values[0].string.text, "all"))) | |
8563 | { | |
8564 | for (i = 0; i < attr->num_values; i ++) | |
8565 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
8566 | } | |
8567 | } | |
8568 | else if (!strcmp(attr->name, "requesting-user-name-denied")) | |
8569 | { | |
8570 | cupsdFreePrinterUsers(printer); | |
8571 | ||
8572 | printer->deny_users = 1; | |
8573 | ||
8574 | if (attr->value_tag == IPP_TAG_NAME && | |
8575 | (attr->num_values > 1 || | |
8576 | strcmp(attr->values[0].string.text, "none"))) | |
8577 | { | |
8578 | for (i = 0; i < attr->num_values; i ++) | |
8579 | cupsdAddPrinterUser(printer, attr->values[i].string.text); | |
8580 | } | |
8581 | } | |
8582 | else if (!strcmp(attr->name, "job-quota-period")) | |
8583 | { | |
8584 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8585 | continue; | |
8586 | ||
8587 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...", | |
8588 | attr->values[0].integer); | |
8589 | cupsdFreeQuotas(printer); | |
8590 | ||
8591 | printer->quota_period = attr->values[0].integer; | |
8592 | } | |
8593 | else if (!strcmp(attr->name, "job-k-limit")) | |
8594 | { | |
8595 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8596 | continue; | |
8597 | ||
8598 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...", | |
8599 | attr->values[0].integer); | |
8600 | cupsdFreeQuotas(printer); | |
8601 | ||
8602 | printer->k_limit = attr->values[0].integer; | |
8603 | } | |
8604 | else if (!strcmp(attr->name, "job-page-limit")) | |
8605 | { | |
8606 | if (attr->value_tag != IPP_TAG_INTEGER) | |
8607 | continue; | |
8608 | ||
8609 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...", | |
8610 | attr->values[0].integer); | |
8611 | cupsdFreeQuotas(printer); | |
8612 | ||
8613 | printer->page_limit = attr->values[0].integer; | |
8614 | } | |
8615 | else if (!strcmp(attr->name, "printer-op-policy")) | |
8616 | { | |
8617 | cupsd_policy_t *p; /* Policy */ | |
8618 | ||
8619 | ||
8620 | if (attr->value_tag != IPP_TAG_NAME) | |
8621 | continue; | |
8622 | ||
8623 | if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL) | |
8624 | { | |
8625 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8626 | "Setting printer-op-policy to \"%s\"...", | |
8627 | attr->values[0].string.text); | |
8628 | cupsdSetString(&printer->op_policy, attr->values[0].string.text); | |
8629 | printer->op_policy_ptr = p; | |
8630 | } | |
8631 | else | |
8632 | { | |
8633 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8634 | _("Unknown printer-op-policy \"%s\"."), | |
8635 | attr->values[0].string.text); | |
8636 | return; | |
8637 | } | |
8638 | } | |
8639 | else if (!strcmp(attr->name, "printer-error-policy")) | |
8640 | { | |
8641 | if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD) | |
8642 | continue; | |
8643 | ||
8644 | if (strcmp(attr->values[0].string.text, "abort-job") && | |
8645 | strcmp(attr->values[0].string.text, "retry-job") && | |
8646 | strcmp(attr->values[0].string.text, "stop-printer")) | |
8647 | { | |
8648 | send_ipp_status(con, IPP_NOT_POSSIBLE, | |
8649 | _("Unknown printer-error-policy \"%s\"."), | |
8650 | attr->values[0].string.text); | |
8651 | return; | |
8652 | } | |
8653 | ||
8654 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8655 | "Setting printer-error-policy to \"%s\"...", | |
8656 | attr->values[0].string.text); | |
8657 | cupsdSetString(&printer->error_policy, attr->values[0].string.text); | |
8658 | } | |
8659 | else if (!strcmp(attr->name, "document-format-default") || | |
8660 | !strcmp(attr->name, "notify-lease-duration-default") || | |
8661 | !strcmp(attr->name, "notify-notify-events-default")) | |
8662 | continue; | |
8663 | ||
8664 | /* | |
8665 | * Skip any other non-default attributes... | |
8666 | */ | |
8667 | ||
8668 | namelen = strlen(attr->name); | |
8669 | if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") || | |
8670 | namelen > (sizeof(name) - 1) || attr->num_values != 1) | |
8671 | continue; | |
8672 | ||
8673 | /* | |
8674 | * OK, anything else must be a user-defined default... | |
8675 | */ | |
8676 | ||
8677 | strlcpy(name, attr->name, sizeof(name)); | |
8678 | name[namelen - 8] = '\0'; /* Strip "-default" */ | |
8679 | ||
8680 | switch (attr->value_tag) | |
8681 | { | |
8682 | case IPP_TAG_DELETEATTR : | |
8683 | printer->num_options = cupsRemoveOption(name, | |
8684 | printer->num_options, | |
8685 | &(printer->options)); | |
8686 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8687 | "Deleting %s", attr->name); | |
8688 | break; | |
8689 | ||
8690 | case IPP_TAG_NAME : | |
8691 | case IPP_TAG_KEYWORD : | |
8692 | case IPP_TAG_URI : | |
8693 | printer->num_options = cupsAddOption(name, | |
8694 | attr->values[0].string.text, | |
8695 | printer->num_options, | |
8696 | &(printer->options)); | |
8697 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8698 | "Setting %s to \"%s\"...", attr->name, | |
8699 | attr->values[0].string.text); | |
8700 | break; | |
8701 | ||
8702 | case IPP_TAG_BOOLEAN : | |
8703 | printer->num_options = cupsAddOption(name, | |
8704 | attr->values[0].boolean ? | |
8705 | "true" : "false", | |
8706 | printer->num_options, | |
8707 | &(printer->options)); | |
8708 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8709 | "Setting %s to %s...", attr->name, | |
8710 | attr->values[0].boolean ? "true" : "false"); | |
8711 | break; | |
8712 | ||
8713 | case IPP_TAG_INTEGER : | |
8714 | case IPP_TAG_ENUM : | |
8715 | sprintf(value, "%d", attr->values[0].integer); | |
8716 | printer->num_options = cupsAddOption(name, value, | |
8717 | printer->num_options, | |
8718 | &(printer->options)); | |
8719 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8720 | "Setting %s to %s...", attr->name, value); | |
8721 | break; | |
8722 | ||
8723 | case IPP_TAG_RANGE : | |
8724 | sprintf(value, "%d-%d", attr->values[0].range.lower, | |
8725 | attr->values[0].range.upper); | |
8726 | printer->num_options = cupsAddOption(name, value, | |
8727 | printer->num_options, | |
8728 | &(printer->options)); | |
8729 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8730 | "Setting %s to %s...", attr->name, value); | |
8731 | break; | |
8732 | ||
8733 | case IPP_TAG_RESOLUTION : | |
8734 | sprintf(value, "%dx%d%s", attr->values[0].resolution.xres, | |
8735 | attr->values[0].resolution.yres, | |
8736 | attr->values[0].resolution.units == IPP_RES_PER_INCH ? | |
8737 | "dpi" : "dpc"); | |
8738 | printer->num_options = cupsAddOption(name, value, | |
8739 | printer->num_options, | |
8740 | &(printer->options)); | |
8741 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
8742 | "Setting %s to %s...", attr->name, value); | |
8743 | break; | |
8744 | ||
8745 | default : | |
8746 | /* Do nothing for other values */ | |
8747 | break; | |
8748 | } | |
8749 | } | |
8750 | } | |
8751 | ||
8752 | ||
ef416fc2 | 8753 | /* |
8754 | * 'start_printer()' - Start a printer. | |
8755 | */ | |
8756 | ||
8757 | static void | |
8758 | start_printer(cupsd_client_t *con, /* I - Client connection */ | |
8759 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8760 | { | |
8761 | http_status_t status; /* Policy status */ | |
8762 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
8763 | char method[HTTP_MAX_URI], | |
8764 | /* Method portion of URI */ | |
8765 | username[HTTP_MAX_URI], | |
8766 | /* Username portion of URI */ | |
8767 | host[HTTP_MAX_URI], | |
8768 | /* Host portion of URI */ | |
8769 | resource[HTTP_MAX_URI]; | |
8770 | /* Resource portion of URI */ | |
8771 | int port; /* Port portion of URI */ | |
8772 | const char *name; /* Printer name */ | |
8773 | cupsd_printer_t *printer; /* Printer data */ | |
8774 | ||
8775 | ||
8776 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con, | |
8777 | con->http.fd, uri->values[0].string.text); | |
8778 | ||
8779 | /* | |
8780 | * Is the destination valid? | |
8781 | */ | |
8782 | ||
a4d04587 | 8783 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8784 | sizeof(method), username, sizeof(username), host, | |
8785 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8786 | |
8787 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
8788 | { | |
8789 | /* | |
8790 | * Bad URI... | |
8791 | */ | |
8792 | ||
8793 | send_ipp_status(con, IPP_NOT_FOUND, | |
8794 | _("The printer or class was not found.")); | |
8795 | return; | |
8796 | } | |
8797 | ||
8798 | /* | |
8799 | * Check policy... | |
8800 | */ | |
8801 | ||
8802 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8803 | { | |
8804 | send_http_error(con, status); | |
8805 | return; | |
8806 | } | |
8807 | ||
8808 | /* | |
8809 | * Start the printer... | |
8810 | */ | |
8811 | ||
8812 | printer->state_message[0] = '\0'; | |
8813 | ||
8814 | cupsdStartPrinter(printer, 1); | |
8815 | ||
8816 | if (dtype & CUPS_PRINTER_CLASS) | |
e53920b9 | 8817 | { |
ef416fc2 | 8818 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", name, |
e00b005a | 8819 | get_username(con)); |
e53920b9 | 8820 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
8821 | "Class \"%s\" started by \"%s\".", name, get_username(con)); | |
8822 | } | |
e00b005a | 8823 | else |
e53920b9 | 8824 | { |
ef416fc2 | 8825 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", name, |
e00b005a | 8826 | get_username(con)); |
e53920b9 | 8827 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
8828 | "Printer \"%s\" started by \"%s\".", name, get_username(con)); | |
8829 | } | |
ef416fc2 | 8830 | |
8831 | cupsdCheckJobs(); | |
8832 | ||
8833 | /* | |
8834 | * Everything was ok, so return OK status... | |
8835 | */ | |
8836 | ||
8837 | con->response->request.status.status_code = IPP_OK; | |
8838 | } | |
8839 | ||
8840 | ||
8841 | /* | |
8842 | * 'stop_printer()' - Stop a printer. | |
8843 | */ | |
8844 | ||
8845 | static void | |
8846 | stop_printer(cupsd_client_t *con, /* I - Client connection */ | |
8847 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8848 | { | |
8849 | http_status_t status; /* Policy status */ | |
8850 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
8851 | char method[HTTP_MAX_URI], | |
8852 | /* Method portion of URI */ | |
8853 | username[HTTP_MAX_URI], | |
8854 | /* Username portion of URI */ | |
8855 | host[HTTP_MAX_URI], | |
8856 | /* Host portion of URI */ | |
8857 | resource[HTTP_MAX_URI]; | |
8858 | /* Resource portion of URI */ | |
8859 | int port; /* Port portion of URI */ | |
8860 | const char *name; /* Printer name */ | |
8861 | cupsd_printer_t *printer; /* Printer data */ | |
8862 | ipp_attribute_t *attr; /* printer-state-message attribute */ | |
8863 | ||
8864 | ||
8865 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con, | |
8866 | con->http.fd, uri->values[0].string.text); | |
8867 | ||
8868 | /* | |
8869 | * Is the destination valid? | |
8870 | */ | |
8871 | ||
a4d04587 | 8872 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
8873 | sizeof(method), username, sizeof(username), host, | |
8874 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 8875 | |
8876 | if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL) | |
8877 | { | |
8878 | /* | |
8879 | * Bad URI... | |
8880 | */ | |
8881 | ||
8882 | send_ipp_status(con, IPP_NOT_FOUND, | |
8883 | _("The printer or class was not found.")); | |
8884 | return; | |
8885 | } | |
8886 | ||
8887 | /* | |
8888 | * Check policy... | |
8889 | */ | |
8890 | ||
8891 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
8892 | { | |
8893 | send_http_error(con, status); | |
8894 | return; | |
8895 | } | |
8896 | ||
8897 | /* | |
8898 | * Stop the printer... | |
8899 | */ | |
8900 | ||
8901 | if ((attr = ippFindAttribute(con->request, "printer-state-message", | |
8902 | IPP_TAG_TEXT)) == NULL) | |
8903 | strcpy(printer->state_message, "Paused"); | |
8904 | else | |
8905 | { | |
8906 | strlcpy(printer->state_message, attr->values[0].string.text, | |
8907 | sizeof(printer->state_message)); | |
8908 | } | |
8909 | ||
8910 | cupsdStopPrinter(printer, 1); | |
8911 | ||
8912 | if (dtype & CUPS_PRINTER_CLASS) | |
e53920b9 | 8913 | { |
ef416fc2 | 8914 | cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", name, |
e00b005a | 8915 | get_username(con)); |
e53920b9 | 8916 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
8917 | "Class \"%s\" stopped by \"%s\".", name, get_username(con)); | |
8918 | } | |
ef416fc2 | 8919 | else |
e53920b9 | 8920 | { |
ef416fc2 | 8921 | cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", name, |
e00b005a | 8922 | get_username(con)); |
e53920b9 | 8923 | cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL, |
8924 | "Printer \"%s\" stopped by \"%s\".", name, get_username(con)); | |
8925 | } | |
ef416fc2 | 8926 | |
8927 | /* | |
8928 | * Everything was ok, so return OK status... | |
8929 | */ | |
8930 | ||
8931 | con->response->request.status.status_code = IPP_OK; | |
8932 | } | |
8933 | ||
8934 | ||
8935 | /* | |
8936 | * 'user_allowed()' - See if a user is allowed to print to a queue. | |
8937 | */ | |
8938 | ||
8939 | static int /* O - 0 if not allowed, 1 if allowed */ | |
8940 | user_allowed(cupsd_printer_t *p, /* I - Printer or class */ | |
8941 | const char *username) /* I - Username */ | |
8942 | { | |
8943 | int i; /* Looping var */ | |
8944 | struct passwd *pw; /* User password data */ | |
8945 | ||
8946 | ||
8947 | if (p->num_users == 0) | |
8948 | return (1); | |
8949 | ||
8950 | if (!strcmp(username, "root")) | |
8951 | return (1); | |
8952 | ||
8953 | pw = getpwnam(username); | |
8954 | endpwent(); | |
8955 | ||
8956 | for (i = 0; i < p->num_users; i ++) | |
8957 | { | |
8958 | if (p->users[i][0] == '@') | |
8959 | { | |
8960 | /* | |
8961 | * Check group membership... | |
8962 | */ | |
8963 | ||
8964 | if (cupsdCheckGroup(username, pw, p->users[i] + 1)) | |
8965 | break; | |
8966 | } | |
8967 | else if (!strcasecmp(username, p->users[i])) | |
8968 | break; | |
8969 | } | |
8970 | ||
8971 | return ((i < p->num_users) != p->deny_users); | |
8972 | } | |
8973 | ||
8974 | ||
8975 | /* | |
8976 | * 'validate_job()' - Validate printer options and destination. | |
8977 | */ | |
8978 | ||
8979 | static void | |
8980 | validate_job(cupsd_client_t *con, /* I - Client connection */ | |
8981 | ipp_attribute_t *uri) /* I - Printer URI */ | |
8982 | { | |
8983 | http_status_t status; /* Policy status */ | |
8984 | ipp_attribute_t *attr; /* Current attribute */ | |
8985 | ipp_attribute_t *format; /* Document-format attribute */ | |
8986 | cups_ptype_t dtype; /* Destination type (printer or class) */ | |
8987 | char method[HTTP_MAX_URI], | |
8988 | /* Method portion of URI */ | |
8989 | username[HTTP_MAX_URI], | |
8990 | /* Username portion of URI */ | |
8991 | host[HTTP_MAX_URI], | |
8992 | /* Host portion of URI */ | |
8993 | resource[HTTP_MAX_URI]; | |
8994 | /* Resource portion of URI */ | |
8995 | int port; /* Port portion of URI */ | |
8996 | char super[MIME_MAX_SUPER], | |
8997 | /* Supertype of file */ | |
8998 | type[MIME_MAX_TYPE]; | |
8999 | /* Subtype of file */ | |
9000 | cupsd_printer_t *printer; /* Printer */ | |
9001 | ||
9002 | ||
9003 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con, | |
9004 | con->http.fd, uri->values[0].string.text); | |
9005 | ||
9006 | /* | |
9007 | * OK, see if the client is sending the document compressed - CUPS | |
9008 | * doesn't support compression yet... | |
9009 | */ | |
9010 | ||
fa73b229 | 9011 | if ((attr = ippFindAttribute(con->request, "compression", |
9012 | IPP_TAG_KEYWORD)) != NULL && | |
9013 | !strcmp(attr->values[0].string.text, "none")) | |
ef416fc2 | 9014 | { |
9015 | send_ipp_status(con, IPP_ATTRIBUTES, | |
9016 | _("Unsupported compression attribute %s!"), | |
9017 | attr->values[0].string.text); | |
9018 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD, | |
9019 | "compression", NULL, attr->values[0].string.text); | |
9020 | return; | |
9021 | } | |
9022 | ||
9023 | /* | |
9024 | * Is it a format we support? | |
9025 | */ | |
9026 | ||
9027 | if ((format = ippFindAttribute(con->request, "document-format", | |
9028 | IPP_TAG_MIMETYPE)) != NULL) | |
9029 | { | |
9030 | if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2) | |
9031 | { | |
9032 | send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"), | |
9033 | format->values[0].string.text); | |
9034 | return; | |
9035 | } | |
9036 | ||
fa73b229 | 9037 | if ((strcmp(super, "application") || strcmp(type, "octet-stream")) && |
9038 | !mimeType(MimeDatabase, super, type)) | |
ef416fc2 | 9039 | { |
9040 | cupsdLogMessage(CUPSD_LOG_INFO, | |
9041 | "Hint: Do you have the raw file printing rules enabled?"); | |
9042 | send_ipp_status(con, IPP_DOCUMENT_FORMAT, | |
9043 | _("Unsupported format \"%s\"!"), | |
9044 | format->values[0].string.text); | |
9045 | ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE, | |
9046 | "document-format", NULL, format->values[0].string.text); | |
9047 | return; | |
9048 | } | |
9049 | } | |
9050 | ||
9051 | /* | |
9052 | * Is the destination valid? | |
9053 | */ | |
9054 | ||
a4d04587 | 9055 | httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method, |
9056 | sizeof(method), username, sizeof(username), host, | |
9057 | sizeof(host), &port, resource, sizeof(resource)); | |
ef416fc2 | 9058 | |
9059 | if (cupsdValidateDest(host, resource, &dtype, &printer) == NULL) | |
9060 | { | |
9061 | /* | |
9062 | * Bad URI... | |
9063 | */ | |
9064 | ||
9065 | send_ipp_status(con, IPP_NOT_FOUND, | |
9066 | _("The printer or class was not found.")); | |
9067 | return; | |
9068 | } | |
9069 | ||
9070 | /* | |
9071 | * Check policy... | |
9072 | */ | |
9073 | ||
9074 | if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK) | |
9075 | { | |
9076 | send_http_error(con, status); | |
9077 | return; | |
9078 | } | |
9079 | ||
9080 | /* | |
9081 | * Everything was ok, so return OK status... | |
9082 | */ | |
9083 | ||
9084 | con->response->request.status.status_code = IPP_OK; | |
9085 | } | |
9086 | ||
9087 | ||
9088 | /* | |
9089 | * 'validate_name()' - Make sure the printer name only contains valid chars. | |
9090 | */ | |
9091 | ||
9092 | static int /* O - 0 if name is no good, 1 if name is good */ | |
9093 | validate_name(const char *name) /* I - Name to check */ | |
9094 | { | |
9095 | const char *ptr; /* Pointer into name */ | |
9096 | ||
9097 | ||
9098 | /* | |
9099 | * Scan the whole name... | |
9100 | */ | |
9101 | ||
9102 | for (ptr = name; *ptr; ptr ++) | |
9103 | if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') | |
9104 | return (0); | |
9105 | ||
9106 | /* | |
9107 | * All the characters are good; validate the length, too... | |
9108 | */ | |
9109 | ||
9110 | return ((ptr - name) < 128); | |
9111 | } | |
9112 | ||
9113 | ||
9114 | /* | |
9115 | * 'validate_user()' - Validate the user for the request. | |
9116 | */ | |
9117 | ||
9118 | static int /* O - 1 if permitted, 0 otherwise */ | |
9119 | validate_user(cupsd_job_t *job, /* I - Job */ | |
9120 | cupsd_client_t *con, /* I - Client connection */ | |
9121 | const char *owner, /* I - Owner of job/resource */ | |
9122 | char *username, /* O - Authenticated username */ | |
9123 | int userlen) /* I - Length of username */ | |
9124 | { | |
ef416fc2 | 9125 | cupsd_printer_t *printer; /* Printer for job */ |
9126 | ||
9127 | ||
9128 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
9129 | "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, " | |
9130 | "userlen=%d)", | |
9131 | job ? job->id : 0, con->http.fd, owner ? owner : "(null)", | |
9132 | username, userlen); | |
9133 | ||
9134 | /* | |
9135 | * Validate input... | |
9136 | */ | |
9137 | ||
9138 | if (!con || !owner || !username || userlen <= 0) | |
9139 | return (0); | |
9140 | ||
9141 | /* | |
9142 | * Get the best authenticated username that is available. | |
9143 | */ | |
9144 | ||
e00b005a | 9145 | strlcpy(username, get_username(con), userlen); |
ef416fc2 | 9146 | |
9147 | /* | |
9148 | * Check the username against the owner... | |
9149 | */ | |
9150 | ||
80ca4592 | 9151 | printer = cupsdFindDest(job->dest); |
ef416fc2 | 9152 | |
9153 | return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr, | |
9154 | con, owner) == HTTP_OK); | |
9155 | } | |
9156 | ||
9157 | ||
9158 | /* | |
e53920b9 | 9159 | * End of "$Id: ipp.c 5383 2006-04-07 15:36:10Z mike $". |
ef416fc2 | 9160 | */ |