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