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