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