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