1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1983, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #if 0 40 #endif 41 42 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 43 /* 44 * lpd -- line printer daemon. 45 * 46 * Listen for a connection and perform the requested operation. 47 * Operations are: 48 * \1printer\n 49 * check the queue for jobs and print any found. 50 * \2printer\n 51 * receive a job from another machine and queue it. 52 * \3printer [users ...] [jobs ...]\n 53 * return the current state of the queue (short form). 54 * \4printer [users ...] [jobs ...]\n 55 * return the current state of the queue (long form). 56 * \5printer person [users ...] [jobs ...]\n 57 * remove jobs from the queue. 58 * 59 * Strategy to maintain protected spooling area: 60 * 1. Spooling area is writable only by daemon and spooling group 61 * 2. lpr runs setuid root and setgrp spooling group; it uses 62 * root to access any file it wants (verifying things before 63 * with an access call) and group id to know how it should 64 * set up ownership of files in the spooling area. 65 * 3. Files in spooling area are owned by root, group spooling 66 * group, with mode 660. 67 * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 68 * access files and printer. Users can't get to anything 69 * w/o help of lpq and lprm programs. 70 */ 71 72 #include <sys/param.h> 73 #include <sys/wait.h> 74 #include <sys/types.h> 75 #include <sys/socket.h> 76 #include <sys/un.h> 77 #include <sys/stat.h> 78 #include <sys/file.h> 79 #include <netinet/in.h> 80 #include <arpa/inet.h> 81 82 #include <netdb.h> 83 #include <unistd.h> 84 #include <syslog.h> 85 #include <signal.h> 86 #include <err.h> 87 #include <errno.h> 88 #include <fcntl.h> 89 #include <dirent.h> 90 #include <stdio.h> 91 #include <stdlib.h> 92 #include <string.h> 93 #include <sysexits.h> 94 #include <ctype.h> 95 #include "lp.h" 96 #include "lp.local.h" 97 #include "pathnames.h" 98 #include "extern.h" 99 100 int lflag; /* log requests flag */ 101 int sflag; /* no incoming port flag */ 102 int Fflag; /* run in foreground flag */ 103 int from_remote; /* from remote socket */ 104 105 int main(int argc, char **_argv); 106 static void reapchild(int _signo); 107 static void mcleanup(int _signo); 108 static void doit(void); 109 static void startup(void); 110 static void chkhost(struct sockaddr *_f, int _ch_opts); 111 static int ckqueue(struct printer *_pp); 112 static void fhosterr(int _ch_opts, char *_sysmsg, char *_usermsg); 113 static int *socksetup(int _af, int _debuglvl); 114 static void usage(void); 115 116 /* XXX from libc/net/rcmd.c */ 117 extern int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, 118 const char *, const char *); 119 120 uid_t uid, euid; 121 122 #define LPD_NOPORTCHK 0001 /* skip reserved-port check */ 123 #define LPD_LOGCONNERR 0002 /* (sys)log connection errors */ 124 #define LPD_ADDFROMLINE 0004 /* just used for fhosterr() */ 125 126 int 127 main(int argc, char **argv) 128 { 129 int ch_options, errs, f, funix, *finet, i, lfd, socket_debug; 130 fd_set defreadfds; 131 struct sockaddr_un un, fromunix; 132 struct sockaddr_storage frominet; 133 socklen_t fromlen; 134 sigset_t omask, nmask; 135 struct servent *sp, serv; 136 int inet_flag = 0, inet6_flag = 0; 137 138 euid = geteuid(); /* these shouldn't be different */ 139 uid = getuid(); 140 141 ch_options = 0; 142 socket_debug = 0; 143 gethostname(local_host, sizeof(local_host)); 144 145 progname = "lpd"; 146 147 if (euid != 0) 148 errx(EX_NOPERM,"must run as root"); 149 150 errs = 0; 151 while ((i = getopt(argc, argv, "cdlpswFW46")) != -1) 152 switch (i) { 153 case 'c': 154 /* log all kinds of connection-errors to syslog */ 155 ch_options |= LPD_LOGCONNERR; 156 break; 157 case 'd': 158 socket_debug++; 159 break; 160 case 'l': 161 lflag++; 162 break; 163 case 'p': /* letter initially used for -s */ 164 /* 165 * This will probably be removed with 5.0-release. 166 */ 167 /* FALLTHROUGH */ 168 case 's': /* secure (no inet) */ 169 sflag++; 170 break; 171 case 'w': /* netbsd uses -w for maxwait */ 172 /* 173 * This will be removed after the release of 4.4, as 174 * it conflicts with -w in netbsd's lpd. For now it 175 * is just a warning, so we won't suddenly break lpd 176 * for anyone who is currently using the option. 177 */ 178 syslog(LOG_WARNING, 179 "NOTE: the -w option has been renamed -W"); 180 syslog(LOG_WARNING, 181 "NOTE: please change your lpd config to use -W"); 182 /* FALLTHROUGH */ 183 case 'F': 184 Fflag++; 185 break; 186 case 'W': 187 /* allow connections coming from a non-reserved port */ 188 /* (done by some lpr-implementations for MS-Windows) */ 189 ch_options |= LPD_NOPORTCHK; 190 break; 191 case '4': 192 family = PF_INET; 193 inet_flag++; 194 break; 195 case '6': 196 #ifdef INET6 197 family = PF_INET6; 198 inet6_flag++; 199 #else 200 errx(EX_USAGE, "lpd compiled sans INET6 (IPv6 support)"); 201 #endif 202 break; 203 /* 204 * The following options are not in FreeBSD (yet?), but are 205 * listed here to "reserve" them, because the option-letters 206 * are used by either NetBSD or OpenBSD (as of July 2001). 207 */ 208 case 'b': /* set bind-addr */ 209 case 'n': /* set max num of children */ 210 case 'r': /* allow 'of' for remote ptrs */ 211 /* ...[not needed in freebsd] */ 212 /* FALLTHROUGH */ 213 default: 214 errs++; 215 } 216 if (inet_flag && inet6_flag) 217 family = PF_UNSPEC; 218 argc -= optind; 219 argv += optind; 220 if (errs) 221 usage(); 222 223 if (argc == 1) { 224 if ((i = atoi(argv[0])) == 0) 225 usage(); 226 if (i < 0 || i > USHRT_MAX) 227 errx(EX_USAGE, "port # %d is invalid", i); 228 229 serv.s_port = htons(i); 230 sp = &serv; 231 argc--; 232 } else { 233 sp = getservbyname("printer", "tcp"); 234 if (sp == NULL) 235 errx(EX_OSFILE, "printer/tcp: unknown service"); 236 } 237 238 if (argc != 0) 239 usage(); 240 241 /* 242 * We run chkprintcap right away to catch any errors and blat them 243 * to stderr while we still have it open, rather than sending them 244 * to syslog and leaving the user wondering why lpd started and 245 * then stopped. There should probably be a command-line flag to 246 * ignore errors from chkprintcap. 247 */ 248 { 249 pid_t pid; 250 int status; 251 pid = fork(); 252 if (pid < 0) { 253 err(EX_OSERR, "cannot fork"); 254 } else if (pid == 0) { /* child */ 255 execl(_PATH_CHKPRINTCAP, _PATH_CHKPRINTCAP, (char *)0); 256 err(EX_OSERR, "cannot execute %s", _PATH_CHKPRINTCAP); 257 } 258 if (waitpid(pid, &status, 0) < 0) { 259 err(EX_OSERR, "cannot wait"); 260 } 261 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) 262 errx(EX_OSFILE, "%d errors in printcap file, exiting", 263 WEXITSTATUS(status)); 264 } 265 266 #ifdef DEBUG 267 Fflag++; 268 #endif 269 /* 270 * Set up standard environment by detaching from the parent 271 * if -F not specified 272 */ 273 if (Fflag == 0) { 274 daemon(0, 0); 275 } 276 277 openlog("lpd", LOG_PID, LOG_LPR); 278 syslog(LOG_INFO, "lpd startup: logging=%d%s%s", lflag, 279 socket_debug ? " dbg" : "", sflag ? " net-secure" : ""); 280 (void) umask(0); 281 /* 282 * NB: This depends on O_NONBLOCK semantics doing the right thing; 283 * i.e., applying only to the O_EXLOCK and not to the rest of the 284 * open/creation. As of 1997-12-02, this is the case for commonly- 285 * used filesystems. There are other places in this code which 286 * make the same assumption. 287 */ 288 lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 289 LOCK_FILE_MODE); 290 if (lfd < 0) { 291 if (errno == EWOULDBLOCK) /* active daemon present */ 292 exit(0); 293 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 294 exit(1); 295 } 296 fcntl(lfd, F_SETFL, 0); /* turn off non-blocking mode */ 297 ftruncate(lfd, 0); 298 /* 299 * write process id for others to know 300 */ 301 sprintf(line, "%u\n", getpid()); 302 f = strlen(line); 303 if (write(lfd, line, f) != f) { 304 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 305 exit(1); 306 } 307 signal(SIGCHLD, reapchild); 308 /* 309 * Restart all the printers. 310 */ 311 startup(); 312 (void) unlink(_PATH_SOCKETNAME); 313 funix = socket(AF_UNIX, SOCK_STREAM, 0); 314 if (funix < 0) { 315 syslog(LOG_ERR, "socket: %m"); 316 exit(1); 317 } 318 319 sigemptyset(&nmask); 320 sigaddset(&nmask, SIGHUP); 321 sigaddset(&nmask, SIGINT); 322 sigaddset(&nmask, SIGQUIT); 323 sigaddset(&nmask, SIGTERM); 324 sigprocmask(SIG_BLOCK, &nmask, &omask); 325 326 (void) umask(07); 327 signal(SIGHUP, mcleanup); 328 signal(SIGINT, mcleanup); 329 signal(SIGQUIT, mcleanup); 330 signal(SIGTERM, mcleanup); 331 memset(&un, 0, sizeof(un)); 332 un.sun_family = AF_UNIX; 333 strcpy(un.sun_path, _PATH_SOCKETNAME); 334 #ifndef SUN_LEN 335 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 336 #endif 337 if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 338 syslog(LOG_ERR, "ubind: %m"); 339 exit(1); 340 } 341 (void) umask(0); 342 sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0); 343 FD_ZERO(&defreadfds); 344 FD_SET(funix, &defreadfds); 345 listen(funix, 5); 346 if (sflag == 0) { 347 finet = socksetup(family, socket_debug); 348 } else 349 finet = NULL; /* pretend we couldn't open TCP socket. */ 350 if (finet) { 351 for (i = 1; i <= *finet; i++) { 352 FD_SET(finet[i], &defreadfds); 353 listen(finet[i], 5); 354 } 355 } 356 /* 357 * Main loop: accept, do a request, continue. 358 */ 359 memset(&frominet, 0, sizeof(frominet)); 360 memset(&fromunix, 0, sizeof(fromunix)); 361 if (lflag) 362 syslog(LOG_INFO, "lpd startup: ready to accept requests"); 363 /* 364 * XXX - should be redone for multi-protocol 365 */ 366 for (;;) { 367 int domain, nfds, s; 368 fd_set readfds; 369 370 FD_COPY(&defreadfds, &readfds); 371 nfds = select(20, &readfds, 0, 0, 0); 372 if (nfds <= 0) { 373 if (nfds < 0 && errno != EINTR) 374 syslog(LOG_WARNING, "select: %m"); 375 continue; 376 } 377 domain = -1; /* avoid compile-time warning */ 378 s = -1; /* avoid compile-time warning */ 379 if (FD_ISSET(funix, &readfds)) { 380 domain = AF_UNIX, fromlen = sizeof(fromunix); 381 s = accept(funix, 382 (struct sockaddr *)&fromunix, &fromlen); 383 } else { 384 for (i = 1; i <= *finet; i++) 385 if (FD_ISSET(finet[i], &readfds)) { 386 domain = AF_INET; 387 fromlen = sizeof(frominet); 388 s = accept(finet[i], 389 (struct sockaddr *)&frominet, 390 &fromlen); 391 } 392 } 393 if (s < 0) { 394 if (errno != EINTR) 395 syslog(LOG_WARNING, "accept: %m"); 396 continue; 397 } 398 if (fork() == 0) { 399 /* 400 * Note that printjob() also plays around with 401 * signal-handling routines, and may need to be 402 * changed when making changes to signal-handling. 403 */ 404 signal(SIGCHLD, SIG_DFL); 405 signal(SIGHUP, SIG_IGN); 406 signal(SIGINT, SIG_IGN); 407 signal(SIGQUIT, SIG_IGN); 408 signal(SIGTERM, SIG_IGN); 409 (void) close(funix); 410 if (sflag == 0 && finet) { 411 for (i = 1; i <= *finet; i++) 412 (void)close(finet[i]); 413 } 414 dup2(s, STDOUT_FILENO); 415 (void) close(s); 416 if (domain == AF_INET) { 417 /* for both AF_INET and AF_INET6 */ 418 from_remote = 1; 419 chkhost((struct sockaddr *)&frominet, 420 ch_options); 421 } else 422 from_remote = 0; 423 doit(); 424 exit(0); 425 } 426 (void) close(s); 427 } 428 } 429 430 static void 431 reapchild(int signo __unused) 432 { 433 int status; 434 435 while (wait3(&status, WNOHANG, 0) > 0) 436 ; 437 } 438 439 static void 440 mcleanup(int signo) 441 { 442 /* 443 * XXX syslog(3) is not signal-safe. 444 */ 445 if (lflag) { 446 if (signo) 447 syslog(LOG_INFO, "exiting on signal %d", signo); 448 else 449 syslog(LOG_INFO, "exiting"); 450 } 451 unlink(_PATH_SOCKETNAME); 452 exit(0); 453 } 454 455 /* 456 * Stuff for handling job specifications 457 */ 458 char *user[MAXUSERS]; /* users to process */ 459 int users; /* # of users in user array */ 460 int requ[MAXREQUESTS]; /* job number of spool entries */ 461 int requests; /* # of spool requests */ 462 char *person; /* name of person doing lprm */ 463 464 /* buffer to hold the client's machine-name */ 465 static char frombuf[MAXHOSTNAMELEN]; 466 char cbuf[BUFSIZ]; /* command line buffer */ 467 const char *cmdnames[] = { 468 "null", 469 "printjob", 470 "recvjob", 471 "displayq short", 472 "displayq long", 473 "rmjob" 474 }; 475 476 static void 477 doit(void) 478 { 479 char *cp, *printer; 480 int n; 481 int status; 482 struct printer myprinter, *pp = &myprinter; 483 484 init_printer(&myprinter); 485 486 for (;;) { 487 cp = cbuf; 488 do { 489 if (cp >= &cbuf[sizeof(cbuf) - 1]) 490 fatal(0, "Command line too long"); 491 if ((n = read(STDOUT_FILENO, cp, 1)) != 1) { 492 if (n < 0) 493 fatal(0, "Lost connection"); 494 return; 495 } 496 } while (*cp++ != '\n'); 497 *--cp = '\0'; 498 cp = cbuf; 499 if (lflag) { 500 if (*cp >= '\1' && *cp <= '\5') 501 syslog(LOG_INFO, "%s requests %s %s", 502 from_host, cmdnames[(u_char)*cp], cp+1); 503 else 504 syslog(LOG_INFO, "bad request (%d) from %s", 505 *cp, from_host); 506 } 507 switch (*cp++) { 508 case CMD_CHECK_QUE: /* check the queue, print any jobs there */ 509 startprinting(cp); 510 break; 511 case CMD_TAKE_THIS: /* receive files to be queued */ 512 if (!from_remote) { 513 syslog(LOG_INFO, "illegal request (%d)", *cp); 514 exit(1); 515 } 516 recvjob(cp); 517 break; 518 case CMD_SHOWQ_SHORT: /* display the queue (short form) */ 519 case CMD_SHOWQ_LONG: /* display the queue (long form) */ 520 /* XXX - this all needs to be redone. */ 521 printer = cp; 522 while (*cp) { 523 if (*cp != ' ') { 524 cp++; 525 continue; 526 } 527 *cp++ = '\0'; 528 while (isspace(*cp)) 529 cp++; 530 if (*cp == '\0') 531 break; 532 if (isdigit(*cp)) { 533 if (requests >= MAXREQUESTS) 534 fatal(0, "Too many requests"); 535 requ[requests++] = atoi(cp); 536 } else { 537 if (users >= MAXUSERS) 538 fatal(0, "Too many users"); 539 user[users++] = cp; 540 } 541 } 542 status = getprintcap(printer, pp); 543 if (status < 0) 544 fatal(pp, "%s", pcaperr(status)); 545 displayq(pp, cbuf[0] == CMD_SHOWQ_LONG); 546 exit(0); 547 case CMD_RMJOB: /* remove a job from the queue */ 548 if (!from_remote) { 549 syslog(LOG_INFO, "illegal request (%d)", *cp); 550 exit(1); 551 } 552 printer = cp; 553 while (*cp && *cp != ' ') 554 cp++; 555 if (!*cp) 556 break; 557 *cp++ = '\0'; 558 person = cp; 559 while (*cp) { 560 if (*cp != ' ') { 561 cp++; 562 continue; 563 } 564 *cp++ = '\0'; 565 while (isspace(*cp)) 566 cp++; 567 if (*cp == '\0') 568 break; 569 if (isdigit(*cp)) { 570 if (requests >= MAXREQUESTS) 571 fatal(0, "Too many requests"); 572 requ[requests++] = atoi(cp); 573 } else { 574 if (users >= MAXUSERS) 575 fatal(0, "Too many users"); 576 user[users++] = cp; 577 } 578 } 579 rmjob(printer); 580 break; 581 } 582 fatal(0, "Illegal service request"); 583 } 584 } 585 586 /* 587 * Make a pass through the printcap database and start printing any 588 * files left from the last time the machine went down. 589 */ 590 static void 591 startup(void) 592 { 593 int pid, status, more; 594 struct printer myprinter, *pp = &myprinter; 595 596 more = firstprinter(pp, &status); 597 if (status) 598 goto errloop; 599 while (more) { 600 if (ckqueue(pp) <= 0) { 601 goto next; 602 } 603 if (lflag) 604 syslog(LOG_INFO, "lpd startup: work for %s", 605 pp->printer); 606 if ((pid = fork()) < 0) { 607 syslog(LOG_WARNING, "lpd startup: cannot fork for %s", 608 pp->printer); 609 mcleanup(0); 610 } 611 if (pid == 0) { 612 lastprinter(); 613 printjob(pp); 614 /* NOTREACHED */ 615 } 616 do { 617 next: 618 more = nextprinter(pp, &status); 619 errloop: 620 if (status) 621 syslog(LOG_WARNING, 622 "lpd startup: printcap entry for %s has errors, skipping", 623 pp->printer ? pp->printer : "<noname?>"); 624 } while (more && status); 625 } 626 } 627 628 /* 629 * Make sure there's some work to do before forking off a child 630 */ 631 static int 632 ckqueue(struct printer *pp) 633 { 634 register struct dirent *d; 635 DIR *dirp; 636 char *spooldir; 637 638 spooldir = pp->spool_dir; 639 if ((dirp = opendir(spooldir)) == NULL) 640 return (-1); 641 while ((d = readdir(dirp)) != NULL) { 642 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 643 continue; /* daemon control files only */ 644 closedir(dirp); 645 return (1); /* found something */ 646 } 647 closedir(dirp); 648 return (0); 649 } 650 651 #define DUMMY ":nobody::" 652 653 /* 654 * Check to see if the host connecting to this host has access to any 655 * lpd services on this host. 656 */ 657 static void 658 chkhost(struct sockaddr *f, int ch_opts) 659 { 660 struct addrinfo hints, *res, *r; 661 register FILE *hostf; 662 char hostbuf[NI_MAXHOST], ip[NI_MAXHOST]; 663 char serv[NI_MAXSERV]; 664 char *syserr, *usererr; 665 int error, errsav, fpass, good; 666 667 from_host = ".na."; 668 669 /* Need real hostname for temporary filenames */ 670 error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0, 671 NI_NAMEREQD); 672 if (error) { 673 errsav = error; 674 error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), 675 NULL, 0, NI_NUMERICHOST); 676 if (error) { 677 asprintf(&syserr, 678 "can not determine hostname for remote host (%d,%d)", 679 errsav, error); 680 asprintf(&usererr, 681 "Host name for your address is not known"); 682 fhosterr(ch_opts, syserr, usererr); 683 /* NOTREACHED */ 684 } 685 asprintf(&syserr, 686 "Host name for remote host (%s) not known (%d)", 687 hostbuf, errsav); 688 asprintf(&usererr, 689 "Host name for your address (%s) is not known", 690 hostbuf); 691 fhosterr(ch_opts, syserr, usererr); 692 /* NOTREACHED */ 693 } 694 695 strlcpy(frombuf, hostbuf, sizeof(frombuf)); 696 from_host = frombuf; 697 ch_opts |= LPD_ADDFROMLINE; 698 699 /* Need address in stringform for comparison (no DNS lookup here) */ 700 error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0, 701 NI_NUMERICHOST); 702 if (error) { 703 asprintf(&syserr, "Cannot print IP address (error %d)", 704 error); 705 asprintf(&usererr, "Cannot print IP address for your host"); 706 fhosterr(ch_opts, syserr, usererr); 707 /* NOTREACHED */ 708 } 709 from_ip = strdup(hostbuf); 710 711 /* Reject numeric addresses */ 712 memset(&hints, 0, sizeof(hints)); 713 hints.ai_family = family; 714 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 715 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 716 if (getaddrinfo(from_host, NULL, &hints, &res) == 0) { 717 freeaddrinfo(res); 718 /* This syslog message already includes from_host */ 719 ch_opts &= ~LPD_ADDFROMLINE; 720 asprintf(&syserr, "reverse lookup results in non-FQDN %s", 721 from_host); 722 /* same message to both syslog and remote user */ 723 fhosterr(ch_opts, syserr, syserr); 724 /* NOTREACHED */ 725 } 726 727 /* Check for spoof, ala rlogind */ 728 memset(&hints, 0, sizeof(hints)); 729 hints.ai_family = family; 730 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 731 error = getaddrinfo(from_host, NULL, &hints, &res); 732 if (error) { 733 asprintf(&syserr, "dns lookup for address %s failed: %s", 734 from_ip, gai_strerror(error)); 735 asprintf(&usererr, "hostname for your address (%s) unknown: %s", 736 from_ip, gai_strerror(error)); 737 fhosterr(ch_opts, syserr, usererr); 738 /* NOTREACHED */ 739 } 740 good = 0; 741 for (r = res; good == 0 && r; r = r->ai_next) { 742 error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip), 743 NULL, 0, NI_NUMERICHOST); 744 if (!error && !strcmp(from_ip, ip)) 745 good = 1; 746 } 747 if (res) 748 freeaddrinfo(res); 749 if (good == 0) { 750 asprintf(&syserr, "address for remote host (%s) not matched", 751 from_ip); 752 asprintf(&usererr, 753 "address for your hostname (%s) not matched", from_ip); 754 fhosterr(ch_opts, syserr, usererr); 755 /* NOTREACHED */ 756 } 757 758 fpass = 1; 759 hostf = fopen(_PATH_HOSTSEQUIV, "r"); 760 again: 761 if (hostf) { 762 if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) { 763 (void) fclose(hostf); 764 goto foundhost; 765 } 766 (void) fclose(hostf); 767 } 768 if (fpass == 1) { 769 fpass = 2; 770 hostf = fopen(_PATH_HOSTSLPD, "r"); 771 goto again; 772 } 773 /* This syslog message already includes from_host */ 774 ch_opts &= ~LPD_ADDFROMLINE; 775 asprintf(&syserr, "refused connection from %s, sip=%s", from_host, 776 from_ip); 777 asprintf(&usererr, 778 "Print-services are not available to your host (%s).", from_host); 779 fhosterr(ch_opts, syserr, usererr); 780 /* NOTREACHED */ 781 782 foundhost: 783 if (ch_opts & LPD_NOPORTCHK) 784 return; /* skip the reserved-port check */ 785 786 error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv), 787 NI_NUMERICSERV); 788 if (error) { 789 /* same message to both syslog and remote user */ 790 asprintf(&syserr, "malformed from-address (%d)", error); 791 fhosterr(ch_opts, syserr, syserr); 792 /* NOTREACHED */ 793 } 794 795 if (atoi(serv) >= IPPORT_RESERVED) { 796 /* same message to both syslog and remote user */ 797 asprintf(&syserr, "connected from invalid port (%s)", serv); 798 fhosterr(ch_opts, syserr, syserr); 799 /* NOTREACHED */ 800 } 801 } 802 803 /* 804 * Handle fatal errors in chkhost. The first message will optionally be 805 * sent to syslog, the second one is sent to the connecting host. 806 * 807 * The idea is that the syslog message is meant for an administrator of a 808 * print server (the host receiving connections), while the usermsg is meant 809 * for a remote user who may or may not be clueful, and may or may not be 810 * doing something nefarious. Some remote users (eg, MS-Windows...) may not 811 * even see whatever message is sent, which is why there's the option to 812 * start 'lpd' with the connection-errors also sent to syslog. 813 * 814 * Given that hostnames can theoretically be fairly long (well, over 250 815 * bytes), it would probably be helpful to have the 'from_host' field at 816 * the end of any error messages which include that info. 817 * 818 * These are Fatal host-connection errors, so this routine does not return. 819 */ 820 static void 821 fhosterr(int ch_opts, char *sysmsg, char *usermsg) 822 { 823 824 /* 825 * If lpd was started up to print connection errors, then write 826 * the syslog message before the user message. 827 * And for many of the syslog messages, it is helpful to first 828 * write the from_host (if it is known) as a separate syslog 829 * message, since the hostname may be so long. 830 */ 831 if (ch_opts & LPD_LOGCONNERR) { 832 if (ch_opts & LPD_ADDFROMLINE) { 833 syslog(LOG_WARNING, "for connection from %s:", from_host); 834 } 835 syslog(LOG_WARNING, "%s", sysmsg); 836 } 837 838 /* 839 * Now send the error message to the remote host which is trying 840 * to make the connection. 841 */ 842 printf("%s [@%s]: %s\n", progname, local_host, usermsg); 843 fflush(stdout); 844 845 /* 846 * Add a minimal delay before exiting (and disconnecting from the 847 * sending-host). This is just in case that machine responds by 848 * INSTANTLY retrying (and instantly re-failing...). This may also 849 * give the other side more time to read the error message. 850 */ 851 sleep(2); /* a paranoid throttling measure */ 852 exit(1); 853 } 854 855 /* setup server socket for specified address family */ 856 /* if af is PF_UNSPEC more than one socket may be returned */ 857 /* the returned list is dynamically allocated, so caller needs to free it */ 858 static int * 859 socksetup(int af, int debuglvl) 860 { 861 struct addrinfo hints, *res, *r; 862 int error, maxs, *s, *socks; 863 const int on = 1; 864 865 memset(&hints, 0, sizeof(hints)); 866 hints.ai_flags = AI_PASSIVE; 867 hints.ai_family = af; 868 hints.ai_socktype = SOCK_STREAM; 869 error = getaddrinfo(NULL, "printer", &hints, &res); 870 if (error) { 871 syslog(LOG_ERR, "%s", gai_strerror(error)); 872 mcleanup(0); 873 } 874 875 /* Count max number of sockets we may open */ 876 for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 877 ; 878 socks = malloc((maxs + 1) * sizeof(int)); 879 if (!socks) { 880 syslog(LOG_ERR, "couldn't allocate memory for sockets"); 881 mcleanup(0); 882 } 883 884 *socks = 0; /* num of sockets counter at start of array */ 885 s = socks + 1; 886 for (r = res; r; r = r->ai_next) { 887 *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 888 if (*s < 0) { 889 syslog(LOG_DEBUG, "socket(): %m"); 890 continue; 891 } 892 if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) 893 < 0) { 894 syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m"); 895 close(*s); 896 continue; 897 } 898 if (debuglvl) 899 if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl, 900 sizeof(debuglvl)) < 0) { 901 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 902 close(*s); 903 continue; 904 } 905 if (r->ai_family == AF_INET6) { 906 if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, 907 &on, sizeof(on)) < 0) { 908 syslog(LOG_ERR, 909 "setsockopt (IPV6_V6ONLY): %m"); 910 close(*s); 911 continue; 912 } 913 } 914 if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 915 syslog(LOG_DEBUG, "bind(): %m"); 916 close(*s); 917 continue; 918 } 919 (*socks)++; 920 s++; 921 } 922 923 if (res) 924 freeaddrinfo(res); 925 926 if (*socks == 0) { 927 syslog(LOG_ERR, "Couldn't bind to any socket"); 928 free(socks); 929 mcleanup(0); 930 } 931 return(socks); 932 } 933 934 static void 935 usage(void) 936 { 937 #ifdef INET6 938 fprintf(stderr, "usage: lpd [-cdlsFW46] [port#]\n"); 939 #else 940 fprintf(stderr, "usage: lpd [-cdlsFW] [port#]\n"); 941 #endif 942 exit(EX_USAGE); 943 } 944