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