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