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 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 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 * Extension to log by program name as well as facility and priority 69 * by Peter da Silva. 70 * -u and -v by Harlan Stenn. 71 * Priority comparison code by Harlan Stenn. 72 */ 73 74 #define MAXLINE 1024 /* maximum line length */ 75 #define MAXSVLINE 120 /* maximum saved line length */ 76 #define DEFUPRI (LOG_USER|LOG_NOTICE) 77 #define DEFSPRI (LOG_KERN|LOG_CRIT) 78 #define TIMERINTVL 30 /* interval for checking flush, mark */ 79 #define TTYMSGTIME 1 /* timed out passed to ttymsg */ 80 81 #include <sys/param.h> 82 #include <sys/ioctl.h> 83 #include <sys/stat.h> 84 #include <sys/wait.h> 85 #include <sys/socket.h> 86 #include <sys/queue.h> 87 #include <sys/uio.h> 88 #include <sys/un.h> 89 #include <sys/time.h> 90 #include <sys/resource.h> 91 #include <sys/syslimits.h> 92 #include <paths.h> 93 94 #include <netinet/in.h> 95 #include <netdb.h> 96 #include <arpa/inet.h> 97 98 #include <ctype.h> 99 #include <err.h> 100 #include <errno.h> 101 #include <fcntl.h> 102 #include <regex.h> 103 #include <setjmp.h> 104 #include <signal.h> 105 #include <stdio.h> 106 #include <stdlib.h> 107 #include <string.h> 108 #include <sysexits.h> 109 #include <unistd.h> 110 #include <utmp.h> 111 #include "pathnames.h" 112 113 #define SYSLOG_NAMES 114 #include <sys/syslog.h> 115 116 const char *ConfFile = _PATH_LOGCONF; 117 const char *PidFile = _PATH_LOGPID; 118 const char ctty[] = _PATH_CONSOLE; 119 120 #define dprintf if (Debug) printf 121 122 #define MAXUNAMES 20 /* maximum number of user names */ 123 124 #define MAXFUNIX 20 125 126 int nfunix = 1; 127 char *funixn[MAXFUNIX] = { _PATH_LOG }; 128 int funix[MAXFUNIX]; 129 130 /* 131 * Flags to logmsg(). 132 */ 133 134 #define IGN_CONS 0x001 /* don't print on console */ 135 #define SYNC_FILE 0x002 /* do fsync on file after printing */ 136 #define ADDDATE 0x004 /* add a date to the message */ 137 #define MARK 0x008 /* this message is a mark */ 138 #define ISKERNEL 0x010 /* kernel generated message */ 139 140 /* 141 * This structure represents the files that will have log 142 * copies printed. 143 */ 144 145 struct filed { 146 struct filed *f_next; /* next in linked list */ 147 short f_type; /* entry type, see below */ 148 short f_file; /* file descriptor */ 149 time_t f_time; /* time this was last written */ 150 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 151 u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ 152 #define PRI_LT 0x1 153 #define PRI_EQ 0x2 154 #define PRI_GT 0x4 155 char *f_program; /* program this applies to */ 156 union { 157 char f_uname[MAXUNAMES][UT_NAMESIZE+1]; 158 struct { 159 char f_hname[MAXHOSTNAMELEN+1]; 160 struct sockaddr_in f_addr; 161 } f_forw; /* forwarding address */ 162 char f_fname[MAXPATHLEN]; 163 struct { 164 char f_pname[MAXPATHLEN]; 165 pid_t f_pid; 166 } f_pipe; 167 } f_un; 168 char f_prevline[MAXSVLINE]; /* last message logged */ 169 char f_lasttime[16]; /* time of last occurrence */ 170 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */ 171 int f_prevpri; /* pri of f_prevline */ 172 int f_prevlen; /* length of f_prevline */ 173 int f_prevcount; /* repetition cnt of prevline */ 174 int f_repeatcount; /* number of "repeated" msgs */ 175 }; 176 177 /* 178 * Queue of about-to-be dead processes we should watch out for. 179 */ 180 181 TAILQ_HEAD(stailhead, deadq_entry) deadq_head; 182 struct stailhead *deadq_headp; 183 184 struct deadq_entry { 185 pid_t dq_pid; 186 int dq_timeout; 187 TAILQ_ENTRY(deadq_entry) dq_entries; 188 }; 189 190 /* 191 * The timeout to apply to processes waiting on the dead queue. Unit 192 * of measure is `mark intervals', i.e. 20 minutes by default. 193 * Processes on the dead queue will be terminated after that time. 194 */ 195 196 #define DQ_TIMO_INIT 2 197 198 typedef struct deadq_entry *dq_t; 199 200 201 /* 202 * Struct to hold records of network addresses that are allowed to log 203 * to us. 204 */ 205 struct allowedpeer { 206 int isnumeric; 207 u_short port; 208 union { 209 struct { 210 struct in_addr addr; 211 struct in_addr mask; 212 } numeric; 213 char *name; 214 } u; 215 #define a_addr u.numeric.addr 216 #define a_mask u.numeric.mask 217 #define a_name u.name 218 }; 219 220 221 /* 222 * Intervals at which we flush out "message repeated" messages, 223 * in seconds after previous message is logged. After each flush, 224 * we move to the next interval until we reach the largest. 225 */ 226 int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 227 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 228 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 229 #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 230 (f)->f_repeatcount = MAXREPEAT; \ 231 } 232 233 /* values for f_type */ 234 #define F_UNUSED 0 /* unused entry */ 235 #define F_FILE 1 /* regular file */ 236 #define F_TTY 2 /* terminal */ 237 #define F_CONSOLE 3 /* console terminal */ 238 #define F_FORW 4 /* remote machine */ 239 #define F_USERS 5 /* list of users */ 240 #define F_WALL 6 /* everyone logged on */ 241 #define F_PIPE 7 /* pipe to program */ 242 243 char *TypeNames[8] = { 244 "UNUSED", "FILE", "TTY", "CONSOLE", 245 "FORW", "USERS", "WALL", "PIPE" 246 }; 247 248 struct filed *Files; 249 struct filed consfile; 250 251 int Debug; /* debug flag */ 252 char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */ 253 char *LocalDomain; /* our local domain name */ 254 int finet = -1; /* Internet datagram socket */ 255 int fklog = -1; /* /dev/klog */ 256 int LogPort; /* port number for INET connections */ 257 int Initialized = 0; /* set when we have initialized ourselves */ 258 int MarkInterval = 20 * 60; /* interval between marks in seconds */ 259 int MarkSeq = 0; /* mark sequence number */ 260 int SecureMode = 0; /* when true, receive only unix domain socks */ 261 262 char bootfile[MAXLINE+1]; /* booted kernel file */ 263 264 struct allowedpeer *AllowedPeers; 265 int NumAllowed = 0; /* # of AllowedPeer entries */ 266 267 int UniquePriority = 0; /* Only log specified priority? */ 268 int LogFacPri = 0; /* Put facility and priority in log message: */ 269 /* 0=no, 1=numeric, 2=names */ 270 271 int allowaddr __P((char *)); 272 void cfline __P((char *, struct filed *, char *)); 273 char *cvthname __P((struct sockaddr_in *)); 274 void deadq_enter __P((pid_t)); 275 int decode __P((const char *, CODE *)); 276 void die __P((int)); 277 void domark __P((int)); 278 void fprintlog __P((struct filed *, int, char *)); 279 void init __P((int)); 280 void logerror __P((const char *)); 281 void logmsg __P((int, char *, char *, int)); 282 void printline __P((char *, char *)); 283 void printsys __P((char *)); 284 int p_open __P((char *, pid_t *)); 285 void readklog __P((void)); 286 void reapchild __P((int)); 287 char *ttymsg __P((struct iovec *, int, char *, int)); 288 static void usage __P((void)); 289 int validate __P((struct sockaddr_in *, const char *)); 290 void wallmsg __P((struct filed *, struct iovec *)); 291 int waitdaemon __P((int, int, int)); 292 void timedout __P((int)); 293 294 int 295 main(argc, argv) 296 int argc; 297 char *argv[]; 298 { 299 int ch, i, l; 300 struct sockaddr_un sunx, fromunix; 301 struct sockaddr_in sin, frominet; 302 FILE *fp; 303 char *p, *hname, line[MAXLINE + 1]; 304 struct timeval tv, *tvp; 305 pid_t ppid = 1; 306 socklen_t len; 307 308 while ((ch = getopt(argc, argv, "a:dl:f:m:p:suv")) != -1) 309 switch(ch) { 310 case 'd': /* debug */ 311 Debug++; 312 break; 313 case 'a': /* allow specific network addresses only */ 314 if (allowaddr(optarg) == -1) 315 usage(); 316 break; 317 case 'f': /* configuration file */ 318 ConfFile = optarg; 319 break; 320 case 'm': /* mark interval */ 321 MarkInterval = atoi(optarg) * 60; 322 break; 323 case 'p': /* path */ 324 funixn[0] = optarg; 325 break; 326 case 's': /* no network mode */ 327 SecureMode++; 328 break; 329 case 'l': 330 if (nfunix < MAXFUNIX) 331 funixn[nfunix++] = optarg; 332 else 333 fprintf(stderr, 334 "syslogd: out of descriptors, ignoring %s\n", 335 optarg); 336 break; 337 case 'u': /* only log specified priority */ 338 UniquePriority++; 339 break; 340 case 'v': /* log facility and priority */ 341 LogFacPri++; 342 break; 343 case '?': 344 default: 345 usage(); 346 } 347 if ((argc -= optind) != 0) 348 usage(); 349 350 if (!Debug) { 351 ppid = waitdaemon(0, 0, 30); 352 if (ppid < 0) 353 err(1, "could not become daemon"); 354 } else 355 setlinebuf(stdout); 356 357 if (NumAllowed) 358 endservent(); 359 360 consfile.f_type = F_CONSOLE; 361 (void)strcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1); 362 (void)gethostname(LocalHostName, sizeof(LocalHostName)); 363 if ((p = strchr(LocalHostName, '.')) != NULL) { 364 *p++ = '\0'; 365 LocalDomain = p; 366 } else 367 LocalDomain = ""; 368 (void)strcpy(bootfile, getbootfile()); 369 (void)signal(SIGTERM, die); 370 (void)signal(SIGINT, Debug ? die : SIG_IGN); 371 (void)signal(SIGQUIT, Debug ? die : SIG_IGN); 372 (void)signal(SIGCHLD, reapchild); 373 (void)signal(SIGALRM, domark); 374 (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ 375 (void)alarm(TIMERINTVL); 376 377 TAILQ_INIT(&deadq_head); 378 379 #ifndef SUN_LEN 380 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 381 #endif 382 for (i = 0; i < nfunix; i++) { 383 memset(&sunx, 0, sizeof(sunx)); 384 sunx.sun_family = AF_UNIX; 385 (void)strncpy(sunx.sun_path, funixn[i], sizeof(sunx.sun_path)); 386 funix[i] = socket(AF_UNIX, SOCK_DGRAM, 0); 387 if (funix[i] < 0 || 388 bind(funix[i], (struct sockaddr *)&sunx, 389 SUN_LEN(&sunx)) < 0 || 390 chmod(funixn[i], 0666) < 0) { 391 (void) snprintf(line, sizeof line, 392 "cannot create %s", funixn[i]); 393 logerror(line); 394 dprintf("cannot create %s (%d)\n", funixn[i], errno); 395 if (i == 0) 396 die(0); 397 } 398 } 399 if (SecureMode <= 1) 400 finet = socket(AF_INET, SOCK_DGRAM, 0); 401 if (finet >= 0) { 402 struct servent *sp; 403 404 sp = getservbyname("syslog", "udp"); 405 if (sp == NULL) { 406 errno = 0; 407 logerror("syslog/udp: unknown service"); 408 die(0); 409 } 410 memset(&sin, 0, sizeof(sin)); 411 sin.sin_family = AF_INET; 412 sin.sin_port = LogPort = sp->s_port; 413 414 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 415 logerror("bind"); 416 if (!Debug) 417 die(0); 418 } 419 } 420 if (finet >= 0 && SecureMode) { 421 if (shutdown(finet, SHUT_RD) < 0) { 422 logerror("shutdown"); 423 if (!Debug) 424 die(0); 425 } 426 } 427 428 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) 429 if (fcntl(fklog, F_SETFL, O_NONBLOCK) < 0) 430 fklog = -1; 431 if (fklog < 0) 432 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 433 434 /* tuck my process id away */ 435 fp = fopen(PidFile, "w"); 436 if (fp != NULL) { 437 fprintf(fp, "%d\n", getpid()); 438 (void) fclose(fp); 439 } 440 441 dprintf("off & running....\n"); 442 443 init(0); 444 (void)signal(SIGHUP, init); 445 446 tvp = &tv; 447 tv.tv_sec = tv.tv_usec = 0; 448 449 for (;;) { 450 fd_set readfds; 451 int nfds = 0; 452 453 FD_ZERO(&readfds); 454 if (fklog != -1) { 455 FD_SET(fklog, &readfds); 456 if (fklog > nfds) 457 nfds = fklog; 458 } 459 if (finet != -1 && !SecureMode) { 460 FD_SET(finet, &readfds); 461 if (finet > nfds) 462 nfds = finet; 463 } 464 for (i = 0; i < nfunix; i++) { 465 if (funix[i] != -1) { 466 FD_SET(funix[i], &readfds); 467 if (funix[i] > nfds) 468 nfds = funix[i]; 469 } 470 } 471 472 /*dprintf("readfds = %#x\n", readfds);*/ 473 nfds = select(nfds+1, &readfds, (fd_set *)NULL, 474 (fd_set *)NULL, tvp); 475 if (nfds == 0) { 476 if (tvp) { 477 tvp = NULL; 478 if (ppid != 1) 479 kill(ppid, SIGALRM); 480 } 481 continue; 482 } 483 if (nfds < 0) { 484 if (errno != EINTR) 485 logerror("select"); 486 continue; 487 } 488 /*dprintf("got a message (%d, %#x)\n", nfds, readfds);*/ 489 if (fklog != -1 && FD_ISSET(fklog, &readfds)) 490 readklog(); 491 if (finet != -1 && FD_ISSET(finet, &readfds)) { 492 len = sizeof(frominet); 493 l = recvfrom(finet, line, MAXLINE, 0, 494 (struct sockaddr *)&frominet, &len); 495 if (l > 0) { 496 line[l] = '\0'; 497 hname = cvthname(&frominet); 498 if (validate(&frominet, hname)) 499 printline(hname, line); 500 } else if (l < 0 && errno != EINTR) 501 logerror("recvfrom inet"); 502 } 503 for (i = 0; i < nfunix; i++) { 504 if (funix[i] != -1 && FD_ISSET(funix[i], &readfds)) { 505 len = sizeof(fromunix); 506 l = recvfrom(funix[i], line, MAXLINE, 0, 507 (struct sockaddr *)&fromunix, &len); 508 if (l > 0) { 509 line[l] = '\0'; 510 printline(LocalHostName, line); 511 } else if (l < 0 && errno != EINTR) 512 logerror("recvfrom unix"); 513 } 514 } 515 } 516 } 517 518 static void 519 usage() 520 { 521 522 fprintf(stderr, "%s\n%s\n%s\n", 523 "usage: syslogd [-dsuv] [-a allowed_peer] [-f config_file]", 524 " [-m mark_interval] [-p log_socket]", 525 " [-l log_socket]"); 526 exit(1); 527 } 528 529 /* 530 * Take a raw input line, decode the message, and print the message 531 * on the appropriate log files. 532 */ 533 void 534 printline(hname, msg) 535 char *hname; 536 char *msg; 537 { 538 int c, pri; 539 char *p, *q, line[MAXLINE + 1]; 540 541 /* test for special codes */ 542 pri = DEFUPRI; 543 p = msg; 544 if (*p == '<') { 545 pri = 0; 546 while (isdigit(*++p)) 547 pri = 10 * pri + (*p - '0'); 548 if (*p == '>') 549 ++p; 550 } 551 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 552 pri = DEFUPRI; 553 554 /* don't allow users to log kernel messages */ 555 if (LOG_FAC(pri) == LOG_KERN) 556 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 557 558 q = line; 559 560 while ((c = *p++ & 0177) != '\0' && 561 q < &line[sizeof(line) - 1]) 562 if (iscntrl(c)) 563 if (c == '\n') 564 *q++ = ' '; 565 else if (c == '\t') 566 *q++ = '\t'; 567 else { 568 *q++ = '^'; 569 *q++ = c ^ 0100; 570 } 571 else 572 *q++ = c; 573 *q = '\0'; 574 575 logmsg(pri, line, hname, 0); 576 } 577 578 /* 579 * Read /dev/klog while data are available, split into lines. 580 */ 581 void 582 readklog() 583 { 584 char *p, *q, line[MAXLINE + 1]; 585 int len, i; 586 587 len = 0; 588 for (;;) { 589 i = read(fklog, line + len, MAXLINE - 1 - len); 590 if (i > 0) 591 line[i + len] = '\0'; 592 else if (i < 0 && errno != EINTR && errno != EAGAIN) { 593 logerror("klog"); 594 fklog = -1; 595 break; 596 } else 597 break; 598 599 for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { 600 *q = '\0'; 601 printsys(p); 602 } 603 len = strlen(p); 604 if (len >= MAXLINE - 1) { 605 printsys(p); 606 len = 0; 607 } 608 if (len > 0) 609 memmove(line, p, len + 1); 610 } 611 if (len > 0) 612 printsys(line); 613 } 614 615 /* 616 * Take a raw input line from /dev/klog, format similar to syslog(). 617 */ 618 void 619 printsys(p) 620 char *p; 621 { 622 int pri, flags; 623 624 flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */ 625 pri = DEFSPRI; 626 if (*p == '<') { 627 pri = 0; 628 while (isdigit(*++p)) 629 pri = 10 * pri + (*p - '0'); 630 if (*p == '>') 631 ++p; 632 } else { 633 /* kernel printf's come out on console */ 634 flags |= IGN_CONS; 635 } 636 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 637 pri = DEFSPRI; 638 logmsg(pri, p, LocalHostName, flags); 639 } 640 641 time_t now; 642 643 /* 644 * Log a message to the appropriate log files, users, etc. based on 645 * the priority. 646 */ 647 void 648 logmsg(pri, msg, from, flags) 649 int pri; 650 char *msg, *from; 651 int flags; 652 { 653 struct filed *f; 654 int i, fac, msglen, omask, prilev; 655 char *timestamp; 656 char prog[NAME_MAX+1]; 657 char buf[MAXLINE+1]; 658 659 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 660 pri, flags, from, msg); 661 662 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 663 664 /* 665 * Check to see if msg looks non-standard. 666 */ 667 msglen = strlen(msg); 668 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 669 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 670 flags |= ADDDATE; 671 672 (void)time(&now); 673 if (flags & ADDDATE) 674 timestamp = ctime(&now) + 4; 675 else { 676 timestamp = msg; 677 msg += 16; 678 msglen -= 16; 679 } 680 681 /* skip leading blanks */ 682 while(isspace(*msg)) { 683 msg++; 684 msglen--; 685 } 686 687 /* extract facility and priority level */ 688 if (flags & MARK) 689 fac = LOG_NFACILITIES; 690 else 691 fac = LOG_FAC(pri); 692 prilev = LOG_PRI(pri); 693 694 /* extract program name */ 695 for(i = 0; i < NAME_MAX; i++) { 696 if(!isalnum(msg[i])) 697 break; 698 prog[i] = msg[i]; 699 } 700 prog[i] = 0; 701 702 /* add kernel prefix for kernel messages */ 703 if (flags & ISKERNEL) { 704 snprintf(buf, sizeof(buf), "%s: %s", bootfile, msg); 705 msg = buf; 706 msglen = strlen(buf); 707 } 708 709 /* log the message to the particular outputs */ 710 if (!Initialized) { 711 f = &consfile; 712 f->f_file = open(ctty, O_WRONLY, 0); 713 714 if (f->f_file >= 0) { 715 fprintlog(f, flags, msg); 716 (void)close(f->f_file); 717 } 718 (void)sigsetmask(omask); 719 return; 720 } 721 for (f = Files; f; f = f->f_next) { 722 /* skip messages that are incorrect priority */ 723 if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev)) 724 ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev)) 725 ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev)) 726 ) 727 || f->f_pmask[fac] == INTERNAL_NOPRI) 728 continue; 729 /* skip messages with the incorrect program name */ 730 if(f->f_program) 731 if(strcmp(prog, f->f_program) != 0) 732 continue; 733 734 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 735 continue; 736 737 /* don't output marks to recently written files */ 738 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 739 continue; 740 741 /* 742 * suppress duplicate lines to this file 743 */ 744 if ((flags & MARK) == 0 && msglen == f->f_prevlen && 745 !strcmp(msg, f->f_prevline) && 746 !strcasecmp(from, f->f_prevhost)) { 747 (void)strncpy(f->f_lasttime, timestamp, 15); 748 f->f_prevcount++; 749 dprintf("msg repeated %d times, %ld sec of %d\n", 750 f->f_prevcount, (long)(now - f->f_time), 751 repeatinterval[f->f_repeatcount]); 752 /* 753 * If domark would have logged this by now, 754 * flush it now (so we don't hold isolated messages), 755 * but back off so we'll flush less often 756 * in the future. 757 */ 758 if (now > REPEATTIME(f)) { 759 fprintlog(f, flags, (char *)NULL); 760 BACKOFF(f); 761 } 762 } else { 763 /* new line, save it */ 764 if (f->f_prevcount) 765 fprintlog(f, 0, (char *)NULL); 766 f->f_repeatcount = 0; 767 f->f_prevpri = pri; 768 (void)strncpy(f->f_lasttime, timestamp, 15); 769 (void)strncpy(f->f_prevhost, from, 770 sizeof(f->f_prevhost)-1); 771 f->f_prevhost[sizeof(f->f_prevhost)-1] = '\0'; 772 if (msglen < MAXSVLINE) { 773 f->f_prevlen = msglen; 774 (void)strcpy(f->f_prevline, msg); 775 fprintlog(f, flags, (char *)NULL); 776 } else { 777 f->f_prevline[0] = 0; 778 f->f_prevlen = 0; 779 fprintlog(f, flags, msg); 780 } 781 } 782 } 783 (void)sigsetmask(omask); 784 } 785 786 void 787 fprintlog(f, flags, msg) 788 struct filed *f; 789 int flags; 790 char *msg; 791 { 792 struct iovec iov[7]; 793 struct iovec *v; 794 int l; 795 char line[MAXLINE + 1], repbuf[80], greetings[200]; 796 char *msgret; 797 798 v = iov; 799 if (f->f_type == F_WALL) { 800 v->iov_base = greetings; 801 v->iov_len = snprintf(greetings, sizeof greetings, 802 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 803 f->f_prevhost, ctime(&now)); 804 v++; 805 v->iov_base = ""; 806 v->iov_len = 0; 807 v++; 808 } else { 809 v->iov_base = f->f_lasttime; 810 v->iov_len = 15; 811 v++; 812 v->iov_base = " "; 813 v->iov_len = 1; 814 v++; 815 } 816 817 if (LogFacPri) { 818 static char fp_buf[30]; /* Hollow laugh */ 819 int fac = f->f_prevpri & LOG_FACMASK; 820 int pri = LOG_PRI(f->f_prevpri); 821 char *f_s = 0; 822 char f_n[5]; /* Hollow laugh */ 823 char *p_s = 0; 824 char p_n[5]; /* Hollow laugh */ 825 826 if (LogFacPri > 1) { 827 CODE *c; 828 829 for (c = facilitynames; c->c_name; c++) { 830 if (c->c_val == fac) { 831 f_s = c->c_name; 832 break; 833 } 834 } 835 for (c = prioritynames; c->c_name; c++) { 836 if (c->c_val == pri) { 837 p_s = c->c_name; 838 break; 839 } 840 } 841 } 842 if (!f_s) { 843 snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac)); 844 f_s = f_n; 845 } 846 if (!p_s) { 847 snprintf(p_n, sizeof p_n, "%d", pri); 848 p_s = p_n; 849 } 850 snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s); 851 v->iov_base = fp_buf; 852 v->iov_len = strlen(fp_buf); 853 } else { 854 v->iov_base=""; 855 v->iov_len = 0; 856 } 857 v++; 858 859 v->iov_base = f->f_prevhost; 860 v->iov_len = strlen(v->iov_base); 861 v++; 862 v->iov_base = " "; 863 v->iov_len = 1; 864 v++; 865 866 if (msg) { 867 v->iov_base = msg; 868 v->iov_len = strlen(msg); 869 } else if (f->f_prevcount > 1) { 870 v->iov_base = repbuf; 871 v->iov_len = sprintf(repbuf, "last message repeated %d times", 872 f->f_prevcount); 873 } else { 874 v->iov_base = f->f_prevline; 875 v->iov_len = f->f_prevlen; 876 } 877 v++; 878 879 dprintf("Logging to %s", TypeNames[f->f_type]); 880 f->f_time = now; 881 882 switch (f->f_type) { 883 case F_UNUSED: 884 dprintf("\n"); 885 break; 886 887 case F_FORW: 888 dprintf(" %s\n", f->f_un.f_forw.f_hname); 889 /* check for local vs remote messages */ 890 if (strcasecmp(f->f_prevhost, LocalHostName)) 891 l = snprintf(line, sizeof line - 1, 892 "<%d>%.15s Forwarded from %s: %s", 893 f->f_prevpri, iov[0].iov_base, f->f_prevhost, 894 iov[5].iov_base); 895 else 896 l = snprintf(line, sizeof line - 1, "<%d>%.15s %s", 897 f->f_prevpri, iov[0].iov_base, iov[5].iov_base); 898 if (l > MAXLINE) 899 l = MAXLINE; 900 if ((finet >= 0) && 901 (sendto(finet, line, l, 0, 902 (struct sockaddr *)&f->f_un.f_forw.f_addr, 903 sizeof(f->f_un.f_forw.f_addr)) != l)) { 904 int e = errno; 905 (void)close(f->f_file); 906 f->f_type = F_UNUSED; 907 errno = e; 908 logerror("sendto"); 909 } 910 break; 911 912 case F_FILE: 913 dprintf(" %s\n", f->f_un.f_fname); 914 v->iov_base = "\n"; 915 v->iov_len = 1; 916 if (writev(f->f_file, iov, 7) < 0) { 917 int e = errno; 918 (void)close(f->f_file); 919 f->f_type = F_UNUSED; 920 errno = e; 921 logerror(f->f_un.f_fname); 922 } else if (flags & SYNC_FILE) 923 (void)fsync(f->f_file); 924 break; 925 926 case F_PIPE: 927 dprintf(" %s\n", f->f_un.f_pipe.f_pname); 928 v->iov_base = "\n"; 929 v->iov_len = 1; 930 if (f->f_un.f_pipe.f_pid == 0) { 931 if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, 932 &f->f_un.f_pipe.f_pid)) < 0) { 933 f->f_type = F_UNUSED; 934 logerror(f->f_un.f_pipe.f_pname); 935 break; 936 } 937 } 938 if (writev(f->f_file, iov, 7) < 0) { 939 int e = errno; 940 (void)close(f->f_file); 941 if (f->f_un.f_pipe.f_pid > 0) 942 deadq_enter(f->f_un.f_pipe.f_pid); 943 f->f_un.f_pipe.f_pid = 0; 944 errno = e; 945 logerror(f->f_un.f_pipe.f_pname); 946 } 947 break; 948 949 case F_CONSOLE: 950 if (flags & IGN_CONS) { 951 dprintf(" (ignored)\n"); 952 break; 953 } 954 /* FALLTHROUGH */ 955 956 case F_TTY: 957 dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname); 958 v->iov_base = "\r\n"; 959 v->iov_len = 2; 960 961 errno = 0; /* ttymsg() only sometimes returns an errno */ 962 if ((msgret = ttymsg(iov, 7, f->f_un.f_fname, 10))) { 963 f->f_type = F_UNUSED; 964 logerror(msgret); 965 } 966 break; 967 968 case F_USERS: 969 case F_WALL: 970 dprintf("\n"); 971 v->iov_base = "\r\n"; 972 v->iov_len = 2; 973 wallmsg(f, iov); 974 break; 975 } 976 f->f_prevcount = 0; 977 } 978 979 /* 980 * WALLMSG -- Write a message to the world at large 981 * 982 * Write the specified message to either the entire 983 * world, or a list of approved users. 984 */ 985 void 986 wallmsg(f, iov) 987 struct filed *f; 988 struct iovec *iov; 989 { 990 static int reenter; /* avoid calling ourselves */ 991 FILE *uf; 992 struct utmp ut; 993 int i; 994 char *p; 995 char line[sizeof(ut.ut_line) + 1]; 996 997 if (reenter++) 998 return; 999 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) { 1000 logerror(_PATH_UTMP); 1001 reenter = 0; 1002 return; 1003 } 1004 /* NOSTRICT */ 1005 while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) { 1006 if (ut.ut_name[0] == '\0') 1007 continue; 1008 strncpy(line, ut.ut_line, sizeof(ut.ut_line)); 1009 line[sizeof(ut.ut_line)] = '\0'; 1010 if (f->f_type == F_WALL) { 1011 if ((p = ttymsg(iov, 7, line, TTYMSGTIME)) != NULL) { 1012 errno = 0; /* already in msg */ 1013 logerror(p); 1014 } 1015 continue; 1016 } 1017 /* should we send the message to this user? */ 1018 for (i = 0; i < MAXUNAMES; i++) { 1019 if (!f->f_un.f_uname[i][0]) 1020 break; 1021 if (!strncmp(f->f_un.f_uname[i], ut.ut_name, 1022 UT_NAMESIZE)) { 1023 if ((p = ttymsg(iov, 7, line, TTYMSGTIME)) 1024 != NULL) { 1025 errno = 0; /* already in msg */ 1026 logerror(p); 1027 } 1028 break; 1029 } 1030 } 1031 } 1032 (void)fclose(uf); 1033 reenter = 0; 1034 } 1035 1036 void 1037 reapchild(signo) 1038 int signo; 1039 { 1040 int status, code; 1041 pid_t pid; 1042 struct filed *f; 1043 char buf[256]; 1044 const char *reason; 1045 dq_t q; 1046 1047 while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { 1048 if (!Initialized) 1049 /* Don't tell while we are initting. */ 1050 continue; 1051 1052 /* First, look if it's a process from the dead queue. */ 1053 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries)) 1054 if (q->dq_pid == pid) { 1055 TAILQ_REMOVE(&deadq_head, q, dq_entries); 1056 free(q); 1057 goto oncemore; 1058 } 1059 1060 /* Now, look in list of active processes. */ 1061 for (f = Files; f; f = f->f_next) 1062 if (f->f_type == F_PIPE && 1063 f->f_un.f_pipe.f_pid == pid) { 1064 (void)close(f->f_file); 1065 1066 errno = 0; /* Keep strerror() stuff out of logerror messages. */ 1067 f->f_un.f_pipe.f_pid = 0; 1068 if (WIFSIGNALED(status)) { 1069 reason = "due to signal"; 1070 code = WTERMSIG(status); 1071 } else { 1072 reason = "with status"; 1073 code = WEXITSTATUS(status); 1074 if (code == 0) 1075 goto oncemore; /* Exited OK. */ 1076 } 1077 (void)snprintf(buf, sizeof buf, 1078 "Logging subprocess %d (%s) exited %s %d.", 1079 pid, f->f_un.f_pipe.f_pname, 1080 reason, code); 1081 logerror(buf); 1082 break; 1083 } 1084 oncemore: 1085 continue; 1086 } 1087 } 1088 1089 /* 1090 * Return a printable representation of a host address. 1091 */ 1092 char * 1093 cvthname(f) 1094 struct sockaddr_in *f; 1095 { 1096 struct hostent *hp; 1097 sigset_t omask, nmask; 1098 char *p; 1099 1100 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr)); 1101 1102 if (f->sin_family != AF_INET) { 1103 dprintf("Malformed from address\n"); 1104 return ("???"); 1105 } 1106 sigemptyset(&nmask); 1107 sigaddset(&nmask, SIGHUP); 1108 sigprocmask(SIG_BLOCK, &nmask, &omask); 1109 hp = gethostbyaddr((char *)&f->sin_addr, 1110 sizeof(struct in_addr), f->sin_family); 1111 sigprocmask(SIG_SETMASK, &omask, NULL); 1112 if (hp == 0) { 1113 dprintf("Host name for your address (%s) unknown\n", 1114 inet_ntoa(f->sin_addr)); 1115 return (inet_ntoa(f->sin_addr)); 1116 } 1117 if ((p = strchr(hp->h_name, '.')) && 1118 strcasecmp(p + 1, LocalDomain) == 0) 1119 *p = '\0'; 1120 return (hp->h_name); 1121 } 1122 1123 void 1124 domark(signo) 1125 int signo; 1126 { 1127 struct filed *f; 1128 dq_t q; 1129 1130 now = time((time_t *)NULL); 1131 MarkSeq += TIMERINTVL; 1132 if (MarkSeq >= MarkInterval) { 1133 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); 1134 MarkSeq = 0; 1135 } 1136 1137 for (f = Files; f; f = f->f_next) { 1138 if (f->f_prevcount && now >= REPEATTIME(f)) { 1139 dprintf("flush %s: repeated %d times, %d sec.\n", 1140 TypeNames[f->f_type], f->f_prevcount, 1141 repeatinterval[f->f_repeatcount]); 1142 fprintlog(f, 0, (char *)NULL); 1143 BACKOFF(f); 1144 } 1145 } 1146 1147 /* Walk the dead queue, and see if we should signal somebody. */ 1148 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries)) 1149 switch (q->dq_timeout) { 1150 case 0: 1151 /* Already signalled once, try harder now. */ 1152 kill(q->dq_pid, SIGKILL); 1153 break; 1154 1155 case 1: 1156 /* 1157 * Timed out on dead queue, send terminate 1158 * signal. Note that we leave the removal 1159 * from the dead queue to reapchild(), which 1160 * will also log the event. 1161 */ 1162 kill(q->dq_pid, SIGTERM); 1163 /* FALLTROUGH */ 1164 1165 default: 1166 q->dq_timeout--; 1167 } 1168 1169 (void)alarm(TIMERINTVL); 1170 } 1171 1172 /* 1173 * Print syslogd errors some place. 1174 */ 1175 void 1176 logerror(type) 1177 const char *type; 1178 { 1179 char buf[512]; 1180 1181 if (errno) 1182 (void)snprintf(buf, 1183 sizeof buf, "syslogd: %s: %s", type, strerror(errno)); 1184 else 1185 (void)snprintf(buf, sizeof buf, "syslogd: %s", type); 1186 errno = 0; 1187 dprintf("%s\n", buf); 1188 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 1189 } 1190 1191 void 1192 die(signo) 1193 int signo; 1194 { 1195 struct filed *f; 1196 int was_initialized; 1197 char buf[100]; 1198 int i; 1199 1200 was_initialized = Initialized; 1201 Initialized = 0; /* Don't log SIGCHLDs. */ 1202 for (f = Files; f != NULL; f = f->f_next) { 1203 /* flush any pending output */ 1204 if (f->f_prevcount) 1205 fprintlog(f, 0, (char *)NULL); 1206 if (f->f_type == F_PIPE) 1207 (void)close(f->f_file); 1208 } 1209 Initialized = was_initialized; 1210 if (signo) { 1211 dprintf("syslogd: exiting on signal %d\n", signo); 1212 (void)sprintf(buf, "exiting on signal %d", signo); 1213 errno = 0; 1214 logerror(buf); 1215 } 1216 for (i = 0; i < nfunix; i++) 1217 if (funixn[i] && funix[i] != -1) 1218 (void)unlink(funixn[i]); 1219 exit(1); 1220 } 1221 1222 /* 1223 * INIT -- Initialize syslogd from configuration table 1224 */ 1225 void 1226 init(signo) 1227 int signo; 1228 { 1229 int i; 1230 FILE *cf; 1231 struct filed *f, *next, **nextp; 1232 char *p; 1233 char cline[LINE_MAX]; 1234 char prog[NAME_MAX+1]; 1235 1236 dprintf("init\n"); 1237 1238 /* 1239 * Close all open log files. 1240 */ 1241 Initialized = 0; 1242 for (f = Files; f != NULL; f = next) { 1243 /* flush any pending output */ 1244 if (f->f_prevcount) 1245 fprintlog(f, 0, (char *)NULL); 1246 1247 switch (f->f_type) { 1248 case F_FILE: 1249 case F_FORW: 1250 case F_CONSOLE: 1251 case F_TTY: 1252 (void)close(f->f_file); 1253 break; 1254 case F_PIPE: 1255 (void)close(f->f_file); 1256 if (f->f_un.f_pipe.f_pid > 0) 1257 deadq_enter(f->f_un.f_pipe.f_pid); 1258 f->f_un.f_pipe.f_pid = 0; 1259 break; 1260 } 1261 next = f->f_next; 1262 if(f->f_program) free(f->f_program); 1263 free((char *)f); 1264 } 1265 Files = NULL; 1266 nextp = &Files; 1267 1268 /* open the configuration file */ 1269 if ((cf = fopen(ConfFile, "r")) == NULL) { 1270 dprintf("cannot open %s\n", ConfFile); 1271 *nextp = (struct filed *)calloc(1, sizeof(*f)); 1272 cfline("*.ERR\t/dev/console", *nextp, "*"); 1273 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 1274 cfline("*.PANIC\t*", (*nextp)->f_next, "*"); 1275 Initialized = 1; 1276 return; 1277 } 1278 1279 /* 1280 * Foreach line in the conf table, open that file. 1281 */ 1282 f = NULL; 1283 strcpy(prog, "*"); 1284 while (fgets(cline, sizeof(cline), cf) != NULL) { 1285 /* 1286 * check for end-of-section, comments, strip off trailing 1287 * spaces and newline character. #!prog is treated specially: 1288 * following lines apply only to that program. 1289 */ 1290 for (p = cline; isspace(*p); ++p) 1291 continue; 1292 if (*p == 0) 1293 continue; 1294 if(*p == '#') { 1295 p++; 1296 if(*p!='!') 1297 continue; 1298 } 1299 if(*p=='!') { 1300 p++; 1301 while(isspace(*p)) p++; 1302 if((!*p) || (*p == '*')) { 1303 strcpy(prog, "*"); 1304 continue; 1305 } 1306 for(i = 0; i < NAME_MAX; i++) { 1307 if(!isalnum(p[i])) 1308 break; 1309 prog[i] = p[i]; 1310 } 1311 prog[i] = 0; 1312 continue; 1313 } 1314 for (p = strchr(cline, '\0'); isspace(*--p);) 1315 continue; 1316 *++p = '\0'; 1317 f = (struct filed *)calloc(1, sizeof(*f)); 1318 *nextp = f; 1319 nextp = &f->f_next; 1320 cfline(cline, f, prog); 1321 } 1322 1323 /* close the configuration file */ 1324 (void)fclose(cf); 1325 1326 Initialized = 1; 1327 1328 if (Debug) { 1329 for (f = Files; f; f = f->f_next) { 1330 for (i = 0; i <= LOG_NFACILITIES; i++) 1331 if (f->f_pmask[i] == INTERNAL_NOPRI) 1332 printf("X "); 1333 else 1334 printf("%d ", f->f_pmask[i]); 1335 printf("%s: ", TypeNames[f->f_type]); 1336 switch (f->f_type) { 1337 case F_FILE: 1338 printf("%s", f->f_un.f_fname); 1339 break; 1340 1341 case F_CONSOLE: 1342 case F_TTY: 1343 printf("%s%s", _PATH_DEV, f->f_un.f_fname); 1344 break; 1345 1346 case F_FORW: 1347 printf("%s", f->f_un.f_forw.f_hname); 1348 break; 1349 1350 case F_PIPE: 1351 printf("%s", f->f_un.f_pipe.f_pname); 1352 break; 1353 1354 case F_USERS: 1355 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1356 printf("%s, ", f->f_un.f_uname[i]); 1357 break; 1358 } 1359 if(f->f_program) { 1360 printf(" (%s)", f->f_program); 1361 } 1362 printf("\n"); 1363 } 1364 } 1365 1366 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1367 dprintf("syslogd: restarted\n"); 1368 } 1369 1370 /* 1371 * Crack a configuration file line 1372 */ 1373 void 1374 cfline(line, f, prog) 1375 char *line; 1376 struct filed *f; 1377 char *prog; 1378 { 1379 struct hostent *hp; 1380 int i, pri; 1381 char *bp, *p, *q; 1382 char buf[MAXLINE], ebuf[100]; 1383 1384 dprintf("cfline(\"%s\", f, \"%s\")\n", line, prog); 1385 1386 errno = 0; /* keep strerror() stuff out of logerror messages */ 1387 1388 /* clear out file entry */ 1389 memset(f, 0, sizeof(*f)); 1390 for (i = 0; i <= LOG_NFACILITIES; i++) 1391 f->f_pmask[i] = INTERNAL_NOPRI; 1392 1393 /* save program name if any */ 1394 if(prog && *prog=='*') prog = NULL; 1395 if(prog) { 1396 f->f_program = calloc(1, strlen(prog)+1); 1397 if(f->f_program) { 1398 strcpy(f->f_program, prog); 1399 } 1400 } 1401 1402 /* scan through the list of selectors */ 1403 for (p = line; *p && *p != '\t' && *p != ' ';) { 1404 int pri_done; 1405 int pri_cmp; 1406 1407 /* find the end of this facility name list */ 1408 for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; ) 1409 continue; 1410 1411 /* get the priority comparison */ 1412 pri_cmp = 0; 1413 pri_done = 0; 1414 while (!pri_done) { 1415 switch (*q) { 1416 case '<': 1417 pri_cmp |= PRI_LT; 1418 q++; 1419 break; 1420 case '=': 1421 pri_cmp |= PRI_EQ; 1422 q++; 1423 break; 1424 case '>': 1425 pri_cmp |= PRI_GT; 1426 q++; 1427 break; 1428 default: 1429 pri_done++; 1430 break; 1431 } 1432 } 1433 if (!pri_cmp) 1434 pri_cmp = (UniquePriority) 1435 ? (PRI_EQ) 1436 : (PRI_EQ | PRI_GT) 1437 ; 1438 1439 /* collect priority name */ 1440 for (bp = buf; *q && !strchr("\t,; ", *q); ) 1441 *bp++ = *q++; 1442 *bp = '\0'; 1443 1444 /* skip cruft */ 1445 while (strchr(",;", *q)) 1446 q++; 1447 1448 /* decode priority name */ 1449 if (*buf == '*') 1450 pri = LOG_PRIMASK + 1; 1451 else { 1452 pri = decode(buf, prioritynames); 1453 if (pri < 0) { 1454 (void)snprintf(ebuf, sizeof ebuf, 1455 "unknown priority name \"%s\"", buf); 1456 logerror(ebuf); 1457 return; 1458 } 1459 } 1460 1461 /* scan facilities */ 1462 while (*p && !strchr("\t.; ", *p)) { 1463 for (bp = buf; *p && !strchr("\t,;. ", *p); ) 1464 *bp++ = *p++; 1465 *bp = '\0'; 1466 1467 if (*buf == '*') 1468 for (i = 0; i < LOG_NFACILITIES; i++) { 1469 f->f_pmask[i] = pri; 1470 f->f_pcmp[i] = pri_cmp; 1471 } 1472 else { 1473 i = decode(buf, facilitynames); 1474 if (i < 0) { 1475 (void)snprintf(ebuf, sizeof ebuf, 1476 "unknown facility name \"%s\"", 1477 buf); 1478 logerror(ebuf); 1479 return; 1480 } 1481 f->f_pmask[i >> 3] = pri; 1482 f->f_pcmp[i >> 3] = pri_cmp; 1483 } 1484 while (*p == ',' || *p == ' ') 1485 p++; 1486 } 1487 1488 p = q; 1489 } 1490 1491 /* skip to action part */ 1492 while (*p == '\t' || *p == ' ') 1493 p++; 1494 1495 switch (*p) 1496 { 1497 case '@': 1498 (void)strncpy(f->f_un.f_forw.f_hname, ++p, 1499 sizeof(f->f_un.f_forw.f_hname)-1); 1500 f->f_un.f_forw.f_hname[sizeof(f->f_un.f_forw.f_hname)-1] = '\0'; 1501 hp = gethostbyname(f->f_un.f_forw.f_hname); 1502 if (hp == NULL) { 1503 extern int h_errno; 1504 1505 logerror(hstrerror(h_errno)); 1506 break; 1507 } 1508 memset(&f->f_un.f_forw.f_addr, 0, 1509 sizeof(f->f_un.f_forw.f_addr)); 1510 f->f_un.f_forw.f_addr.sin_family = AF_INET; 1511 f->f_un.f_forw.f_addr.sin_port = LogPort; 1512 memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length); 1513 f->f_type = F_FORW; 1514 break; 1515 1516 case '/': 1517 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { 1518 f->f_type = F_UNUSED; 1519 logerror(p); 1520 break; 1521 } 1522 if (isatty(f->f_file)) { 1523 if (strcmp(p, ctty) == 0) 1524 f->f_type = F_CONSOLE; 1525 else 1526 f->f_type = F_TTY; 1527 (void)strcpy(f->f_un.f_fname, p + sizeof _PATH_DEV - 1); 1528 } else { 1529 (void)strcpy(f->f_un.f_fname, p); 1530 f->f_type = F_FILE; 1531 } 1532 break; 1533 1534 case '|': 1535 f->f_un.f_pipe.f_pid = 0; 1536 (void)strcpy(f->f_un.f_pipe.f_pname, p + 1); 1537 f->f_type = F_PIPE; 1538 break; 1539 1540 case '*': 1541 f->f_type = F_WALL; 1542 break; 1543 1544 default: 1545 for (i = 0; i < MAXUNAMES && *p; i++) { 1546 for (q = p; *q && *q != ','; ) 1547 q++; 1548 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1549 if ((q - p) > UT_NAMESIZE) 1550 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1551 else 1552 f->f_un.f_uname[i][q - p] = '\0'; 1553 while (*q == ',' || *q == ' ') 1554 q++; 1555 p = q; 1556 } 1557 f->f_type = F_USERS; 1558 break; 1559 } 1560 } 1561 1562 1563 /* 1564 * Decode a symbolic name to a numeric value 1565 */ 1566 int 1567 decode(name, codetab) 1568 const char *name; 1569 CODE *codetab; 1570 { 1571 CODE *c; 1572 char *p, buf[40]; 1573 1574 if (isdigit(*name)) 1575 return (atoi(name)); 1576 1577 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1578 if (isupper(*name)) 1579 *p = tolower(*name); 1580 else 1581 *p = *name; 1582 } 1583 *p = '\0'; 1584 for (c = codetab; c->c_name; c++) 1585 if (!strcmp(buf, c->c_name)) 1586 return (c->c_val); 1587 1588 return (-1); 1589 } 1590 1591 /* 1592 * fork off and become a daemon, but wait for the child to come online 1593 * before returing to the parent, or we get disk thrashing at boot etc. 1594 * Set a timer so we don't hang forever if it wedges. 1595 */ 1596 int 1597 waitdaemon(nochdir, noclose, maxwait) 1598 int nochdir, noclose, maxwait; 1599 { 1600 int fd; 1601 int status; 1602 pid_t pid, childpid; 1603 1604 switch (childpid = fork()) { 1605 case -1: 1606 return (-1); 1607 case 0: 1608 break; 1609 default: 1610 signal(SIGALRM, timedout); 1611 alarm(maxwait); 1612 while ((pid = wait3(&status, 0, NULL)) != -1) { 1613 if (WIFEXITED(status)) 1614 errx(1, "child pid %d exited with return code %d", 1615 pid, WEXITSTATUS(status)); 1616 if (WIFSIGNALED(status)) 1617 errx(1, "child pid %d exited on signal %d%s", 1618 pid, WTERMSIG(status), 1619 WCOREDUMP(status) ? " (core dumped)" : 1620 ""); 1621 if (pid == childpid) /* it's gone... */ 1622 break; 1623 } 1624 exit(0); 1625 } 1626 1627 if (setsid() == -1) 1628 return (-1); 1629 1630 if (!nochdir) 1631 (void)chdir("/"); 1632 1633 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1634 (void)dup2(fd, STDIN_FILENO); 1635 (void)dup2(fd, STDOUT_FILENO); 1636 (void)dup2(fd, STDERR_FILENO); 1637 if (fd > 2) 1638 (void)close (fd); 1639 } 1640 return (getppid()); 1641 } 1642 1643 /* 1644 * We get a SIGALRM from the child when it's running and finished doing it's 1645 * fsync()'s or O_SYNC writes for all the boot messages. 1646 * 1647 * We also get a signal from the kernel if the timer expires, so check to 1648 * see what happened. 1649 */ 1650 void 1651 timedout(sig) 1652 int sig __unused; 1653 { 1654 int left; 1655 left = alarm(0); 1656 signal(SIGALRM, SIG_DFL); 1657 if (left == 0) 1658 errx(1, "timed out waiting for child"); 1659 else 1660 exit(0); 1661 } 1662 1663 /* 1664 * Add `s' to the list of allowable peer addresses to accept messages 1665 * from. 1666 * 1667 * `s' is a string in the form: 1668 * 1669 * [*]domainname[:{servicename|portnumber|*}] 1670 * 1671 * or 1672 * 1673 * netaddr/maskbits[:{servicename|portnumber|*}] 1674 * 1675 * Returns -1 on error, 0 if the argument was valid. 1676 */ 1677 int 1678 allowaddr(s) 1679 char *s; 1680 { 1681 char *cp1, *cp2; 1682 struct allowedpeer ap; 1683 struct servent *se; 1684 regex_t re; 1685 int i; 1686 1687 if ((cp1 = strrchr(s, ':'))) { 1688 /* service/port provided */ 1689 *cp1++ = '\0'; 1690 if (strlen(cp1) == 1 && *cp1 == '*') 1691 /* any port allowed */ 1692 ap.port = htons(0); 1693 else if ((se = getservbyname(cp1, "udp"))) 1694 ap.port = se->s_port; 1695 else { 1696 ap.port = htons((int)strtol(cp1, &cp2, 0)); 1697 if (*cp2 != '\0') 1698 return -1; /* port not numeric */ 1699 } 1700 } else { 1701 if ((se = getservbyname("syslog", "udp"))) 1702 ap.port = se->s_port; 1703 else 1704 /* sanity, should not happen */ 1705 ap.port = htons(514); 1706 } 1707 1708 /* the regexp's are ugly, but the cleanest way */ 1709 1710 if (regcomp(&re, "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(/[0-9]+)?$", 1711 REG_EXTENDED)) 1712 /* if RE compilation fails, that's an internal error */ 1713 abort(); 1714 if (regexec(&re, s, 0, 0, 0) == 0) { 1715 /* arg `s' is numeric */ 1716 ap.isnumeric = 1; 1717 if ((cp1 = strchr(s, '/')) != NULL) { 1718 *cp1++ = '\0'; 1719 i = atoi(cp1); 1720 if (i < 0 || i > 32) 1721 return -1; 1722 /* convert masklen to netmask */ 1723 ap.a_mask.s_addr = htonl(~((1 << (32 - i)) - 1)); 1724 } 1725 if (ascii2addr(AF_INET, s, &ap.a_addr) == -1) 1726 return -1; 1727 if (cp1 == NULL) { 1728 /* use default netmask */ 1729 if (IN_CLASSA(ntohl(ap.a_addr.s_addr))) 1730 ap.a_mask.s_addr = htonl(IN_CLASSA_NET); 1731 else if (IN_CLASSB(ntohl(ap.a_addr.s_addr))) 1732 ap.a_mask.s_addr = htonl(IN_CLASSB_NET); 1733 else 1734 ap.a_mask.s_addr = htonl(IN_CLASSC_NET); 1735 } 1736 } else { 1737 /* arg `s' is domain name */ 1738 ap.isnumeric = 0; 1739 ap.a_name = s; 1740 } 1741 regfree(&re); 1742 1743 if (Debug) { 1744 printf("allowaddr: rule %d: ", NumAllowed); 1745 if (ap.isnumeric) { 1746 printf("numeric, "); 1747 printf("addr = %s, ", 1748 addr2ascii(AF_INET, &ap.a_addr, sizeof(struct in_addr), 0)); 1749 printf("mask = %s; ", 1750 addr2ascii(AF_INET, &ap.a_mask, sizeof(struct in_addr), 0)); 1751 } else 1752 printf("domainname = %s; ", ap.a_name); 1753 printf("port = %d\n", ntohs(ap.port)); 1754 } 1755 1756 if ((AllowedPeers = realloc(AllowedPeers, 1757 ++NumAllowed * sizeof(struct allowedpeer))) 1758 == NULL) { 1759 fprintf(stderr, "Out of memory!\n"); 1760 exit(EX_OSERR); 1761 } 1762 memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer)); 1763 return 0; 1764 } 1765 1766 /* 1767 * Validate that the remote peer has permission to log to us. 1768 */ 1769 int 1770 validate(sin, hname) 1771 struct sockaddr_in *sin; 1772 const char *hname; 1773 { 1774 int i; 1775 size_t l1, l2; 1776 char *cp, name[MAXHOSTNAMELEN]; 1777 struct allowedpeer *ap; 1778 1779 if (NumAllowed == 0) 1780 /* traditional behaviour, allow everything */ 1781 return 1; 1782 1783 strncpy(name, hname, sizeof name); 1784 if (strchr(name, '.') == NULL) { 1785 strncat(name, ".", sizeof name - strlen(name) - 1); 1786 strncat(name, LocalDomain, sizeof name - strlen(name) - 1); 1787 } 1788 dprintf("validate: dgram from IP %s, port %d, name %s;\n", 1789 addr2ascii(AF_INET, &sin->sin_addr, sizeof(struct in_addr), 0), 1790 ntohs(sin->sin_port), name); 1791 1792 /* now, walk down the list */ 1793 for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) { 1794 if (ntohs(ap->port) != 0 && ap->port != sin->sin_port) { 1795 dprintf("rejected in rule %d due to port mismatch.\n", i); 1796 continue; 1797 } 1798 1799 if (ap->isnumeric) { 1800 if ((sin->sin_addr.s_addr & ap->a_mask.s_addr) 1801 != ap->a_addr.s_addr) { 1802 dprintf("rejected in rule %d due to IP mismatch.\n", i); 1803 continue; 1804 } 1805 } else { 1806 cp = ap->a_name; 1807 l1 = strlen(name); 1808 if (*cp == '*') { 1809 /* allow wildmatch */ 1810 cp++; 1811 l2 = strlen(cp); 1812 if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) { 1813 dprintf("rejected in rule %d due to name mismatch.\n", i); 1814 continue; 1815 } 1816 } else { 1817 /* exact match */ 1818 l2 = strlen(cp); 1819 if (l2 != l1 || memcmp(cp, name, l1) != 0) { 1820 dprintf("rejected in rule %d due to name mismatch.\n", i); 1821 continue; 1822 } 1823 } 1824 } 1825 dprintf("accepted in rule %d.\n", i); 1826 return 1; /* hooray! */ 1827 } 1828 return 0; 1829 } 1830 1831 /* 1832 * Fairly similar to popen(3), but returns an open descriptor, as 1833 * opposed to a FILE *. 1834 */ 1835 int 1836 p_open(prog, pid) 1837 char *prog; 1838 pid_t *pid; 1839 { 1840 int pfd[2], nulldesc, i; 1841 sigset_t omask, mask; 1842 char *argv[4]; /* sh -c cmd NULL */ 1843 char errmsg[200]; 1844 1845 if (pipe(pfd) == -1) 1846 return -1; 1847 if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) 1848 /* we are royally screwed anyway */ 1849 return -1; 1850 1851 sigemptyset(&mask); 1852 sigaddset(&mask, SIGALRM); 1853 sigaddset(&mask, SIGHUP); 1854 sigprocmask(SIG_BLOCK, &mask, &omask); 1855 switch ((*pid = fork())) { 1856 case -1: 1857 sigprocmask(SIG_SETMASK, &omask, 0); 1858 close(nulldesc); 1859 return -1; 1860 1861 case 0: 1862 argv[0] = "sh"; 1863 argv[1] = "-c"; 1864 argv[2] = prog; 1865 argv[3] = NULL; 1866 1867 alarm(0); 1868 (void)setsid(); /* Avoid catching SIGHUPs. */ 1869 1870 /* 1871 * Throw away pending signals, and reset signal 1872 * behaviour to standard values. 1873 */ 1874 signal(SIGALRM, SIG_IGN); 1875 signal(SIGHUP, SIG_IGN); 1876 sigprocmask(SIG_SETMASK, &omask, 0); 1877 signal(SIGPIPE, SIG_DFL); 1878 signal(SIGQUIT, SIG_DFL); 1879 signal(SIGALRM, SIG_DFL); 1880 signal(SIGHUP, SIG_DFL); 1881 1882 dup2(pfd[0], STDIN_FILENO); 1883 dup2(nulldesc, STDOUT_FILENO); 1884 dup2(nulldesc, STDERR_FILENO); 1885 for (i = getdtablesize(); i > 2; i--) 1886 (void) close(i); 1887 1888 (void) execvp(_PATH_BSHELL, argv); 1889 _exit(255); 1890 } 1891 1892 sigprocmask(SIG_SETMASK, &omask, 0); 1893 close(nulldesc); 1894 close(pfd[0]); 1895 /* 1896 * Avoid blocking on a hung pipe. With O_NONBLOCK, we are 1897 * supposed to get an EWOULDBLOCK on writev(2), which is 1898 * caught by the logic above anyway, which will in turn close 1899 * the pipe, and fork a new logging subprocess if necessary. 1900 * The stale subprocess will be killed some time later unless 1901 * it terminated itself due to closing its input pipe (so we 1902 * get rid of really dead puppies). 1903 */ 1904 if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { 1905 /* This is bad. */ 1906 (void)snprintf(errmsg, sizeof errmsg, 1907 "Warning: cannot change pipe to PID %d to " 1908 "non-blocking behaviour.", 1909 (int)*pid); 1910 logerror(errmsg); 1911 } 1912 return pfd[1]; 1913 } 1914 1915 void 1916 deadq_enter(pid) 1917 pid_t pid; 1918 { 1919 dq_t p; 1920 1921 p = malloc(sizeof(struct deadq_entry)); 1922 if (p == 0) { 1923 errno = 0; 1924 logerror("panic: out of virtual memory!"); 1925 exit(1); 1926 } 1927 1928 p->dq_pid = pid; 1929 p->dq_timeout = DQ_TIMO_INIT; 1930 TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); 1931 } 1932