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