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