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