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