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