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