]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
4744bd90 | 2 | * "$Id: cups-lpd.c 5204 2006-02-28 20:32:43Z mike $" |
ef416fc2 | 3 | * |
4 | * Line Printer Daemon interface for the Common UNIX Printing System (CUPS). | |
5 | * | |
bd7854cb | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 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 | * | |
bd7854cb | 26 | * main() - Process an incoming LPD request... |
27 | * create_job() - Create a new print job. | |
28 | * get_printer() - Get the named printer and its options. | |
29 | * print_file() - Add a file to the current job. | |
30 | * recv_print_job() - Receive a print job from the client. | |
31 | * remove_jobs() - Cancel one or more jobs. | |
32 | * send_state() - Send the queue state. | |
33 | * smart_gets() - Get a line of text, removing the trailing CR and/or LF. | |
ef416fc2 | 34 | */ |
35 | ||
36 | /* | |
37 | * Include necessary headers... | |
38 | */ | |
39 | ||
4400e98d | 40 | #include <cups/http-private.h> |
ef416fc2 | 41 | #include <cups/cups.h> |
42 | #include <cups/string.h> | |
43 | #include <cups/language.h> | |
44 | #include <stdlib.h> | |
45 | #include <errno.h> | |
46 | #include <syslog.h> | |
47 | #include <ctype.h> | |
48 | #include <unistd.h> | |
49 | #include <fcntl.h> | |
50 | ||
51 | #include <sys/types.h> | |
52 | #include <sys/socket.h> | |
53 | #include <netinet/in.h> | |
54 | #include <netdb.h> | |
55 | ||
bd7854cb | 56 | #ifdef HAVE_INTTYPES_H |
57 | # include <inttypes.h> | |
58 | #endif /* HAVE_INTTYPES_H */ | |
59 | ||
60 | #ifdef HAVE_COREFOUNDATION_H | |
61 | # include <CoreFoundation/CoreFoundation.h> | |
62 | #endif /* HAVE_COREFOUNDATION_H */ | |
63 | #ifdef HAVE_CFPRIV_H | |
64 | # include <CoreFoundation/CFPriv.h> | |
65 | #endif /* HAVE_CFPRIV_H */ | |
66 | ||
ef416fc2 | 67 | |
68 | /* | |
69 | * LPD "mini-daemon" for CUPS. This program must be used in conjunction | |
70 | * with inetd or another similar program that monitors ports and starts | |
71 | * daemons for each client connection. A typical configuration is: | |
72 | * | |
73 | * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd | |
74 | * | |
75 | * This daemon implements most of RFC 1179 (the unofficial LPD specification) | |
76 | * except for: | |
77 | * | |
78 | * - This daemon does not check to make sure that the source port is | |
79 | * between 721 and 731, since it isn't necessary for proper | |
80 | * functioning and port-based security is no security at all! | |
81 | * | |
82 | * - The "Print any waiting jobs" command is a no-op. | |
83 | * | |
84 | * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats | |
85 | * currently match the Solaris LPD mini-daemon. | |
86 | */ | |
87 | ||
88 | /* | |
89 | * Prototypes... | |
90 | */ | |
91 | ||
bd7854cb | 92 | static int create_job(http_t *http, const char *dest, const char *title, |
93 | const char *user, int num_options, | |
94 | cups_option_t *options); | |
95 | static int get_printer(http_t *http, const char *name, char *dest, | |
96 | int destsize, cups_option_t **options, | |
97 | int *accepting, int *shared, ipp_pstate_t *state); | |
98 | static int print_file(http_t *http, int id, const char *filename, | |
99 | const char *docname, const char *user, int last); | |
100 | static int recv_print_job(const char *name, int num_defaults, | |
101 | cups_option_t *defaults); | |
102 | static int remove_jobs(const char *name, const char *agent, | |
103 | const char *list); | |
104 | static int send_state(const char *name, const char *list, | |
105 | int longstatus); | |
106 | static char *smart_gets(char *s, int len, FILE *fp); | |
ef416fc2 | 107 | |
108 | ||
109 | /* | |
110 | * 'main()' - Process an incoming LPD request... | |
111 | */ | |
112 | ||
113 | int /* O - Exit status */ | |
114 | main(int argc, /* I - Number of command-line arguments */ | |
115 | char *argv[]) /* I - Command-line arguments */ | |
116 | { | |
117 | int i; /* Looping var */ | |
118 | int num_defaults; /* Number of default options */ | |
119 | cups_option_t *defaults; /* Default options */ | |
120 | char line[256], /* Command string */ | |
121 | command, /* Command code */ | |
122 | *dest, /* Pointer to destination */ | |
123 | *list, /* Pointer to list */ | |
124 | *agent, /* Pointer to user */ | |
125 | status; /* Status for client */ | |
126 | socklen_t hostlen; /* Size of client address */ | |
127 | http_addr_t hostaddr; /* Address of client */ | |
128 | char hostname[256], /* Name of client */ | |
129 | hostip[256], /* IP address */ | |
130 | *hostfamily; /* Address family */ | |
131 | ||
132 | ||
133 | /* | |
134 | * Don't buffer the output... | |
135 | */ | |
136 | ||
137 | setbuf(stdout, NULL); | |
138 | ||
139 | /* | |
140 | * Log things using the "cups-lpd" name... | |
141 | */ | |
142 | ||
143 | openlog("cups-lpd", LOG_PID, LOG_LPR); | |
144 | ||
145 | /* | |
146 | * Get the address of the client... | |
147 | */ | |
148 | ||
149 | hostlen = sizeof(hostaddr); | |
150 | ||
151 | if (getpeername(0, (struct sockaddr *)&hostaddr, &hostlen)) | |
152 | { | |
153 | syslog(LOG_WARNING, "Unable to get client address - %s", strerror(errno)); | |
154 | strcpy(hostname, "unknown"); | |
155 | } | |
156 | else | |
157 | { | |
158 | httpAddrLookup(&hostaddr, hostname, sizeof(hostname)); | |
159 | httpAddrString(&hostaddr, hostip, sizeof(hostip)); | |
160 | ||
161 | #ifdef AF_INET6 | |
162 | if (hostaddr.addr.sa_family == AF_INET6) | |
163 | hostfamily = "IPv6"; | |
164 | else | |
165 | #endif /* AF_INET6 */ | |
166 | hostfamily = "IPv4"; | |
167 | ||
bd7854cb | 168 | syslog(LOG_INFO, "Connection from %s (%s %s)", hostname, hostfamily, |
169 | hostip); | |
ef416fc2 | 170 | } |
171 | ||
172 | /* | |
173 | * Scan the command-line for options... | |
174 | */ | |
175 | ||
176 | num_defaults = 0; | |
177 | defaults = NULL; | |
178 | ||
179 | num_defaults = cupsAddOption("job-originating-host-name", hostname, | |
180 | num_defaults, &defaults); | |
181 | ||
182 | for (i = 1; i < argc; i ++) | |
183 | if (argv[i][0] == '-') | |
184 | { | |
185 | switch (argv[i][1]) | |
186 | { | |
187 | case 'o' : /* Option */ | |
188 | if (argv[i][2]) | |
189 | num_defaults = cupsParseOptions(argv[i] + 2, num_defaults, | |
190 | &defaults); | |
191 | else | |
192 | { | |
193 | i ++; | |
194 | if (i < argc) | |
bd7854cb | 195 | num_defaults = cupsParseOptions(argv[i], num_defaults, |
196 | &defaults); | |
ef416fc2 | 197 | else |
198 | syslog(LOG_WARNING, "Expected option string after -o option!"); | |
199 | } | |
200 | break; | |
201 | default : | |
202 | syslog(LOG_WARNING, "Unknown option \"%c\" ignored!", argv[i][1]); | |
203 | break; | |
204 | } | |
205 | } | |
206 | else | |
bd7854cb | 207 | syslog(LOG_WARNING, "Unknown command-line option \"%s\" ignored!", |
208 | argv[i]); | |
ef416fc2 | 209 | |
210 | /* | |
211 | * RFC1179 specifies that only 1 daemon command can be received for | |
212 | * every connection. | |
213 | */ | |
214 | ||
215 | if (smart_gets(line, sizeof(line), stdin) == NULL) | |
216 | { | |
217 | /* | |
218 | * Unable to get command from client! Send an error status and return. | |
219 | */ | |
220 | ||
221 | syslog(LOG_ERR, "Unable to get command line from client!"); | |
222 | putchar(1); | |
223 | return (1); | |
224 | } | |
225 | ||
226 | /* | |
227 | * The first byte is the command byte. After that will be the queue name, | |
228 | * resource list, and/or user name. | |
229 | */ | |
230 | ||
231 | command = line[0]; | |
232 | dest = line + 1; | |
233 | ||
bd7854cb | 234 | if (command == 0x02) |
235 | list = NULL; | |
236 | else | |
237 | { | |
238 | for (list = dest + 1; *list && !isspace(*list & 255); list ++); | |
ef416fc2 | 239 | |
bd7854cb | 240 | while (isspace(*list & 255)) |
241 | *list++ = '\0'; | |
242 | } | |
ef416fc2 | 243 | |
244 | /* | |
245 | * Do the command... | |
246 | */ | |
247 | ||
248 | switch (command) | |
249 | { | |
250 | default : /* Unknown command */ | |
251 | syslog(LOG_ERR, "Unknown LPD command 0x%02X!", command); | |
252 | syslog(LOG_ERR, "Command line = %s", line + 1); | |
253 | putchar(1); | |
254 | ||
255 | status = 1; | |
256 | break; | |
257 | ||
258 | case 0x01 : /* Print any waiting jobs */ | |
259 | syslog(LOG_INFO, "Print waiting jobs (no-op)"); | |
260 | putchar(0); | |
261 | ||
262 | status = 0; | |
263 | break; | |
264 | ||
265 | case 0x02 : /* Receive a printer job */ | |
266 | syslog(LOG_INFO, "Receive print job for %s", dest); | |
267 | /* recv_print_job() sends initial status byte */ | |
268 | ||
269 | status = recv_print_job(dest, num_defaults, defaults); | |
270 | break; | |
271 | ||
272 | case 0x03 : /* Send queue state (short) */ | |
273 | syslog(LOG_INFO, "Send queue state (short) for %s %s", dest, list); | |
274 | /* no status byte for this command */ | |
275 | ||
276 | status = send_state(dest, list, 0); | |
277 | break; | |
278 | ||
279 | case 0x04 : /* Send queue state (long) */ | |
280 | syslog(LOG_INFO, "Send queue state (long) for %s %s", dest, list); | |
281 | /* no status byte for this command */ | |
282 | ||
283 | status = send_state(dest, list, 1); | |
284 | break; | |
285 | ||
286 | case 0x05 : /* Remove jobs */ | |
287 | /* | |
288 | * Grab the agent and skip to the list of users and/or jobs. | |
289 | */ | |
290 | ||
291 | agent = list; | |
292 | ||
293 | for (; *list && !isspace(*list & 255); list ++); | |
294 | while (isspace(*list & 255)) | |
295 | *list++ = '\0'; | |
296 | ||
297 | syslog(LOG_INFO, "Remove jobs %s on %s by %s", list, dest, agent); | |
298 | ||
299 | status = remove_jobs(dest, agent, list); | |
300 | ||
301 | putchar(status); | |
302 | break; | |
303 | } | |
304 | ||
305 | syslog(LOG_INFO, "Closing connection"); | |
306 | closelog(); | |
307 | ||
308 | return (status); | |
309 | } | |
310 | ||
311 | ||
312 | /* | |
bd7854cb | 313 | * 'create_job()' - Create a new print job. |
ef416fc2 | 314 | */ |
315 | ||
bd7854cb | 316 | static int /* O - Job ID or -1 on error */ |
317 | create_job(http_t *http, /* I - HTTP connection */ | |
318 | const char *dest, /* I - Destination name */ | |
319 | const char *title, /* I - job-name */ | |
320 | const char *user, /* I - requesting-user-name */ | |
321 | int num_options, /* I - Number of options for job */ | |
322 | cups_option_t *options) /* I - Options for job */ | |
ef416fc2 | 323 | { |
ef416fc2 | 324 | ipp_t *request; /* IPP request */ |
325 | ipp_t *response; /* IPP response */ | |
bd7854cb | 326 | ipp_attribute_t *attr; /* IPP attribute */ |
ef416fc2 | 327 | char uri[HTTP_MAX_URI]; /* Printer URI */ |
bd7854cb | 328 | int id; /* Job ID */ |
ef416fc2 | 329 | |
330 | ||
331 | /* | |
bd7854cb | 332 | * Setup the Create-Job request... |
ef416fc2 | 333 | */ |
334 | ||
bd7854cb | 335 | request = ippNewRequest(IPP_CREATE_JOB); |
ef416fc2 | 336 | |
a4d04587 | 337 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
bd7854cb | 338 | "localhost", 0, "/printers/%s", dest); |
ef416fc2 | 339 | |
340 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
341 | NULL, uri); | |
342 | ||
bd7854cb | 343 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
344 | "requesting-user-name", NULL, user); | |
345 | ||
346 | if (title) | |
347 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", | |
348 | NULL, title); | |
349 | ||
350 | cupsEncodeOptions(request, num_options, options); | |
ef416fc2 | 351 | |
352 | /* | |
353 | * Do the request... | |
354 | */ | |
355 | ||
bd7854cb | 356 | snprintf(uri, sizeof(uri), "/printers/%s", dest); |
ef416fc2 | 357 | |
bd7854cb | 358 | response = cupsDoRequest(http, request, uri); |
359 | ||
360 | if (!response || cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 361 | { |
bd7854cb | 362 | syslog(LOG_ERR, "Unable to create job - %s", cupsLastErrorString()); |
363 | ||
364 | ippDelete(response); | |
365 | ||
366 | return (-1); | |
ef416fc2 | 367 | } |
bd7854cb | 368 | |
369 | /* | |
370 | * Get the job-id value from the response and return it... | |
371 | */ | |
372 | ||
373 | if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL) | |
ef416fc2 | 374 | { |
bd7854cb | 375 | id = -1; |
376 | ||
377 | syslog(LOG_ERR, "No job-id attribute found in response from server!"); | |
ef416fc2 | 378 | } |
379 | else | |
bd7854cb | 380 | { |
381 | id = attr->values[0].integer; | |
ef416fc2 | 382 | |
bd7854cb | 383 | syslog(LOG_INFO, "Print file - job ID = %d", id); |
384 | } | |
ef416fc2 | 385 | |
bd7854cb | 386 | ippDelete(response); |
ef416fc2 | 387 | |
bd7854cb | 388 | return (id); |
ef416fc2 | 389 | } |
390 | ||
391 | ||
392 | /* | |
bd7854cb | 393 | * 'get_printer()' - Get the named printer and its options. |
ef416fc2 | 394 | */ |
395 | ||
bd7854cb | 396 | static int /* O - Number of options or -1 on error */ |
397 | get_printer(http_t *http, /* I - HTTP connection */ | |
398 | const char *name, /* I - Printer name from request */ | |
399 | char *dest, /* I - Destination buffer */ | |
400 | int destsize, /* I - Size of destination buffer */ | |
401 | cups_option_t **options, /* O - Printer options */ | |
402 | int *accepting, /* O - printer-is-accepting-jobs value */ | |
403 | int *shared, /* O - printer-is-shared value */ | |
404 | ipp_pstate_t *state) /* O - printer-state value */ | |
ef416fc2 | 405 | { |
bd7854cb | 406 | int num_options; /* Number of options */ |
407 | cups_file_t *fp; /* lpoptions file */ | |
408 | char line[1024], /* Line from lpoptions file */ | |
409 | *value, /* Pointer to value on line */ | |
410 | *optptr; /* Pointer to options on line */ | |
411 | int linenum; /* Line number in file */ | |
412 | const char *cups_serverroot; /* CUPS_SERVERROOT env var */ | |
ef416fc2 | 413 | ipp_t *request; /* IPP request */ |
414 | ipp_t *response; /* IPP response */ | |
bd7854cb | 415 | ipp_attribute_t *attr; /* IPP attribute */ |
ef416fc2 | 416 | char uri[HTTP_MAX_URI]; /* Printer URI */ |
bd7854cb | 417 | static const char * const requested[] = |
418 | { /* Requested attributes */ | |
419 | "printer-info", | |
420 | "printer-is-accepting-jobs", | |
421 | "printer-is-shared", | |
422 | "printer-name", | |
423 | "printer-state" | |
424 | }; | |
ef416fc2 | 425 | |
426 | ||
427 | /* | |
bd7854cb | 428 | * Initialize everything... |
ef416fc2 | 429 | */ |
430 | ||
bd7854cb | 431 | if (accepting) |
432 | *accepting = 0; | |
433 | if (shared) | |
434 | *shared = 0; | |
435 | if (state) | |
436 | *state = IPP_PRINTER_STOPPED; | |
437 | if (options) | |
438 | *options = NULL; | |
439 | ||
440 | /* | |
441 | * If the queue name contains a space, lookup the printer-name using | |
442 | * the printer-info value... | |
443 | */ | |
444 | ||
445 | if (strchr(name, ' ')) | |
ef416fc2 | 446 | { |
bd7854cb | 447 | /* |
448 | * Lookup the printer-info... | |
449 | */ | |
450 | ||
451 | ipp_attribute_t *accepting_attr,/* printer-is-accepting-jobs */ | |
452 | *info_attr, /* printer-info */ | |
453 | *name_attr, /* printer-name */ | |
454 | *shared_attr, /* printer-is-shared */ | |
455 | *state_attr; /* printer-state */ | |
456 | ||
457 | ||
458 | /* | |
459 | * Setup the CUPS-Get-Printers request... | |
460 | */ | |
461 | ||
462 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
463 | ||
464 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
465 | "requested-attributes", | |
466 | (int)(sizeof(requested) / sizeof(requested[0])), | |
467 | NULL, requested); | |
468 | ||
469 | /* | |
470 | * Do the request... | |
471 | */ | |
472 | ||
473 | response = cupsDoRequest(http, request, "/"); | |
474 | ||
475 | if (!response || cupsLastError() > IPP_OK_CONFLICT) | |
476 | { | |
477 | syslog(LOG_ERR, "Unable to get list of printers - %s", | |
478 | cupsLastErrorString()); | |
479 | ||
480 | ippDelete(response); | |
481 | ||
482 | return (-1); | |
483 | } | |
484 | ||
485 | /* | |
486 | * Scan the response for printers... | |
487 | */ | |
488 | ||
489 | *dest = '\0'; | |
490 | attr = response->attrs; | |
491 | ||
492 | while (attr) | |
493 | { | |
494 | /* | |
495 | * Skip to the next printer... | |
496 | */ | |
497 | ||
498 | while (attr && attr->group_tag != IPP_TAG_PRINTER) | |
499 | attr = attr->next; | |
500 | ||
501 | if (!attr) | |
502 | break; | |
503 | ||
504 | /* | |
505 | * Get all of the attributes for the current printer... | |
506 | */ | |
507 | ||
508 | accepting_attr = NULL; | |
509 | info_attr = NULL; | |
510 | name_attr = NULL; | |
511 | shared_attr = NULL; | |
512 | state_attr = NULL; | |
513 | ||
514 | while (attr && attr->group_tag == IPP_TAG_PRINTER) | |
515 | { | |
516 | if (!strcmp(attr->name, "printer-is-accepting-jobs") && | |
517 | attr->value_tag == IPP_TAG_BOOLEAN) | |
518 | accepting_attr = attr; | |
519 | else if (!strcmp(attr->name, "printer-info") && | |
520 | attr->value_tag == IPP_TAG_TEXT) | |
521 | info_attr = attr; | |
522 | else if (!strcmp(attr->name, "printer-name") && | |
523 | attr->value_tag == IPP_TAG_NAME) | |
524 | name_attr = attr; | |
525 | else if (!strcmp(attr->name, "printer-is-shared") && | |
526 | attr->value_tag == IPP_TAG_BOOLEAN) | |
527 | shared_attr = attr; | |
528 | else if (!strcmp(attr->name, "printer-state") && | |
529 | attr->value_tag == IPP_TAG_ENUM) | |
530 | state_attr = attr; | |
531 | ||
532 | attr = attr->next; | |
533 | } | |
534 | ||
535 | if (info_attr && name_attr && | |
536 | !strcasecmp(name, info_attr->values[0].string.text)) | |
537 | { | |
538 | /* | |
539 | * Found a match, use this one! | |
540 | */ | |
541 | ||
542 | strlcpy(dest, name_attr->values[0].string.text, destsize); | |
543 | ||
544 | if (accepting && accepting_attr) | |
545 | *accepting = accepting_attr->values[0].boolean; | |
546 | ||
547 | if (shared && shared_attr) | |
548 | *shared = shared_attr->values[0].boolean; | |
549 | ||
550 | if (state && state_attr) | |
551 | *state = (ipp_pstate_t)state_attr->values[0].integer; | |
552 | ||
553 | break; | |
554 | } | |
555 | } | |
556 | ||
557 | ippDelete(response); | |
558 | ||
559 | if (!*dest) | |
560 | { | |
561 | syslog(LOG_ERR, "Unable to find \"%s\" in list of printers!", name); | |
562 | ||
563 | return (-1); | |
564 | } | |
565 | ||
566 | name = dest; | |
567 | } | |
568 | else | |
569 | { | |
570 | /* | |
571 | * Otherwise treat it as a queue name optionally with an instance name. | |
572 | */ | |
573 | ||
574 | strlcpy(dest, name, destsize); | |
575 | if ((value = strchr(dest, '/')) != NULL) | |
576 | *value = '\0'; | |
577 | ||
578 | /* | |
579 | * Setup the Get-Printer-Attributes request... | |
580 | */ | |
581 | ||
582 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
583 | ||
584 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
585 | "localhost", 0, "/printers/%s", dest); | |
586 | ||
587 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
588 | NULL, uri); | |
589 | ||
590 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
591 | "requested-attributes", | |
592 | (int)(sizeof(requested) / sizeof(requested[0])), | |
593 | NULL, requested); | |
594 | ||
595 | /* | |
596 | * Do the request... | |
597 | */ | |
598 | ||
599 | response = cupsDoRequest(http, request, "/"); | |
600 | ||
601 | if (!response || cupsLastError() > IPP_OK_CONFLICT) | |
602 | { | |
603 | syslog(LOG_ERR, "Unable to check printer status - %s", | |
604 | cupsLastErrorString()); | |
605 | ||
606 | ippDelete(response); | |
607 | ||
608 | return (-1); | |
609 | } | |
610 | ||
611 | /* | |
612 | * Get values from the response... | |
613 | */ | |
614 | ||
615 | if (accepting) | |
616 | { | |
617 | if ((attr = ippFindAttribute(response, "printer-is-accepting-jobs", | |
618 | IPP_TAG_BOOLEAN)) == NULL) | |
619 | syslog(LOG_ERR, "No printer-is-accepting-jobs attribute found in " | |
620 | "response from server!"); | |
621 | else | |
622 | *accepting = attr->values[0].boolean; | |
623 | } | |
624 | ||
625 | if (shared) | |
626 | { | |
627 | if ((attr = ippFindAttribute(response, "printer-is-shared", | |
628 | IPP_TAG_BOOLEAN)) == NULL) | |
629 | { | |
630 | syslog(LOG_ERR, "No printer-is-shared attribute found in " | |
631 | "response from server!"); | |
632 | *shared = 1; | |
633 | } | |
634 | else | |
635 | *shared = attr->values[0].boolean; | |
636 | } | |
637 | ||
638 | if (state) | |
639 | { | |
640 | if ((attr = ippFindAttribute(response, "printer-state", | |
641 | IPP_TAG_INTEGER)) == NULL) | |
642 | syslog(LOG_ERR, "No printer-state attribute found in " | |
643 | "response from server!"); | |
644 | else | |
645 | *state = (ipp_pstate_t)attr->values[0].integer; | |
646 | } | |
647 | ||
648 | ippDelete(response); | |
ef416fc2 | 649 | } |
650 | ||
651 | /* | |
bd7854cb | 652 | * Override shared value for LPD using system-specific APIs... |
ef416fc2 | 653 | */ |
654 | ||
bd7854cb | 655 | #ifdef HAVE_CFPRIV_H /* MacOS X */ |
656 | if (shared && *shared) | |
ef416fc2 | 657 | { |
bd7854cb | 658 | CFURLRef prefsurl; /* */ |
659 | CFDataRef xmldata; /* */ | |
660 | CFPropertyListRef plist; /* */ | |
661 | CFStringRef queueid; /* */ | |
662 | CFArrayRef lprqarray; /* */ | |
663 | CFBooleanRef serverflag; /* */ | |
664 | Boolean prefsok; /* */ | |
665 | static const char printerprefsfile[] = | |
666 | "/Library/Preferences/com.apple.printservice.plist"; | |
667 | /* Preferences file */ | |
668 | ||
669 | ||
670 | /* | |
671 | * See if we are running on MacOS X Server... | |
672 | */ | |
673 | ||
674 | CFDictionaryRef versdict = _CFCopyServerVersionDictionary(); | |
675 | ||
676 | if (versdict) | |
677 | { | |
678 | /* | |
679 | * Yes, use the LPR sharing preference... | |
680 | */ | |
681 | ||
682 | CFRelease(versdict); | |
683 | ||
684 | *shared = 0; | |
685 | ||
686 | prefsurl = CFURLCreateFromFileSystemRepresentation( | |
687 | kCFAllocatorDefault, | |
688 | (const UInt8 *)printerprefsfile, | |
689 | (CFIndex)strlen(printerprefsfile), | |
690 | false); | |
691 | if (prefsurl) | |
692 | { | |
693 | prefsok = CFURLCreateDataAndPropertiesFromResource( | |
694 | kCFAllocatorDefault, prefsurl, &xmldata, | |
695 | NULL, NULL, NULL); | |
696 | if (prefsok) | |
697 | { | |
698 | plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, xmldata, | |
699 | kCFPropertyListImmutable, NULL); | |
700 | if (plist) | |
701 | { | |
702 | serverflag = (CFBooleanRef)CFDictionaryGetValue( | |
703 | (CFDictionaryRef)plist, CFSTR("serviceState")); | |
704 | ||
705 | if (serverflag && CFBooleanGetValue(serverflag)) | |
706 | { | |
707 | lprqarray = (CFArrayRef)CFDictionaryGetValue( | |
708 | (CFDictionaryRef)plist, CFSTR("lprSharedQueues")); | |
709 | ||
710 | if (lprqarray) | |
711 | { | |
712 | queueid = CFStringCreateWithCString(CFAllocatorGetDefault(), | |
713 | dest, | |
714 | kCFStringEncodingUTF8); | |
715 | ||
716 | if (queueid) | |
717 | { | |
718 | *shared = CFArrayContainsValue( | |
719 | lprqarray, | |
720 | CFRangeMake(0, CFArrayGetCount(lprqarray)), | |
721 | queueid); | |
722 | ||
723 | CFRelease(queueid); | |
724 | } | |
725 | ||
726 | CFRelease(lprqarray); | |
727 | } | |
728 | } | |
729 | ||
730 | if (serverflag) | |
731 | CFRelease(serverflag); | |
732 | ||
733 | CFRelease(plist); | |
734 | } | |
bd7854cb | 735 | } |
736 | ||
737 | CFRelease(prefsurl); | |
738 | } | |
739 | ||
740 | if (!shared) | |
741 | syslog(LOG_ERR, "Warning - Print Service sharing disabled for LPD " | |
742 | "on queue: %s", name); | |
743 | } | |
ef416fc2 | 744 | } |
bd7854cb | 745 | #endif /* HAVE_CFPRIV_H */ |
ef416fc2 | 746 | |
bd7854cb | 747 | /* |
748 | * Next look for the printer in the lpoptions file... | |
749 | */ | |
ef416fc2 | 750 | |
bd7854cb | 751 | num_options = 0; |
ef416fc2 | 752 | |
bd7854cb | 753 | if (options && shared && accepting) |
754 | { | |
755 | if ((cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
756 | cups_serverroot = CUPS_SERVERROOT; | |
ef416fc2 | 757 | |
bd7854cb | 758 | snprintf(line, sizeof(line), "%s/lpoptions", cups_serverroot); |
759 | if ((fp = cupsFileOpen(line, "r")) != NULL) | |
760 | { | |
761 | linenum = 0; | |
762 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
763 | { | |
764 | /* | |
765 | * Make sure we have "Dest name options" or "Default name options"... | |
766 | */ | |
ef416fc2 | 767 | |
bd7854cb | 768 | if ((strcasecmp(line, "Dest") && strcasecmp(line, "Default")) || !value) |
769 | continue; | |
ef416fc2 | 770 | |
bd7854cb | 771 | /* |
772 | * Separate destination name from options... | |
773 | */ | |
ef416fc2 | 774 | |
bd7854cb | 775 | for (optptr = value; *optptr && !isspace(*optptr & 255); optptr ++); |
ef416fc2 | 776 | |
bd7854cb | 777 | while (*optptr == ' ') |
778 | *optptr++ = '\0'; | |
779 | ||
780 | /* | |
781 | * If this is our destination, parse the options and break out of | |
782 | * the loop - we're done! | |
783 | */ | |
784 | ||
785 | if (!strcasecmp(value, name)) | |
786 | { | |
787 | num_options = cupsParseOptions(optptr, num_options, options); | |
788 | break; | |
789 | } | |
790 | } | |
791 | ||
792 | cupsFileClose(fp); | |
793 | } | |
794 | } | |
795 | else if (options) | |
796 | *options = NULL; | |
ef416fc2 | 797 | |
798 | /* | |
bd7854cb | 799 | * Return the number of options for this destination... |
ef416fc2 | 800 | */ |
801 | ||
bd7854cb | 802 | return (num_options); |
803 | } | |
804 | ||
805 | ||
806 | /* | |
807 | * 'print_file()' - Add a file to the current job. | |
808 | */ | |
809 | ||
810 | static int /* O - 0 on success, -1 on failure */ | |
811 | print_file(http_t *http, /* I - HTTP connection */ | |
812 | int id, /* I - Job ID */ | |
813 | const char *filename, /* I - File to print */ | |
814 | const char *docname, /* I - document-name */ | |
815 | const char *user, /* I - requesting-user-name */ | |
816 | int last) /* I - 1 = last file in job */ | |
817 | { | |
818 | ipp_t *request; /* IPP request */ | |
819 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
820 | ||
821 | ||
822 | /* | |
823 | * Setup the Send-Document request... | |
824 | */ | |
825 | ||
826 | request = ippNewRequest(IPP_SEND_DOCUMENT); | |
827 | ||
828 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", id); | |
829 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
830 | ||
831 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
832 | "requesting-user-name", NULL, user); | |
833 | ||
834 | if (docname) | |
835 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
836 | "document-name", NULL, docname); | |
837 | ||
838 | if (last) | |
839 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1); | |
ef416fc2 | 840 | |
841 | /* | |
842 | * Do the request... | |
843 | */ | |
844 | ||
bd7854cb | 845 | snprintf(uri, sizeof(uri), "/jobs/%d", id); |
ef416fc2 | 846 | |
bd7854cb | 847 | ippDelete(cupsDoFileRequest(http, request, uri, filename)); |
ef416fc2 | 848 | |
bd7854cb | 849 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 850 | { |
bd7854cb | 851 | syslog(LOG_ERR, "Unable to send document - %s", cupsLastErrorString()); |
ef416fc2 | 852 | |
bd7854cb | 853 | return (-1); |
ef416fc2 | 854 | } |
855 | ||
bd7854cb | 856 | return (0); |
ef416fc2 | 857 | } |
858 | ||
859 | ||
860 | /* | |
861 | * 'recv_print_job()' - Receive a print job from the client. | |
862 | */ | |
863 | ||
bd7854cb | 864 | static int /* O - Command status */ |
865 | recv_print_job( | |
866 | const char *queue, /* I - Printer name */ | |
867 | int num_defaults, /* I - Number of default options */ | |
868 | cups_option_t *defaults) /* I - Default options */ | |
ef416fc2 | 869 | { |
bd7854cb | 870 | http_t *http; /* HTTP connection */ |
ef416fc2 | 871 | int i; /* Looping var */ |
872 | int status; /* Command status */ | |
873 | int fd; /* Temporary file */ | |
874 | FILE *fp; /* File pointer */ | |
875 | char filename[1024]; /* Temporary filename */ | |
876 | int bytes; /* Bytes received */ | |
877 | char line[256], /* Line from file/stdin */ | |
878 | command, /* Command from line */ | |
879 | *count, /* Number of bytes */ | |
880 | *name; /* Name of file */ | |
bd7854cb | 881 | const char *job_sheets; /* Job sheets */ |
ef416fc2 | 882 | int num_data; /* Number of data files */ |
883 | char control[1024], /* Control filename */ | |
bd7854cb | 884 | data[100][256], /* Data files */ |
885 | temp[100][1024]; /* Temporary files */ | |
ef416fc2 | 886 | char user[1024], /* User name */ |
887 | title[1024], /* Job title */ | |
888 | docname[1024], /* Document name */ | |
bd7854cb | 889 | dest[256]; /* Printer/class queue */ |
890 | int accepting, /* printer-is-accepting */ | |
891 | shared, /* printer-is-shared */ | |
892 | num_options; /* Number of options */ | |
ef416fc2 | 893 | cups_option_t *options; /* Options */ |
bd7854cb | 894 | int id; /* Job ID */ |
895 | int docnumber, /* Current document number */ | |
896 | doccount; /* Count of documents */ | |
ef416fc2 | 897 | |
ef416fc2 | 898 | |
bd7854cb | 899 | /* |
900 | * Connect to the server... | |
901 | */ | |
ef416fc2 | 902 | |
bd7854cb | 903 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
904 | if (!http) | |
ef416fc2 | 905 | { |
bd7854cb | 906 | syslog(LOG_ERR, "Unable to connect to server: %s", strerror(errno)); |
ef416fc2 | 907 | |
bd7854cb | 908 | putchar(1); |
ef416fc2 | 909 | |
bd7854cb | 910 | return (1); |
911 | } | |
ef416fc2 | 912 | |
bd7854cb | 913 | /* |
914 | * See if the printer is available... | |
915 | */ | |
ef416fc2 | 916 | |
bd7854cb | 917 | num_options = get_printer(http, queue, dest, sizeof(dest), &options, |
918 | &accepting, &shared, NULL); | |
ef416fc2 | 919 | |
bd7854cb | 920 | if (num_options < 0 || !accepting || !shared) |
ef416fc2 | 921 | { |
bd7854cb | 922 | if (dest[0]) |
923 | syslog(LOG_INFO, "Rejecting job because \"%s\" is not %s", dest, | |
924 | !accepting ? "accepting jobs" : "shared"); | |
925 | else | |
926 | syslog(LOG_ERR, "Unable to get printer information for \"%s\"", queue); | |
927 | ||
928 | httpClose(http); | |
ef416fc2 | 929 | |
930 | putchar(1); | |
931 | ||
932 | return (1); | |
933 | } | |
934 | ||
bd7854cb | 935 | putchar(0); /* OK so far... */ |
936 | ||
937 | /* | |
938 | * Read the request... | |
939 | */ | |
940 | ||
941 | status = 0; | |
942 | num_data = 0; | |
943 | fd = -1; | |
944 | ||
945 | control[0] = '\0'; | |
ef416fc2 | 946 | |
947 | while (smart_gets(line, sizeof(line), stdin) != NULL) | |
948 | { | |
949 | if (strlen(line) < 2) | |
950 | { | |
951 | status = 1; | |
952 | break; | |
953 | } | |
954 | ||
955 | command = line[0]; | |
956 | count = line + 1; | |
957 | ||
958 | for (name = count + 1; *name && !isspace(*name & 255); name ++); | |
959 | while (isspace(*name & 255)) | |
960 | *name++ = '\0'; | |
961 | ||
962 | switch (command) | |
963 | { | |
964 | default : | |
965 | case 0x01 : /* Abort */ | |
966 | status = 1; | |
967 | break; | |
968 | ||
969 | case 0x02 : /* Receive control file */ | |
970 | if (strlen(name) < 2) | |
971 | { | |
972 | syslog(LOG_ERR, "Bad control file name \"%s\"", name); | |
973 | putchar(1); | |
974 | status = 1; | |
975 | break; | |
976 | } | |
977 | ||
978 | if (control[0]) | |
979 | { | |
980 | /* | |
981 | * Append to the existing control file - the LPD spec is | |
982 | * not entirely clear, but at least the OS/2 LPD code sends | |
983 | * multiple control files per connection... | |
984 | */ | |
985 | ||
986 | if ((fd = open(control, O_WRONLY)) < 0) | |
987 | { | |
988 | syslog(LOG_ERR, | |
989 | "Unable to append to temporary control file \"%s\" - %s", | |
990 | control, strerror(errno)); | |
991 | putchar(1); | |
992 | status = 1; | |
993 | break; | |
994 | } | |
995 | ||
996 | lseek(fd, 0, SEEK_END); | |
997 | } | |
998 | else | |
999 | { | |
1000 | if ((fd = cupsTempFd(control, sizeof(control))) < 0) | |
1001 | { | |
1002 | syslog(LOG_ERR, "Unable to open temporary control file \"%s\" - %s", | |
1003 | control, strerror(errno)); | |
1004 | putchar(1); | |
1005 | status = 1; | |
1006 | break; | |
1007 | } | |
1008 | ||
1009 | strcpy(filename, control); | |
1010 | } | |
1011 | break; | |
1012 | ||
1013 | case 0x03 : /* Receive data file */ | |
1014 | if (strlen(name) < 2) | |
1015 | { | |
1016 | syslog(LOG_ERR, "Bad data file name \"%s\"", name); | |
1017 | putchar(1); | |
1018 | status = 1; | |
1019 | break; | |
1020 | } | |
1021 | ||
bd7854cb | 1022 | if (num_data >= (int)(sizeof(data) / sizeof(data[0]))) |
ef416fc2 | 1023 | { |
1024 | /* | |
1025 | * Too many data files... | |
1026 | */ | |
1027 | ||
1028 | syslog(LOG_ERR, "Too many data files (%d)", num_data); | |
1029 | putchar(1); | |
1030 | status = 1; | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | strlcpy(data[num_data], name, sizeof(data[0])); | |
1035 | ||
1036 | if ((fd = cupsTempFd(temp[num_data], sizeof(temp[0]))) < 0) | |
1037 | { | |
1038 | syslog(LOG_ERR, "Unable to open temporary data file \"%s\" - %s", | |
1039 | temp[num_data], strerror(errno)); | |
1040 | putchar(1); | |
1041 | status = 1; | |
1042 | break; | |
1043 | } | |
1044 | ||
1045 | strcpy(filename, temp[num_data]); | |
1046 | ||
1047 | num_data ++; | |
1048 | break; | |
1049 | } | |
1050 | ||
1051 | putchar(status); | |
1052 | ||
1053 | if (status) | |
1054 | break; | |
1055 | ||
1056 | /* | |
1057 | * Copy the data or control file from the client... | |
1058 | */ | |
1059 | ||
1060 | for (i = atoi(count); i > 0; i -= bytes) | |
1061 | { | |
1062 | if (i > sizeof(line)) | |
1063 | bytes = sizeof(line); | |
1064 | else | |
1065 | bytes = i; | |
1066 | ||
1067 | if ((bytes = fread(line, 1, bytes, stdin)) > 0) | |
1068 | bytes = write(fd, line, bytes); | |
1069 | ||
1070 | if (bytes < 1) | |
1071 | { | |
1072 | syslog(LOG_ERR, "Error while reading file - %s", | |
1073 | strerror(errno)); | |
1074 | status = 1; | |
1075 | break; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * Read trailing nul... | |
1081 | */ | |
1082 | ||
1083 | if (!status) | |
1084 | { | |
1085 | if (fread(line, 1, 1, stdin) < 1) | |
1086 | { | |
1087 | status = 1; | |
1088 | syslog(LOG_ERR, "Error while reading trailing nul - %s", | |
1089 | strerror(errno)); | |
1090 | } | |
1091 | else if (line[0]) | |
1092 | { | |
1093 | status = 1; | |
1094 | syslog(LOG_ERR, "Trailing character after file is not nul (%02X)!", | |
1095 | line[0]); | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * Close the file and send an acknowledgement... | |
1101 | */ | |
1102 | ||
1103 | close(fd); | |
1104 | ||
1105 | putchar(status); | |
1106 | ||
1107 | if (status) | |
1108 | break; | |
1109 | } | |
1110 | ||
1111 | if (!status) | |
1112 | { | |
1113 | /* | |
1114 | * Process the control file and print stuff... | |
1115 | */ | |
1116 | ||
1117 | if ((fp = fopen(control, "rb")) == NULL) | |
1118 | status = 1; | |
1119 | else | |
1120 | { | |
1121 | /* | |
bd7854cb | 1122 | * Copy the default options... |
ef416fc2 | 1123 | */ |
1124 | ||
bd7854cb | 1125 | for (i = 0; i < num_defaults; i ++) |
1126 | num_options = cupsAddOption(defaults[i].name, | |
1127 | defaults[i].value, | |
1128 | num_options, &options); | |
1129 | ||
1130 | /* | |
1131 | * Grab the job information... | |
1132 | */ | |
1133 | ||
1134 | title[0] = '\0'; | |
1135 | user[0] = '\0'; | |
1136 | doccount = 0; | |
ef416fc2 | 1137 | |
1138 | while (smart_gets(line, sizeof(line), fp) != NULL) | |
1139 | { | |
1140 | /* | |
1141 | * Process control lines... | |
1142 | */ | |
1143 | ||
1144 | switch (line[0]) | |
1145 | { | |
1146 | case 'J' : /* Job name */ | |
1147 | strlcpy(title, line + 1, sizeof(title)); | |
1148 | break; | |
1149 | ||
ef416fc2 | 1150 | case 'P' : /* User identification */ |
1151 | strlcpy(user, line + 1, sizeof(user)); | |
1152 | break; | |
1153 | ||
1154 | case 'L' : /* Print banner page */ | |
bd7854cb | 1155 | /* |
1156 | * If a banner was requested and it's not overridden by a | |
1157 | * command line option and the destination's default is none | |
1158 | * then add the standard banner... | |
1159 | */ | |
ef416fc2 | 1160 | |
bd7854cb | 1161 | if (cupsGetOption("job-sheets", num_defaults, defaults) == NULL && |
1162 | ((job_sheets = cupsGetOption("job-sheets", num_options, | |
1163 | options)) == NULL || | |
1164 | !strcmp(job_sheets, "none,none"))) | |
1165 | { | |
1166 | num_options = cupsAddOption("job-sheets", "standard", | |
1167 | num_options, &options); | |
1168 | } | |
1169 | break; | |
ef416fc2 | 1170 | |
ef416fc2 | 1171 | case 'c' : /* Plot CIF file */ |
1172 | case 'd' : /* Print DVI file */ | |
1173 | case 'f' : /* Print formatted file */ | |
1174 | case 'g' : /* Plot file */ | |
1175 | case 'l' : /* Print file leaving control characters (raw) */ | |
1176 | case 'n' : /* Print ditroff output file */ | |
1177 | case 'o' : /* Print PostScript output file */ | |
1178 | case 'p' : /* Print file with 'pr' format (prettyprint) */ | |
1179 | case 'r' : /* File to print with FORTRAN carriage control */ | |
1180 | case 't' : /* Print troff output file */ | |
1181 | case 'v' : /* Print raster file */ | |
bd7854cb | 1182 | doccount ++; |
ef416fc2 | 1183 | |
bd7854cb | 1184 | if (line[0] == 'l' && |
1185 | !cupsGetOption("document-format", num_options, options)) | |
1186 | num_options = cupsAddOption("raw", "", num_options, &options); | |
ef416fc2 | 1187 | |
bd7854cb | 1188 | if (line[0] == 'p') |
1189 | num_options = cupsAddOption("prettyprint", "", num_options, | |
1190 | &options); | |
1191 | break; | |
1192 | } | |
ef416fc2 | 1193 | |
bd7854cb | 1194 | if (status) |
1195 | break; | |
1196 | } | |
ef416fc2 | 1197 | |
bd7854cb | 1198 | /* |
1199 | * Check that we have a username... | |
1200 | */ | |
ef416fc2 | 1201 | |
bd7854cb | 1202 | if (!user[0]) |
1203 | { | |
1204 | syslog(LOG_WARNING, "No username specified by client! " | |
1205 | "Using \"anonymous\"..."); | |
1206 | strcpy(user, "anonymous"); | |
1207 | } | |
ef416fc2 | 1208 | |
bd7854cb | 1209 | /* |
1210 | * Create the job... | |
1211 | */ | |
ef416fc2 | 1212 | |
bd7854cb | 1213 | if ((id = create_job(http, dest, title, user, num_options, options)) < 0) |
1214 | status = 1; | |
1215 | else | |
1216 | { | |
1217 | /* | |
1218 | * Then print the job files... | |
1219 | */ | |
ef416fc2 | 1220 | |
bd7854cb | 1221 | rewind(fp); |
ef416fc2 | 1222 | |
bd7854cb | 1223 | docname[0] = '\0'; |
1224 | docnumber = 0; | |
ef416fc2 | 1225 | |
bd7854cb | 1226 | while (smart_gets(line, sizeof(line), fp) != NULL) |
1227 | { | |
1228 | /* | |
1229 | * Process control lines... | |
1230 | */ | |
1231 | ||
1232 | switch (line[0]) | |
1233 | { | |
1234 | case 'N' : /* Document name */ | |
1235 | strlcpy(docname, line + 1, sizeof(docname)); | |
1236 | break; | |
ef416fc2 | 1237 | |
bd7854cb | 1238 | case 'c' : /* Plot CIF file */ |
1239 | case 'd' : /* Print DVI file */ | |
1240 | case 'f' : /* Print formatted file */ | |
1241 | case 'g' : /* Plot file */ | |
1242 | case 'l' : /* Print file leaving control characters (raw) */ | |
1243 | case 'n' : /* Print ditroff output file */ | |
1244 | case 'o' : /* Print PostScript output file */ | |
1245 | case 'p' : /* Print file with 'pr' format (prettyprint) */ | |
1246 | case 'r' : /* File to print with FORTRAN carriage control */ | |
1247 | case 't' : /* Print troff output file */ | |
1248 | case 'v' : /* Print raster file */ | |
1249 | /* | |
1250 | * Figure out which file we are printing... | |
1251 | */ | |
1252 | ||
1253 | for (i = 0; i < num_data; i ++) | |
1254 | if (!strcmp(data[i], line + 1)) | |
1255 | break; | |
1256 | ||
1257 | if (i >= num_data) | |
1258 | { | |
1259 | status = 1; | |
ef416fc2 | 1260 | break; |
bd7854cb | 1261 | } |
ef416fc2 | 1262 | |
bd7854cb | 1263 | /* |
1264 | * Send the print file... | |
1265 | */ | |
ef416fc2 | 1266 | |
bd7854cb | 1267 | docnumber ++; |
ef416fc2 | 1268 | |
bd7854cb | 1269 | if (print_file(http, id, temp[i], docname, user, |
1270 | docnumber == doccount)) | |
1271 | status = 1; | |
1272 | else | |
1273 | status = 0; | |
ef416fc2 | 1274 | |
bd7854cb | 1275 | break; |
1276 | } | |
1277 | ||
1278 | if (status) | |
1279 | break; | |
ef416fc2 | 1280 | } |
1281 | ||
bd7854cb | 1282 | fclose(fp); |
ef416fc2 | 1283 | } |
ef416fc2 | 1284 | } |
1285 | } | |
1286 | ||
bd7854cb | 1287 | cupsFreeOptions(num_options, options); |
1288 | ||
1289 | httpClose(http); | |
1290 | ||
ef416fc2 | 1291 | /* |
1292 | * Clean up all temporary files and return... | |
1293 | */ | |
1294 | ||
1295 | unlink(control); | |
1296 | ||
1297 | for (i = 0; i < num_data; i ++) | |
1298 | unlink(temp[i]); | |
1299 | ||
ef416fc2 | 1300 | return (status); |
1301 | } | |
1302 | ||
1303 | ||
1304 | /* | |
1305 | * 'remove_jobs()' - Cancel one or more jobs. | |
1306 | */ | |
1307 | ||
b423cd4c | 1308 | static int /* O - Command status */ |
ef416fc2 | 1309 | remove_jobs(const char *dest, /* I - Destination */ |
1310 | const char *agent, /* I - User agent */ | |
1311 | const char *list) /* I - List of jobs or users */ | |
1312 | { | |
1313 | int id; /* Job ID */ | |
1314 | http_t *http; /* HTTP server connection */ | |
bd7854cb | 1315 | ipp_t *request; /* IPP Request */ |
ef416fc2 | 1316 | char uri[HTTP_MAX_URI]; /* Job URI */ |
1317 | ||
1318 | ||
1319 | (void)dest; /* Suppress compiler warnings... */ | |
1320 | ||
1321 | /* | |
1322 | * Try connecting to the local server... | |
1323 | */ | |
1324 | ||
1325 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
1326 | cupsEncryption())) == NULL) | |
1327 | { | |
1328 | syslog(LOG_ERR, "Unable to connect to server %s: %s", cupsServer(), | |
1329 | strerror(errno)); | |
1330 | return (1); | |
1331 | } | |
1332 | ||
ef416fc2 | 1333 | /* |
1334 | * Loop for each job... | |
1335 | */ | |
1336 | ||
1337 | while ((id = atoi(list)) > 0) | |
1338 | { | |
1339 | /* | |
1340 | * Skip job ID in list... | |
1341 | */ | |
1342 | ||
1343 | while (isdigit(*list & 255)) | |
1344 | list ++; | |
1345 | while (isspace(*list & 255)) | |
1346 | list ++; | |
1347 | ||
1348 | /* | |
1349 | * Build an IPP_CANCEL_JOB request, which requires the following | |
1350 | * attributes: | |
1351 | * | |
1352 | * attributes-charset | |
1353 | * attributes-natural-language | |
1354 | * job-uri | |
1355 | * requesting-user-name | |
1356 | */ | |
1357 | ||
bd7854cb | 1358 | request = ippNewRequest(IPP_CANCEL_JOB); |
ef416fc2 | 1359 | |
1360 | sprintf(uri, "ipp://localhost/jobs/%d", id); | |
1361 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
1362 | ||
1363 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1364 | "requesting-user-name", NULL, agent); | |
1365 | ||
1366 | /* | |
1367 | * Do the request and get back a response... | |
1368 | */ | |
1369 | ||
bd7854cb | 1370 | ippDelete(cupsDoRequest(http, request, "/jobs")); |
ef416fc2 | 1371 | |
bd7854cb | 1372 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 1373 | { |
1374 | syslog(LOG_WARNING, "Cancel of job ID %d failed: %s\n", id, | |
bd7854cb | 1375 | cupsLastErrorString()); |
ef416fc2 | 1376 | httpClose(http); |
1377 | return (1); | |
1378 | } | |
bd7854cb | 1379 | else |
1380 | syslog(LOG_INFO, "Job ID %d cancelled", id); | |
ef416fc2 | 1381 | } |
1382 | ||
ef416fc2 | 1383 | httpClose(http); |
1384 | ||
1385 | return (0); | |
1386 | } | |
1387 | ||
1388 | ||
1389 | /* | |
1390 | * 'send_state()' - Send the queue state. | |
1391 | */ | |
1392 | ||
b423cd4c | 1393 | static int /* O - Command status */ |
bd7854cb | 1394 | send_state(const char *queue, /* I - Destination */ |
ef416fc2 | 1395 | const char *list, /* I - Job or user */ |
1396 | int longstatus) /* I - List of jobs or users */ | |
1397 | { | |
1398 | int id; /* Job ID from list */ | |
1399 | http_t *http; /* HTTP server connection */ | |
1400 | ipp_t *request, /* IPP Request */ | |
1401 | *response; /* IPP Response */ | |
1402 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 1403 | ipp_pstate_t state; /* Printer state */ |
1404 | const char *jobdest, /* Pointer into job-printer-uri */ | |
1405 | *jobuser, /* Pointer to job-originating-user-name */ | |
1406 | *jobname; /* Pointer to job-name */ | |
1407 | ipp_jstate_t jobstate; /* job-state */ | |
1408 | int jobid, /* job-id */ | |
1409 | jobsize, /* job-k-octets */ | |
1410 | jobcount, /* Number of jobs */ | |
1411 | jobcopies, /* Number of copies */ | |
1412 | rank; /* Rank of job */ | |
1413 | char rankstr[255]; /* Rank string */ | |
1414 | char namestr[1024]; /* Job name string */ | |
1415 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
bd7854cb | 1416 | char dest[256]; /* Printer/class queue */ |
ef416fc2 | 1417 | static const char * const ranks[10] = /* Ranking strings */ |
1418 | { | |
1419 | "th", | |
1420 | "st", | |
1421 | "nd", | |
1422 | "rd", | |
1423 | "th", | |
1424 | "th", | |
1425 | "th", | |
1426 | "th", | |
1427 | "th", | |
1428 | "th" | |
1429 | }; | |
1430 | static const char * const requested[] = | |
1431 | { /* Requested attributes */ | |
1432 | "job-id", | |
1433 | "job-k-octets", | |
1434 | "job-state", | |
1435 | "job-printer-uri", | |
1436 | "job-originating-user-name", | |
1437 | "job-name", | |
1438 | "copies" | |
1439 | }; | |
1440 | ||
1441 | ||
ef416fc2 | 1442 | /* |
1443 | * Try connecting to the local server... | |
1444 | */ | |
1445 | ||
1446 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
1447 | cupsEncryption())) == NULL) | |
1448 | { | |
1449 | syslog(LOG_ERR, "Unable to connect to server %s: %s", cupsServer(), | |
1450 | strerror(errno)); | |
1451 | printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno)); | |
1452 | return (1); | |
1453 | } | |
1454 | ||
1455 | /* | |
bd7854cb | 1456 | * Get the actual destination name and printer state... |
ef416fc2 | 1457 | */ |
1458 | ||
bd7854cb | 1459 | if (get_printer(http, queue, dest, sizeof(dest), NULL, NULL, NULL, &state)) |
1460 | { | |
1461 | syslog(LOG_ERR, "Unable to get printer %s: %s", queue, | |
1462 | cupsLastErrorString()); | |
1463 | printf("Unable to get printer %s: %s", queue, cupsLastErrorString()); | |
1464 | return (1); | |
1465 | } | |
ef416fc2 | 1466 | |
1467 | /* | |
bd7854cb | 1468 | * Show the queue state... |
ef416fc2 | 1469 | */ |
1470 | ||
bd7854cb | 1471 | switch (state) |
ef416fc2 | 1472 | { |
bd7854cb | 1473 | case IPP_PRINTER_IDLE : |
1474 | printf("%s is ready\n", dest); | |
1475 | break; | |
1476 | case IPP_PRINTER_PROCESSING : | |
1477 | printf("%s is ready and printing\n", dest); | |
1478 | break; | |
1479 | case IPP_PRINTER_STOPPED : | |
1480 | printf("%s is not ready\n", dest); | |
1481 | break; | |
ef416fc2 | 1482 | } |
1483 | ||
1484 | /* | |
1485 | * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires | |
1486 | * the following attributes: | |
1487 | * | |
1488 | * attributes-charset | |
1489 | * attributes-natural-language | |
1490 | * job-uri or printer-uri | |
1491 | */ | |
1492 | ||
1493 | id = atoi(list); | |
1494 | ||
bd7854cb | 1495 | request = ippNewRequest(id ? IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS); |
ef416fc2 | 1496 | |
a4d04587 | 1497 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
bd7854cb | 1498 | "localhost", 0, "/printers/%s", dest); |
ef416fc2 | 1499 | |
1500 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1501 | NULL, uri); | |
1502 | ||
1503 | if (id) | |
1504 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id); | |
1505 | else | |
1506 | { | |
1507 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1508 | "requesting-user-name", NULL, list); | |
1509 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
1510 | } | |
1511 | ||
1512 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
bd7854cb | 1513 | "requested-attributes", |
1514 | sizeof(requested) / sizeof(requested[0]), | |
ef416fc2 | 1515 | NULL, requested); |
1516 | ||
1517 | /* | |
1518 | * Do the request and get back a response... | |
1519 | */ | |
1520 | ||
1521 | jobcount = 0; | |
bd7854cb | 1522 | response = cupsDoRequest(http, request, "/"); |
ef416fc2 | 1523 | |
bd7854cb | 1524 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 1525 | { |
bd7854cb | 1526 | printf("get-jobs failed: %s\n", cupsLastErrorString()); |
1527 | ippDelete(response); | |
1528 | return (1); | |
1529 | } | |
ef416fc2 | 1530 | |
bd7854cb | 1531 | /* |
1532 | * Loop through the job list and display them... | |
1533 | */ | |
ef416fc2 | 1534 | |
bd7854cb | 1535 | for (attr = response->attrs, rank = 1; attr; attr = attr->next) |
1536 | { | |
ef416fc2 | 1537 | /* |
bd7854cb | 1538 | * Skip leading attributes until we hit a job... |
ef416fc2 | 1539 | */ |
1540 | ||
bd7854cb | 1541 | while (attr && (attr->group_tag != IPP_TAG_JOB || !attr->name)) |
1542 | attr = attr->next; | |
ef416fc2 | 1543 | |
bd7854cb | 1544 | if (!attr) |
1545 | break; | |
ef416fc2 | 1546 | |
bd7854cb | 1547 | /* |
1548 | * Pull the needed attributes from this job... | |
1549 | */ | |
ef416fc2 | 1550 | |
bd7854cb | 1551 | jobid = 0; |
1552 | jobsize = 0; | |
1553 | jobstate = IPP_JOB_PENDING; | |
1554 | jobname = "untitled"; | |
1555 | jobuser = NULL; | |
1556 | jobdest = NULL; | |
1557 | jobcopies = 1; | |
ef416fc2 | 1558 | |
bd7854cb | 1559 | while (attr && attr->group_tag == IPP_TAG_JOB) |
1560 | { | |
1561 | if (!strcmp(attr->name, "job-id") && | |
1562 | attr->value_tag == IPP_TAG_INTEGER) | |
1563 | jobid = attr->values[0].integer; | |
ef416fc2 | 1564 | |
bd7854cb | 1565 | if (!strcmp(attr->name, "job-k-octets") && |
1566 | attr->value_tag == IPP_TAG_INTEGER) | |
1567 | jobsize = attr->values[0].integer; | |
ef416fc2 | 1568 | |
bd7854cb | 1569 | if (!strcmp(attr->name, "job-state") && |
1570 | attr->value_tag == IPP_TAG_ENUM) | |
1571 | jobstate = (ipp_jstate_t)attr->values[0].integer; | |
ef416fc2 | 1572 | |
bd7854cb | 1573 | if (!strcmp(attr->name, "job-printer-uri") && |
1574 | attr->value_tag == IPP_TAG_URI) | |
1575 | if ((jobdest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
1576 | jobdest ++; | |
ef416fc2 | 1577 | |
bd7854cb | 1578 | if (!strcmp(attr->name, "job-originating-user-name") && |
1579 | attr->value_tag == IPP_TAG_NAME) | |
1580 | jobuser = attr->values[0].string.text; | |
ef416fc2 | 1581 | |
bd7854cb | 1582 | if (!strcmp(attr->name, "job-name") && |
1583 | attr->value_tag == IPP_TAG_NAME) | |
1584 | jobname = attr->values[0].string.text; | |
ef416fc2 | 1585 | |
bd7854cb | 1586 | if (!strcmp(attr->name, "copies") && |
1587 | attr->value_tag == IPP_TAG_INTEGER) | |
1588 | jobcopies = attr->values[0].integer; | |
ef416fc2 | 1589 | |
bd7854cb | 1590 | attr = attr->next; |
1591 | } | |
ef416fc2 | 1592 | |
bd7854cb | 1593 | /* |
1594 | * See if we have everything needed... | |
1595 | */ | |
ef416fc2 | 1596 | |
bd7854cb | 1597 | if (!jobdest || !jobid) |
1598 | { | |
1599 | if (!attr) | |
1600 | break; | |
1601 | else | |
1602 | continue; | |
1603 | } | |
ef416fc2 | 1604 | |
bd7854cb | 1605 | if (!longstatus && jobcount == 0) |
1606 | puts("Rank Owner Job File(s) Total Size"); | |
ef416fc2 | 1607 | |
bd7854cb | 1608 | jobcount ++; |
ef416fc2 | 1609 | |
bd7854cb | 1610 | /* |
1611 | * Display the job... | |
1612 | */ | |
ef416fc2 | 1613 | |
bd7854cb | 1614 | if (jobstate == IPP_JOB_PROCESSING) |
1615 | strcpy(rankstr, "active"); | |
1616 | else | |
1617 | { | |
1618 | snprintf(rankstr, sizeof(rankstr), "%d%s", rank, ranks[rank % 10]); | |
1619 | rank ++; | |
1620 | } | |
ef416fc2 | 1621 | |
bd7854cb | 1622 | if (longstatus) |
1623 | { | |
1624 | puts(""); | |
ef416fc2 | 1625 | |
bd7854cb | 1626 | if (jobcopies > 1) |
1627 | snprintf(namestr, sizeof(namestr), "%d copies of %s", jobcopies, | |
1628 | jobname); | |
ef416fc2 | 1629 | else |
bd7854cb | 1630 | strlcpy(namestr, jobname, sizeof(namestr)); |
ef416fc2 | 1631 | |
bd7854cb | 1632 | printf("%s: %-33.33s [job %d localhost]\n", jobuser, rankstr, jobid); |
1633 | printf(" %-39.39s %.0f bytes\n", namestr, 1024.0 * jobsize); | |
ef416fc2 | 1634 | } |
bd7854cb | 1635 | else |
1636 | printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr, jobuser, | |
1637 | jobid, jobname, 1024.0 * jobsize); | |
ef416fc2 | 1638 | |
bd7854cb | 1639 | if (!attr) |
1640 | break; | |
ef416fc2 | 1641 | } |
1642 | ||
bd7854cb | 1643 | ippDelete(response); |
1644 | ||
ef416fc2 | 1645 | if (jobcount == 0) |
1646 | puts("no entries"); | |
1647 | ||
ef416fc2 | 1648 | httpClose(http); |
1649 | ||
1650 | return (0); | |
1651 | } | |
1652 | ||
1653 | ||
1654 | /* | |
1655 | * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF. | |
1656 | */ | |
1657 | ||
b423cd4c | 1658 | static char * /* O - Line read or NULL */ |
ef416fc2 | 1659 | smart_gets(char *s, /* I - Pointer to line buffer */ |
1660 | int len, /* I - Size of line buffer */ | |
1661 | FILE *fp) /* I - File to read from */ | |
1662 | { | |
1663 | char *ptr, /* Pointer into line */ | |
1664 | *end; /* End of line */ | |
1665 | int ch; /* Character from file */ | |
1666 | ||
1667 | ||
1668 | /* | |
1669 | * Read the line; unlike fgets(), we read the entire line but dump | |
1670 | * characters that go past the end of the buffer. Also, we accept | |
1671 | * CR, LF, or CR LF for the line endings to be "safe", although | |
1672 | * RFC 1179 specifically says "just use LF". | |
1673 | */ | |
1674 | ||
1675 | ptr = s; | |
1676 | end = s + len - 1; | |
1677 | ||
1678 | while ((ch = getc(fp)) != EOF) | |
1679 | { | |
1680 | if (ch == '\n') | |
1681 | break; | |
1682 | else if (ch == '\r') | |
1683 | { | |
1684 | /* | |
1685 | * See if a LF follows... | |
1686 | */ | |
1687 | ||
1688 | ch = getc(fp); | |
1689 | ||
1690 | if (ch != '\n') | |
1691 | ungetc(ch, fp); | |
1692 | ||
1693 | break; | |
1694 | } | |
1695 | else if (ptr < end) | |
1696 | *ptr++ = ch; | |
1697 | } | |
1698 | ||
1699 | *ptr = '\0'; | |
1700 | ||
1701 | if (ch == EOF && ptr == s) | |
1702 | return (NULL); | |
1703 | else | |
1704 | return (s); | |
1705 | } | |
1706 | ||
1707 | ||
1708 | /* | |
4744bd90 | 1709 | * End of "$Id: cups-lpd.c 5204 2006-02-28 20:32:43Z mike $". |
ef416fc2 | 1710 | */ |