]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Don't allow filters and backends to have group write permissions.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
43e2fc22 1/*
b2e10895 2 * "$Id$"
43e2fc22 3 *
e73c6c0a 4 * AppSocket backend for the Common UNIX Printing System (CUPS).
43e2fc22 5 *
9fafda28 6 * Copyright 2007-2009 by Apple Inc.
761a542e 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
43e2fc22 8 *
9 * These coded instructions, statements, and computer programs are the
4e8d321f 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
43e2fc22 12 * "LICENSE" which should have been included with this file. If this
4e8d321f 13 * file is missing or damaged, see the license at "http://www.cups.org/".
43e2fc22 14 *
dab1a4d8 15 * This file is subject to the Apple OS-Developed Software exception.
16 *
43e2fc22 17 * Contents:
18 *
761a542e 19 * main() - Send a file to the printer or server.
313509aa 20 * wait_bc() - Wait for back-channel data...
43e2fc22 21 */
22
23/*
24 * Include necessary headers.
25 */
26
6ca34e4e 27#include <cups/http-private.h>
43b5ad0d 28#include "backend-private.h"
e73c6c0a 29#include <stdarg.h>
e73c6c0a 30#include <sys/types.h>
31#include <sys/stat.h>
32
c3026ddc 33#ifdef WIN32
e73c6c0a 34# include <winsock.h>
35#else
267cf96a 36# include <unistd.h>
37# include <fcntl.h>
e73c6c0a 38# include <sys/socket.h>
39# include <netinet/in.h>
40# include <arpa/inet.h>
41# include <netdb.h>
c3026ddc 42#endif /* WIN32 */
e73c6c0a 43
44
761a542e 45/*
46 * Local functions...
47 */
48
313509aa 49static int wait_bc(int device_fd, int secs);
761a542e 50
51
e73c6c0a 52/*
53 * 'main()' - Send a file to the printer or server.
54 *
55 * Usage:
56 *
57 * printer-uri job-id user title copies options [file]
58 */
59
5baa731f 60int /* O - Exit status */
61main(int argc, /* I - Number of command-line arguments (6 or 7) */
62 char *argv[]) /* I - Command-line arguments */
e73c6c0a 63{
cda2b561 64 const char *device_uri; /* Device URI */
9fa9ba8f 65 char scheme[255], /* Scheme in URI */
5baa731f 66 hostname[1024], /* Hostname */
67 username[255], /* Username info (not used) */
41ed0c21 68 resource[1024], /* Resource info (not used) */
69 *options, /* Pointer to options */
abd1f85f 70 *name, /* Name of option */
71 *value, /* Value of option */
72 sep; /* Option separator */
43b5ad0d 73 int print_fd; /* Print file */
5baa731f 74 int copies; /* Number of copies to print */
ef494175 75 time_t start_time, /* Time of first connect */
8953a911 76 current_time, /* Current time */
ef494175 77 wait_time; /* Time to wait before shutting down socket */
ff0295f0 78 int recoverable; /* Recoverable error shown? */
79 int contimeout; /* Connection timeout */
41ed0c21 80 int waiteof; /* Wait for end-of-file? */
5baa731f 81 int port; /* Port number */
086c584d 82 char portname[255]; /* Port name */
5baa731f 83 int delay; /* Delay for retries... */
43b5ad0d 84 int device_fd; /* AppSocket */
5baa731f 85 int error; /* Error code (if any) */
a0c0bbe7 86 http_addrlist_t *addrlist, /* Address list */
abd1f85f 87 *addr; /* Connected address */
a0c0bbe7 88 char addrname[256]; /* Address name */
5ba153ab 89 int snmp_fd, /* SNMP socket */
90 start_count, /* Page count via SNMP at start */
6bffe4a6 91 page_count, /* Page count via SNMP */
92 have_supplies; /* Printer supports supply levels? */
43b5ad0d 93 ssize_t tbytes; /* Total number of bytes written */
4ff40357 94#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
5baa731f 95 struct sigaction action; /* Actions for POSIX signals */
4ff40357 96#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
e73c6c0a 97
98
4b23f3b3 99 /*
100 * Make sure status messages are not buffered...
101 */
102
103 setbuf(stderr, NULL);
104
98904cd6 105 /*
106 * Ignore SIGPIPE signals...
107 */
108
109#ifdef HAVE_SIGSET
110 sigset(SIGPIPE, SIG_IGN);
111#elif defined(HAVE_SIGACTION)
112 memset(&action, 0, sizeof(action));
113 action.sa_handler = SIG_IGN;
114 sigaction(SIGPIPE, &action, NULL);
115#else
116 signal(SIGPIPE, SIG_IGN);
117#endif /* HAVE_SIGSET */
118
4b23f3b3 119 /*
120 * Check command-line...
121 */
122
68edc300 123 if (argc == 1)
124 {
3037604c 125 printf("network socket \"Unknown\" \"%s\"\n",
126 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
6248387b 127 return (CUPS_BACKEND_OK);
68edc300 128 }
129 else if (argc < 6 || argc > 7)
e73c6c0a 130 {
472af6f3 131 _cupsLangPrintf(stderr,
132 _("Usage: %s job-id user title copies options [file]\n"),
133 argv[0]);
6248387b 134 return (CUPS_BACKEND_FAILED);
e73c6c0a 135 }
136
137 /*
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
140 */
141
142 if (argc == 6)
3f9cb6c6 143 {
43b5ad0d 144 print_fd = 0;
145 copies = 1;
3f9cb6c6 146 }
e73c6c0a 147 else
148 {
149 /*
150 * Try to open the print file...
151 */
152
43b5ad0d 153 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
e73c6c0a 154 {
537a5f1d 155 _cupsLangPrintf(stderr,
156 _("ERROR: Unable to open print file \"%s\": %s\n"),
157 argv[6], strerror(errno));
6248387b 158 return (CUPS_BACKEND_FAILED);
e73c6c0a 159 }
3f9cb6c6 160
161 copies = atoi(argv[4]);
e73c6c0a 162 }
163
164 /*
165 * Extract the hostname and port number from the URI...
166 */
167
cda2b561 168 if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
169 return (CUPS_BACKEND_FAILED);
170
9fa9ba8f 171 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
172 username, sizeof(username), hostname, sizeof(hostname), &port,
0e66904d 173 resource, sizeof(resource));
e73c6c0a 174
175 if (port == 0)
176 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
177
41ed0c21 178 /*
179 * Get options, if any...
180 */
181
ff0295f0 182 waiteof = 1;
183 contimeout = 7 * 24 * 60 * 60;
41ed0c21 184
185 if ((options = strchr(resource, '?')) != NULL)
186 {
187 /*
188 * Yup, terminate the device name string and move to the first
189 * character of the options...
190 */
191
192 *options++ = '\0';
193
194 /*
195 * Parse options...
196 */
197
198 while (*options)
199 {
200 /*
201 * Get the name...
202 */
203
abd1f85f 204 name = options;
41ed0c21 205
abd1f85f 206 while (*options && *options != '=' && *options != '+' && *options != '&')
207 options ++;
208
209 if ((sep = *options) != '\0')
210 *options++ = '\0';
211
212 if (sep == '=')
41ed0c21 213 {
214 /*
215 * Get the value...
216 */
217
abd1f85f 218 value = options;
41ed0c21 219
abd1f85f 220 while (*options && *options != '+' && *options != '&')
41ed0c21 221 options ++;
abd1f85f 222
223 if (*options)
224 *options++ = '\0';
41ed0c21 225 }
226 else
abd1f85f 227 value = (char *)"";
41ed0c21 228
229 /*
230 * Process the option...
231 */
232
233 if (!strcasecmp(name, "waiteof"))
234 {
235 /*
236 * Set the wait-for-eof value...
237 */
238
239 waiteof = !value[0] || !strcasecmp(value, "on") ||
240 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
241 }
ff0295f0 242 else if (!strcasecmp(name, "contimeout"))
243 {
244 /*
245 * Set the connection timeout...
246 */
247
248 if (atoi(value) > 0)
249 contimeout = atoi(value);
250 }
41ed0c21 251 }
252 }
253
e73c6c0a 254 /*
255 * Then try to connect to the remote host...
256 */
257
ff0295f0 258 recoverable = 0;
259 start_time = time(NULL);
260
086c584d 261 sprintf(portname, "%d", port);
262
9fa9ba8f 263 fputs("STATE: +connecting-to-device\n", stderr);
bd8ccc10 264 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
265
086c584d 266 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
e73c6c0a 267 {
472af6f3 268 _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
269 hostname);
6248387b 270 return (CUPS_BACKEND_STOP);
e73c6c0a 271 }
272
9fa9ba8f 273 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
22c808e2 274 _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
e73c6c0a 275
43b5ad0d 276 for (delay = 5;;)
e73c6c0a 277 {
a0c0bbe7 278 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
e73c6c0a 279 {
43b5ad0d 280 error = errno;
281 device_fd = -1;
3f9cb6c6 282
43b5ad0d 283 if (getenv("CLASS") != NULL)
284 {
285 /*
286 * If the CLASS environment variable is set, the job was submitted
287 * to a class and not to a specific queue. In this case, we want
288 * to abort immediately so that the job can be requeued on the next
289 * available printer in the class.
290 */
b7b63780 291
472af6f3 292 _cupsLangPuts(stderr,
293 _("INFO: Unable to contact printer, queuing on next "
294 "printer in class...\n"));
b7b63780 295
43b5ad0d 296 /*
297 * Sleep 5 seconds to keep the job from requeuing too rapidly...
298 */
b7b63780 299
43b5ad0d 300 sleep(5);
b7b63780 301
43b5ad0d 302 return (CUPS_BACKEND_FAILED);
303 }
b7b63780 304
43b5ad0d 305 if (error == ECONNREFUSED || error == EHOSTDOWN ||
306 error == EHOSTUNREACH)
307 {
ff0295f0 308 if (contimeout && (time(NULL) - start_time) > contimeout)
309 {
472af6f3 310 _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
ff0295f0 311 return (CUPS_BACKEND_FAILED);
312 }
313
314 recoverable = 1;
315
472af6f3 316 _cupsLangPrintf(stderr,
317 _("WARNING: recoverable: Network host \'%s\' is busy; "
318 "will retry in %d seconds...\n"),
319 hostname, delay);
ff0295f0 320
43b5ad0d 321 sleep(delay);
10f725af 322
43b5ad0d 323 if (delay < 30)
324 delay += 5;
e73c6c0a 325 }
3f9cb6c6 326 else
43b5ad0d 327 {
ff0295f0 328 recoverable = 1;
329
472af6f3 330 _cupsLangPrintf(stderr, "DEBUG: Connection error: %s\n",
331 strerror(errno));
332 _cupsLangPuts(stderr,
333 _("ERROR: recoverable: Unable to connect to printer; "
334 "will retry in 30 seconds...\n"));
43b5ad0d 335 sleep(30);
336 }
e73c6c0a 337 }
43b5ad0d 338 else
339 break;
340 }
e73c6c0a 341
ff0295f0 342 if (recoverable)
343 {
344 /*
9fa9ba8f 345 * If we've shown a recoverable error make sure the printer proxies have a
346 * chance to see the recovered message. Not pretty but necessary for now...
ff0295f0 347 */
348
349 fputs("INFO: recovered: \n", stderr);
350 sleep(5);
351 }
352
43b5ad0d 353 fputs("STATE: -connecting-to-device\n", stderr);
22c808e2 354 _cupsLangPuts(stderr, _("INFO: Connected to printer...\n"));
a0c0bbe7 355
356#ifdef AF_INET6
357 if (addr->addr.addr.sa_family == AF_INET6)
ff0295f0 358 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
359 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
360 ntohs(addr->addr.ipv6.sin6_port));
a0c0bbe7 361 else
362#endif /* AF_INET6 */
363 if (addr->addr.addr.sa_family == AF_INET)
ff0295f0 364 fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
365 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
366 ntohs(addr->addr.ipv4.sin_port));
4ff40357 367
5ba153ab 368 /*
369 * See if the printer supports SNMP...
370 */
371
b9e7ae13 372 if ((snmp_fd = _cupsSNMPOpen(addr->addr.addr.sa_family)) >= 0)
c97b7208 373 {
6bffe4a6 374 have_supplies = !backendSNMPSupplies(snmp_fd, &(addr->addr), &start_count,
375 NULL);
c97b7208 376 }
377 else
6bffe4a6 378 have_supplies = start_count = 0;
5ba153ab 379
43b5ad0d 380 /*
381 * Print everything...
382 */
4ff40357 383
43b5ad0d 384 tbytes = 0;
e73c6c0a 385
43b5ad0d 386 while (copies > 0 && tbytes >= 0)
387 {
3f9cb6c6 388 copies --;
7c88bf41 389
43b5ad0d 390 if (print_fd != 0)
e73c6c0a 391 {
3f9cb6c6 392 fputs("PAGE: 1 1\n", stderr);
43b5ad0d 393 lseek(print_fd, 0, SEEK_SET);
e73c6c0a 394 }
e73c6c0a 395
5ba153ab 396 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addr->addr), 1,
397 backendNetworkSideCB);
5baa731f 398
43b5ad0d 399 if (print_fd != 0 && tbytes >= 0)
472af6f3 400 _cupsLangPrintf(stderr,
ff0295f0 401#ifdef HAVE_LONG_LONG
472af6f3 402 _("INFO: Sent print file, %lld bytes...\n"),
ff0295f0 403#else
472af6f3 404 _("INFO: Sent print file, %ld bytes...\n"),
ff0295f0 405#endif /* HAVE_LONG_LONG */
472af6f3 406 CUPS_LLCAST tbytes);
43b5ad0d 407 }
3f9cb6c6 408
313509aa 409 /*
ef494175 410 * Wait up to 5 seconds to get any pending back-channel data...
313509aa 411 */
412
ef494175 413 wait_time = time(NULL) + 5;
8953a911 414 while (wait_time >= time(&current_time))
415 if (wait_bc(device_fd, wait_time - current_time) <= 0)
ef494175 416 break;
313509aa 417
43b5ad0d 418 if (waiteof)
419 {
420 /*
421 * Shutdown the socket and wait for the other end to finish...
422 */
3f9cb6c6 423
472af6f3 424 _cupsLangPuts(stderr,
425 _("INFO: Print file sent, waiting for printer to finish...\n"));
0cd4970e 426
43b5ad0d 427 shutdown(device_fd, 1);
3f9cb6c6 428
313509aa 429 while (wait_bc(device_fd, 90) > 0);
43b5ad0d 430 }
b5cb0608 431
5ba153ab 432 /*
433 * Collect the final page count as needed...
434 */
435
6bffe4a6 436 if (have_supplies &&
5ba153ab 437 !backendSNMPSupplies(snmp_fd, &(addr->addr), &page_count, NULL) &&
438 page_count > start_count)
439 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
440
43b5ad0d 441 /*
442 * Close the socket connection...
443 */
3f9cb6c6 444
43b5ad0d 445 close(device_fd);
e73c6c0a 446
086c584d 447 httpAddrFreeList(addrlist);
448
e73c6c0a 449 /*
3f9cb6c6 450 * Close the input file and return...
e73c6c0a 451 */
452
43b5ad0d 453 if (print_fd != 0)
454 close(print_fd);
e73c6c0a 455
43b5ad0d 456 if (tbytes >= 0)
472af6f3 457 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
6248387b 458
43b5ad0d 459 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
e73c6c0a 460}
43e2fc22 461
462
313509aa 463/*
464 * 'wait_bc()' - Wait for back-channel data...
465 */
466
467static int /* O - # bytes read or -1 on error */
468wait_bc(int device_fd, /* I - Socket */
469 int secs) /* I - Seconds to wait */
470{
471 struct timeval timeout; /* Timeout for select() */
472 fd_set input; /* Input set for select() */
473 ssize_t bytes; /* Number of back-channel bytes read */
474 char buffer[1024]; /* Back-channel buffer */
475
476
477 /*
478 * Wait up to "secs" seconds for backchannel data...
479 */
480
481 timeout.tv_sec = secs;
482 timeout.tv_usec = 0;
483
484 FD_ZERO(&input);
485 FD_SET(device_fd, &input);
486
487 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
488 {
489 /*
490 * Grab the data coming back and spit it out to stderr...
491 */
492
493 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
494 {
495 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
496 (int)bytes);
497 cupsBackChannelWrite(buffer, bytes, 1.0);
498 }
499
500 return (bytes);
501 }
502 else
503 return (-1);
504}
505
506
b2e10895 507/*
508 * End of "$Id$".
43e2fc22 509 */