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