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