1/* 2 * Copyright (c) 1983, 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif /* not lint */ 35 36#ifndef lint 37#if 0 38static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 39#endif 40#endif /* not lint */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/10/usr.sbin/syslogd/syslogd.c 333357 2018-05-08 05:11:06Z delphij $"); 44 45/* 46 * syslogd -- log system messages 47 * 48 * This program implements a system log. It takes a series of lines. 49 * Each line may have a priority, signified as "<n>" as 50 * the first characters of the line. If this is 51 * not present, a default priority is used. 52 * 53 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will 54 * cause it to reread its configuration file. 55 * 56 * Defined Constants: 57 * 58 * MAXLINE -- the maximum line length that can be handled. 59 * DEFUPRI -- the default priority for user messages 60 * DEFSPRI -- the default priority for kernel messages 61 * 62 * Author: Eric Allman 63 * extensive changes by Ralph Campbell 64 * more extensive changes by Eric Allman (again) 65 * Extension to log by program name as well as facility and priority 66 * by Peter da Silva. 67 * -u and -v by Harlan Stenn. 68 * Priority comparison code by Harlan Stenn. 69 */ 70 71/* Maximum number of characters in time of last occurrence */ 72#define MAXDATELEN 16 73#define MAXLINE 1024 /* maximum line length */ 74#define MAXSVLINE MAXLINE /* maximum saved line length */ 75#define DEFUPRI (LOG_USER|LOG_NOTICE) 76#define DEFSPRI (LOG_KERN|LOG_CRIT) 77#define TIMERINTVL 30 /* interval for checking flush, mark */ 78#define TTYMSGTIME 1 /* timeout passed to ttymsg */ 79#define RCVBUF_MINSIZE (80 * 1024) /* minimum size of dgram rcv buffer */ 80 81#include <sys/param.h> 82#include <sys/ioctl.h> 83#include <sys/mman.h> 84#include <sys/queue.h> 85#include <sys/resource.h> 86#include <sys/socket.h> 87#include <sys/stat.h> 88#include <sys/syslimits.h> 89#include <sys/time.h> 90#include <sys/uio.h> 91#include <sys/un.h> 92#include <sys/wait.h> 93#include <sys/types.h> 94 95#include <netinet/in.h> 96#include <netdb.h> 97#include <arpa/inet.h> 98 99#include <ctype.h> 100#include <dirent.h> 101#include <err.h> 102#include <errno.h> 103#include <fcntl.h> 104#include <libutil.h> 105#include <limits.h> 106#include <paths.h> 107#include <signal.h> 108#include <stdio.h> 109#include <stdlib.h> 110#include <string.h> 111#include <sysexits.h> 112#include <unistd.h> 113#include <utmpx.h> 114 115#include "pathnames.h" 116#include "ttymsg.h" 117 118#define SYSLOG_NAMES 119#include <sys/syslog.h> 120 121const char *ConfFile = _PATH_LOGCONF; 122const char *PidFile = _PATH_LOGPID; 123const char ctty[] = _PATH_CONSOLE; 124static const char include_str[] = "include"; 125static const char include_ext[] = ".conf"; 126 127#define dprintf if (Debug) printf 128 129#define MAXUNAMES 20 /* maximum number of user names */ 130 131/* 132 * Unix sockets. 133 * We have two default sockets, one with 666 permissions, 134 * and one for privileged programs. 135 */ 136struct funix { 137 int s; 138 const char *name; 139 mode_t mode; 140 STAILQ_ENTRY(funix) next; 141}; 142struct funix funix_secure = { -1, _PATH_LOG_PRIV, S_IRUSR | S_IWUSR, 143 { NULL } }; 144struct funix funix_default = { -1, _PATH_LOG, DEFFILEMODE, 145 { &funix_secure } }; 146 147STAILQ_HEAD(, funix) funixes = { &funix_default, 148 &(funix_secure.next.stqe_next) }; 149 150/* 151 * Flags to logmsg(). 152 */ 153 154#define IGN_CONS 0x001 /* don't print on console */ 155#define SYNC_FILE 0x002 /* do fsync on file after printing */ 156#define ADDDATE 0x004 /* add a date to the message */ 157#define MARK 0x008 /* this message is a mark */ 158#define ISKERNEL 0x010 /* kernel generated message */ 159 160/* 161 * This structure represents the files that will have log 162 * copies printed. 163 * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY 164 * or if f_type is F_PIPE and f_pid > 0. 165 */ 166 167struct filed { 168 struct filed *f_next; /* next in linked list */ 169 short f_type; /* entry type, see below */ 170 short f_file; /* file descriptor */ 171 time_t f_time; /* time this was last written */ 172 char *f_host; /* host from which to recd. */ 173 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 174 u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ 175#define PRI_LT 0x1 176#define PRI_EQ 0x2 177#define PRI_GT 0x4 178 char *f_program; /* program this applies to */ 179 union { 180 char f_uname[MAXUNAMES][MAXLOGNAME]; 181 struct { 182 char f_hname[MAXHOSTNAMELEN]; 183 struct addrinfo *f_addr; 184 185 } f_forw; /* forwarding address */ 186 char f_fname[MAXPATHLEN]; 187 struct { 188 char f_pname[MAXPATHLEN]; 189 pid_t f_pid; 190 } f_pipe; 191 } f_un; 192 char f_prevline[MAXSVLINE]; /* last message logged */ 193 char f_lasttime[MAXDATELEN]; /* time of last occurrence */ 194 char f_prevhost[MAXHOSTNAMELEN]; /* host from which recd. */ 195 int f_prevpri; /* pri of f_prevline */ 196 int f_prevlen; /* length of f_prevline */ 197 int f_prevcount; /* repetition cnt of prevline */ 198 u_int f_repeatcount; /* number of "repeated" msgs */ 199 int f_flags; /* file-specific flags */ 200#define FFLAG_SYNC 0x01 201#define FFLAG_NEEDSYNC 0x02 202}; 203 204/* 205 * Queue of about-to-be dead processes we should watch out for. 206 */ 207 208TAILQ_HEAD(stailhead, deadq_entry) deadq_head; 209struct stailhead *deadq_headp; 210 211struct deadq_entry { 212 pid_t dq_pid; 213 int dq_timeout; 214 TAILQ_ENTRY(deadq_entry) dq_entries; 215}; 216 217/* 218 * The timeout to apply to processes waiting on the dead queue. Unit 219 * of measure is `mark intervals', i.e. 20 minutes by default. 220 * Processes on the dead queue will be terminated after that time. 221 */ 222 223#define DQ_TIMO_INIT 2 224 225typedef struct deadq_entry *dq_t; 226 227 228/* 229 * Struct to hold records of network addresses that are allowed to log 230 * to us. 231 */ 232struct allowedpeer { 233 int isnumeric; 234 u_short port; 235 union { 236 struct { 237 struct sockaddr_storage addr; 238 struct sockaddr_storage mask; 239 } numeric; 240 char *name; 241 } u; 242#define a_addr u.numeric.addr 243#define a_mask u.numeric.mask 244#define a_name u.name 245}; 246 247 248/* 249 * Intervals at which we flush out "message repeated" messages, 250 * in seconds after previous message is logged. After each flush, 251 * we move to the next interval until we reach the largest. 252 */ 253int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 254#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 255#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 256#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 257 (f)->f_repeatcount = MAXREPEAT; \ 258 } 259 260/* values for f_type */ 261#define F_UNUSED 0 /* unused entry */ 262#define F_FILE 1 /* regular file */ 263#define F_TTY 2 /* terminal */ 264#define F_CONSOLE 3 /* console terminal */ 265#define F_FORW 4 /* remote machine */ 266#define F_USERS 5 /* list of users */ 267#define F_WALL 6 /* everyone logged on */ 268#define F_PIPE 7 /* pipe to program */ 269 270const char *TypeNames[8] = { 271 "UNUSED", "FILE", "TTY", "CONSOLE", 272 "FORW", "USERS", "WALL", "PIPE" 273}; 274 275static struct filed *Files; /* Log files that we write to */ 276static struct filed consfile; /* Console */ 277 278static int Debug; /* debug flag */ 279static int Foreground = 0; /* Run in foreground, instead of daemonizing */ 280static int resolve = 1; /* resolve hostname */ 281static char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ 282static const char *LocalDomain; /* our local domain name */ 283static int *finet; /* Internet datagram socket */ 284static int fklog = -1; /* /dev/klog */ 285static int Initialized; /* set when we have initialized ourselves */ 286static int MarkInterval = 20 * 60; /* interval between marks in seconds */ 287static int MarkSeq; /* mark sequence number */ 288static int NoBind; /* don't bind() as suggested by RFC 3164 */ 289static int SecureMode; /* when true, receive only unix domain socks */ 290#ifdef INET6 291static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 292#else 293static int family = PF_INET; /* protocol family (IPv4 only) */ 294#endif 295static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */ 296static int send_to_all; /* send message to all IPv4/IPv6 addresses */ 297static int use_bootfile; /* log entire bootfile for every kern msg */ 298static int no_compress; /* don't compress messages (1=pipes, 2=all) */ 299static int logflags = O_WRONLY|O_APPEND; /* flags used to open log files */ 300 301static char bootfile[MAXLINE+1]; /* booted kernel file */ 302 303struct allowedpeer *AllowedPeers; /* List of allowed peers */ 304static int NumAllowed; /* Number of entries in AllowedPeers */ 305static int RemoteAddDate; /* Always set the date on remote messages */ 306 307static int UniquePriority; /* Only log specified priority? */ 308static int LogFacPri; /* Put facility and priority in log message: */ 309 /* 0=no, 1=numeric, 2=names */ 310static int KeepKernFac; /* Keep remotely logged kernel facility */ 311static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */ 312static struct pidfh *pfh; 313 314volatile sig_atomic_t MarkSet, WantDie; 315 316static int allowaddr(char *); 317static void cfline(const char *, struct filed *, 318 const char *, const char *); 319static const char *cvthname(struct sockaddr *); 320static void deadq_enter(pid_t, const char *); 321static int deadq_remove(pid_t); 322static int decode(const char *, const CODE *); 323static void die(int); 324static void dodie(int); 325static void dofsync(void); 326static void domark(int); 327static void fprintlog(struct filed *, int, const char *); 328static int *socksetup(int, char *); 329static void init(int); 330static void logerror(const char *); 331static void logmsg(int, const char *, const char *, int); 332static void log_deadchild(pid_t, int, const char *); 333static void markit(void); 334static int skip_message(const char *, const char *, int); 335static void printline(const char *, char *, int); 336static void printsys(char *); 337static int p_open(const char *, pid_t *); 338static void readklog(void); 339static void reapchild(int); 340static void usage(void); 341static int validate(struct sockaddr *, const char *); 342static void unmapped(struct sockaddr *); 343static void wallmsg(struct filed *, struct iovec *, const int iovlen); 344static int waitdaemon(int, int, int); 345static void timedout(int); 346static void increase_rcvbuf(int); 347 348static void 349close_filed(struct filed *f) 350{ 351 352 if (f == NULL || f->f_file == -1) 353 return; 354 355 switch (f->f_type) { 356 case F_FORW: 357 if (f->f_un.f_forw.f_addr) { 358 freeaddrinfo(f->f_un.f_forw.f_addr); 359 f->f_un.f_forw.f_addr = NULL; 360 } 361 /*FALLTHROUGH*/ 362 363 case F_FILE: 364 case F_TTY: 365 case F_CONSOLE: 366 f->f_type = F_UNUSED; 367 break; 368 case F_PIPE: 369 f->f_un.f_pipe.f_pid = 0; 370 break; 371 } 372 (void)close(f->f_file); 373 f->f_file = -1; 374} 375 376int 377main(int argc, char *argv[]) 378{ 379 int ch, i, fdsrmax = 0, l; 380 struct sockaddr_un sunx, fromunix; 381 struct sockaddr_storage frominet; 382 fd_set *fdsr = NULL; 383 char line[MAXLINE + 1]; 384 char *bindhostname; 385 const char *hname; 386 struct timeval tv, *tvp; 387 struct sigaction sact; 388 struct funix *fx, *fx1; 389 sigset_t mask; 390 pid_t ppid = 1, spid; 391 socklen_t len; 392 393 if (madvise(NULL, 0, MADV_PROTECT) != 0) 394 dprintf("madvise() failed: %s\n", strerror(errno)); 395 396 bindhostname = NULL; 397 while ((ch = getopt(argc, argv, "468Aa:b:cCdf:Fkl:m:nNop:P:sS:Tuv")) 398 != -1) 399 switch (ch) { 400 case '4': 401 family = PF_INET; 402 break; 403#ifdef INET6 404 case '6': 405 family = PF_INET6; 406 break; 407#endif 408 case '8': 409 mask_C1 = 0; 410 break; 411 case 'A': 412 send_to_all++; 413 break; 414 case 'a': /* allow specific network addresses only */ 415 if (allowaddr(optarg) == -1) 416 usage(); 417 break; 418 case 'b': 419 bindhostname = optarg; 420 break; 421 case 'c': 422 no_compress++; 423 break; 424 case 'C': 425 logflags |= O_CREAT; 426 break; 427 case 'd': /* debug */ 428 Debug++; 429 break; 430 case 'f': /* configuration file */ 431 ConfFile = optarg; 432 break; 433 case 'F': /* run in foreground instead of daemon */ 434 Foreground++; 435 break; 436 case 'k': /* keep remote kern fac */ 437 KeepKernFac = 1; 438 break; 439 case 'l': 440 { 441 long perml; 442 mode_t mode; 443 char *name, *ep; 444 445 if (optarg[0] == '/') { 446 mode = DEFFILEMODE; 447 name = optarg; 448 } else if ((name = strchr(optarg, ':')) != NULL) { 449 *name++ = '\0'; 450 if (name[0] != '/') 451 errx(1, "socket name must be absolute " 452 "path"); 453 if (isdigit(*optarg)) { 454 perml = strtol(optarg, &ep, 8); 455 if (*ep || perml < 0 || 456 perml & ~(S_IRWXU|S_IRWXG|S_IRWXO)) 457 errx(1, "invalid mode %s, exiting", 458 optarg); 459 mode = (mode_t )perml; 460 } else 461 errx(1, "invalid mode %s, exiting", 462 optarg); 463 } else /* doesn't begin with '/', and no ':' */ 464 errx(1, "can't parse path %s", optarg); 465 466 if (strlen(name) >= sizeof(sunx.sun_path)) 467 errx(1, "%s path too long, exiting", name); 468 if ((fx = malloc(sizeof(struct funix))) == NULL) 469 errx(1, "malloc failed"); 470 fx->s = -1; 471 fx->name = name; 472 fx->mode = mode; 473 STAILQ_INSERT_TAIL(&funixes, fx, next); 474 break; 475 } 476 case 'm': /* mark interval */ 477 MarkInterval = atoi(optarg) * 60; 478 break; 479 case 'N': 480 NoBind = 1; 481 SecureMode = 1; 482 break; 483 case 'n': 484 resolve = 0; 485 break; 486 case 'o': 487 use_bootfile = 1; 488 break; 489 case 'p': /* path */ 490 if (strlen(optarg) >= sizeof(sunx.sun_path)) 491 errx(1, "%s path too long, exiting", optarg); 492 funix_default.name = optarg; 493 break; 494 case 'P': /* path for alt. PID */ 495 PidFile = optarg; 496 break; 497 case 's': /* no network mode */ 498 SecureMode++; 499 break; 500 case 'S': /* path for privileged originator */ 501 if (strlen(optarg) >= sizeof(sunx.sun_path)) 502 errx(1, "%s path too long, exiting", optarg); 503 funix_secure.name = optarg; 504 break; 505 case 'T': 506 RemoteAddDate = 1; 507 break; 508 case 'u': /* only log specified priority */ 509 UniquePriority++; 510 break; 511 case 'v': /* log facility and priority */ 512 LogFacPri++; 513 break; 514 default: 515 usage(); 516 } 517 if ((argc -= optind) != 0) 518 usage(); 519 520 pfh = pidfile_open(PidFile, 0600, &spid); 521 if (pfh == NULL) { 522 if (errno == EEXIST) 523 errx(1, "syslogd already running, pid: %d", spid); 524 warn("cannot open pid file"); 525 } 526 527 if ((!Foreground) && (!Debug)) { 528 ppid = waitdaemon(0, 0, 30); 529 if (ppid < 0) { 530 warn("could not become daemon"); 531 pidfile_remove(pfh); 532 exit(1); 533 } 534 } else if (Debug) { 535 setlinebuf(stdout); 536 } 537 538 if (NumAllowed) 539 endservent(); 540 541 consfile.f_type = F_CONSOLE; 542 (void)strlcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1, 543 sizeof(consfile.f_un.f_fname)); 544 (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); 545 (void)signal(SIGTERM, dodie); 546 (void)signal(SIGINT, Debug ? dodie : SIG_IGN); 547 (void)signal(SIGQUIT, Debug ? dodie : SIG_IGN); 548 /* 549 * We don't want the SIGCHLD and SIGHUP handlers to interfere 550 * with each other; they are likely candidates for being called 551 * simultaneously (SIGHUP closes pipe descriptor, process dies, 552 * SIGCHLD happens). 553 */ 554 sigemptyset(&mask); 555 sigaddset(&mask, SIGHUP); 556 sact.sa_handler = reapchild; 557 sact.sa_mask = mask; 558 sact.sa_flags = SA_RESTART; 559 (void)sigaction(SIGCHLD, &sact, NULL); 560 (void)signal(SIGALRM, domark); 561 (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ 562 (void)alarm(TIMERINTVL); 563 564 TAILQ_INIT(&deadq_head); 565 566#ifndef SUN_LEN 567#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 568#endif 569 STAILQ_FOREACH_SAFE(fx, &funixes, next, fx1) { 570 (void)unlink(fx->name); 571 memset(&sunx, 0, sizeof(sunx)); 572 sunx.sun_family = AF_LOCAL; 573 (void)strlcpy(sunx.sun_path, fx->name, sizeof(sunx.sun_path)); 574 fx->s = socket(PF_LOCAL, SOCK_DGRAM, 0); 575 if (fx->s < 0 || 576 bind(fx->s, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || 577 chmod(fx->name, fx->mode) < 0) { 578 (void)snprintf(line, sizeof line, 579 "cannot create %s", fx->name); 580 logerror(line); 581 dprintf("cannot create %s (%d)\n", fx->name, errno); 582 if (fx == &funix_default || fx == &funix_secure) 583 die(0); 584 else { 585 STAILQ_REMOVE(&funixes, fx, funix, next); 586 continue; 587 } 588 } 589 increase_rcvbuf(fx->s); 590 } 591 if (SecureMode <= 1) 592 finet = socksetup(family, bindhostname); 593 594 if (finet) { 595 if (SecureMode) { 596 for (i = 0; i < *finet; i++) { 597 if (shutdown(finet[i+1], SHUT_RD) < 0 && 598 errno != ENOTCONN) { 599 logerror("shutdown"); 600 if (!Debug) 601 die(0); 602 } 603 } 604 } else { 605 dprintf("listening on inet and/or inet6 socket\n"); 606 } 607 dprintf("sending on inet and/or inet6 socket\n"); 608 } 609 610 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) 611 if (fcntl(fklog, F_SETFL, O_NONBLOCK) < 0) 612 fklog = -1; 613 if (fklog < 0) 614 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 615 616 /* tuck my process id away */ 617 pidfile_write(pfh); 618 619 dprintf("off & running....\n"); 620 621 init(0); 622 /* prevent SIGHUP and SIGCHLD handlers from running in parallel */ 623 sigemptyset(&mask); 624 sigaddset(&mask, SIGCHLD); 625 sact.sa_handler = init; 626 sact.sa_mask = mask; 627 sact.sa_flags = SA_RESTART; 628 (void)sigaction(SIGHUP, &sact, NULL); 629 630 tvp = &tv; 631 tv.tv_sec = tv.tv_usec = 0; 632 633 if (fklog != -1 && fklog > fdsrmax) 634 fdsrmax = fklog; 635 if (finet && !SecureMode) { 636 for (i = 0; i < *finet; i++) { 637 if (finet[i+1] != -1 && finet[i+1] > fdsrmax) 638 fdsrmax = finet[i+1]; 639 } 640 } 641 STAILQ_FOREACH(fx, &funixes, next) 642 if (fx->s > fdsrmax) 643 fdsrmax = fx->s; 644 645 fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS), 646 sizeof(*fdsr)); 647 if (fdsr == NULL) 648 errx(1, "calloc fd_set"); 649 650 for (;;) { 651 if (MarkSet) 652 markit(); 653 if (WantDie) 654 die(WantDie); 655 656 bzero(fdsr, howmany(fdsrmax+1, NFDBITS) * 657 sizeof(*fdsr)); 658 659 if (fklog != -1) 660 FD_SET(fklog, fdsr); 661 if (finet && !SecureMode) { 662 for (i = 0; i < *finet; i++) { 663 if (finet[i+1] != -1) 664 FD_SET(finet[i+1], fdsr); 665 } 666 } 667 STAILQ_FOREACH(fx, &funixes, next) 668 FD_SET(fx->s, fdsr); 669 670 i = select(fdsrmax+1, fdsr, NULL, NULL, 671 needdofsync ? &tv : tvp); 672 switch (i) { 673 case 0: 674 dofsync(); 675 needdofsync = 0; 676 if (tvp) { 677 tvp = NULL; 678 if (ppid != 1) 679 kill(ppid, SIGALRM); 680 } 681 continue; 682 case -1: 683 if (errno != EINTR) 684 logerror("select"); 685 continue; 686 } 687 if (fklog != -1 && FD_ISSET(fklog, fdsr)) 688 readklog(); 689 if (finet && !SecureMode) { 690 for (i = 0; i < *finet; i++) { 691 if (FD_ISSET(finet[i+1], fdsr)) { 692 len = sizeof(frominet); 693 l = recvfrom(finet[i+1], line, MAXLINE, 694 0, (struct sockaddr *)&frominet, 695 &len); 696 if (l > 0) { 697 line[l] = '\0'; 698 hname = cvthname((struct sockaddr *)&frominet); 699 unmapped((struct sockaddr *)&frominet); 700 if (validate((struct sockaddr *)&frominet, hname)) 701 printline(hname, line, RemoteAddDate ? ADDDATE : 0); 702 } else if (l < 0 && errno != EINTR) 703 logerror("recvfrom inet"); 704 } 705 } 706 } 707 STAILQ_FOREACH(fx, &funixes, next) { 708 if (FD_ISSET(fx->s, fdsr)) { 709 len = sizeof(fromunix); 710 l = recvfrom(fx->s, line, MAXLINE, 0, 711 (struct sockaddr *)&fromunix, &len); 712 if (l > 0) { 713 line[l] = '\0'; 714 printline(LocalHostName, line, 0); 715 } else if (l < 0 && errno != EINTR) 716 logerror("recvfrom unix"); 717 } 718 } 719 } 720 if (fdsr) 721 free(fdsr); 722} 723 724static void 725unmapped(struct sockaddr *sa) 726{ 727 struct sockaddr_in6 *sin6; 728 struct sockaddr_in sin4; 729 730 if (sa->sa_family != AF_INET6) 731 return; 732 if (sa->sa_len != sizeof(struct sockaddr_in6) || 733 sizeof(sin4) > sa->sa_len) 734 return; 735 sin6 = (struct sockaddr_in6 *)sa; 736 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 737 return; 738 739 memset(&sin4, 0, sizeof(sin4)); 740 sin4.sin_family = AF_INET; 741 sin4.sin_len = sizeof(struct sockaddr_in); 742 memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12], 743 sizeof(sin4.sin_addr)); 744 sin4.sin_port = sin6->sin6_port; 745 746 memcpy(sa, &sin4, sin4.sin_len); 747} 748 749static void 750usage(void) 751{ 752 753 fprintf(stderr, "%s\n%s\n%s\n%s\n", 754 "usage: syslogd [-468ACcdFknosTuv] [-a allowed_peer]", 755 " [-b bind_address] [-f config_file]", 756 " [-l [mode:]path] [-m mark_interval]", 757 " [-P pid_file] [-p log_socket]"); 758 exit(1); 759} 760 761/* 762 * Take a raw input line, decode the message, and print the message 763 * on the appropriate log files. 764 */ 765static void 766printline(const char *hname, char *msg, int flags) 767{ 768 char *p, *q; 769 long n; 770 int c, pri; 771 char line[MAXLINE + 1]; 772 773 /* test for special codes */ 774 p = msg; 775 pri = DEFUPRI; 776 if (*p == '<') { 777 errno = 0; 778 n = strtol(p + 1, &q, 10); 779 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 780 p = q + 1; 781 pri = n; 782 } 783 } 784 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 785 pri = DEFUPRI; 786 787 /* 788 * Don't allow users to log kernel messages. 789 * NOTE: since LOG_KERN == 0 this will also match 790 * messages with no facility specified. 791 */ 792 if ((pri & LOG_FACMASK) == LOG_KERN && !KeepKernFac) 793 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 794 795 q = line; 796 797 while ((c = (unsigned char)*p++) != '\0' && 798 q < &line[sizeof(line) - 4]) { 799 if (mask_C1 && (c & 0x80) && c < 0xA0) { 800 c &= 0x7F; 801 *q++ = 'M'; 802 *q++ = '-'; 803 } 804 if (isascii(c) && iscntrl(c)) { 805 if (c == '\n') { 806 *q++ = ' '; 807 } else if (c == '\t') { 808 *q++ = '\t'; 809 } else { 810 *q++ = '^'; 811 *q++ = c ^ 0100; 812 } 813 } else { 814 *q++ = c; 815 } 816 } 817 *q = '\0'; 818 819 logmsg(pri, line, hname, flags); 820} 821 822/* 823 * Read /dev/klog while data are available, split into lines. 824 */ 825static void 826readklog(void) 827{ 828 char *p, *q, line[MAXLINE + 1]; 829 int len, i; 830 831 len = 0; 832 for (;;) { 833 i = read(fklog, line + len, MAXLINE - 1 - len); 834 if (i > 0) { 835 line[i + len] = '\0'; 836 } else { 837 if (i < 0 && errno != EINTR && errno != EAGAIN) { 838 logerror("klog"); 839 fklog = -1; 840 } 841 break; 842 } 843 844 for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { 845 *q = '\0'; 846 printsys(p); 847 } 848 len = strlen(p); 849 if (len >= MAXLINE - 1) { 850 printsys(p); 851 len = 0; 852 } 853 if (len > 0) 854 memmove(line, p, len + 1); 855 } 856 if (len > 0) 857 printsys(line); 858} 859 860/* 861 * Take a raw input line from /dev/klog, format similar to syslog(). 862 */ 863static void 864printsys(char *msg) 865{ 866 char *p, *q; 867 long n; 868 int flags, isprintf, pri; 869 870 flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */ 871 p = msg; 872 pri = DEFSPRI; 873 isprintf = 1; 874 if (*p == '<') { 875 errno = 0; 876 n = strtol(p + 1, &q, 10); 877 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 878 p = q + 1; 879 pri = n; 880 isprintf = 0; 881 } 882 } 883 /* 884 * Kernel printf's and LOG_CONSOLE messages have been displayed 885 * on the console already. 886 */ 887 if (isprintf || (pri & LOG_FACMASK) == LOG_CONSOLE) 888 flags |= IGN_CONS; 889 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 890 pri = DEFSPRI; 891 logmsg(pri, p, LocalHostName, flags); 892} 893 894static time_t now; 895 896/* 897 * Match a program or host name against a specification. 898 * Return a non-0 value if the message must be ignored 899 * based on the specification. 900 */ 901static int 902skip_message(const char *name, const char *spec, int checkcase) 903{ 904 const char *s; 905 char prev, next; 906 int exclude = 0; 907 /* Behaviour on explicit match */ 908 909 if (spec == NULL) 910 return 0; 911 switch (*spec) { 912 case '-': 913 exclude = 1; 914 /*FALLTHROUGH*/ 915 case '+': 916 spec++; 917 break; 918 default: 919 break; 920 } 921 if (checkcase) 922 s = strstr (spec, name); 923 else 924 s = strcasestr (spec, name); 925 926 if (s != NULL) { 927 prev = (s == spec ? ',' : *(s - 1)); 928 next = *(s + strlen (name)); 929 930 if (prev == ',' && (next == '\0' || next == ',')) 931 /* Explicit match: skip iff the spec is an 932 exclusive one. */ 933 return exclude; 934 } 935 936 /* No explicit match for this name: skip the message iff 937 the spec is an inclusive one. */ 938 return !exclude; 939} 940 941/* 942 * Log a message to the appropriate log files, users, etc. based on 943 * the priority. 944 */ 945static void 946logmsg(int pri, const char *msg, const char *from, int flags) 947{ 948 struct filed *f; 949 int i, fac, msglen, omask, prilev; 950 const char *timestamp; 951 char prog[NAME_MAX+1]; 952 char buf[MAXLINE+1]; 953 954 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 955 pri, flags, from, msg); 956 957 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 958 959 /* 960 * Check to see if msg looks non-standard. 961 */ 962 msglen = strlen(msg); 963 if (msglen < MAXDATELEN || msg[3] != ' ' || msg[6] != ' ' || 964 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 965 flags |= ADDDATE; 966 967 (void)time(&now); 968 if (flags & ADDDATE) { 969 timestamp = ctime(&now) + 4; 970 } else { 971 timestamp = msg; 972 msg += MAXDATELEN; 973 msglen -= MAXDATELEN; 974 } 975 976 /* skip leading blanks */ 977 while (isspace(*msg)) { 978 msg++; 979 msglen--; 980 } 981 982 /* extract facility and priority level */ 983 if (flags & MARK) 984 fac = LOG_NFACILITIES; 985 else 986 fac = LOG_FAC(pri); 987 988 /* Check maximum facility number. */ 989 if (fac > LOG_NFACILITIES) { 990 (void)sigsetmask(omask); 991 return; 992 } 993 994 prilev = LOG_PRI(pri); 995 996 /* extract program name */ 997 for (i = 0; i < NAME_MAX; i++) { 998 if (!isprint(msg[i]) || msg[i] == ':' || msg[i] == '[' || 999 msg[i] == '/' || isspace(msg[i])) 1000 break; 1001 prog[i] = msg[i]; 1002 } 1003 prog[i] = 0; 1004 1005 /* add kernel prefix for kernel messages */ 1006 if (flags & ISKERNEL) { 1007 snprintf(buf, sizeof(buf), "%s: %s", 1008 use_bootfile ? bootfile : "kernel", msg); 1009 msg = buf; 1010 msglen = strlen(buf); 1011 } 1012 1013 /* log the message to the particular outputs */ 1014 if (!Initialized) { 1015 f = &consfile; 1016 /* 1017 * Open in non-blocking mode to avoid hangs during open 1018 * and close(waiting for the port to drain). 1019 */ 1020 f->f_file = open(ctty, O_WRONLY | O_NONBLOCK, 0); 1021 1022 if (f->f_file >= 0) { 1023 (void)strlcpy(f->f_lasttime, timestamp, 1024 sizeof(f->f_lasttime)); 1025 fprintlog(f, flags, msg); 1026 close(f->f_file); 1027 f->f_file = -1; 1028 } 1029 (void)sigsetmask(omask); 1030 return; 1031 } 1032 for (f = Files; f; f = f->f_next) { 1033 /* skip messages that are incorrect priority */ 1034 if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev)) 1035 ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev)) 1036 ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev)) 1037 ) 1038 || f->f_pmask[fac] == INTERNAL_NOPRI) 1039 continue; 1040 1041 /* skip messages with the incorrect hostname */ 1042 if (skip_message(from, f->f_host, 0)) 1043 continue; 1044 1045 /* skip messages with the incorrect program name */ 1046 if (skip_message(prog, f->f_program, 1)) 1047 continue; 1048 1049 /* skip message to console if it has already been printed */ 1050 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 1051 continue; 1052 1053 /* don't output marks to recently written files */ 1054 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 1055 continue; 1056 1057 /* 1058 * suppress duplicate lines to this file 1059 */ 1060 if (no_compress - (f->f_type != F_PIPE) < 1 && 1061 (flags & MARK) == 0 && msglen == f->f_prevlen && 1062 !strcmp(msg, f->f_prevline) && 1063 !strcasecmp(from, f->f_prevhost)) { 1064 (void)strlcpy(f->f_lasttime, timestamp, 1065 sizeof(f->f_lasttime)); 1066 f->f_prevcount++; 1067 dprintf("msg repeated %d times, %ld sec of %d\n", 1068 f->f_prevcount, (long)(now - f->f_time), 1069 repeatinterval[f->f_repeatcount]); 1070 /* 1071 * If domark would have logged this by now, 1072 * flush it now (so we don't hold isolated messages), 1073 * but back off so we'll flush less often 1074 * in the future. 1075 */ 1076 if (now > REPEATTIME(f)) { 1077 fprintlog(f, flags, (char *)NULL); 1078 BACKOFF(f); 1079 } 1080 } else { 1081 /* new line, save it */ 1082 if (f->f_prevcount) 1083 fprintlog(f, 0, (char *)NULL); 1084 f->f_repeatcount = 0; 1085 f->f_prevpri = pri; 1086 (void)strlcpy(f->f_lasttime, timestamp, 1087 sizeof(f->f_lasttime)); 1088 (void)strlcpy(f->f_prevhost, from, 1089 sizeof(f->f_prevhost)); 1090 if (msglen < MAXSVLINE) { 1091 f->f_prevlen = msglen; 1092 (void)strlcpy(f->f_prevline, msg, sizeof(f->f_prevline)); 1093 fprintlog(f, flags, (char *)NULL); 1094 } else { 1095 f->f_prevline[0] = 0; 1096 f->f_prevlen = 0; 1097 fprintlog(f, flags, msg); 1098 } 1099 } 1100 } 1101 (void)sigsetmask(omask); 1102} 1103 1104static void 1105dofsync(void) 1106{ 1107 struct filed *f; 1108 1109 for (f = Files; f; f = f->f_next) { 1110 if ((f->f_type == F_FILE) && 1111 (f->f_flags & FFLAG_NEEDSYNC)) { 1112 f->f_flags &= ~FFLAG_NEEDSYNC; 1113 (void)fsync(f->f_file); 1114 } 1115 } 1116} 1117 1118#define IOV_SIZE 7 1119static void 1120fprintlog(struct filed *f, int flags, const char *msg) 1121{ 1122 struct iovec iov[IOV_SIZE]; 1123 struct iovec *v; 1124 struct addrinfo *r; 1125 int i, l, lsent = 0; 1126 char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg = NULL; 1127 char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n"; 1128 const char *msgret; 1129 1130 v = iov; 1131 if (f->f_type == F_WALL) { 1132 v->iov_base = greetings; 1133 /* The time displayed is not synchornized with the other log 1134 * destinations (like messages). Following fragment was using 1135 * ctime(&now), which was updating the time every 30 sec. 1136 * With f_lasttime, time is synchronized correctly. 1137 */ 1138 v->iov_len = snprintf(greetings, sizeof greetings, 1139 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 1140 f->f_prevhost, f->f_lasttime); 1141 if (v->iov_len >= sizeof greetings) 1142 v->iov_len = sizeof greetings - 1; 1143 v++; 1144 v->iov_base = nul; 1145 v->iov_len = 0; 1146 v++; 1147 } else { 1148 v->iov_base = f->f_lasttime; 1149 v->iov_len = strlen(f->f_lasttime); 1150 v++; 1151 v->iov_base = space; 1152 v->iov_len = 1; 1153 v++; 1154 } 1155 1156 if (LogFacPri) { 1157 static char fp_buf[30]; /* Hollow laugh */ 1158 int fac = f->f_prevpri & LOG_FACMASK; 1159 int pri = LOG_PRI(f->f_prevpri); 1160 const char *f_s = NULL; 1161 char f_n[5]; /* Hollow laugh */ 1162 const char *p_s = NULL; 1163 char p_n[5]; /* Hollow laugh */ 1164 1165 if (LogFacPri > 1) { 1166 const CODE *c; 1167 1168 for (c = facilitynames; c->c_name; c++) { 1169 if (c->c_val == fac) { 1170 f_s = c->c_name; 1171 break; 1172 } 1173 } 1174 for (c = prioritynames; c->c_name; c++) { 1175 if (c->c_val == pri) { 1176 p_s = c->c_name; 1177 break; 1178 } 1179 } 1180 } 1181 if (!f_s) { 1182 snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac)); 1183 f_s = f_n; 1184 } 1185 if (!p_s) { 1186 snprintf(p_n, sizeof p_n, "%d", pri); 1187 p_s = p_n; 1188 } 1189 snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s); 1190 v->iov_base = fp_buf; 1191 v->iov_len = strlen(fp_buf); 1192 } else { 1193 v->iov_base = nul; 1194 v->iov_len = 0; 1195 } 1196 v++; 1197 1198 v->iov_base = f->f_prevhost; 1199 v->iov_len = strlen(v->iov_base); 1200 v++; 1201 v->iov_base = space; 1202 v->iov_len = 1; 1203 v++; 1204 1205 if (msg) { 1206 wmsg = strdup(msg); /* XXX iov_base needs a `const' sibling. */ 1207 if (wmsg == NULL) { 1208 logerror("strdup"); 1209 exit(1); 1210 } 1211 v->iov_base = wmsg; 1212 v->iov_len = strlen(msg); 1213 } else if (f->f_prevcount > 1) { 1214 v->iov_base = repbuf; 1215 v->iov_len = snprintf(repbuf, sizeof repbuf, 1216 "last message repeated %d times", f->f_prevcount); 1217 } else { 1218 v->iov_base = f->f_prevline; 1219 v->iov_len = f->f_prevlen; 1220 } 1221 v++; 1222 1223 dprintf("Logging to %s", TypeNames[f->f_type]); 1224 f->f_time = now; 1225 1226 switch (f->f_type) { 1227 int port; 1228 case F_UNUSED: 1229 dprintf("\n"); 1230 break; 1231 1232 case F_FORW: 1233 port = (int)ntohs(((struct sockaddr_in *) 1234 (f->f_un.f_forw.f_addr->ai_addr))->sin_port); 1235 if (port != 514) { 1236 dprintf(" %s:%d\n", f->f_un.f_forw.f_hname, port); 1237 } else { 1238 dprintf(" %s\n", f->f_un.f_forw.f_hname); 1239 } 1240 /* check for local vs remote messages */ 1241 if (strcasecmp(f->f_prevhost, LocalHostName)) 1242 l = snprintf(line, sizeof line - 1, 1243 "<%d>%.15s Forwarded from %s: %s", 1244 f->f_prevpri, (char *)iov[0].iov_base, 1245 f->f_prevhost, (char *)iov[5].iov_base); 1246 else 1247 l = snprintf(line, sizeof line - 1, "<%d>%.15s %s", 1248 f->f_prevpri, (char *)iov[0].iov_base, 1249 (char *)iov[5].iov_base); 1250 if (l < 0) 1251 l = 0; 1252 else if (l > MAXLINE) 1253 l = MAXLINE; 1254 1255 if (finet) { 1256 for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) { 1257 for (i = 0; i < *finet; i++) { 1258#if 0 1259 /* 1260 * should we check AF first, or just 1261 * trial and error? FWD 1262 */ 1263 if (r->ai_family == 1264 address_family_of(finet[i+1])) 1265#endif 1266 lsent = sendto(finet[i+1], line, l, 0, 1267 r->ai_addr, r->ai_addrlen); 1268 if (lsent == l) 1269 break; 1270 } 1271 if (lsent == l && !send_to_all) 1272 break; 1273 } 1274 dprintf("lsent/l: %d/%d\n", lsent, l); 1275 if (lsent != l) { 1276 int e = errno; 1277 logerror("sendto"); 1278 errno = e; 1279 switch (errno) { 1280 case ENOBUFS: 1281 case ENETDOWN: 1282 case ENETUNREACH: 1283 case EHOSTUNREACH: 1284 case EHOSTDOWN: 1285 case EADDRNOTAVAIL: 1286 break; 1287 /* case EBADF: */ 1288 /* case EACCES: */ 1289 /* case ENOTSOCK: */ 1290 /* case EFAULT: */ 1291 /* case EMSGSIZE: */ 1292 /* case EAGAIN: */ 1293 /* case ENOBUFS: */ 1294 /* case ECONNREFUSED: */ 1295 default: 1296 dprintf("removing entry: errno=%d\n", e); 1297 f->f_type = F_UNUSED; 1298 break; 1299 } 1300 } 1301 } 1302 break; 1303 1304 case F_FILE: 1305 dprintf(" %s\n", f->f_un.f_fname); 1306 v->iov_base = lf; 1307 v->iov_len = 1; 1308 if (writev(f->f_file, iov, IOV_SIZE) < 0) { 1309 /* 1310 * If writev(2) fails for potentially transient errors 1311 * like the filesystem being full, ignore it. 1312 * Otherwise remove this logfile from the list. 1313 */ 1314 if (errno != ENOSPC) { 1315 int e = errno; 1316 close_filed(f); 1317 errno = e; 1318 logerror(f->f_un.f_fname); 1319 } 1320 } else if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC)) { 1321 f->f_flags |= FFLAG_NEEDSYNC; 1322 needdofsync = 1; 1323 } 1324 break; 1325 1326 case F_PIPE: 1327 dprintf(" %s\n", f->f_un.f_pipe.f_pname); 1328 v->iov_base = lf; 1329 v->iov_len = 1; 1330 if (f->f_un.f_pipe.f_pid == 0) { 1331 if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, 1332 &f->f_un.f_pipe.f_pid)) < 0) { 1333 logerror(f->f_un.f_pipe.f_pname); 1334 break; 1335 } 1336 } 1337 if (writev(f->f_file, iov, IOV_SIZE) < 0) { 1338 int e = errno; 1339 1340 close_filed(f); 1341 deadq_enter(f->f_un.f_pipe.f_pid, 1342 f->f_un.f_pipe.f_pname); 1343 errno = e; 1344 logerror(f->f_un.f_pipe.f_pname); 1345 } 1346 break; 1347 1348 case F_CONSOLE: 1349 if (flags & IGN_CONS) { 1350 dprintf(" (ignored)\n"); 1351 break; 1352 } 1353 /* FALLTHROUGH */ 1354 1355 case F_TTY: 1356 dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname); 1357 v->iov_base = crlf; 1358 v->iov_len = 2; 1359 1360 errno = 0; /* ttymsg() only sometimes returns an errno */ 1361 if ((msgret = ttymsg(iov, IOV_SIZE, f->f_un.f_fname, 10))) { 1362 f->f_type = F_UNUSED; 1363 logerror(msgret); 1364 } 1365 break; 1366 1367 case F_USERS: 1368 case F_WALL: 1369 dprintf("\n"); 1370 v->iov_base = crlf; 1371 v->iov_len = 2; 1372 wallmsg(f, iov, IOV_SIZE); 1373 break; 1374 } 1375 f->f_prevcount = 0; 1376 free(wmsg); 1377} 1378 1379/* 1380 * WALLMSG -- Write a message to the world at large 1381 * 1382 * Write the specified message to either the entire 1383 * world, or a list of approved users. 1384 */ 1385static void 1386wallmsg(struct filed *f, struct iovec *iov, const int iovlen) 1387{ 1388 static int reenter; /* avoid calling ourselves */ 1389 struct utmpx *ut; 1390 int i; 1391 const char *p; 1392 1393 if (reenter++) 1394 return; 1395 setutxent(); 1396 /* NOSTRICT */ 1397 while ((ut = getutxent()) != NULL) { 1398 if (ut->ut_type != USER_PROCESS) 1399 continue; 1400 if (f->f_type == F_WALL) { 1401 if ((p = ttymsg(iov, iovlen, ut->ut_line, 1402 TTYMSGTIME)) != NULL) { 1403 errno = 0; /* already in msg */ 1404 logerror(p); 1405 } 1406 continue; 1407 } 1408 /* should we send the message to this user? */ 1409 for (i = 0; i < MAXUNAMES; i++) { 1410 if (!f->f_un.f_uname[i][0]) 1411 break; 1412 if (!strcmp(f->f_un.f_uname[i], ut->ut_user)) { 1413 if ((p = ttymsg(iov, iovlen, ut->ut_line, 1414 TTYMSGTIME)) != NULL) { 1415 errno = 0; /* already in msg */ 1416 logerror(p); 1417 } 1418 break; 1419 } 1420 } 1421 } 1422 endutxent(); 1423 reenter = 0; 1424} 1425 1426static void 1427reapchild(int signo __unused) 1428{ 1429 int status; 1430 pid_t pid; 1431 struct filed *f; 1432 1433 while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { 1434 if (!Initialized) 1435 /* Don't tell while we are initting. */ 1436 continue; 1437 1438 /* First, look if it's a process from the dead queue. */ 1439 if (deadq_remove(pid)) 1440 goto oncemore; 1441 1442 /* Now, look in list of active processes. */ 1443 for (f = Files; f; f = f->f_next) 1444 if (f->f_type == F_PIPE && 1445 f->f_un.f_pipe.f_pid == pid) { 1446 close_filed(f); 1447 log_deadchild(pid, status, 1448 f->f_un.f_pipe.f_pname); 1449 break; 1450 } 1451 oncemore: 1452 continue; 1453 } 1454} 1455 1456/* 1457 * Return a printable representation of a host address. 1458 */ 1459static const char * 1460cvthname(struct sockaddr *f) 1461{ 1462 int error, hl; 1463 sigset_t omask, nmask; 1464 static char hname[NI_MAXHOST], ip[NI_MAXHOST]; 1465 1466 error = getnameinfo((struct sockaddr *)f, 1467 ((struct sockaddr *)f)->sa_len, 1468 ip, sizeof ip, NULL, 0, NI_NUMERICHOST); 1469 dprintf("cvthname(%s)\n", ip); 1470 1471 if (error) { 1472 dprintf("Malformed from address %s\n", gai_strerror(error)); 1473 return ("???"); 1474 } 1475 if (!resolve) 1476 return (ip); 1477 1478 sigemptyset(&nmask); 1479 sigaddset(&nmask, SIGHUP); 1480 sigprocmask(SIG_BLOCK, &nmask, &omask); 1481 error = getnameinfo((struct sockaddr *)f, 1482 ((struct sockaddr *)f)->sa_len, 1483 hname, sizeof hname, NULL, 0, NI_NAMEREQD); 1484 sigprocmask(SIG_SETMASK, &omask, NULL); 1485 if (error) { 1486 dprintf("Host name for your address (%s) unknown\n", ip); 1487 return (ip); 1488 } 1489 hl = strlen(hname); 1490 if (hl > 0 && hname[hl-1] == '.') 1491 hname[--hl] = '\0'; 1492 trimdomain(hname, hl); 1493 return (hname); 1494} 1495 1496static void 1497dodie(int signo) 1498{ 1499 1500 WantDie = signo; 1501} 1502 1503static void 1504domark(int signo __unused) 1505{ 1506 1507 MarkSet = 1; 1508} 1509 1510/* 1511 * Print syslogd errors some place. 1512 */ 1513static void 1514logerror(const char *type) 1515{ 1516 char buf[512]; 1517 static int recursed = 0; 1518 1519 /* If there's an error while trying to log an error, give up. */ 1520 if (recursed) 1521 return; 1522 recursed++; 1523 if (errno) 1524 (void)snprintf(buf, 1525 sizeof buf, "syslogd: %s: %s", type, strerror(errno)); 1526 else 1527 (void)snprintf(buf, sizeof buf, "syslogd: %s", type); 1528 errno = 0; 1529 dprintf("%s\n", buf); 1530 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 1531 recursed--; 1532} 1533 1534static void 1535die(int signo) 1536{ 1537 struct filed *f; 1538 struct funix *fx; 1539 int was_initialized; 1540 char buf[100]; 1541 1542 was_initialized = Initialized; 1543 Initialized = 0; /* Don't log SIGCHLDs. */ 1544 for (f = Files; f != NULL; f = f->f_next) { 1545 /* flush any pending output */ 1546 if (f->f_prevcount) 1547 fprintlog(f, 0, (char *)NULL); 1548 if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) 1549 close_filed(f); 1550 } 1551 Initialized = was_initialized; 1552 if (signo) { 1553 dprintf("syslogd: exiting on signal %d\n", signo); 1554 (void)snprintf(buf, sizeof(buf), "exiting on signal %d", signo); 1555 errno = 0; 1556 logerror(buf); 1557 } 1558 STAILQ_FOREACH(fx, &funixes, next) 1559 (void)unlink(fx->name); 1560 pidfile_remove(pfh); 1561 1562 exit(1); 1563} 1564 1565static int 1566configfiles(const struct dirent *dp) 1567{ 1568 const char *p; 1569 size_t ext_len; 1570 1571 if (dp->d_name[0] == '.') 1572 return (0); 1573 1574 ext_len = sizeof(include_ext) -1; 1575 1576 if (dp->d_namlen <= ext_len) 1577 return (0); 1578 1579 p = &dp->d_name[dp->d_namlen - ext_len]; 1580 if (strcmp(p, include_ext) != 0) 1581 return (0); 1582 1583 return (1); 1584} 1585 1586static void 1587readconfigfile(FILE *cf, struct filed **nextp, int allow_includes) 1588{ 1589 FILE *cf2; 1590 struct filed *f; 1591 struct dirent **ent; 1592 char cline[LINE_MAX]; 1593 char host[MAXHOSTNAMELEN]; 1594 char prog[LINE_MAX]; 1595 char file[MAXPATHLEN]; 1596 char *p, *tmp; 1597 int i, nents; 1598 size_t include_len; 1599 1600 /* 1601 * Foreach line in the conf table, open that file. 1602 */ 1603 f = NULL; 1604 include_len = sizeof(include_str) -1; 1605 (void)strlcpy(host, "*", sizeof(host)); 1606 (void)strlcpy(prog, "*", sizeof(prog)); 1607 while (fgets(cline, sizeof(cline), cf) != NULL) { 1608 /* 1609 * check for end-of-section, comments, strip off trailing 1610 * spaces and newline character. #!prog is treated specially: 1611 * following lines apply only to that program. 1612 */ 1613 for (p = cline; isspace(*p); ++p) 1614 continue; 1615 if (*p == 0) 1616 continue; 1617 if (allow_includes && 1618 strncmp(p, include_str, include_len) == 0 && 1619 isspace(p[include_len])) { 1620 p += include_len; 1621 while (isspace(*p)) 1622 p++; 1623 tmp = p; 1624 while (*tmp != '\0' && !isspace(*tmp)) 1625 tmp++; 1626 *tmp = '\0'; 1627 dprintf("Trying to include files in '%s'\n", p); 1628 nents = scandir(p, &ent, configfiles, alphasort); 1629 if (nents == -1) { 1630 dprintf("Unable to open '%s': %s\n", p, 1631 strerror(errno)); 1632 continue; 1633 } 1634 for (i = 0; i < nents; i++) { 1635 if (snprintf(file, sizeof(file), "%s/%s", p, 1636 ent[i]->d_name) >= (int)sizeof(file)) { 1637 dprintf("ignoring path too long: " 1638 "'%s/%s'\n", p, ent[i]->d_name); 1639 free(ent[i]); 1640 continue; 1641 } 1642 free(ent[i]); 1643 cf2 = fopen(file, "r"); 1644 if (cf2 == NULL) 1645 continue; 1646 dprintf("reading %s\n", file); 1647 readconfigfile(cf2, nextp, 0); 1648 fclose(cf2); 1649 } 1650 free(ent); 1651 continue; 1652 } 1653 if (*p == '#') { 1654 p++; 1655 if (*p != '!' && *p != '+' && *p != '-') 1656 continue; 1657 } 1658 if (*p == '+' || *p == '-') { 1659 host[0] = *p++; 1660 while (isspace(*p)) 1661 p++; 1662 if ((!*p) || (*p == '*')) { 1663 (void)strlcpy(host, "*", sizeof(host)); 1664 continue; 1665 } 1666 if (*p == '@') 1667 p = LocalHostName; 1668 for (i = 1; i < MAXHOSTNAMELEN - 1; i++) { 1669 if (!isalnum(*p) && *p != '.' && *p != '-' 1670 && *p != ',' && *p != ':' && *p != '%') 1671 break; 1672 host[i] = *p++; 1673 } 1674 host[i] = '\0'; 1675 continue; 1676 } 1677 if (*p == '!') { 1678 p++; 1679 while (isspace(*p)) p++; 1680 if ((!*p) || (*p == '*')) { 1681 (void)strlcpy(prog, "*", sizeof(prog)); 1682 continue; 1683 } 1684 for (i = 0; i < LINE_MAX - 1; i++) { 1685 if (!isprint(p[i]) || isspace(p[i])) 1686 break; 1687 prog[i] = p[i]; 1688 } 1689 prog[i] = 0; 1690 continue; 1691 } 1692 for (p = cline + 1; *p != '\0'; p++) { 1693 if (*p != '#') 1694 continue; 1695 if (*(p - 1) == '\\') { 1696 strcpy(p - 1, p); 1697 p--; 1698 continue; 1699 } 1700 *p = '\0'; 1701 break; 1702 } 1703 for (i = strlen(cline) - 1; i >= 0 && isspace(cline[i]); i--) 1704 cline[i] = '\0'; 1705 f = (struct filed *)calloc(1, sizeof(*f)); 1706 if (f == NULL) { 1707 logerror("calloc"); 1708 exit(1); 1709 } 1710 *nextp = f; 1711 nextp = &f->f_next; 1712 cfline(cline, f, prog, host); 1713 } 1714} 1715 1716/* 1717 * INIT -- Initialize syslogd from configuration table 1718 */ 1719static void 1720init(int signo) 1721{ 1722 int i; 1723 FILE *cf; 1724 struct filed *f, *next, **nextp; 1725 char *p; 1726 char oldLocalHostName[MAXHOSTNAMELEN]; 1727 char hostMsg[2*MAXHOSTNAMELEN+40]; 1728 char bootfileMsg[LINE_MAX]; 1729 1730 dprintf("init\n"); 1731 1732 /* 1733 * Load hostname (may have changed). 1734 */ 1735 if (signo != 0) 1736 (void)strlcpy(oldLocalHostName, LocalHostName, 1737 sizeof(oldLocalHostName)); 1738 if (gethostname(LocalHostName, sizeof(LocalHostName))) 1739 err(EX_OSERR, "gethostname() failed"); 1740 if ((p = strchr(LocalHostName, '.')) != NULL) { 1741 *p++ = '\0'; 1742 LocalDomain = p; 1743 } else { 1744 LocalDomain = ""; 1745 } 1746 1747 /* 1748 * Close all open log files. 1749 */ 1750 Initialized = 0; 1751 for (f = Files; f != NULL; f = next) { 1752 /* flush any pending output */ 1753 if (f->f_prevcount) 1754 fprintlog(f, 0, (char *)NULL); 1755 1756 switch (f->f_type) { 1757 case F_FILE: 1758 case F_FORW: 1759 case F_CONSOLE: 1760 case F_TTY: 1761 close_filed(f); 1762 break; 1763 case F_PIPE: 1764 close_filed(f); 1765 deadq_enter(f->f_un.f_pipe.f_pid, 1766 f->f_un.f_pipe.f_pname); 1767 break; 1768 } 1769 next = f->f_next; 1770 if (f->f_program) free(f->f_program); 1771 if (f->f_host) free(f->f_host); 1772 free((char *)f); 1773 } 1774 Files = NULL; 1775 nextp = &Files; 1776 1777 /* open the configuration file */ 1778 if ((cf = fopen(ConfFile, "r")) == NULL) { 1779 dprintf("cannot open %s\n", ConfFile); 1780 *nextp = (struct filed *)calloc(1, sizeof(*f)); 1781 if (*nextp == NULL) { 1782 logerror("calloc"); 1783 exit(1); 1784 } 1785 cfline("*.ERR\t/dev/console", *nextp, "*", "*"); 1786 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 1787 if ((*nextp)->f_next == NULL) { 1788 logerror("calloc"); 1789 exit(1); 1790 } 1791 cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*"); 1792 Initialized = 1; 1793 return; 1794 } 1795 1796 readconfigfile(cf, &Files, 1); 1797 1798 /* close the configuration file */ 1799 (void)fclose(cf); 1800 1801 Initialized = 1; 1802 1803 if (Debug) { 1804 int port; 1805 for (f = Files; f; f = f->f_next) { 1806 for (i = 0; i <= LOG_NFACILITIES; i++) 1807 if (f->f_pmask[i] == INTERNAL_NOPRI) 1808 printf("X "); 1809 else 1810 printf("%d ", f->f_pmask[i]); 1811 printf("%s: ", TypeNames[f->f_type]); 1812 switch (f->f_type) { 1813 case F_FILE: 1814 printf("%s", f->f_un.f_fname); 1815 break; 1816 1817 case F_CONSOLE: 1818 case F_TTY: 1819 printf("%s%s", _PATH_DEV, f->f_un.f_fname); 1820 break; 1821 1822 case F_FORW: 1823 port = (int)ntohs(((struct sockaddr_in *) 1824 (f->f_un.f_forw.f_addr->ai_addr))->sin_port); 1825 if (port != 514) { 1826 printf("%s:%d", 1827 f->f_un.f_forw.f_hname, port); 1828 } else { 1829 printf("%s", f->f_un.f_forw.f_hname); 1830 } 1831 break; 1832 1833 case F_PIPE: 1834 printf("%s", f->f_un.f_pipe.f_pname); 1835 break; 1836 1837 case F_USERS: 1838 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1839 printf("%s, ", f->f_un.f_uname[i]); 1840 break; 1841 } 1842 if (f->f_program) 1843 printf(" (%s)", f->f_program); 1844 printf("\n"); 1845 } 1846 } 1847 1848 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1849 dprintf("syslogd: restarted\n"); 1850 /* 1851 * Log a change in hostname, but only on a restart. 1852 */ 1853 if (signo != 0 && strcmp(oldLocalHostName, LocalHostName) != 0) { 1854 (void)snprintf(hostMsg, sizeof(hostMsg), 1855 "syslogd: hostname changed, \"%s\" to \"%s\"", 1856 oldLocalHostName, LocalHostName); 1857 logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE); 1858 dprintf("%s\n", hostMsg); 1859 } 1860 /* 1861 * Log the kernel boot file if we aren't going to use it as 1862 * the prefix, and if this is *not* a restart. 1863 */ 1864 if (signo == 0 && !use_bootfile) { 1865 (void)snprintf(bootfileMsg, sizeof(bootfileMsg), 1866 "syslogd: kernel boot file is %s", bootfile); 1867 logmsg(LOG_KERN|LOG_INFO, bootfileMsg, LocalHostName, ADDDATE); 1868 dprintf("%s\n", bootfileMsg); 1869 } 1870} 1871 1872/* 1873 * Crack a configuration file line 1874 */ 1875static void 1876cfline(const char *line, struct filed *f, const char *prog, const char *host) 1877{ 1878 struct addrinfo hints, *res; 1879 int error, i, pri, syncfile; 1880 const char *p, *q; 1881 char *bp; 1882 char buf[MAXLINE], ebuf[100]; 1883 1884 dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host); 1885 1886 errno = 0; /* keep strerror() stuff out of logerror messages */ 1887 1888 /* clear out file entry */ 1889 memset(f, 0, sizeof(*f)); 1890 for (i = 0; i <= LOG_NFACILITIES; i++) 1891 f->f_pmask[i] = INTERNAL_NOPRI; 1892 1893 /* save hostname if any */ 1894 if (host && *host == '*') 1895 host = NULL; 1896 if (host) { 1897 int hl; 1898 1899 f->f_host = strdup(host); 1900 if (f->f_host == NULL) { 1901 logerror("strdup"); 1902 exit(1); 1903 } 1904 hl = strlen(f->f_host); 1905 if (hl > 0 && f->f_host[hl-1] == '.') 1906 f->f_host[--hl] = '\0'; 1907 trimdomain(f->f_host, hl); 1908 } 1909 1910 /* save program name if any */ 1911 if (prog && *prog == '*') 1912 prog = NULL; 1913 if (prog) { 1914 f->f_program = strdup(prog); 1915 if (f->f_program == NULL) { 1916 logerror("strdup"); 1917 exit(1); 1918 } 1919 } 1920 1921 /* scan through the list of selectors */ 1922 for (p = line; *p && *p != '\t' && *p != ' ';) { 1923 int pri_done; 1924 int pri_cmp; 1925 int pri_invert; 1926 1927 /* find the end of this facility name list */ 1928 for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; ) 1929 continue; 1930 1931 /* get the priority comparison */ 1932 pri_cmp = 0; 1933 pri_done = 0; 1934 pri_invert = 0; 1935 if (*q == '!') { 1936 pri_invert = 1; 1937 q++; 1938 } 1939 while (!pri_done) { 1940 switch (*q) { 1941 case '<': 1942 pri_cmp |= PRI_LT; 1943 q++; 1944 break; 1945 case '=': 1946 pri_cmp |= PRI_EQ; 1947 q++; 1948 break; 1949 case '>': 1950 pri_cmp |= PRI_GT; 1951 q++; 1952 break; 1953 default: 1954 pri_done++; 1955 break; 1956 } 1957 } 1958 1959 /* collect priority name */ 1960 for (bp = buf; *q && !strchr("\t,; ", *q); ) 1961 *bp++ = *q++; 1962 *bp = '\0'; 1963 1964 /* skip cruft */ 1965 while (strchr(",;", *q)) 1966 q++; 1967 1968 /* decode priority name */ 1969 if (*buf == '*') { 1970 pri = LOG_PRIMASK; 1971 pri_cmp = PRI_LT | PRI_EQ | PRI_GT; 1972 } else { 1973 /* Ignore trailing spaces. */ 1974 for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--) 1975 buf[i] = '\0'; 1976 1977 pri = decode(buf, prioritynames); 1978 if (pri < 0) { 1979 errno = 0; 1980 (void)snprintf(ebuf, sizeof ebuf, 1981 "unknown priority name \"%s\"", buf); 1982 logerror(ebuf); 1983 return; 1984 } 1985 } 1986 if (!pri_cmp) 1987 pri_cmp = (UniquePriority) 1988 ? (PRI_EQ) 1989 : (PRI_EQ | PRI_GT) 1990 ; 1991 if (pri_invert) 1992 pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT; 1993 1994 /* scan facilities */ 1995 while (*p && !strchr("\t.; ", *p)) { 1996 for (bp = buf; *p && !strchr("\t,;. ", *p); ) 1997 *bp++ = *p++; 1998 *bp = '\0'; 1999 2000 if (*buf == '*') { 2001 for (i = 0; i < LOG_NFACILITIES; i++) { 2002 f->f_pmask[i] = pri; 2003 f->f_pcmp[i] = pri_cmp; 2004 } 2005 } else { 2006 i = decode(buf, facilitynames); 2007 if (i < 0) { 2008 errno = 0; 2009 (void)snprintf(ebuf, sizeof ebuf, 2010 "unknown facility name \"%s\"", 2011 buf); 2012 logerror(ebuf); 2013 return; 2014 } 2015 f->f_pmask[i >> 3] = pri; 2016 f->f_pcmp[i >> 3] = pri_cmp; 2017 } 2018 while (*p == ',' || *p == ' ') 2019 p++; 2020 } 2021 2022 p = q; 2023 } 2024 2025 /* skip to action part */ 2026 while (*p == '\t' || *p == ' ') 2027 p++; 2028 2029 if (*p == '-') { 2030 syncfile = 0; 2031 p++; 2032 } else 2033 syncfile = 1; 2034 2035 switch (*p) { 2036 case '@': 2037 { 2038 char *tp; 2039 char endkey = ':'; 2040 /* 2041 * scan forward to see if there is a port defined. 2042 * so we can't use strlcpy.. 2043 */ 2044 i = sizeof(f->f_un.f_forw.f_hname); 2045 tp = f->f_un.f_forw.f_hname; 2046 p++; 2047 2048 /* 2049 * an ipv6 address should start with a '[' in that case 2050 * we should scan for a ']' 2051 */ 2052 if (*p == '[') { 2053 p++; 2054 endkey = ']'; 2055 } 2056 while (*p && (*p != endkey) && (i-- > 0)) { 2057 *tp++ = *p++; 2058 } 2059 if (endkey == ']' && *p == endkey) 2060 p++; 2061 *tp = '\0'; 2062 } 2063 /* See if we copied a domain and have a port */ 2064 if (*p == ':') 2065 p++; 2066 else 2067 p = NULL; 2068 2069 memset(&hints, 0, sizeof(hints)); 2070 hints.ai_family = family; 2071 hints.ai_socktype = SOCK_DGRAM; 2072 error = getaddrinfo(f->f_un.f_forw.f_hname, 2073 p ? p : "syslog", &hints, &res); 2074 if (error) { 2075 logerror(gai_strerror(error)); 2076 break; 2077 } 2078 f->f_un.f_forw.f_addr = res; 2079 f->f_type = F_FORW; 2080 break; 2081 2082 case '/': 2083 if ((f->f_file = open(p, logflags, 0600)) < 0) { 2084 f->f_type = F_UNUSED; 2085 logerror(p); 2086 break; 2087 } 2088 if (syncfile) 2089 f->f_flags |= FFLAG_SYNC; 2090 if (isatty(f->f_file)) { 2091 if (strcmp(p, ctty) == 0) 2092 f->f_type = F_CONSOLE; 2093 else 2094 f->f_type = F_TTY; 2095 (void)strlcpy(f->f_un.f_fname, p + sizeof(_PATH_DEV) - 1, 2096 sizeof(f->f_un.f_fname)); 2097 } else { 2098 (void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname)); 2099 f->f_type = F_FILE; 2100 } 2101 break; 2102 2103 case '|': 2104 f->f_un.f_pipe.f_pid = 0; 2105 (void)strlcpy(f->f_un.f_pipe.f_pname, p + 1, 2106 sizeof(f->f_un.f_pipe.f_pname)); 2107 f->f_type = F_PIPE; 2108 break; 2109 2110 case '*': 2111 f->f_type = F_WALL; 2112 break; 2113 2114 default: 2115 for (i = 0; i < MAXUNAMES && *p; i++) { 2116 for (q = p; *q && *q != ','; ) 2117 q++; 2118 (void)strncpy(f->f_un.f_uname[i], p, MAXLOGNAME - 1); 2119 if ((q - p) >= MAXLOGNAME) 2120 f->f_un.f_uname[i][MAXLOGNAME - 1] = '\0'; 2121 else 2122 f->f_un.f_uname[i][q - p] = '\0'; 2123 while (*q == ',' || *q == ' ') 2124 q++; 2125 p = q; 2126 } 2127 f->f_type = F_USERS; 2128 break; 2129 } 2130} 2131 2132 2133/* 2134 * Decode a symbolic name to a numeric value 2135 */ 2136static int 2137decode(const char *name, const CODE *codetab) 2138{ 2139 const CODE *c; 2140 char *p, buf[40]; 2141 2142 if (isdigit(*name)) 2143 return (atoi(name)); 2144 2145 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 2146 if (isupper(*name)) 2147 *p = tolower(*name); 2148 else 2149 *p = *name; 2150 } 2151 *p = '\0'; 2152 for (c = codetab; c->c_name; c++) 2153 if (!strcmp(buf, c->c_name)) 2154 return (c->c_val); 2155 2156 return (-1); 2157} 2158 2159static void 2160markit(void) 2161{ 2162 struct filed *f; 2163 dq_t q, next; 2164 2165 now = time((time_t *)NULL); 2166 MarkSeq += TIMERINTVL; 2167 if (MarkSeq >= MarkInterval) { 2168 logmsg(LOG_INFO, "-- MARK --", 2169 LocalHostName, ADDDATE|MARK); 2170 MarkSeq = 0; 2171 } 2172 2173 for (f = Files; f; f = f->f_next) { 2174 if (f->f_prevcount && now >= REPEATTIME(f)) { 2175 dprintf("flush %s: repeated %d times, %d sec.\n", 2176 TypeNames[f->f_type], f->f_prevcount, 2177 repeatinterval[f->f_repeatcount]); 2178 fprintlog(f, 0, (char *)NULL); 2179 BACKOFF(f); 2180 } 2181 } 2182 2183 /* Walk the dead queue, and see if we should signal somebody. */ 2184 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = next) { 2185 next = TAILQ_NEXT(q, dq_entries); 2186 2187 switch (q->dq_timeout) { 2188 case 0: 2189 /* Already signalled once, try harder now. */ 2190 if (kill(q->dq_pid, SIGKILL) != 0) 2191 (void)deadq_remove(q->dq_pid); 2192 break; 2193 2194 case 1: 2195 /* 2196 * Timed out on dead queue, send terminate 2197 * signal. Note that we leave the removal 2198 * from the dead queue to reapchild(), which 2199 * will also log the event (unless the process 2200 * didn't even really exist, in case we simply 2201 * drop it from the dead queue). 2202 */ 2203 if (kill(q->dq_pid, SIGTERM) != 0) 2204 (void)deadq_remove(q->dq_pid); 2205 /* FALLTHROUGH */ 2206 2207 default: 2208 q->dq_timeout--; 2209 } 2210 } 2211 MarkSet = 0; 2212 (void)alarm(TIMERINTVL); 2213} 2214 2215/* 2216 * fork off and become a daemon, but wait for the child to come online 2217 * before returning to the parent, or we get disk thrashing at boot etc. 2218 * Set a timer so we don't hang forever if it wedges. 2219 */ 2220static int 2221waitdaemon(int nochdir, int noclose, int maxwait) 2222{ 2223 int fd; 2224 int status; 2225 pid_t pid, childpid; 2226 2227 switch (childpid = fork()) { 2228 case -1: 2229 return (-1); 2230 case 0: 2231 break; 2232 default: 2233 signal(SIGALRM, timedout); 2234 alarm(maxwait); 2235 while ((pid = wait3(&status, 0, NULL)) != -1) { 2236 if (WIFEXITED(status)) 2237 errx(1, "child pid %d exited with return code %d", 2238 pid, WEXITSTATUS(status)); 2239 if (WIFSIGNALED(status)) 2240 errx(1, "child pid %d exited on signal %d%s", 2241 pid, WTERMSIG(status), 2242 WCOREDUMP(status) ? " (core dumped)" : 2243 ""); 2244 if (pid == childpid) /* it's gone... */ 2245 break; 2246 } 2247 exit(0); 2248 } 2249 2250 if (setsid() == -1) 2251 return (-1); 2252 2253 if (!nochdir) 2254 (void)chdir("/"); 2255 2256 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 2257 (void)dup2(fd, STDIN_FILENO); 2258 (void)dup2(fd, STDOUT_FILENO); 2259 (void)dup2(fd, STDERR_FILENO); 2260 if (fd > 2) 2261 (void)close (fd); 2262 } 2263 return (getppid()); 2264} 2265 2266/* 2267 * We get a SIGALRM from the child when it's running and finished doing it's 2268 * fsync()'s or O_SYNC writes for all the boot messages. 2269 * 2270 * We also get a signal from the kernel if the timer expires, so check to 2271 * see what happened. 2272 */ 2273static void 2274timedout(int sig __unused) 2275{ 2276 int left; 2277 left = alarm(0); 2278 signal(SIGALRM, SIG_DFL); 2279 if (left == 0) 2280 errx(1, "timed out waiting for child"); 2281 else 2282 _exit(0); 2283} 2284 2285/* 2286 * Add `s' to the list of allowable peer addresses to accept messages 2287 * from. 2288 * 2289 * `s' is a string in the form: 2290 * 2291 * [*]domainname[:{servicename|portnumber|*}] 2292 * 2293 * or 2294 * 2295 * netaddr/maskbits[:{servicename|portnumber|*}] 2296 * 2297 * Returns -1 on error, 0 if the argument was valid. 2298 */ 2299static int 2300allowaddr(char *s) 2301{ 2302 char *cp1, *cp2; 2303 struct allowedpeer ap; 2304 struct servent *se; 2305 int masklen = -1; 2306 struct addrinfo hints, *res; 2307 struct in_addr *addrp, *maskp; 2308#ifdef INET6 2309 int i; 2310 u_int32_t *addr6p, *mask6p; 2311#endif 2312 char ip[NI_MAXHOST]; 2313 2314#ifdef INET6 2315 if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL) 2316#endif 2317 cp1 = s; 2318 if ((cp1 = strrchr(cp1, ':'))) { 2319 /* service/port provided */ 2320 *cp1++ = '\0'; 2321 if (strlen(cp1) == 1 && *cp1 == '*') 2322 /* any port allowed */ 2323 ap.port = 0; 2324 else if ((se = getservbyname(cp1, "udp"))) { 2325 ap.port = ntohs(se->s_port); 2326 } else { 2327 ap.port = strtol(cp1, &cp2, 0); 2328 if (*cp2 != '\0') 2329 return (-1); /* port not numeric */ 2330 } 2331 } else { 2332 if ((se = getservbyname("syslog", "udp"))) 2333 ap.port = ntohs(se->s_port); 2334 else 2335 /* sanity, should not happen */ 2336 ap.port = 514; 2337 } 2338 2339 if ((cp1 = strchr(s, '/')) != NULL && 2340 strspn(cp1 + 1, "0123456789") == strlen(cp1 + 1)) { 2341 *cp1 = '\0'; 2342 if ((masklen = atoi(cp1 + 1)) < 0) 2343 return (-1); 2344 } 2345#ifdef INET6 2346 if (*s == '[') { 2347 cp2 = s + strlen(s) - 1; 2348 if (*cp2 == ']') { 2349 ++s; 2350 *cp2 = '\0'; 2351 } else { 2352 cp2 = NULL; 2353 } 2354 } else { 2355 cp2 = NULL; 2356 } 2357#endif 2358 memset(&hints, 0, sizeof(hints)); 2359 hints.ai_family = PF_UNSPEC; 2360 hints.ai_socktype = SOCK_DGRAM; 2361 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 2362 if (getaddrinfo(s, NULL, &hints, &res) == 0) { 2363 ap.isnumeric = 1; 2364 memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen); 2365 memset(&ap.a_mask, 0, sizeof(ap.a_mask)); 2366 ap.a_mask.ss_family = res->ai_family; 2367 if (res->ai_family == AF_INET) { 2368 ap.a_mask.ss_len = sizeof(struct sockaddr_in); 2369 maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr; 2370 addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr; 2371 if (masklen < 0) { 2372 /* use default netmask */ 2373 if (IN_CLASSA(ntohl(addrp->s_addr))) 2374 maskp->s_addr = htonl(IN_CLASSA_NET); 2375 else if (IN_CLASSB(ntohl(addrp->s_addr))) 2376 maskp->s_addr = htonl(IN_CLASSB_NET); 2377 else 2378 maskp->s_addr = htonl(IN_CLASSC_NET); 2379 } else if (masklen <= 32) { 2380 /* convert masklen to netmask */ 2381 if (masklen == 0) 2382 maskp->s_addr = 0; 2383 else 2384 maskp->s_addr = htonl(~((1 << (32 - masklen)) - 1)); 2385 } else { 2386 freeaddrinfo(res); 2387 return (-1); 2388 } 2389 /* Lose any host bits in the network number. */ 2390 addrp->s_addr &= maskp->s_addr; 2391 } 2392#ifdef INET6 2393 else if (res->ai_family == AF_INET6 && masklen <= 128) { 2394 ap.a_mask.ss_len = sizeof(struct sockaddr_in6); 2395 if (masklen < 0) 2396 masklen = 128; 2397 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2398 /* convert masklen to netmask */ 2399 while (masklen > 0) { 2400 if (masklen < 32) { 2401 *mask6p = htonl(~(0xffffffff >> masklen)); 2402 break; 2403 } 2404 *mask6p++ = 0xffffffff; 2405 masklen -= 32; 2406 } 2407 /* Lose any host bits in the network number. */ 2408 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2409 addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr; 2410 for (i = 0; i < 4; i++) 2411 addr6p[i] &= mask6p[i]; 2412 } 2413#endif 2414 else { 2415 freeaddrinfo(res); 2416 return (-1); 2417 } 2418 freeaddrinfo(res); 2419 } else { 2420 /* arg `s' is domain name */ 2421 ap.isnumeric = 0; 2422 ap.a_name = s; 2423 if (cp1) 2424 *cp1 = '/'; 2425#ifdef INET6 2426 if (cp2) { 2427 *cp2 = ']'; 2428 --s; 2429 } 2430#endif 2431 } 2432 2433 if (Debug) { 2434 printf("allowaddr: rule %d: ", NumAllowed); 2435 if (ap.isnumeric) { 2436 printf("numeric, "); 2437 getnameinfo((struct sockaddr *)&ap.a_addr, 2438 ((struct sockaddr *)&ap.a_addr)->sa_len, 2439 ip, sizeof ip, NULL, 0, NI_NUMERICHOST); 2440 printf("addr = %s, ", ip); 2441 getnameinfo((struct sockaddr *)&ap.a_mask, 2442 ((struct sockaddr *)&ap.a_mask)->sa_len, 2443 ip, sizeof ip, NULL, 0, NI_NUMERICHOST); 2444 printf("mask = %s; ", ip); 2445 } else { 2446 printf("domainname = %s; ", ap.a_name); 2447 } 2448 printf("port = %d\n", ap.port); 2449 } 2450 2451 if ((AllowedPeers = realloc(AllowedPeers, 2452 ++NumAllowed * sizeof(struct allowedpeer))) 2453 == NULL) { 2454 logerror("realloc"); 2455 exit(1); 2456 } 2457 memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer)); 2458 return (0); 2459} 2460 2461/* 2462 * Validate that the remote peer has permission to log to us. 2463 */ 2464static int 2465validate(struct sockaddr *sa, const char *hname) 2466{ 2467 int i; 2468 size_t l1, l2; 2469 char *cp, name[NI_MAXHOST], ip[NI_MAXHOST], port[NI_MAXSERV]; 2470 struct allowedpeer *ap; 2471 struct sockaddr_in *sin4, *a4p = NULL, *m4p = NULL; 2472#ifdef INET6 2473 int j, reject; 2474 struct sockaddr_in6 *sin6, *a6p = NULL, *m6p = NULL; 2475#endif 2476 struct addrinfo hints, *res; 2477 u_short sport; 2478 2479 if (NumAllowed == 0) 2480 /* traditional behaviour, allow everything */ 2481 return (1); 2482 2483 (void)strlcpy(name, hname, sizeof(name)); 2484 memset(&hints, 0, sizeof(hints)); 2485 hints.ai_family = PF_UNSPEC; 2486 hints.ai_socktype = SOCK_DGRAM; 2487 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 2488 if (getaddrinfo(name, NULL, &hints, &res) == 0) 2489 freeaddrinfo(res); 2490 else if (strchr(name, '.') == NULL) { 2491 strlcat(name, ".", sizeof name); 2492 strlcat(name, LocalDomain, sizeof name); 2493 } 2494 if (getnameinfo(sa, sa->sa_len, ip, sizeof ip, port, sizeof port, 2495 NI_NUMERICHOST | NI_NUMERICSERV) != 0) 2496 return (0); /* for safety, should not occur */ 2497 dprintf("validate: dgram from IP %s, port %s, name %s;\n", 2498 ip, port, name); 2499 sport = atoi(port); 2500 2501 /* now, walk down the list */ 2502 for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) { 2503 if (ap->port != 0 && ap->port != sport) { 2504 dprintf("rejected in rule %d due to port mismatch.\n", i); 2505 continue; 2506 } 2507 2508 if (ap->isnumeric) { 2509 if (ap->a_addr.ss_family != sa->sa_family) { 2510 dprintf("rejected in rule %d due to address family mismatch.\n", i); 2511 continue; 2512 } 2513 if (ap->a_addr.ss_family == AF_INET) { 2514 sin4 = (struct sockaddr_in *)sa; 2515 a4p = (struct sockaddr_in *)&ap->a_addr; 2516 m4p = (struct sockaddr_in *)&ap->a_mask; 2517 if ((sin4->sin_addr.s_addr & m4p->sin_addr.s_addr) 2518 != a4p->sin_addr.s_addr) { 2519 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2520 continue; 2521 } 2522 } 2523#ifdef INET6 2524 else if (ap->a_addr.ss_family == AF_INET6) { 2525 sin6 = (struct sockaddr_in6 *)sa; 2526 a6p = (struct sockaddr_in6 *)&ap->a_addr; 2527 m6p = (struct sockaddr_in6 *)&ap->a_mask; 2528 if (a6p->sin6_scope_id != 0 && 2529 sin6->sin6_scope_id != a6p->sin6_scope_id) { 2530 dprintf("rejected in rule %d due to scope mismatch.\n", i); 2531 continue; 2532 } 2533 reject = 0; 2534 for (j = 0; j < 16; j += 4) { 2535 if ((*(u_int32_t *)&sin6->sin6_addr.s6_addr[j] & *(u_int32_t *)&m6p->sin6_addr.s6_addr[j]) 2536 != *(u_int32_t *)&a6p->sin6_addr.s6_addr[j]) { 2537 ++reject; 2538 break; 2539 } 2540 } 2541 if (reject) { 2542 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2543 continue; 2544 } 2545 } 2546#endif 2547 else 2548 continue; 2549 } else { 2550 cp = ap->a_name; 2551 l1 = strlen(name); 2552 if (*cp == '*') { 2553 /* allow wildmatch */ 2554 cp++; 2555 l2 = strlen(cp); 2556 if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) { 2557 dprintf("rejected in rule %d due to name mismatch.\n", i); 2558 continue; 2559 } 2560 } else { 2561 /* exact match */ 2562 l2 = strlen(cp); 2563 if (l2 != l1 || memcmp(cp, name, l1) != 0) { 2564 dprintf("rejected in rule %d due to name mismatch.\n", i); 2565 continue; 2566 } 2567 } 2568 } 2569 dprintf("accepted in rule %d.\n", i); 2570 return (1); /* hooray! */ 2571 } 2572 return (0); 2573} 2574 2575/* 2576 * Fairly similar to popen(3), but returns an open descriptor, as 2577 * opposed to a FILE *. 2578 */ 2579static int 2580p_open(const char *prog, pid_t *rpid) 2581{ 2582 int pfd[2], nulldesc; 2583 pid_t pid; 2584 sigset_t omask, mask; 2585 char *argv[4]; /* sh -c cmd NULL */ 2586 char errmsg[200]; 2587 2588 if (pipe(pfd) == -1) 2589 return (-1); 2590 if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) 2591 /* we are royally screwed anyway */ 2592 return (-1); 2593 2594 sigemptyset(&mask); 2595 sigaddset(&mask, SIGALRM); 2596 sigaddset(&mask, SIGHUP); 2597 sigprocmask(SIG_BLOCK, &mask, &omask); 2598 switch ((pid = fork())) { 2599 case -1: 2600 sigprocmask(SIG_SETMASK, &omask, 0); 2601 close(nulldesc); 2602 return (-1); 2603 2604 case 0: 2605 argv[0] = strdup("sh"); 2606 argv[1] = strdup("-c"); 2607 argv[2] = strdup(prog); 2608 argv[3] = NULL; 2609 if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { 2610 logerror("strdup"); 2611 exit(1); 2612 } 2613 2614 alarm(0); 2615 (void)setsid(); /* Avoid catching SIGHUPs. */ 2616 2617 /* 2618 * Throw away pending signals, and reset signal 2619 * behaviour to standard values. 2620 */ 2621 signal(SIGALRM, SIG_IGN); 2622 signal(SIGHUP, SIG_IGN); 2623 sigprocmask(SIG_SETMASK, &omask, 0); 2624 signal(SIGPIPE, SIG_DFL); 2625 signal(SIGQUIT, SIG_DFL); 2626 signal(SIGALRM, SIG_DFL); 2627 signal(SIGHUP, SIG_DFL); 2628 2629 dup2(pfd[0], STDIN_FILENO); 2630 dup2(nulldesc, STDOUT_FILENO); 2631 dup2(nulldesc, STDERR_FILENO); 2632 closefrom(3); 2633 2634 (void)execvp(_PATH_BSHELL, argv); 2635 _exit(255); 2636 } 2637 2638 sigprocmask(SIG_SETMASK, &omask, 0); 2639 close(nulldesc); 2640 close(pfd[0]); 2641 /* 2642 * Avoid blocking on a hung pipe. With O_NONBLOCK, we are 2643 * supposed to get an EWOULDBLOCK on writev(2), which is 2644 * caught by the logic above anyway, which will in turn close 2645 * the pipe, and fork a new logging subprocess if necessary. 2646 * The stale subprocess will be killed some time later unless 2647 * it terminated itself due to closing its input pipe (so we 2648 * get rid of really dead puppies). 2649 */ 2650 if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { 2651 /* This is bad. */ 2652 (void)snprintf(errmsg, sizeof errmsg, 2653 "Warning: cannot change pipe to PID %d to " 2654 "non-blocking behaviour.", 2655 (int)pid); 2656 logerror(errmsg); 2657 } 2658 *rpid = pid; 2659 return (pfd[1]); 2660} 2661 2662static void 2663deadq_enter(pid_t pid, const char *name) 2664{ 2665 dq_t p; 2666 int status; 2667 2668 if (pid == 0) 2669 return; 2670 /* 2671 * Be paranoid, if we can't signal the process, don't enter it 2672 * into the dead queue (perhaps it's already dead). If possible, 2673 * we try to fetch and log the child's status. 2674 */ 2675 if (kill(pid, 0) != 0) { 2676 if (waitpid(pid, &status, WNOHANG) > 0) 2677 log_deadchild(pid, status, name); 2678 return; 2679 } 2680 2681 p = malloc(sizeof(struct deadq_entry)); 2682 if (p == NULL) { 2683 logerror("malloc"); 2684 exit(1); 2685 } 2686 2687 p->dq_pid = pid; 2688 p->dq_timeout = DQ_TIMO_INIT; 2689 TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); 2690} 2691 2692static int 2693deadq_remove(pid_t pid) 2694{ 2695 dq_t q; 2696 2697 TAILQ_FOREACH(q, &deadq_head, dq_entries) { 2698 if (q->dq_pid == pid) { 2699 TAILQ_REMOVE(&deadq_head, q, dq_entries); 2700 free(q); 2701 return (1); 2702 } 2703 } 2704 2705 return (0); 2706} 2707 2708static void 2709log_deadchild(pid_t pid, int status, const char *name) 2710{ 2711 int code; 2712 char buf[256]; 2713 const char *reason; 2714 2715 errno = 0; /* Keep strerror() stuff out of logerror messages. */ 2716 if (WIFSIGNALED(status)) { 2717 reason = "due to signal"; 2718 code = WTERMSIG(status); 2719 } else { 2720 reason = "with status"; 2721 code = WEXITSTATUS(status); 2722 if (code == 0) 2723 return; 2724 } 2725 (void)snprintf(buf, sizeof buf, 2726 "Logging subprocess %d (%s) exited %s %d.", 2727 pid, name, reason, code); 2728 logerror(buf); 2729} 2730 2731static int * 2732socksetup(int af, char *bindhostname) 2733{ 2734 struct addrinfo hints, *res, *r; 2735 const char *bindservice; 2736 char *cp; 2737 int error, maxs, *s, *socks; 2738 2739 /* 2740 * We have to handle this case for backwards compatibility: 2741 * If there are two (or more) colons but no '[' and ']', 2742 * assume this is an inet6 address without a service. 2743 */ 2744 bindservice = "syslog"; 2745 if (bindhostname != NULL) { 2746#ifdef INET6 2747 if (*bindhostname == '[' && 2748 (cp = strchr(bindhostname + 1, ']')) != NULL) { 2749 ++bindhostname; 2750 *cp = '\0'; 2751 if (cp[1] == ':' && cp[2] != '\0') 2752 bindservice = cp + 2; 2753 } else { 2754#endif 2755 cp = strchr(bindhostname, ':'); 2756 if (cp != NULL && strchr(cp + 1, ':') == NULL) { 2757 *cp = '\0'; 2758 if (cp[1] != '\0') 2759 bindservice = cp + 1; 2760 if (cp == bindhostname) 2761 bindhostname = NULL; 2762 } 2763#ifdef INET6 2764 } 2765#endif 2766 } 2767 2768 memset(&hints, 0, sizeof(hints)); 2769 hints.ai_flags = AI_PASSIVE; 2770 hints.ai_family = af; 2771 hints.ai_socktype = SOCK_DGRAM; 2772 error = getaddrinfo(bindhostname, bindservice, &hints, &res); 2773 if (error) { 2774 logerror(gai_strerror(error)); 2775 errno = 0; 2776 die(0); 2777 } 2778 2779 /* Count max number of sockets we may open */ 2780 for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 2781 socks = malloc((maxs+1) * sizeof(int)); 2782 if (socks == NULL) { 2783 logerror("couldn't allocate memory for sockets"); 2784 die(0); 2785 } 2786 2787 *socks = 0; /* num of sockets counter at start of array */ 2788 s = socks + 1; 2789 for (r = res; r; r = r->ai_next) { 2790 int on = 1; 2791 *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 2792 if (*s < 0) { 2793 logerror("socket"); 2794 continue; 2795 } 2796#ifdef INET6 2797 if (r->ai_family == AF_INET6) { 2798 if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, 2799 (char *)&on, sizeof (on)) < 0) { 2800 logerror("setsockopt"); 2801 close(*s); 2802 continue; 2803 } 2804 } 2805#endif 2806 if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, 2807 (char *)&on, sizeof (on)) < 0) { 2808 logerror("setsockopt"); 2809 close(*s); 2810 continue; 2811 } 2812 /* 2813 * RFC 3164 recommends that client side message 2814 * should come from the privileged syslogd port. 2815 * 2816 * If the system administrator choose not to obey 2817 * this, we can skip the bind() step so that the 2818 * system will choose a port for us. 2819 */ 2820 if (!NoBind) { 2821 if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 2822 logerror("bind"); 2823 close(*s); 2824 continue; 2825 } 2826 2827 if (!SecureMode) 2828 increase_rcvbuf(*s); 2829 } 2830 2831 (*socks)++; 2832 s++; 2833 } 2834 2835 if (*socks == 0) { 2836 free(socks); 2837 if (Debug) 2838 return (NULL); 2839 else 2840 die(0); 2841 } 2842 if (res) 2843 freeaddrinfo(res); 2844 2845 return (socks); 2846} 2847 2848static void 2849increase_rcvbuf(int fd) 2850{ 2851 socklen_t len, slen; 2852 2853 slen = sizeof(len); 2854 2855 if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, &slen) == 0) { 2856 if (len < RCVBUF_MINSIZE) { 2857 len = RCVBUF_MINSIZE; 2858 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); 2859 } 2860 } 2861} 2862