1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1983, 1988, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)syslogd.c 8.2 (Berkeley) 11/16/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) != 0) 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, '.')) != NULL) { 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 char line[sizeof(ut.ut_line) + 1]; 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 strncpy(line, ut.ut_line, sizeof(ut.ut_line)); 710 line[sizeof(ut.ut_line)] = '\0'; 711 if (f->f_type == F_WALL) { 712 if ((p = ttymsg(iov, 6, line, 60*5)) != NULL) { 713 errno = 0; /* already in msg */ 714 logerror(p); 715 } 716 continue; 717 } 718 /* should we send the message to this user? */ 719 for (i = 0; i < MAXUNAMES; i++) { 720 if (!f->f_un.f_uname[i][0]) 721 break; 722 if (!strncmp(f->f_un.f_uname[i], ut.ut_name, 723 UT_NAMESIZE)) { 724 if ((p = ttymsg(iov, 6, line, 60*5)) != NULL) { 725 errno = 0; /* already in msg */ 726 logerror(p); 727 } 728 break; 729 } 730 } 731 } 732 (void)fclose(uf); 733 reenter = 0; 734 } 735 736 void 737 reapchild(signo) 738 int signo; 739 { 740 union wait status; 741 742 while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0) 743 ; 744 } 745 746 /* 747 * Return a printable representation of a host address. 748 */ 749 char * 750 cvthname(f) 751 struct sockaddr_in *f; 752 { 753 struct hostent *hp; 754 register char *p; 755 756 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr)); 757 758 if (f->sin_family != AF_INET) { 759 dprintf("Malformed from address\n"); 760 return ("???"); 761 } 762 hp = gethostbyaddr((char *)&f->sin_addr, 763 sizeof(struct in_addr), f->sin_family); 764 if (hp == 0) { 765 dprintf("Host name for your address (%s) unknown\n", 766 inet_ntoa(f->sin_addr)); 767 return (inet_ntoa(f->sin_addr)); 768 } 769 if ((p = index(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0) 770 *p = '\0'; 771 return (hp->h_name); 772 } 773 774 void 775 domark(signo) 776 int signo; 777 { 778 register struct filed *f; 779 780 now = time((time_t *)NULL); 781 MarkSeq += TIMERINTVL; 782 if (MarkSeq >= MarkInterval) { 783 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); 784 MarkSeq = 0; 785 } 786 787 for (f = Files; f; f = f->f_next) { 788 if (f->f_prevcount && now >= REPEATTIME(f)) { 789 dprintf("flush %s: repeated %d times, %d sec.\n", 790 TypeNames[f->f_type], f->f_prevcount, 791 repeatinterval[f->f_repeatcount]); 792 fprintlog(f, 0, (char *)NULL); 793 BACKOFF(f); 794 } 795 } 796 (void)alarm(TIMERINTVL); 797 } 798 799 /* 800 * Print syslogd errors some place. 801 */ 802 void 803 logerror(type) 804 char *type; 805 { 806 char buf[100]; 807 808 if (errno) 809 (void)snprintf(buf, 810 sizeof(buf), "syslogd: %s: %s", type, strerror(errno)); 811 else 812 (void)snprintf(buf, sizeof(buf), "syslogd: %s", type); 813 errno = 0; 814 dprintf("%s\n", buf); 815 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 816 } 817 818 void 819 die(signo) 820 int signo; 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 (signo) { 831 dprintf("syslogd: exiting on signal %d\n", signo); 832 (void)sprintf(buf, "exiting on signal %d", signo); 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 void 844 init(signo) 845 int signo; 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 void 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], ebuf[100]; 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 if (*buf == '*') 989 pri = LOG_PRIMASK + 1; 990 else { 991 pri = decode(buf, prioritynames); 992 if (pri < 0) { 993 (void)sprintf(ebuf, 994 "unknown priority name \"%s\"", buf); 995 logerror(ebuf); 996 return; 997 } 998 } 999 1000 /* scan facilities */ 1001 while (*p && !index("\t.;", *p)) { 1002 for (bp = buf; *p && !index("\t,;.", *p); ) 1003 *bp++ = *p++; 1004 *bp = '\0'; 1005 if (*buf == '*') 1006 for (i = 0; i < LOG_NFACILITIES; i++) 1007 f->f_pmask[i] = pri; 1008 else { 1009 i = decode(buf, facilitynames); 1010 if (i < 0) { 1011 (void)sprintf(ebuf, 1012 "unknown facility name \"%s\"", 1013 buf); 1014 logerror(ebuf); 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 else 1063 f->f_type = F_FILE; 1064 if (strcmp(p, ctty) == 0) 1065 f->f_type = F_CONSOLE; 1066 break; 1067 1068 case '*': 1069 f->f_type = F_WALL; 1070 break; 1071 1072 default: 1073 for (i = 0; i < MAXUNAMES && *p; i++) { 1074 for (q = p; *q && *q != ','; ) 1075 q++; 1076 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1077 if ((q - p) > UT_NAMESIZE) 1078 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1079 else 1080 f->f_un.f_uname[i][q - p] = '\0'; 1081 while (*q == ',' || *q == ' ') 1082 q++; 1083 p = q; 1084 } 1085 f->f_type = F_USERS; 1086 break; 1087 } 1088 } 1089 1090 1091 /* 1092 * Decode a symbolic name to a numeric value 1093 */ 1094 int 1095 decode(name, codetab) 1096 const char *name; 1097 CODE *codetab; 1098 { 1099 register CODE *c; 1100 register char *p; 1101 char buf[40]; 1102 1103 if (isdigit(*name)) 1104 return (atoi(name)); 1105 1106 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1107 if (isupper(*name)) 1108 *p = tolower(*name); 1109 else 1110 *p = *name; 1111 } 1112 *p = '\0'; 1113 for (c = codetab; c->c_name; c++) 1114 if (!strcmp(buf, c->c_name)) 1115 return (c->c_val); 1116 1117 return (-1); 1118 } 1119