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