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