1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
|
/*
* Port 9100+n daemon
* Accepts a connection from port 9100+n and copy stream to
* /dev/lpn, where n = 0,1,2.
*
* GPLv2 license, read COPYING
*
* Run standalone as: p910nd [0|1|2]
*
* Run under inetd as:
* p910n stream tcp nowait root /usr/sbin/tcpd p910nd [0|1|2]
* where p910n is an /etc/services entry for
* port 9100, 9101 or 9102 as the case may be.
* root can be replaced by any uid with rw permission on /dev/lpn
*
* Port 9100+n will then be passively opened
* n defaults to 0
*
* Version 0.97
* Patches by Stefan Sichler.
* Stream to printer is only closed after EOF from network if
* it is no more busy, otherwise printer driver may discard data of last
* write() at close().
*
* Version 0.96
* Patches by Stefan Sichler.
* Fixed bi-directional mode to stay alive until connection is closed.
* Fixed timeout value in select() (controlling in/out balancing).
* Fixed network-to-printer buffer reporting EOF although there was data
* left in the buffer.
* Added log-to-stdout option (-d).
*
* Version 0.95
* Patch by Mario Izquierdo
* Fix incomplete conversion to manipulate new ip_addr structure
* when LIBWRAP is selected
*
* Version 0.94
* Patch by Guenther Niess:
* Support IPv6
* Patch by Philip Prindeville:
* Increase socket buffer size
* Use %hu for printing port
* Makefile fixes for LIBWRAP
*
* Version 0.93
* Fix open call to include mode, required for O_CREAT
*
* Version 0.92
* Patches by Dave Brown. Use raw I/O syscalls instead of
* stdio buffering. Buffer system to handle talkative bidi
* devices better on low-powered hosts.
*
* Version 0.91
* Patch by Hans Harder. Close printer device after each use to
* avoid crashing when hotpluggable devices going away.
* Don't wait 10 seconds after successful open.
*
* Version 0.9
* Patch by Kostas Liakakis to keep retrying every 10 seconds
* if EBUSY is returned by open_printer, apparently NetBSD
* does this if the printer is not on.
* Patch by Albert Bartoszko (al_bin@vp_pl), August 2006
* Work with hotpluggable devices
* Improve Makefile
*
* (The last two patches conflict somewhat, Liakakis's patch
* retries opening the device every 10 seconds until successful,
* whereas Bartoszko's patch exits if the printer device cannot be opened.
* The problem is with a hotpluggable device, that device node may
* not appear again.
*
* I have opted for Liakakis's behaviour. Let me know if this can
* be improved. Perhaps we need another option that chooses the
* behaviour. - Ken)
*
* Version 0.8
* Allow specifying address to bind to
*
* Version 0.7
* Bidirectional data transfer
*
* Version 0.6
* Arne Bernin fixed some cast warnings, corrected the version number
* and added a -v option to print the version.
*
* Version 0.5
* -DUSE_LIBWRAP and -lwrap enables hosts_access (tcpwrappers) checking.
*
* Version 0.4
* Ken Yap (greenpossum@users.sourceforge.net), April 2001
* Placed under GPL.
*
* Added -f switch to specify device which overrides /dev/lpn.
* But number is still required get distinct ports and locks.
*
* Added locking so that two invocations of the daemon under inetd
* don't try to open the printer at the same time. This can happen
* even if there is one host running clients because the previous
* client can exit after it has sent all data but the printer has not
* finished printing and inetd starts up a new daemon when the next
* request comes in too soon.
*
* Various things could be Linux specific. I don't
* think there is much demand for this program outside of PCs,
* but if you port it to other distributions or platforms,
* I'd be happy to receive your patches.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <syslog.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef USE_LIBWRAP
#include "tcpd.h"
int allow_severity, deny_severity;
extern int hosts_ctl(char *daemon, char *client_name, char *client_addr, char *client_user);
#endif
#define BASEPORT 9100
#define PIDFILE "/var/run/p910%cd.pid"
#ifdef LOCKFILE_DIR
#define LOCKFILE LOCKFILE_DIR "/p910%cd"
#else
#define LOCKFILE "/var/lock/p910nd/p910%cd"
#endif
#ifndef PRINTERFILE
#define PRINTERFILE "/dev/lp%c"
#endif
#define LOGOPTS LOG_ERR
#define BUFFER_SIZE 8192
/* Circular buffer used for each direction. */
typedef struct {
int detectEof; /* If nonzero, EOF is marked when read returns 0 bytes. */
int infd; /* Input file descriptor. */
int outfd; /* Output file descriptor. */
int startidx; /* Index of the start of valid data. */
int endidx; /* Index of the end of valid data. */
int bytes; /* The number of bytes currently buffered. */
int totalin; /* Total bytes that have been read. */
int totalout; /* Total bytes that have been written. */
int eof_read; /* Nonzero indicates the input file has reached EOF. */
int eof_sent; /* Nonzero indicates the output file has fully received all data. */
int err; /* Nonzero indicates an error detected on the output file. */
char buffer[BUFFER_SIZE]; /* Buffered data goes here. */
} Buffer_t;
static char *progname;
static char version[] = "Version 0.97";
static char copyright[] = "Copyright (c) 2008-2014 Ken Yap and others, GPLv2";
static int lockfd = -1;
static char *device = 0;
static int bidir = 0;
static char *bindaddr = 0;
static int log_to_stdout = 0;
/* Helper function: convert a struct sockaddr address (IPv4 and IPv6) to a string */
char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), s, maxlen);
break;
default:
strncpy(s, "Unknown AF", maxlen);
return NULL;
}
return s;
}
uint16_t get_port(const struct sockaddr *sa)
{
uint16_t port;
switch(sa->sa_family) {
case AF_INET:
port = ntohs(((struct sockaddr_in *)sa)->sin_port);
break;
case AF_INET6:
port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
break;
default:
return 0;
}
return port;
}
void usage(void)
{
fprintf(stderr, "%s %s %s\n", progname, version, copyright);
fprintf(stderr, "Usage: %s [-f device] [-i bindaddr] [-bvd] [0|1|2]\n", progname);
exit(1);
}
void show_version(void)
{
fprintf(stdout, "%s %s\n", progname, version);
}
void dolog(int level, char* msg, ...)
{
va_list argp;
va_start(argp, msg);
if (log_to_stdout)
vfprintf(stdout, msg, argp);
else if (level != LOG_DEBUG)
vsyslog(level, msg, argp);
va_end(argp);
}
int open_printer(int lpnumber)
{
int lp;
char lpname[sizeof(PRINTERFILE)];
#ifdef TESTING
(void)snprintf(lpname, sizeof(lpname), "/dev/tty");
#else
(void)snprintf(lpname, sizeof(lpname), PRINTERFILE, lpnumber);
#endif
if (device == 0)
device = lpname;
if ((lp = open(device, bidir ? (O_RDWR|O_NONBLOCK) : O_WRONLY)) == -1) {
if (errno != EBUSY)
dolog(LOGOPTS, "%s: %m\n", device);
dolog(LOGOPTS, "%s: %m, will try opening later\n", device);
}
return (lp);
}
int get_lock(int lpnumber)
{
char lockname[sizeof(LOCKFILE)];
struct flock lplock;
(void)snprintf(lockname, sizeof(lockname), LOCKFILE, lpnumber);
if ((lockfd = open(lockname, O_CREAT | O_RDWR, 0666)) < 0) {
dolog(LOGOPTS, "%s: %m\n", lockname);
return (0);
}
memset(&lplock, 0, sizeof(lplock));
lplock.l_type = F_WRLCK;
lplock.l_pid = getpid();
if (fcntl(lockfd, F_SETLKW, &lplock) < 0) {
dolog(LOGOPTS, "%s: %m\n", lockname);
return (0);
}
return (1);
}
void free_lock(void)
{
if (lockfd >= 0)
(void)close(lockfd);
}
/* Initializes the buffer, at the start. */
void initBuffer(Buffer_t * b, int infd, int outfd, int detectEof)
{
b->detectEof = detectEof;
b->infd = infd;
b->outfd = outfd;
b->startidx = 0;
b->endidx = 0;
b->bytes = 0;
b->totalin = 0;
b->totalout = 0;
b->eof_read = 0;
b->eof_sent = 0;
b->err = 0;
}
/* Sets the readfds and writefds (used by select) based on current buffer state. */
void prepBuffer(Buffer_t * b, fd_set * readfds, fd_set * writefds)
{
if (b->outfd>=0 && !b->err && (b->bytes != 0 || b->eof_read)) {
FD_SET(b->outfd, writefds);
}
if (b->infd>=0 && !b->eof_read && b->bytes < sizeof(b->buffer)) {
FD_SET(b->infd, readfds);
}
}
/* Reads data into a buffer from its input file. */
ssize_t readBuffer(Buffer_t * b)
{
int avail;
ssize_t result = 0;
/* If err, the data will not be written, so no need to store it. */
if (b->bytes == 0 || b->err) {
/* The buffer is empty. */
b->startidx = b->endidx = 0;
avail = sizeof(b->buffer);
} else if (b->bytes == sizeof(b->buffer)) {
/* The buffer is full. */
avail = 0;
} else if (b->endidx > b->startidx) {
/* The buffer is not wrapped: from endidx to end of buffer is free. */
avail = sizeof(b->buffer) - b->endidx;
} else {
/* The buffer is wrapped: gap between endidx and startidx is free. */
avail = b->startidx - b->endidx;
}
if (avail) {
result = read(b->infd, b->buffer + b->endidx, avail);
if (result > 0) {
/* Some data was read. Update accordingly. */
b->endidx += result;
b->totalin += result;
b->bytes += result;
if (b->endidx == sizeof(b->buffer)) {
/* Time to wrap the buffer. */
b->endidx = 0;
}
} else if (result < 0) {
dolog(LOGOPTS, "read: %m\n");
b->err = 1;
}
else if (b->detectEof) {
dolog(LOG_DEBUG, "read: eof\n");
b->eof_read = 1;
} else
result = 0; // in case there is still data in the buffer, ignore the error by now
}
/* Return the value returned by read(), which is -1 (error), or #bytes read. */
return result;
}
/* Writes data from a buffer to the output file or discard if no output file is set. */
ssize_t writeBuffer(Buffer_t * b)
{
int avail;
ssize_t result = 0;
if (b->bytes == 0 || b->err) {
/* Buffer is empty. */
avail = 0;
} else if (b->endidx > b->startidx) {
/* Buffer is not wrapped. Can write all the data. */
avail = b->endidx - b->startidx;
} else {
/* Buffer is wrapped. Can only write the top (first) part. */
avail = sizeof(b->buffer) - b->startidx;
}
if (avail) {
if (b->outfd>=0)
result = write(b->outfd, b->buffer + b->startidx, avail);
else
result = avail;
if (result < 0) {
/* Mark the output file in an error condition. */
dolog(LOGOPTS, "write: %m\n");
b->err = 1;
} else {
/* Zero or more bytes were written. */
b->startidx += result;
if (b->outfd>=0)
b->totalout += result;
b->bytes -= result;
if (b->startidx == sizeof(b->buffer)) {
/* Unwrap the buffer. */
b->startidx = 0;
}
}
}
else if (b->eof_read)
b->eof_sent = 1;
/* Return the write() result, -1 (error) or #bytes written. */
return result;
}
/* Copy network data from file descriptor fd (network) to lp (printer) until EOS */
/* If bidir, also copy data from printer (lp) to network (fd). */
int copy_stream(int fd, int lp)
{
int result;
Buffer_t networkToPrinterBuffer;
initBuffer(&networkToPrinterBuffer, fd, lp, 1);
if (bidir) {
struct timeval now;
struct timeval then;
struct timeval timeout;
int timer = 0;
Buffer_t printerToNetworkBuffer;
initBuffer(&printerToNetworkBuffer, lp, fd, 0);
fd_set readfds;
fd_set writefds;
/* Finish when network sent EOF. */
/* Although the printer to network stream may not be finished (does this matter?) */
while (!networkToPrinterBuffer.eof_sent && !networkToPrinterBuffer.err) {
FD_ZERO(&readfds);
FD_ZERO(&writefds);
prepBuffer(&networkToPrinterBuffer, &readfds, &writefds);
prepBuffer(&printerToNetworkBuffer, &readfds, &writefds);
int maxfd = lp > fd ? lp : fd;
if (timer) {
/* Delay after reading from the printer, so the */
/* return stream cannot dominate. */
/* Don't read from the printer until the timer expires. */
gettimeofday(&now, 0);
if ((now.tv_sec > then.tv_sec) || (now.tv_sec == then.tv_sec && now.tv_usec > then.tv_usec))
timer = 0;
else
FD_CLR(lp, &readfds);
}
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
result = select(maxfd + 1, &readfds, &writefds, 0, &timeout);
if (result < 0)
return (result);
if (FD_ISSET(fd, &readfds)) {
/* Read network data. */
result = readBuffer(&networkToPrinterBuffer);
if (result > 0)
dolog(LOG_DEBUG,"read %d bytes from network\n",result);
}
if (FD_ISSET(lp, &readfds)) {
/* Read printer data, but pace it more slowly. */
result = readBuffer(&printerToNetworkBuffer);
if (result > 0) {
dolog(LOG_DEBUG,"read %d bytes from printer\n",result);
gettimeofday(&then, 0);
// wait 100 msec before reading again.
then.tv_usec += 100000;
if (then.tv_usec > 1000000) {
then.tv_usec -= 1000000;
then.tv_sec++;
}
timer = 1;
}
}
if (FD_ISSET(lp, &writefds)) {
/* Write data to printer. */
result = writeBuffer(&networkToPrinterBuffer);
if (result > 0)
dolog(LOG_DEBUG,"wrote %d bytes to printer\n",result);
}
if (FD_ISSET(fd, &writefds) || printerToNetworkBuffer.outfd == -1) {
/* Write data to network. */
result = writeBuffer(&printerToNetworkBuffer);
/* If socket write error, discard further data from printer */
if (result < 0) {
printerToNetworkBuffer.outfd = -1;
printerToNetworkBuffer.err = 0;
result = 0;
dolog(LOG_DEBUG,"network write error, discarding further printer data\n",result);
}
else if (result > 0) {
if (printerToNetworkBuffer.outfd == -1)
dolog(LOG_DEBUG,"discarded %d bytes from printer\n",result);
else
dolog(LOG_DEBUG,"wrote %d bytes to network\n",result);
}
}
}
dolog(LOG_NOTICE,
"Finished job: %d/%d bytes sent to printer, %d/%d bytes sent to network\n",
networkToPrinterBuffer.totalout,networkToPrinterBuffer.totalin, printerToNetworkBuffer.totalout, printerToNetworkBuffer.totalin);
} else {
/* Unidirectional: simply read from network, and write to printer. */
while (!networkToPrinterBuffer.eof_sent && !networkToPrinterBuffer.err) {
result = readBuffer(&networkToPrinterBuffer);
if (result > 0)
dolog(LOG_DEBUG,"read %d bytes from network\n",result);
result = writeBuffer(&networkToPrinterBuffer);
if (result > 0)
dolog(LOG_DEBUG,"wrote %d bytes to printer\n",result);
}
dolog(LOG_NOTICE, "Finished job: %d/%d bytes sent to printer\n", networkToPrinterBuffer.totalout, networkToPrinterBuffer.totalin);
}
return (networkToPrinterBuffer.err?-1:0);
}
void one_job(int lpnumber)
{
int lp;
struct sockaddr_storage client;
socklen_t clientlen = sizeof(client);
if (getpeername(0, (struct sockaddr *)&client, &clientlen) >= 0) {
char host[INET6_ADDRSTRLEN];
dolog(LOG_NOTICE, "Connection from %s port %hu\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client));
}
if (get_lock(lpnumber) == 0)
return;
/* Make sure lp device is open... */
while ((lp = open_printer(lpnumber)) == -1)
sleep(10);
if (copy_stream(0, lp) < 0)
dolog(LOGOPTS, "copy_stream: %m\n");
close(lp);
free_lock();
}
void server(int lpnumber)
{
struct rlimit resourcelimit;
#ifdef USE_GETPROTOBYNAME
struct protoent *proto;
#endif
int netfd = -1, fd, lp, one = 1;
socklen_t clientlen;
struct sockaddr_storage client;
struct addrinfo hints, *res, *ressave;
char pidfilename[sizeof(PIDFILE)];
char service[sizeof(BASEPORT+lpnumber-'0')+1];
FILE *f;
const int bufsiz = 65536;
#ifndef TESTING
if (!log_to_stdout)
{
switch (fork()) {
case -1:
dolog(LOGOPTS, "fork: %m\n");
exit(1);
case 0: /* child */
break;
default: /* parent */
exit(0);
}
/* Now in child process */
resourcelimit.rlim_max = 0;
if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0) {
dolog(LOGOPTS, "getrlimit: %m\n");
exit(1);
}
for (fd = 0; fd < resourcelimit.rlim_max; ++fd)
(void)close(fd);
if (setsid() < 0) {
dolog(LOGOPTS, "setsid: %m\n");
exit(1);
}
(void)chdir("/");
(void)umask(022);
fd = open("/dev/null", O_RDWR); /* stdin */
(void)dup(fd); /* stdout */
(void)dup(fd); /* stderr */
(void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber);
if ((f = fopen(pidfilename, "w")) == NULL) {
dolog(LOGOPTS, "%s: %m\n", pidfilename);
exit(1);
}
(void)fprintf(f, "%d\n", getpid());
(void)fclose(f);
}
if (get_lock(lpnumber) == 0)
exit(1);
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
(void)snprintf(service, sizeof(service), "%hu", (BASEPORT + lpnumber - '0'));
if (getaddrinfo(bindaddr, service, &hints, &res) != 0) {
dolog(LOGOPTS, "getaddr: %m\n");
exit(1);
}
ressave = res;
while (res) {
#ifdef USE_GETPROTOBYNAME
if ((proto = getprotobyname("tcp6")) == NULL) {
if ((proto = getprotobyname("tcp")) == NULL) {
dolog(LOGOPTS, "Cannot find protocol for TCP!\n");
exit(1);
}
}
if ((netfd = socket(res->ai_family, res->ai_socktype, proto->p_proto)) < 0)
#else
if ((netfd = socket(res->ai_family, res->ai_socktype, IPPROTO_IP)) < 0)
#endif
{
dolog(LOGOPTS, "socket: %m\n");
close(netfd);
res = res->ai_next;
continue;
}
if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz)) < 0) {
dolog(LOGOPTS, "setsocketopt: SO_RCVBUF: %m\n");
/* not fatal if it fails */
}
if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz)) < 0) {
dolog(LOGOPTS, "setsocketopt: SO_SNDBUF: %m\n");
/* not fatal if it fails */
}
if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
dolog(LOGOPTS, "setsocketopt: SO_REUSEADDR: %m\n");
close(netfd);
res = res->ai_next;
continue;
}
if (bind(netfd, res->ai_addr, res->ai_addrlen) < 0) {
dolog(LOGOPTS, "bind: %m\n");
close(netfd);
res = res->ai_next;
continue;
}
if (listen(netfd, 5) < 0) {
dolog(LOGOPTS, "listen: %m\n");
close(netfd);
res = res->ai_next;
continue;
}
break;
}
freeaddrinfo(ressave);
clientlen = sizeof(client);
memset(&client, 0, sizeof(client));
while ((fd = accept(netfd, (struct sockaddr *)&client, &clientlen)) >= 0) {
char host[INET6_ADDRSTRLEN];
#ifdef USE_LIBWRAP
if (hosts_ctl("p910nd", STRING_UNKNOWN, get_ip_str((struct sockaddr *)&client, host, sizeof(host)), STRING_UNKNOWN) == 0) {
dolog(LOGOPTS,
"Connection from %s port %hu rejected\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client));
close(fd);
continue;
}
#endif
dolog(LOG_NOTICE, "Connection from %s port %hu accepted\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client));
/*write(fd, "Printing", 8); */
/* Make sure lp device is open... */
while ((lp = open_printer(lpnumber)) == -1)
sleep(10);
if (copy_stream(fd, lp) < 0)
dolog(LOGOPTS, "copy_stream: %m\n");
(void)close(fd);
(void)close(lp);
}
dolog(LOGOPTS, "accept: %m\n");
free_lock();
exit(1);
}
int is_standalone(void)
{
struct sockaddr_storage bind_addr;
socklen_t ba_len;
/*
* Check to see if a socket was passed to us from (x)inetd.
*
* Use getsockname() to determine if descriptor 0 is indeed a socket
* (and thus we are probably a child of (x)inetd) or if it is instead
* something else and we are running standalone.
*/
ba_len = sizeof(bind_addr);
if (getsockname(0, (struct sockaddr *)&bind_addr, &ba_len) == 0)
return (0); /* under (x)inetd */
if (errno != ENOTSOCK) /* strange... */
dolog(LOGOPTS, "getsockname: %m\n");
return (1);
}
int main(int argc, char *argv[])
{
int c, lpnumber;
char *p;
if (argc <= 0) /* in case not provided in (x)inetd config */
progname = "p910nd";
else {
progname = argv[0];
if ((p = strrchr(progname, '/')) != 0)
progname = p + 1;
}
lpnumber = '0';
while ((c = getopt(argc, argv, "bdi:f:v")) != EOF) {
switch (c) {
case 'b':
bidir = 1;
break;
case 'd':
log_to_stdout = 1;
break;
case 'f':
device = optarg;
break;
case 'i':
bindaddr = optarg;
break;
case 'v':
show_version();
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
/* exit if port number is bigger than 9 */
/*(2 digits breaks some things, ex LOCKFILE) */
if ( strlen(argv[0]) > 1) {
fprintf(stderr, "Error, port number > 9\n");
return(-1);
}
if (isdigit(argv[0][0]))
lpnumber = argv[0][0];
}
/* change the n in argv[0] to match the port so ps will show that */
if ((p = strstr(progname, "p910n")) != NULL)
p[4] = lpnumber;
/* We used to pass (LOG_PERROR|LOG_PID|LOG_LPR|LOG_ERR) to syslog, but
* syslog ignored the LOG_PID and LOG_PERROR option. I.e. the intention
* was to add both options but the effect was to have neither.
* I disagree with the intention to add PERROR. --Stef */
if (!log_to_stdout)
openlog(p, LOG_PID, LOG_LPR);
if (log_to_stdout || is_standalone())
server(lpnumber);
else
one_job(lpnumber);
return (0);
}
|