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