syslogd.c revision 17245
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/* 39static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 40*/ 41static const char rcsid[] = 42 "$Id: syslogd.c,v 1.8 1995/11/14 23:39:39 peter Exp $"; 43#endif /* not lint */ 44 45/* 46 * syslogd -- log system messages 47 * 48 * This program implements a system log. It takes a series of lines. 49 * Each line may have a priority, signified as "<n>" as 50 * the first characters of the line. If this is 51 * not present, a default priority is used. 52 * 53 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will 54 * cause it to reread its configuration file. 55 * 56 * Defined Constants: 57 * 58 * MAXLINE -- the maximimum line length that can be handled. 59 * DEFUPRI -- the default priority for user messages 60 * DEFSPRI -- the default priority for kernel messages 61 * 62 * Author: Eric Allman 63 * extensive changes by Ralph Campbell 64 * more extensive changes by Eric Allman (again) 65 * Extension to log by program name as well as facility and priority 66 * by Peter da Silva. 67 */ 68 69#define MAXLINE 1024 /* maximum line length */ 70#define MAXSVLINE 120 /* maximum saved line length */ 71#define DEFUPRI (LOG_USER|LOG_NOTICE) 72#define DEFSPRI (LOG_KERN|LOG_CRIT) 73#define TIMERINTVL 30 /* interval for checking flush, mark */ 74#define TTYMSGTIME 1 /* timeout passed to ttymsg */ 75 76#include <sys/param.h> 77#include <sys/ioctl.h> 78#include <sys/stat.h> 79#include <sys/wait.h> 80#include <sys/socket.h> 81#include <sys/msgbuf.h> 82#include <sys/uio.h> 83#include <sys/un.h> 84#include <sys/time.h> 85#include <sys/resource.h> 86#include <sys/syslimits.h> 87#include <paths.h> 88 89#include <netinet/in.h> 90#include <netdb.h> 91#include <arpa/inet.h> 92 93#include <ctype.h> 94#include <errno.h> 95#include <fcntl.h> 96#include <setjmp.h> 97#include <signal.h> 98#include <stdio.h> 99#include <stdlib.h> 100#include <string.h> 101#include <unistd.h> 102#include <utmp.h> 103#include "pathnames.h" 104 105#define SYSLOG_NAMES 106#include <sys/syslog.h> 107 108const char *LogName = _PATH_LOG; 109const char *ConfFile = _PATH_LOGCONF; 110const char *PidFile = _PATH_LOGPID; 111const char ctty[] = _PATH_CONSOLE; 112 113#define FDMASK(fd) (1 << (fd)) 114 115#define dprintf if (Debug) printf 116 117#define MAXUNAMES 20 /* maximum number of user names */ 118 119/* 120 * Flags to logmsg(). 121 */ 122 123#define IGN_CONS 0x001 /* don't print on console */ 124#define SYNC_FILE 0x002 /* do fsync on file after printing */ 125#define ADDDATE 0x004 /* add a date to the message */ 126#define MARK 0x008 /* this message is a mark */ 127 128/* 129 * This structure represents the files that will have log 130 * copies printed. 131 */ 132 133struct filed { 134 struct filed *f_next; /* next in linked list */ 135 short f_type; /* entry type, see below */ 136 short f_file; /* file descriptor */ 137 time_t f_time; /* time this was last written */ 138 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 139 char *f_program; /* program this applies to */ 140 union { 141 char f_uname[MAXUNAMES][UT_NAMESIZE+1]; 142 struct { 143 char f_hname[MAXHOSTNAMELEN+1]; 144 struct sockaddr_in f_addr; 145 } f_forw; /* forwarding address */ 146 char f_fname[MAXPATHLEN]; 147 } f_un; 148 char f_prevline[MAXSVLINE]; /* last message logged */ 149 char f_lasttime[16]; /* time of last occurrence */ 150 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */ 151 int f_prevpri; /* pri of f_prevline */ 152 int f_prevlen; /* length of f_prevline */ 153 int f_prevcount; /* repetition cnt of prevline */ 154 int f_repeatcount; /* number of "repeated" msgs */ 155}; 156 157/* 158 * Intervals at which we flush out "message repeated" messages, 159 * in seconds after previous message is logged. After each flush, 160 * we move to the next interval until we reach the largest. 161 */ 162int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 163#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 164#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 165#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 166 (f)->f_repeatcount = MAXREPEAT; \ 167 } 168 169/* values for f_type */ 170#define F_UNUSED 0 /* unused entry */ 171#define F_FILE 1 /* regular file */ 172#define F_TTY 2 /* terminal */ 173#define F_CONSOLE 3 /* console terminal */ 174#define F_FORW 4 /* remote machine */ 175#define F_USERS 5 /* list of users */ 176#define F_WALL 6 /* everyone logged on */ 177 178char *TypeNames[7] = { 179 "UNUSED", "FILE", "TTY", "CONSOLE", 180 "FORW", "USERS", "WALL" 181}; 182 183struct filed *Files; 184struct filed consfile; 185 186int Debug; /* debug flag */ 187char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */ 188char *LocalDomain; /* our local domain name */ 189int InetInuse = 0; /* non-zero if INET sockets are being used */ 190int finet; /* Internet datagram socket */ 191int LogPort; /* port number for INET connections */ 192int Initialized = 0; /* set when we have initialized ourselves */ 193int MarkInterval = 20 * 60; /* interval between marks in seconds */ 194int MarkSeq = 0; /* mark sequence number */ 195int SecureMode = 0; /* when true, speak only unix domain socks */ 196 197int created_lsock = 0; /* Flag if local socket created */ 198char bootfile[MAXLINE+1]; /* booted kernel file */ 199 200void cfline __P((char *, struct filed *, char *)); 201char *cvthname __P((struct sockaddr_in *)); 202int decode __P((const char *, CODE *)); 203void die __P((int)); 204void domark __P((int)); 205void fprintlog __P((struct filed *, int, char *)); 206void init __P((int)); 207void logerror __P((const char *)); 208void logmsg __P((int, char *, char *, int)); 209void printline __P((char *, char *)); 210void printsys __P((char *)); 211void reapchild __P((int)); 212char *ttymsg __P((struct iovec *, int, char *, int)); 213void usage __P((void)); 214void wallmsg __P((struct filed *, struct iovec *)); 215 216int 217main(argc, argv) 218 int argc; 219 char *argv[]; 220{ 221 int ch, funix, i, inetm, fklog, klogm, len; 222 struct sockaddr_un sunx, fromunix; 223 struct sockaddr_in sin, frominet; 224 FILE *fp; 225 char *p, line[MSG_BSIZE + 1]; 226 227 while ((ch = getopt(argc, argv, "dsf:Im:p:")) != EOF) 228 switch(ch) { 229 case 'd': /* debug */ 230 Debug++; 231 break; 232 case 'f': /* configuration file */ 233 ConfFile = optarg; 234 break; 235 case 'm': /* mark interval */ 236 MarkInterval = atoi(optarg) * 60; 237 break; 238 case 'p': /* path */ 239 LogName = optarg; 240 break; 241 case 'I': /* backwards compatible w/FreeBSD */ 242 case 's': /* no network mode */ 243 SecureMode++; 244 break; 245 case '?': 246 default: 247 usage(); 248 } 249 if ((argc -= optind) != 0) 250 usage(); 251 252 if (!Debug) 253 (void)daemon(0, 0); 254 else 255 setlinebuf(stdout); 256 257 consfile.f_type = F_CONSOLE; 258 (void)strcpy(consfile.f_un.f_fname, ctty); 259 (void)gethostname(LocalHostName, sizeof(LocalHostName)); 260 if ((p = strchr(LocalHostName, '.')) != NULL) { 261 *p++ = '\0'; 262 LocalDomain = p; 263 } else 264 LocalDomain = ""; 265 (void)strcpy(bootfile, getbootfile()); 266 (void)signal(SIGTERM, die); 267 (void)signal(SIGINT, Debug ? die : SIG_IGN); 268 (void)signal(SIGQUIT, Debug ? die : SIG_IGN); 269 (void)signal(SIGCHLD, reapchild); 270 (void)signal(SIGALRM, domark); 271 (void)alarm(TIMERINTVL); 272 273#ifndef SUN_LEN 274#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 275#endif 276 memset(&sunx, 0, sizeof(sunx)); 277 sunx.sun_family = AF_UNIX; 278 (void)strncpy(sunx.sun_path, LogName, sizeof(sunx.sun_path)); 279 funix = socket(AF_UNIX, SOCK_DGRAM, 0); 280 if (funix < 0 || 281 bind(funix, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || 282 chmod(LogName, 0666) < 0) { 283 (void) sprintf(line, "cannot create %s", LogName); 284 logerror(line); 285 dprintf("cannot create %s (%d)\n", LogName, errno); 286 die(0); 287 } else 288 created_lsock = 1; 289 290 if (!SecureMode) 291 finet = socket(AF_INET, SOCK_DGRAM, 0); 292 else 293 finet = -1; 294 295 inetm = 0; 296 if (finet >= 0) { 297 struct servent *sp; 298 299 sp = getservbyname("syslog", "udp"); 300 if (sp == NULL) { 301 errno = 0; 302 logerror("syslog/udp: unknown service"); 303 die(0); 304 } 305 memset(&sin, 0, sizeof(sin)); 306 sin.sin_family = AF_INET; 307 sin.sin_port = LogPort = sp->s_port; 308 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 309 logerror("bind"); 310 if (!Debug) 311 die(0); 312 } else { 313 inetm = FDMASK(finet); 314 InetInuse = 1; 315 } 316 } 317 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) 318 klogm = FDMASK(fklog); 319 else { 320 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 321 klogm = 0; 322 } 323 324 /* tuck my process id away */ 325 fp = fopen(PidFile, "w"); 326 if (fp != NULL) { 327 fprintf(fp, "%d\n", getpid()); 328 (void) fclose(fp); 329 } 330 331 dprintf("off & running....\n"); 332 333 init(0); 334 (void)signal(SIGHUP, init); 335 336 for (;;) { 337 int nfds, readfds = FDMASK(funix) | inetm | klogm; 338 339 dprintf("readfds = %#x\n", readfds); 340 nfds = select(20, (fd_set *)&readfds, (fd_set *)NULL, 341 (fd_set *)NULL, (struct timeval *)NULL); 342 if (nfds == 0) 343 continue; 344 if (nfds < 0) { 345 if (errno != EINTR) 346 logerror("select"); 347 continue; 348 } 349 dprintf("got a message (%d, %#x)\n", nfds, readfds); 350 if (readfds & klogm) { 351 i = read(fklog, line, sizeof(line) - 1); 352 if (i > 0) { 353 line[i] = '\0'; 354 printsys(line); 355 } else if (i < 0 && errno != EINTR) { 356 logerror("klog"); 357 fklog = -1; 358 klogm = 0; 359 } 360 } 361 if (readfds & FDMASK(funix)) { 362 len = sizeof(fromunix); 363 i = recvfrom(funix, line, MAXLINE, 0, 364 (struct sockaddr *)&fromunix, &len); 365 if (i > 0) { 366 line[i] = '\0'; 367 printline(LocalHostName, line); 368 } else if (i < 0 && errno != EINTR) 369 logerror("recvfrom unix"); 370 } 371 if (readfds & inetm) { 372 len = sizeof(frominet); 373 i = recvfrom(finet, line, MAXLINE, 0, 374 (struct sockaddr *)&frominet, &len); 375 if (i > 0) { 376 line[i] = '\0'; 377 printline(cvthname(&frominet), line); 378 } else if (i < 0 && errno != EINTR) 379 logerror("recvfrom inet"); 380 } 381 } 382} 383 384void 385usage() 386{ 387 388 fprintf(stderr, 389 "usage: syslogd [-ds] [-f conffile] [-m markinterval]" 390 " [-p logpath]\n"); 391 exit(1); 392} 393 394/* 395 * Take a raw input line, decode the message, and print the message 396 * on the appropriate log files. 397 */ 398void 399printline(hname, msg) 400 char *hname; 401 char *msg; 402{ 403 int c, pri; 404 char *p, *q, line[MAXLINE + 1]; 405 406 /* test for special codes */ 407 pri = DEFUPRI; 408 p = msg; 409 if (*p == '<') { 410 pri = 0; 411 while (isdigit(*++p)) 412 pri = 10 * pri + (*p - '0'); 413 if (*p == '>') 414 ++p; 415 } 416 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 417 pri = DEFUPRI; 418 419 /* don't allow users to log kernel messages */ 420 if (LOG_FAC(pri) == LOG_KERN) 421 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 422 423 q = line; 424 425 while ((c = *p++ & 0177) != '\0' && 426 q < &line[sizeof(line) - 1]) 427 if (iscntrl(c)) 428 if (c == '\n') 429 *q++ = ' '; 430 else if (c == '\t') 431 *q++ = '\t'; 432 else { 433 *q++ = '^'; 434 *q++ = c ^ 0100; 435 } 436 else 437 *q++ = c; 438 *q = '\0'; 439 440 logmsg(pri, line, hname, 0); 441} 442 443/* 444 * Take a raw input line from /dev/klog, split and format similar to syslog(). 445 */ 446void 447printsys(msg) 448 char *msg; 449{ 450 int c, pri, flags; 451 char *lp, *p, *q, line[MAXLINE + 1]; 452 453 (void)strcpy(line, bootfile); 454 (void)strcat(line, ": "); 455 lp = line + strlen(line); 456 for (p = msg; *p != '\0'; ) { 457 flags = SYNC_FILE | ADDDATE; /* fsync file after write */ 458 pri = DEFSPRI; 459 if (*p == '<') { 460 pri = 0; 461 while (isdigit(*++p)) 462 pri = 10 * pri + (*p - '0'); 463 if (*p == '>') 464 ++p; 465 } else { 466 /* kernel printf's come out on console */ 467 flags |= IGN_CONS; 468 } 469 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 470 pri = DEFSPRI; 471 q = lp; 472 while (*p != '\0' && (c = *p++) != '\n' && 473 q < &line[MAXLINE]) 474 *q++ = c; 475 *q = '\0'; 476 logmsg(pri, line, LocalHostName, flags); 477 } 478} 479 480time_t now; 481 482/* 483 * Log a message to the appropriate log files, users, etc. based on 484 * the priority. 485 */ 486void 487logmsg(pri, msg, from, flags) 488 int pri; 489 char *msg, *from; 490 int flags; 491{ 492 struct filed *f; 493 int fac, msglen, omask, prilev; 494 char *timestamp; 495 char prog[NAME_MAX+1]; 496 int i; 497 498 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 499 pri, flags, from, msg); 500 501 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 502 503 /* 504 * Check to see if msg looks non-standard. 505 */ 506 msglen = strlen(msg); 507 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 508 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 509 flags |= ADDDATE; 510 511 (void)time(&now); 512 if (flags & ADDDATE) 513 timestamp = ctime(&now) + 4; 514 else { 515 timestamp = msg; 516 msg += 16; 517 msglen -= 16; 518 } 519 520 /* skip leading blanks */ 521 while(isspace(*msg)) { 522 msg++; 523 msglen--; 524 } 525 526 /* extract facility and priority level */ 527 if (flags & MARK) 528 fac = LOG_NFACILITIES; 529 else 530 fac = LOG_FAC(pri); 531 prilev = LOG_PRI(pri); 532 533 /* extract program name */ 534 for(i = 0; i < NAME_MAX; i++) { 535 if(!isalnum(msg[i])) 536 break; 537 prog[i] = msg[i]; 538 } 539 prog[i] = 0; 540 541 /* log the message to the particular outputs */ 542 if (!Initialized) { 543 f = &consfile; 544 f->f_file = open(ctty, O_WRONLY, 0); 545 546 if (f->f_file >= 0) { 547 fprintlog(f, flags, msg); 548 (void)close(f->f_file); 549 } 550 (void)sigsetmask(omask); 551 return; 552 } 553 for (f = Files; f; f = f->f_next) { 554 /* skip messages that are incorrect priority */ 555 if (f->f_pmask[fac] < prilev || 556 f->f_pmask[fac] == INTERNAL_NOPRI) 557 continue; 558 /* skip messages with the incorrect program name */ 559 if(f->f_program) 560 if(strcmp(prog, f->f_program) != 0) 561 continue; 562 563 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 564 continue; 565 566 /* don't output marks to recently written files */ 567 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 568 continue; 569 570 /* 571 * suppress duplicate lines to this file 572 */ 573 if ((flags & MARK) == 0 && msglen == f->f_prevlen && 574 !strcmp(msg, f->f_prevline) && 575 !strcmp(from, f->f_prevhost)) { 576 (void)strncpy(f->f_lasttime, timestamp, 15); 577 f->f_prevcount++; 578 dprintf("msg repeated %d times, %ld sec of %d\n", 579 f->f_prevcount, now - f->f_time, 580 repeatinterval[f->f_repeatcount]); 581 /* 582 * If domark would have logged this by now, 583 * flush it now (so we don't hold isolated messages), 584 * but back off so we'll flush less often 585 * in the future. 586 */ 587 if (now > REPEATTIME(f)) { 588 fprintlog(f, flags, (char *)NULL); 589 BACKOFF(f); 590 } 591 } else { 592 /* new line, save it */ 593 if (f->f_prevcount) 594 fprintlog(f, 0, (char *)NULL); 595 f->f_repeatcount = 0; 596 f->f_prevpri = pri; 597 (void)strncpy(f->f_lasttime, timestamp, 15); 598 (void)strncpy(f->f_prevhost, from, 599 sizeof(f->f_prevhost)); 600 if (msglen < MAXSVLINE) { 601 f->f_prevlen = msglen; 602 (void)strcpy(f->f_prevline, msg); 603 fprintlog(f, flags, (char *)NULL); 604 } else { 605 f->f_prevline[0] = 0; 606 f->f_prevlen = 0; 607 fprintlog(f, flags, msg); 608 } 609 } 610 } 611 (void)sigsetmask(omask); 612} 613 614void 615fprintlog(f, flags, msg) 616 struct filed *f; 617 int flags; 618 char *msg; 619{ 620 struct iovec iov[6]; 621 struct iovec *v; 622 int l; 623 char line[MAXLINE + 1], repbuf[80], greetings[200]; 624 625 v = iov; 626 if (f->f_type == F_WALL) { 627 v->iov_base = greetings; 628 v->iov_len = sprintf(greetings, 629 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 630 f->f_prevhost, ctime(&now)); 631 v++; 632 v->iov_base = ""; 633 v->iov_len = 0; 634 v++; 635 } else { 636 v->iov_base = f->f_lasttime; 637 v->iov_len = 15; 638 v++; 639 v->iov_base = " "; 640 v->iov_len = 1; 641 v++; 642 } 643 v->iov_base = f->f_prevhost; 644 v->iov_len = strlen(v->iov_base); 645 v++; 646 v->iov_base = " "; 647 v->iov_len = 1; 648 v++; 649 650 if (msg) { 651 v->iov_base = msg; 652 v->iov_len = strlen(msg); 653 } else if (f->f_prevcount > 1) { 654 v->iov_base = repbuf; 655 v->iov_len = sprintf(repbuf, "last message repeated %d times", 656 f->f_prevcount); 657 } else { 658 v->iov_base = f->f_prevline; 659 v->iov_len = f->f_prevlen; 660 } 661 v++; 662 663 dprintf("Logging to %s", TypeNames[f->f_type]); 664 f->f_time = now; 665 666 switch (f->f_type) { 667 case F_UNUSED: 668 dprintf("\n"); 669 break; 670 671 case F_FORW: 672 dprintf(" %s\n", f->f_un.f_forw.f_hname); 673 l = sprintf(line, "<%d>%.15s %s", f->f_prevpri, 674 iov[0].iov_base, iov[4].iov_base); 675 if (l > MAXLINE) 676 l = MAXLINE; 677 if ((finet >= 0) && 678 (sendto(finet, line, l, 0, 679 (struct sockaddr *)&f->f_un.f_forw.f_addr, 680 sizeof(f->f_un.f_forw.f_addr)) != l)) { 681 int e = errno; 682 (void)close(f->f_file); 683 f->f_type = F_UNUSED; 684 errno = e; 685 logerror("sendto"); 686 } 687 break; 688 689 case F_CONSOLE: 690 if (flags & IGN_CONS) { 691 dprintf(" (ignored)\n"); 692 break; 693 } 694 /* FALLTHROUGH */ 695 696 case F_TTY: 697 case F_FILE: 698 dprintf(" %s\n", f->f_un.f_fname); 699 if (f->f_type != F_FILE) { 700 v->iov_base = "\r\n"; 701 v->iov_len = 2; 702 } else { 703 v->iov_base = "\n"; 704 v->iov_len = 1; 705 } 706 again: 707 if (writev(f->f_file, iov, 6) < 0) { 708 int e = errno; 709 (void)close(f->f_file); 710 /* 711 * Check for errors on TTY's due to loss of tty 712 */ 713 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) { 714 f->f_file = open(f->f_un.f_fname, 715 O_WRONLY|O_APPEND, 0); 716 if (f->f_file < 0) { 717 f->f_type = F_UNUSED; 718 logerror(f->f_un.f_fname); 719 } else 720 goto again; 721 } else { 722 f->f_type = F_UNUSED; 723 errno = e; 724 logerror(f->f_un.f_fname); 725 } 726 } else if (flags & SYNC_FILE) 727 (void)fsync(f->f_file); 728 break; 729 730 case F_USERS: 731 case F_WALL: 732 dprintf("\n"); 733 v->iov_base = "\r\n"; 734 v->iov_len = 2; 735 wallmsg(f, iov); 736 break; 737 } 738 f->f_prevcount = 0; 739} 740 741/* 742 * WALLMSG -- Write a message to the world at large 743 * 744 * Write the specified message to either the entire 745 * world, or a list of approved users. 746 */ 747void 748wallmsg(f, iov) 749 struct filed *f; 750 struct iovec *iov; 751{ 752 static int reenter; /* avoid calling ourselves */ 753 FILE *uf; 754 struct utmp ut; 755 int i; 756 char *p; 757 char line[sizeof(ut.ut_line) + 1]; 758 759 if (reenter++) 760 return; 761 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) { 762 logerror(_PATH_UTMP); 763 reenter = 0; 764 return; 765 } 766 /* NOSTRICT */ 767 while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) { 768 if (ut.ut_name[0] == '\0') 769 continue; 770 strncpy(line, ut.ut_line, sizeof(ut.ut_line)); 771 line[sizeof(ut.ut_line)] = '\0'; 772 if (f->f_type == F_WALL) { 773 if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) { 774 errno = 0; /* already in msg */ 775 logerror(p); 776 } 777 continue; 778 } 779 /* should we send the message to this user? */ 780 for (i = 0; i < MAXUNAMES; i++) { 781 if (!f->f_un.f_uname[i][0]) 782 break; 783 if (!strncmp(f->f_un.f_uname[i], ut.ut_name, 784 UT_NAMESIZE)) { 785 if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) 786 != NULL) { 787 errno = 0; /* already in msg */ 788 logerror(p); 789 } 790 break; 791 } 792 } 793 } 794 (void)fclose(uf); 795 reenter = 0; 796} 797 798void 799reapchild(signo) 800 int signo; 801{ 802 union wait status; 803 804 while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0) 805 ; 806} 807 808/* 809 * Return a printable representation of a host address. 810 */ 811char * 812cvthname(f) 813 struct sockaddr_in *f; 814{ 815 struct hostent *hp; 816 char *p; 817 818 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr)); 819 820 if (f->sin_family != AF_INET) { 821 dprintf("Malformed from address\n"); 822 return ("???"); 823 } 824 hp = gethostbyaddr((char *)&f->sin_addr, 825 sizeof(struct in_addr), f->sin_family); 826 if (hp == 0) { 827 dprintf("Host name for your address (%s) unknown\n", 828 inet_ntoa(f->sin_addr)); 829 return (inet_ntoa(f->sin_addr)); 830 } 831 if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0) 832 *p = '\0'; 833 return (hp->h_name); 834} 835 836void 837domark(signo) 838 int signo; 839{ 840 struct filed *f; 841 842 now = time((time_t *)NULL); 843 MarkSeq += TIMERINTVL; 844 if (MarkSeq >= MarkInterval) { 845 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); 846 MarkSeq = 0; 847 } 848 849 for (f = Files; f; f = f->f_next) { 850 if (f->f_prevcount && now >= REPEATTIME(f)) { 851 dprintf("flush %s: repeated %d times, %d sec.\n", 852 TypeNames[f->f_type], f->f_prevcount, 853 repeatinterval[f->f_repeatcount]); 854 fprintlog(f, 0, (char *)NULL); 855 BACKOFF(f); 856 } 857 } 858 (void)alarm(TIMERINTVL); 859} 860 861/* 862 * Print syslogd errors some place. 863 */ 864void 865logerror(type) 866 const char *type; 867{ 868 char buf[100]; 869 870 if (errno) 871 (void)snprintf(buf, 872 sizeof(buf), "syslogd: %s: %s", type, strerror(errno)); 873 else 874 (void)snprintf(buf, sizeof(buf), "syslogd: %s", type); 875 errno = 0; 876 dprintf("%s\n", buf); 877 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 878} 879 880void 881die(signo) 882 int signo; 883{ 884 struct filed *f; 885 char buf[100]; 886 887 for (f = Files; f != NULL; f = f->f_next) { 888 /* flush any pending output */ 889 if (f->f_prevcount) 890 fprintlog(f, 0, (char *)NULL); 891 } 892 if (signo) { 893 dprintf("syslogd: exiting on signal %d\n", signo); 894 (void)sprintf(buf, "exiting on signal %d", signo); 895 errno = 0; 896 logerror(buf); 897 } 898 if (created_lsock) 899 (void)unlink(LogName); 900 exit(0); 901} 902 903/* 904 * INIT -- Initialize syslogd from configuration table 905 */ 906void 907init(signo) 908 int signo; 909{ 910 int i; 911 FILE *cf; 912 struct filed *f, *next, **nextp; 913 char *p; 914 char cline[LINE_MAX]; 915 char prog[NAME_MAX+1]; 916 917 dprintf("init\n"); 918 919 /* 920 * Close all open log files. 921 */ 922 Initialized = 0; 923 for (f = Files; f != NULL; f = next) { 924 /* flush any pending output */ 925 if (f->f_prevcount) 926 fprintlog(f, 0, (char *)NULL); 927 928 switch (f->f_type) { 929 case F_FILE: 930 case F_TTY: 931 case F_CONSOLE: 932 case F_FORW: 933 (void)close(f->f_file); 934 break; 935 } 936 next = f->f_next; 937 if(f->f_program) free(f->f_program); 938 free((char *)f); 939 } 940 Files = NULL; 941 nextp = &Files; 942 943 /* open the configuration file */ 944 if ((cf = fopen(ConfFile, "r")) == NULL) { 945 dprintf("cannot open %s\n", ConfFile); 946 *nextp = (struct filed *)calloc(1, sizeof(*f)); 947 cfline("*.ERR\t/dev/console", *nextp, "*"); 948 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 949 cfline("*.PANIC\t*", (*nextp)->f_next, "*"); 950 Initialized = 1; 951 return; 952 } 953 954 /* 955 * Foreach line in the conf table, open that file. 956 */ 957 f = NULL; 958 strcpy(prog, "*"); 959 while (fgets(cline, sizeof(cline), cf) != NULL) { 960 /* 961 * check for end-of-section, comments, strip off trailing 962 * spaces and newline character. #!prog is treated specially: 963 * following lines apply only to that program. 964 */ 965 for (p = cline; isspace(*p); ++p) 966 continue; 967 if (*p == 0) 968 continue; 969 if(*p == '#') { 970 p++; 971 if(*p!='!') 972 continue; 973 } 974 if(*p=='!') { 975 p++; 976 while(isspace(*p)) p++; 977 if(!*p) { 978 strcpy(prog, "*"); 979 continue; 980 } 981 for(i = 0; i < NAME_MAX; i++) { 982 if(!isalnum(p[i])) 983 break; 984 prog[i] = p[i]; 985 } 986 prog[i] = 0; 987 continue; 988 } 989 for (p = strchr(cline, '\0'); isspace(*--p);) 990 continue; 991 *++p = '\0'; 992 f = (struct filed *)calloc(1, sizeof(*f)); 993 *nextp = f; 994 nextp = &f->f_next; 995 cfline(cline, f, prog); 996 } 997 998 /* close the configuration file */ 999 (void)fclose(cf); 1000 1001 Initialized = 1; 1002 1003 if (Debug) { 1004 for (f = Files; f; f = f->f_next) { 1005 for (i = 0; i <= LOG_NFACILITIES; i++) 1006 if (f->f_pmask[i] == INTERNAL_NOPRI) 1007 printf("X "); 1008 else 1009 printf("%d ", f->f_pmask[i]); 1010 printf("%s: ", TypeNames[f->f_type]); 1011 switch (f->f_type) { 1012 case F_FILE: 1013 case F_TTY: 1014 case F_CONSOLE: 1015 printf("%s", f->f_un.f_fname); 1016 break; 1017 1018 case F_FORW: 1019 printf("%s", f->f_un.f_forw.f_hname); 1020 break; 1021 1022 case F_USERS: 1023 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1024 printf("%s, ", f->f_un.f_uname[i]); 1025 break; 1026 } 1027 if(f->f_program) { 1028 printf(" (%s)", f->f_program); 1029 } 1030 printf("\n"); 1031 } 1032 } 1033 1034 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1035 dprintf("syslogd: restarted\n"); 1036} 1037 1038/* 1039 * Crack a configuration file line 1040 */ 1041void 1042cfline(line, f, prog) 1043 char *line; 1044 struct filed *f; 1045 char *prog; 1046{ 1047 struct hostent *hp; 1048 int i, pri; 1049 char *bp, *p, *q; 1050 char buf[MAXLINE], ebuf[100]; 1051 1052 dprintf("cfline(\"%s\", f, \"%s\")\n", line, prog); 1053 1054 errno = 0; /* keep strerror() stuff out of logerror messages */ 1055 1056 /* clear out file entry */ 1057 memset(f, 0, sizeof(*f)); 1058 for (i = 0; i <= LOG_NFACILITIES; i++) 1059 f->f_pmask[i] = INTERNAL_NOPRI; 1060 1061 /* save program name if any */ 1062 if(prog && *prog=='*') prog = NULL; 1063 if(prog) { 1064 f->f_program = calloc(1, strlen(prog)+1); 1065 if(f->f_program) { 1066 strcpy(f->f_program, prog); 1067 } 1068 } 1069 1070 /* scan through the list of selectors */ 1071 for (p = line; *p && *p != '\t';) { 1072 1073 /* find the end of this facility name list */ 1074 for (q = p; *q && *q != '\t' && *q++ != '.'; ) 1075 continue; 1076 1077 /* collect priority name */ 1078 for (bp = buf; *q && !strchr("\t,;", *q); ) 1079 *bp++ = *q++; 1080 *bp = '\0'; 1081 1082 /* skip cruft */ 1083 while (strchr(", ;", *q)) 1084 q++; 1085 1086 /* decode priority name */ 1087 if (*buf == '*') 1088 pri = LOG_PRIMASK + 1; 1089 else { 1090 pri = decode(buf, prioritynames); 1091 if (pri < 0) { 1092 (void)sprintf(ebuf, 1093 "unknown priority name \"%s\"", buf); 1094 logerror(ebuf); 1095 return; 1096 } 1097 } 1098 1099 /* scan facilities */ 1100 while (*p && !strchr("\t.;", *p)) { 1101 for (bp = buf; *p && !strchr("\t,;.", *p); ) 1102 *bp++ = *p++; 1103 *bp = '\0'; 1104 if (*buf == '*') 1105 for (i = 0; i < LOG_NFACILITIES; i++) 1106 f->f_pmask[i] = pri; 1107 else { 1108 i = decode(buf, facilitynames); 1109 if (i < 0) { 1110 (void)sprintf(ebuf, 1111 "unknown facility name \"%s\"", 1112 buf); 1113 logerror(ebuf); 1114 return; 1115 } 1116 f->f_pmask[i >> 3] = pri; 1117 } 1118 while (*p == ',' || *p == ' ') 1119 p++; 1120 } 1121 1122 p = q; 1123 } 1124 1125 /* skip to action part */ 1126 while (*p == '\t') 1127 p++; 1128 1129 switch (*p) 1130 { 1131 case '@': 1132 if (!InetInuse) 1133 break; 1134 (void)strcpy(f->f_un.f_forw.f_hname, ++p); 1135 hp = gethostbyname(p); 1136 if (hp == NULL) { 1137 extern int h_errno; 1138 1139 logerror(hstrerror(h_errno)); 1140 break; 1141 } 1142 memset(&f->f_un.f_forw.f_addr, 0, 1143 sizeof(f->f_un.f_forw.f_addr)); 1144 f->f_un.f_forw.f_addr.sin_family = AF_INET; 1145 f->f_un.f_forw.f_addr.sin_port = LogPort; 1146 memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length); 1147 f->f_type = F_FORW; 1148 break; 1149 1150 case '/': 1151 (void)strcpy(f->f_un.f_fname, p); 1152 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { 1153 f->f_file = F_UNUSED; 1154 logerror(p); 1155 break; 1156 } 1157 if (isatty(f->f_file)) 1158 f->f_type = F_TTY; 1159 else 1160 f->f_type = F_FILE; 1161 if (strcmp(p, ctty) == 0) 1162 f->f_type = F_CONSOLE; 1163 break; 1164 1165 case '*': 1166 f->f_type = F_WALL; 1167 break; 1168 1169 default: 1170 for (i = 0; i < MAXUNAMES && *p; i++) { 1171 for (q = p; *q && *q != ','; ) 1172 q++; 1173 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1174 if ((q - p) > UT_NAMESIZE) 1175 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1176 else 1177 f->f_un.f_uname[i][q - p] = '\0'; 1178 while (*q == ',' || *q == ' ') 1179 q++; 1180 p = q; 1181 } 1182 f->f_type = F_USERS; 1183 break; 1184 } 1185} 1186 1187 1188/* 1189 * Decode a symbolic name to a numeric value 1190 */ 1191int 1192decode(name, codetab) 1193 const char *name; 1194 CODE *codetab; 1195{ 1196 CODE *c; 1197 char *p, buf[40]; 1198 1199 if (isdigit(*name)) 1200 return (atoi(name)); 1201 1202 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1203 if (isupper(*name)) 1204 *p = tolower(*name); 1205 else 1206 *p = *name; 1207 } 1208 *p = '\0'; 1209 for (c = codetab; c->c_name; c++) 1210 if (!strcmp(buf, c->c_name)) 1211 return (c->c_val); 1212 1213 return (-1); 1214} 1215