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