]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * AppSocket backend for CUPS. | |
3 | * | |
4 | * Copyright © 2020-2024 by OpenPrinting. | |
5 | * Copyright © 2007-2018 by Apple Inc. | |
6 | * Copyright © 1997-2007 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * Licensed under Apache License v2.0. See the file "LICENSE" for more | |
9 | * information. | |
10 | */ | |
11 | ||
12 | /* | |
13 | * Include necessary headers. | |
14 | */ | |
15 | ||
16 | #include <cups/http-private.h> | |
17 | #include "backend-private.h" | |
18 | #include <stdarg.h> | |
19 | #include <sys/types.h> | |
20 | #include <sys/stat.h> | |
21 | ||
22 | #ifdef _WIN32 | |
23 | # include <winsock.h> | |
24 | #else | |
25 | # include <unistd.h> | |
26 | # include <fcntl.h> | |
27 | # include <sys/socket.h> | |
28 | # include <netinet/in.h> | |
29 | # include <arpa/inet.h> | |
30 | # include <netdb.h> | |
31 | #endif /* _WIN32 */ | |
32 | ||
33 | ||
34 | /* | |
35 | * Local functions... | |
36 | */ | |
37 | ||
38 | static ssize_t wait_bc(int device_fd, int secs); | |
39 | ||
40 | ||
41 | /* | |
42 | * 'main()' - Send a file to the printer or server. | |
43 | * | |
44 | * Usage: | |
45 | * | |
46 | * printer-uri job-id user title copies options [file] | |
47 | */ | |
48 | ||
49 | int /* O - Exit status */ | |
50 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
51 | char *argv[]) /* I - Command-line arguments */ | |
52 | { | |
53 | const char *device_uri; /* Device URI */ | |
54 | char scheme[255], /* Scheme in URI */ | |
55 | hostname[1024], /* Hostname */ | |
56 | username[255], /* Username info (not used) */ | |
57 | resource[1024], /* Resource info (not used) */ | |
58 | *options, /* Pointer to options */ | |
59 | *name, /* Name of option */ | |
60 | *value, /* Value of option */ | |
61 | sep; /* Option separator */ | |
62 | int print_fd; /* Print file */ | |
63 | int copies; /* Number of copies to print */ | |
64 | time_t start_time; /* Time of first connect */ | |
65 | int contimeout; /* Connection timeout */ | |
66 | int waiteof; /* Wait for end-of-file? */ | |
67 | int port; /* Port number */ | |
68 | int delay; /* Delay for retries... */ | |
69 | int device_fd; /* AppSocket */ | |
70 | int error; /* Error code (if any) */ | |
71 | http_addrlist_t *addrlist, /* Address list */ | |
72 | *addr; /* Connected address */ | |
73 | char addrname[256]; /* Address name */ | |
74 | int snmp_enabled = 1; /* Is SNMP enabled? */ | |
75 | int snmp_fd, /* SNMP socket */ | |
76 | start_count, /* Page count via SNMP at start */ | |
77 | page_count, /* Page count via SNMP */ | |
78 | have_supplies; /* Printer supports supply levels? */ | |
79 | ssize_t bytes = 0, /* Initial bytes read */ | |
80 | tbytes; /* Total number of bytes written */ | |
81 | char buffer[1024]; /* Initial print buffer */ | |
82 | struct sigaction action; /* Actions for POSIX signals */ | |
83 | ||
84 | ||
85 | /* | |
86 | * Make sure status messages are not buffered... | |
87 | */ | |
88 | ||
89 | setbuf(stderr, NULL); | |
90 | ||
91 | /* | |
92 | * Ignore SIGPIPE signals... | |
93 | */ | |
94 | ||
95 | memset(&action, 0, sizeof(action)); | |
96 | action.sa_handler = SIG_IGN; | |
97 | sigaction(SIGPIPE, &action, NULL); | |
98 | ||
99 | /* | |
100 | * Check command-line... | |
101 | */ | |
102 | ||
103 | if (argc == 1) | |
104 | { | |
105 | printf("network socket \"Unknown\" \"%s\"\n", | |
106 | _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect"))); | |
107 | return (CUPS_BACKEND_OK); | |
108 | } | |
109 | else if (argc != 6 && argc != 7) | |
110 | { | |
111 | _cupsLangPrintf(stderr, | |
112 | _("Usage: %s job-id user title copies options [file]"), | |
113 | argv[0]); | |
114 | return (CUPS_BACKEND_FAILED); | |
115 | } | |
116 | ||
117 | /* | |
118 | * If we have 7 arguments, print the file named on the command-line. | |
119 | * Otherwise, send stdin instead... | |
120 | */ | |
121 | ||
122 | if (argc == 6) | |
123 | { | |
124 | print_fd = 0; | |
125 | copies = 1; | |
126 | } | |
127 | else | |
128 | { | |
129 | /* | |
130 | * Try to open the print file... | |
131 | */ | |
132 | ||
133 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) | |
134 | { | |
135 | _cupsLangPrintError("ERROR", _("Unable to open print file")); | |
136 | return (CUPS_BACKEND_FAILED); | |
137 | } | |
138 | ||
139 | copies = atoi(argv[4]); | |
140 | } | |
141 | ||
142 | /* | |
143 | * Extract the hostname and port number from the URI... | |
144 | */ | |
145 | ||
146 | while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) | |
147 | { | |
148 | _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); | |
149 | sleep(10); | |
150 | ||
151 | if (getenv("CLASS") != NULL) | |
152 | return (CUPS_BACKEND_FAILED); | |
153 | } | |
154 | ||
155 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), | |
156 | username, sizeof(username), hostname, sizeof(hostname), &port, | |
157 | resource, sizeof(resource)); | |
158 | ||
159 | if (port == 0) | |
160 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
161 | ||
162 | /* | |
163 | * Get options, if any... | |
164 | */ | |
165 | ||
166 | waiteof = 1; | |
167 | contimeout = 7 * 24 * 60 * 60; | |
168 | ||
169 | if ((options = strchr(resource, '?')) != NULL) | |
170 | { | |
171 | /* | |
172 | * Yup, terminate the device name string and move to the first | |
173 | * character of the options... | |
174 | */ | |
175 | ||
176 | *options++ = '\0'; | |
177 | ||
178 | /* | |
179 | * Parse options... | |
180 | */ | |
181 | ||
182 | while (*options) | |
183 | { | |
184 | /* | |
185 | * Get the name... | |
186 | */ | |
187 | ||
188 | name = options; | |
189 | ||
190 | while (*options && *options != '=' && *options != '+' && *options != '&') | |
191 | options ++; | |
192 | ||
193 | if ((sep = *options) != '\0') | |
194 | *options++ = '\0'; | |
195 | ||
196 | if (sep == '=') | |
197 | { | |
198 | /* | |
199 | * Get the value... | |
200 | */ | |
201 | ||
202 | value = options; | |
203 | ||
204 | while (*options && *options != '+' && *options != '&') | |
205 | options ++; | |
206 | ||
207 | if (*options) | |
208 | *options++ = '\0'; | |
209 | } | |
210 | else | |
211 | value = (char *)""; | |
212 | ||
213 | /* | |
214 | * Process the option... | |
215 | */ | |
216 | ||
217 | if (!_cups_strcasecmp(name, "waiteof")) | |
218 | { | |
219 | /* | |
220 | * Set the wait-for-eof value... | |
221 | */ | |
222 | ||
223 | waiteof = !value[0] || !_cups_strcasecmp(value, "on") || | |
224 | !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); | |
225 | } | |
226 | else if (!_cups_strcasecmp(name, "snmp")) | |
227 | { | |
228 | /* | |
229 | * Enable/disable SNMP stuff... | |
230 | */ | |
231 | ||
232 | snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") || | |
233 | !_cups_strcasecmp(value, "yes") || | |
234 | !_cups_strcasecmp(value, "true"); | |
235 | } | |
236 | else if (!_cups_strcasecmp(name, "contimeout")) | |
237 | { | |
238 | /* | |
239 | * Set the connection timeout... | |
240 | */ | |
241 | ||
242 | if (atoi(value) > 0) | |
243 | contimeout = atoi(value); | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | /* | |
249 | * Then try finding the remote host... | |
250 | */ | |
251 | ||
252 | start_time = time(NULL); | |
253 | ||
254 | addrlist = backendLookup(hostname, port, NULL); | |
255 | ||
256 | /* | |
257 | * See if the printer supports SNMP... | |
258 | */ | |
259 | ||
260 | if (snmp_enabled) | |
261 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
262 | else | |
263 | snmp_fd = -1; | |
264 | ||
265 | if (snmp_fd >= 0) | |
266 | have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), | |
267 | &start_count, NULL); | |
268 | else | |
269 | have_supplies = start_count = 0; | |
270 | ||
271 | /* | |
272 | * Wait for data from the filter... | |
273 | */ | |
274 | ||
275 | if (print_fd == 0) | |
276 | { | |
277 | if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB)) | |
278 | return (CUPS_BACKEND_OK); | |
279 | else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0) | |
280 | return (CUPS_BACKEND_OK); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Connect to the printer... | |
285 | */ | |
286 | ||
287 | fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port); | |
288 | _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); | |
289 | ||
290 | for (delay = 5;;) | |
291 | { | |
292 | if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL) | |
293 | { | |
294 | error = errno; | |
295 | device_fd = -1; | |
296 | ||
297 | if (getenv("CLASS") != NULL) | |
298 | { | |
299 | /* | |
300 | * If the CLASS environment variable is set, the job was submitted | |
301 | * to a class and not to a specific queue. In this case, we want | |
302 | * to abort immediately so that the job can be requeued on the next | |
303 | * available printer in the class. | |
304 | */ | |
305 | ||
306 | _cupsLangPrintFilter(stderr, "INFO", | |
307 | _("Unable to contact printer, queuing on next " | |
308 | "printer in class.")); | |
309 | ||
310 | /* | |
311 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
312 | */ | |
313 | ||
314 | sleep(5); | |
315 | ||
316 | return (CUPS_BACKEND_FAILED); | |
317 | } | |
318 | ||
319 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); | |
320 | ||
321 | if (errno == ECONNREFUSED || errno == EHOSTDOWN || errno == EHOSTUNREACH || errno == ETIMEDOUT || errno == ENOTCONN) | |
322 | { | |
323 | if (contimeout && (time(NULL) - start_time) > contimeout) | |
324 | { | |
325 | _cupsLangPrintFilter(stderr, "ERROR", | |
326 | _("The printer is not responding.")); | |
327 | return (CUPS_BACKEND_FAILED); | |
328 | } | |
329 | ||
330 | switch (error) | |
331 | { | |
332 | case EHOSTDOWN : | |
333 | _cupsLangPrintFilter(stderr, "WARNING", | |
334 | _("The printer may not exist or " | |
335 | "is unavailable at this time.")); | |
336 | break; | |
337 | ||
338 | case EHOSTUNREACH : | |
339 | default : | |
340 | _cupsLangPrintFilter(stderr, "WARNING", | |
341 | _("The printer is unreachable at this " | |
342 | "time.")); | |
343 | break; | |
344 | ||
345 | case ECONNREFUSED : | |
346 | _cupsLangPrintFilter(stderr, "WARNING", | |
347 | _("The printer is in use.")); | |
348 | break; | |
349 | } | |
350 | ||
351 | sleep((unsigned)delay); | |
352 | ||
353 | if (delay < 30) | |
354 | delay += 5; | |
355 | } | |
356 | else | |
357 | { | |
358 | _cupsLangPrintFilter(stderr, "ERROR", | |
359 | _("The printer is not responding.")); | |
360 | sleep(30); | |
361 | } | |
362 | } | |
363 | else | |
364 | break; | |
365 | } | |
366 | ||
367 | fputs("STATE: -connecting-to-device\n", stderr); | |
368 | _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); | |
369 | ||
370 | fprintf(stderr, "DEBUG: Connected to %s:%d...\n", | |
371 | httpAddrString(&(addr->addr), addrname, sizeof(addrname)), | |
372 | httpAddrPort(&(addr->addr))); | |
373 | ||
374 | /* | |
375 | * Print everything... | |
376 | */ | |
377 | ||
378 | tbytes = 0; | |
379 | ||
380 | if (bytes > 0) | |
381 | tbytes += write(device_fd, buffer, (size_t)bytes); | |
382 | ||
383 | while (copies > 0 && tbytes >= 0) | |
384 | { | |
385 | copies --; | |
386 | ||
387 | if (print_fd != 0) | |
388 | { | |
389 | fputs("PAGE: 1 1\n", stderr); | |
390 | lseek(print_fd, 0, SEEK_SET); | |
391 | } | |
392 | ||
393 | if ((bytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1, 0, backendNetworkSideCB)) < 0) | |
394 | tbytes = -1; | |
395 | else | |
396 | tbytes = bytes; | |
397 | ||
398 | if (print_fd != 0 && tbytes >= 0) | |
399 | _cupsLangPrintFilter(stderr, "INFO", _("Print file sent.")); | |
400 | } | |
401 | ||
402 | fputs("STATE: +cups-waiting-for-job-completed\n", stderr); | |
403 | ||
404 | if (waiteof && tbytes >= 0) | |
405 | { | |
406 | /* | |
407 | * Shutdown the socket and wait for the other end to finish... | |
408 | */ | |
409 | ||
410 | _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish.")); | |
411 | ||
412 | shutdown(device_fd, 1); | |
413 | ||
414 | while (wait_bc(device_fd, 90) > 0); | |
415 | } | |
416 | ||
417 | /* | |
418 | * Collect the final page count as needed... | |
419 | */ | |
420 | ||
421 | if (have_supplies && | |
422 | !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) && | |
423 | page_count > start_count) | |
424 | fprintf(stderr, "PAGE: total %d\n", page_count - start_count); | |
425 | ||
426 | /* | |
427 | * Close the socket connection... | |
428 | */ | |
429 | ||
430 | close(device_fd); | |
431 | ||
432 | httpAddrFreeList(addrlist); | |
433 | ||
434 | /* | |
435 | * Close the input file and return... | |
436 | */ | |
437 | ||
438 | if (print_fd != 0) | |
439 | close(print_fd); | |
440 | ||
441 | return (tbytes >= 0 ? CUPS_BACKEND_OK : CUPS_BACKEND_FAILED); | |
442 | } | |
443 | ||
444 | ||
445 | /* | |
446 | * 'wait_bc()' - Wait for back-channel data... | |
447 | */ | |
448 | ||
449 | static ssize_t /* O - # bytes read or -1 on error */ | |
450 | wait_bc(int device_fd, /* I - Socket */ | |
451 | int secs) /* I - Seconds to wait */ | |
452 | { | |
453 | struct timeval timeout; /* Timeout for select() */ | |
454 | fd_set input; /* Input set for select() */ | |
455 | ssize_t bytes; /* Number of back-channel bytes read */ | |
456 | char buffer[1024]; /* Back-channel buffer */ | |
457 | ||
458 | ||
459 | /* | |
460 | * Wait up to "secs" seconds for backchannel data... | |
461 | */ | |
462 | ||
463 | timeout.tv_sec = secs; | |
464 | timeout.tv_usec = 0; | |
465 | ||
466 | FD_ZERO(&input); | |
467 | FD_SET(device_fd, &input); | |
468 | ||
469 | if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0) | |
470 | { | |
471 | /* | |
472 | * Grab the data coming back and spit it out to stderr... | |
473 | */ | |
474 | ||
475 | if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0) | |
476 | { | |
477 | fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n", | |
478 | (int)bytes); | |
479 | cupsBackChannelWrite(buffer, (size_t)bytes, 1.0); | |
480 | } | |
481 | ||
482 | return (bytes); | |
483 | } | |
484 | else | |
485 | return (-1); | |
486 | } |