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 <setjmp.h> 103 #include <signal.h> 104 #include <stdio.h> 105 #include <stdlib.h> 106 #include <string.h> 107 #include <sysexits.h> 108 #include <unistd.h> 109 #include <utmp.h> 110 111 #include "pathnames.h" 112 #include "ttymsg.h" 113 114 #define SYSLOG_NAMES 115 #include <sys/syslog.h> 116 117 #ifdef NI_WITHSCOPEID 118 static const int withscopeid = NI_WITHSCOPEID; 119 #else 120 static const int withscopeid = 0; 121 #endif 122 123 const char *ConfFile = _PATH_LOGCONF; 124 const char *PidFile = _PATH_LOGPID; 125 const char ctty[] = _PATH_CONSOLE; 126 127 #define dprintf if (Debug) printf 128 129 #define MAXUNAMES 20 /* maximum number of user names */ 130 131 #define MAXFUNIX 20 132 133 int nfunix = 1; 134 const char *funixn[MAXFUNIX] = { _PATH_LOG }; 135 int funix[MAXFUNIX]; 136 137 /* 138 * Flags to logmsg(). 139 */ 140 141 #define IGN_CONS 0x001 /* don't print on console */ 142 #define SYNC_FILE 0x002 /* do fsync on file after printing */ 143 #define ADDDATE 0x004 /* add a date to the message */ 144 #define MARK 0x008 /* this message is a mark */ 145 #define ISKERNEL 0x010 /* kernel generated message */ 146 147 /* 148 * This structure represents the files that will have log 149 * copies printed. 150 */ 151 152 struct filed { 153 struct filed *f_next; /* next in linked list */ 154 short f_type; /* entry type, see below */ 155 short f_file; /* file descriptor */ 156 time_t f_time; /* time this was last written */ 157 char *f_host; /* host from which to recd. */ 158 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 159 u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ 160 #define PRI_LT 0x1 161 #define PRI_EQ 0x2 162 #define PRI_GT 0x4 163 char *f_program; /* program this applies to */ 164 union { 165 char f_uname[MAXUNAMES][UT_NAMESIZE+1]; 166 struct { 167 char f_hname[MAXHOSTNAMELEN]; 168 struct addrinfo *f_addr; 169 170 } f_forw; /* forwarding address */ 171 char f_fname[MAXPATHLEN]; 172 struct { 173 char f_pname[MAXPATHLEN]; 174 pid_t f_pid; 175 } f_pipe; 176 } f_un; 177 char f_prevline[MAXSVLINE]; /* last message logged */ 178 char f_lasttime[16]; /* time of last occurrence */ 179 char f_prevhost[MAXHOSTNAMELEN]; /* host from which recd. */ 180 int f_prevpri; /* pri of f_prevline */ 181 int f_prevlen; /* length of f_prevline */ 182 int f_prevcount; /* repetition cnt of prevline */ 183 u_int f_repeatcount; /* number of "repeated" msgs */ 184 }; 185 186 /* 187 * Queue of about-to-be dead processes we should watch out for. 188 */ 189 190 TAILQ_HEAD(stailhead, deadq_entry) deadq_head; 191 struct stailhead *deadq_headp; 192 193 struct deadq_entry { 194 pid_t dq_pid; 195 int dq_timeout; 196 TAILQ_ENTRY(deadq_entry) dq_entries; 197 }; 198 199 /* 200 * The timeout to apply to processes waiting on the dead queue. Unit 201 * of measure is `mark intervals', i.e. 20 minutes by default. 202 * Processes on the dead queue will be terminated after that time. 203 */ 204 205 #define DQ_TIMO_INIT 2 206 207 typedef struct deadq_entry *dq_t; 208 209 210 /* 211 * Struct to hold records of network addresses that are allowed to log 212 * to us. 213 */ 214 struct allowedpeer { 215 int isnumeric; 216 u_short port; 217 union { 218 struct { 219 struct sockaddr_storage addr; 220 struct sockaddr_storage mask; 221 } numeric; 222 char *name; 223 } u; 224 #define a_addr u.numeric.addr 225 #define a_mask u.numeric.mask 226 #define a_name u.name 227 }; 228 229 230 /* 231 * Intervals at which we flush out "message repeated" messages, 232 * in seconds after previous message is logged. After each flush, 233 * we move to the next interval until we reach the largest. 234 */ 235 int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 236 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 237 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 238 #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 239 (f)->f_repeatcount = MAXREPEAT; \ 240 } 241 242 /* values for f_type */ 243 #define F_UNUSED 0 /* unused entry */ 244 #define F_FILE 1 /* regular file */ 245 #define F_TTY 2 /* terminal */ 246 #define F_CONSOLE 3 /* console terminal */ 247 #define F_FORW 4 /* remote machine */ 248 #define F_USERS 5 /* list of users */ 249 #define F_WALL 6 /* everyone logged on */ 250 #define F_PIPE 7 /* pipe to program */ 251 252 const char *TypeNames[8] = { 253 "UNUSED", "FILE", "TTY", "CONSOLE", 254 "FORW", "USERS", "WALL", "PIPE" 255 }; 256 257 struct filed *Files; 258 struct filed consfile; 259 260 int Debug; /* debug flag */ 261 int resolve = 1; /* resolve hostname */ 262 char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ 263 const char *LocalDomain; /* our local domain name */ 264 int *finet = NULL; /* Internet datagram socket */ 265 int fklog = -1; /* /dev/klog */ 266 int Initialized = 0; /* set when we have initialized ourselves */ 267 int MarkInterval = 20 * 60; /* interval between marks in seconds */ 268 int MarkSeq = 0; /* mark sequence number */ 269 int SecureMode = 0; /* when true, receive only unix domain socks */ 270 #ifdef INET6 271 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 272 #else 273 int family = PF_INET; /* protocol family (IPv4 only) */ 274 #endif 275 int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 276 int use_bootfile = 0; /* log entire bootfile for every kern msg */ 277 278 char bootfile[MAXLINE+1]; /* booted kernel file */ 279 280 struct allowedpeer *AllowedPeers; 281 int NumAllowed = 0; /* # of AllowedPeer entries */ 282 283 int UniquePriority = 0; /* Only log specified priority? */ 284 int LogFacPri = 0; /* Put facility and priority in log message: */ 285 /* 0=no, 1=numeric, 2=names */ 286 int KeepKernFac = 0; /* Keep remotely logged kernel facility */ 287 288 volatile sig_atomic_t MarkSet, WantDie; 289 290 int allowaddr __P((char *)); 291 void cfline __P((const char *, struct filed *, const char *, const char *)); 292 const char *cvthname __P((struct sockaddr *)); 293 void deadq_enter __P((pid_t, const char *)); 294 int deadq_remove __P((pid_t)); 295 int decode __P((const char *, CODE *)); 296 void die __P((int)); 297 void dodie __P((int)); 298 void domark __P((int)); 299 void fprintlog __P((struct filed *, int, const char *)); 300 int* socksetup __P((int, const char *)); 301 void init __P((int)); 302 void logerror __P((const char *)); 303 void logmsg __P((int, const char *, const char *, int)); 304 void log_deadchild __P((pid_t, int, const char *)); 305 void markit __P((void)); 306 void printline __P((const char *, char *)); 307 void printsys __P((char *)); 308 int p_open __P((const char *, pid_t *)); 309 void readklog __P((void)); 310 void reapchild __P((int)); 311 static void usage __P((void)); 312 int validate __P((struct sockaddr *, const char *)); 313 static void unmapped __P((struct sockaddr *)); 314 void wallmsg __P((struct filed *, struct iovec *)); 315 int waitdaemon __P((int, int, int)); 316 void timedout __P((int)); 317 318 int 319 main(argc, argv) 320 int argc; 321 char *argv[]; 322 { 323 int ch, i, fdsrmax = 0, l; 324 struct sockaddr_un sunx, fromunix; 325 struct sockaddr_storage frominet; 326 fd_set *fdsr = NULL; 327 FILE *fp; 328 char line[MAXLINE + 1]; 329 const char *bindhostname, *hname; 330 struct timeval tv, *tvp; 331 struct sigaction sact; 332 sigset_t mask; 333 pid_t ppid = 1; 334 socklen_t len; 335 336 bindhostname = NULL; 337 while ((ch = getopt(argc, argv, "46Aa:b:df:kl:m:nop:P:suv")) != -1) 338 switch (ch) { 339 case '4': 340 family = PF_INET; 341 break; 342 #ifdef INET6 343 case '6': 344 family = PF_INET6; 345 break; 346 #endif 347 case 'A': 348 send_to_all++; 349 break; 350 case 'a': /* allow specific network addresses only */ 351 if (allowaddr(optarg) == -1) 352 usage(); 353 break; 354 case 'b': 355 bindhostname = optarg; 356 break; 357 case 'd': /* debug */ 358 Debug++; 359 break; 360 case 'f': /* configuration file */ 361 ConfFile = optarg; 362 break; 363 case 'k': /* keep remote kern fac */ 364 KeepKernFac = 1; 365 break; 366 case 'l': 367 if (nfunix < MAXFUNIX) 368 funixn[nfunix++] = optarg; 369 else 370 warnx("out of descriptors, ignoring %s", 371 optarg); 372 break; 373 case 'm': /* mark interval */ 374 MarkInterval = atoi(optarg) * 60; 375 break; 376 case 'n': 377 resolve = 0; 378 break; 379 case 'o': 380 use_bootfile = 1; 381 break; 382 case 'p': /* path */ 383 funixn[0] = optarg; 384 break; 385 case 'P': /* path for alt. PID */ 386 PidFile = optarg; 387 break; 388 case 's': /* no network mode */ 389 SecureMode++; 390 break; 391 case 'u': /* only log specified priority */ 392 UniquePriority++; 393 break; 394 case 'v': /* log facility and priority */ 395 LogFacPri++; 396 break; 397 case '?': 398 default: 399 usage(); 400 } 401 if ((argc -= optind) != 0) 402 usage(); 403 404 if (!Debug) { 405 ppid = waitdaemon(0, 0, 30); 406 if (ppid < 0) 407 err(1, "could not become daemon"); 408 } else 409 setlinebuf(stdout); 410 411 if (NumAllowed) 412 endservent(); 413 414 consfile.f_type = F_CONSOLE; 415 (void)strlcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1, 416 sizeof(consfile.f_un.f_fname)); 417 (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); 418 (void)signal(SIGTERM, dodie); 419 (void)signal(SIGINT, Debug ? dodie : SIG_IGN); 420 (void)signal(SIGQUIT, Debug ? dodie : SIG_IGN); 421 /* 422 * We don't want the SIGCHLD and SIGHUP handlers to interfere 423 * with each other; they are likely candidates for being called 424 * simultaneously (SIGHUP closes pipe descriptor, process dies, 425 * SIGCHLD happens). 426 */ 427 sigemptyset(&mask); 428 sigaddset(&mask, SIGHUP); 429 sact.sa_handler = reapchild; 430 sact.sa_mask = mask; 431 sact.sa_flags = SA_RESTART; 432 (void)sigaction(SIGCHLD, &sact, NULL); 433 (void)signal(SIGALRM, domark); 434 (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ 435 (void)alarm(TIMERINTVL); 436 437 TAILQ_INIT(&deadq_head); 438 439 #ifndef SUN_LEN 440 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 441 #endif 442 for (i = 0; i < nfunix; i++) { 443 memset(&sunx, 0, sizeof(sunx)); 444 sunx.sun_family = AF_UNIX; 445 (void)strlcpy(sunx.sun_path, funixn[i], sizeof(sunx.sun_path)); 446 funix[i] = socket(AF_UNIX, SOCK_DGRAM, 0); 447 if (funix[i] < 0 || 448 bind(funix[i], (struct sockaddr *)&sunx, 449 SUN_LEN(&sunx)) < 0 || 450 chmod(funixn[i], 0666) < 0) { 451 (void) snprintf(line, sizeof line, 452 "cannot create %s", funixn[i]); 453 logerror(line); 454 dprintf("cannot create %s (%d)\n", funixn[i], errno); 455 if (i == 0) 456 die(0); 457 } 458 } 459 if (SecureMode <= 1) 460 finet = socksetup(family, bindhostname); 461 462 if (finet) { 463 if (SecureMode) { 464 for (i = 0; i < *finet; i++) { 465 if (shutdown(finet[i+1], SHUT_RD) < 0) { 466 logerror("shutdown"); 467 if (!Debug) 468 die(0); 469 } 470 } 471 } else 472 dprintf("listening on inet and/or inet6 socket\n"); 473 dprintf("sending on inet and/or inet6 socket\n"); 474 } 475 476 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) 477 if (fcntl(fklog, F_SETFL, O_NONBLOCK) < 0) 478 fklog = -1; 479 if (fklog < 0) 480 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 481 482 /* tuck my process id away */ 483 fp = fopen(PidFile, "w"); 484 if (fp != NULL) { 485 fprintf(fp, "%d\n", getpid()); 486 (void) fclose(fp); 487 } 488 489 dprintf("off & running....\n"); 490 491 init(0); 492 /* prevent SIGHUP and SIGCHLD handlers from running in parallel */ 493 sigemptyset(&mask); 494 sigaddset(&mask, SIGCHLD); 495 sact.sa_handler = init; 496 sact.sa_mask = mask; 497 sact.sa_flags = SA_RESTART; 498 (void)sigaction(SIGHUP, &sact, NULL); 499 500 tvp = &tv; 501 tv.tv_sec = tv.tv_usec = 0; 502 503 if (fklog != -1 && fklog > fdsrmax) 504 fdsrmax = fklog; 505 if (finet && !SecureMode) { 506 for (i = 0; i < *finet; i++) { 507 if (finet[i+1] != -1 && finet[i+1] > fdsrmax) 508 fdsrmax = finet[i+1]; 509 } 510 } 511 for (i = 0; i < nfunix; i++) { 512 if (funix[i] != -1 && funix[i] > fdsrmax) 513 fdsrmax = funix[i]; 514 } 515 516 fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS), 517 sizeof(fd_mask)); 518 if (fdsr == NULL) 519 errx(1, "calloc fd_set"); 520 521 for (;;) { 522 if (MarkSet) 523 markit(); 524 if (WantDie) 525 die(WantDie); 526 527 bzero(fdsr, howmany(fdsrmax+1, NFDBITS) * 528 sizeof(fd_mask)); 529 530 if (fklog != -1) 531 FD_SET(fklog, fdsr); 532 if (finet && !SecureMode) { 533 for (i = 0; i < *finet; i++) { 534 if (finet[i+1] != -1) 535 FD_SET(finet[i+1], fdsr); 536 } 537 } 538 for (i = 0; i < nfunix; i++) { 539 if (funix[i] != -1) 540 FD_SET(funix[i], fdsr); 541 } 542 543 i = select(fdsrmax+1, fdsr, NULL, NULL, tvp); 544 switch (i) { 545 case 0: 546 if (tvp) { 547 tvp = NULL; 548 if (ppid != 1) 549 kill(ppid, SIGALRM); 550 } 551 continue; 552 case -1: 553 if (errno != EINTR) 554 logerror("select"); 555 continue; 556 } 557 if (fklog != -1 && FD_ISSET(fklog, fdsr)) 558 readklog(); 559 if (finet && !SecureMode) { 560 for (i = 0; i < *finet; i++) { 561 if (FD_ISSET(finet[i+1], fdsr)) { 562 len = sizeof(frominet); 563 l = recvfrom(finet[i+1], line, MAXLINE, 564 0, (struct sockaddr *)&frominet, 565 &len); 566 if (l > 0) { 567 line[l] = '\0'; 568 hname = cvthname((struct sockaddr *)&frominet); 569 unmapped((struct sockaddr *)&frominet); 570 if (validate((struct sockaddr *)&frominet, hname)) 571 printline(hname, line); 572 } else if (l < 0 && errno != EINTR) 573 logerror("recvfrom inet"); 574 } 575 } 576 } 577 for (i = 0; i < nfunix; i++) { 578 if (funix[i] != -1 && FD_ISSET(funix[i], fdsr)) { 579 len = sizeof(fromunix); 580 l = recvfrom(funix[i], line, MAXLINE, 0, 581 (struct sockaddr *)&fromunix, &len); 582 if (l > 0) { 583 line[l] = '\0'; 584 printline(LocalHostName, line); 585 } else if (l < 0 && errno != EINTR) 586 logerror("recvfrom unix"); 587 } 588 } 589 } 590 if (fdsr) 591 free(fdsr); 592 } 593 594 static void 595 unmapped(sa) 596 struct sockaddr *sa; 597 { 598 struct sockaddr_in6 *sin6; 599 struct sockaddr_in sin4; 600 601 if (sa->sa_family != AF_INET6) 602 return; 603 if (sa->sa_len != sizeof(struct sockaddr_in6) || 604 sizeof(sin4) > sa->sa_len) 605 return; 606 sin6 = (struct sockaddr_in6 *)sa; 607 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 608 return; 609 610 memset(&sin4, 0, sizeof(sin4)); 611 sin4.sin_family = AF_INET; 612 sin4.sin_len = sizeof(struct sockaddr_in); 613 memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12], 614 sizeof(sin4.sin_addr)); 615 sin4.sin_port = sin6->sin6_port; 616 617 memcpy(sa, &sin4, sin4.sin_len); 618 } 619 620 static void 621 usage() 622 { 623 624 fprintf(stderr, "%s\n%s\n%s\n", 625 "usage: syslogd [-46Adnsuv] [-a allowed_peer] [-f config_file]", 626 " [-m mark_interval] [-l log_socket]", 627 " [-p log_socket] [-P pid_file]"); 628 exit(1); 629 } 630 631 /* 632 * Take a raw input line, decode the message, and print the message 633 * on the appropriate log files. 634 */ 635 void 636 printline(hname, msg) 637 const char *hname; 638 char *msg; 639 { 640 int c, pri; 641 char *p, *q, line[MAXLINE + 1]; 642 643 /* test for special codes */ 644 pri = DEFUPRI; 645 p = msg; 646 if (*p == '<') { 647 pri = 0; 648 while (isdigit(*++p)) 649 pri = 10 * pri + (*p - '0'); 650 if (*p == '>') 651 ++p; 652 } 653 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 654 pri = DEFUPRI; 655 656 /* don't allow users to log kernel messages */ 657 if (LOG_FAC(pri) == LOG_KERN && !KeepKernFac) 658 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 659 660 q = line; 661 662 while ((c = (unsigned char)*p++) != '\0' && 663 q < &line[sizeof(line) - 4]) { 664 if ((c & 0x80) && c < 0xA0) { 665 c &= 0x7F; 666 *q++ = 'M'; 667 *q++ = '-'; 668 } 669 if (isascii(c) && iscntrl(c)) { 670 if (c == '\n') 671 *q++ = ' '; 672 else if (c == '\t') 673 *q++ = '\t'; 674 else { 675 *q++ = '^'; 676 *q++ = c ^ 0100; 677 } 678 } else 679 *q++ = c; 680 } 681 *q = '\0'; 682 683 logmsg(pri, line, hname, 0); 684 } 685 686 /* 687 * Read /dev/klog while data are available, split into lines. 688 */ 689 void 690 readklog() 691 { 692 char *p, *q, line[MAXLINE + 1]; 693 int len, i; 694 695 len = 0; 696 for (;;) { 697 i = read(fklog, line + len, MAXLINE - 1 - len); 698 if (i > 0) 699 line[i + len] = '\0'; 700 else if (i < 0 && errno != EINTR && errno != EAGAIN) { 701 logerror("klog"); 702 fklog = -1; 703 break; 704 } else 705 break; 706 707 for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { 708 *q = '\0'; 709 printsys(p); 710 } 711 len = strlen(p); 712 if (len >= MAXLINE - 1) { 713 printsys(p); 714 len = 0; 715 } 716 if (len > 0) 717 memmove(line, p, len + 1); 718 } 719 if (len > 0) 720 printsys(line); 721 } 722 723 /* 724 * Take a raw input line from /dev/klog, format similar to syslog(). 725 */ 726 void 727 printsys(p) 728 char *p; 729 { 730 int pri, flags; 731 732 flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */ 733 pri = DEFSPRI; 734 if (*p == '<') { 735 pri = 0; 736 while (isdigit(*++p)) 737 pri = 10 * pri + (*p - '0'); 738 if (*p == '>') 739 ++p; 740 if ((pri & LOG_FACMASK) == LOG_CONSOLE) 741 flags |= IGN_CONS; 742 } else { 743 /* kernel printf's come out on console */ 744 flags |= IGN_CONS; 745 } 746 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 747 pri = DEFSPRI; 748 logmsg(pri, p, LocalHostName, flags); 749 } 750 751 time_t now; 752 753 /* 754 * Log a message to the appropriate log files, users, etc. based on 755 * the priority. 756 */ 757 void 758 logmsg(pri, msg, from, flags) 759 int pri; 760 const char *msg, *from; 761 int flags; 762 { 763 struct filed *f; 764 int i, fac, msglen, omask, prilev; 765 const char *timestamp; 766 char prog[NAME_MAX+1]; 767 char buf[MAXLINE+1]; 768 769 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 770 pri, flags, from, msg); 771 772 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 773 774 /* 775 * Check to see if msg looks non-standard. 776 */ 777 msglen = strlen(msg); 778 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 779 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 780 flags |= ADDDATE; 781 782 (void)time(&now); 783 if (flags & ADDDATE) 784 timestamp = ctime(&now) + 4; 785 else { 786 timestamp = msg; 787 msg += 16; 788 msglen -= 16; 789 } 790 791 /* skip leading blanks */ 792 while (isspace(*msg)) { 793 msg++; 794 msglen--; 795 } 796 797 /* extract facility and priority level */ 798 if (flags & MARK) 799 fac = LOG_NFACILITIES; 800 else 801 fac = LOG_FAC(pri); 802 prilev = LOG_PRI(pri); 803 804 /* extract program name */ 805 for (i = 0; i < NAME_MAX; i++) { 806 if (!isalnum(msg[i])) 807 break; 808 prog[i] = msg[i]; 809 } 810 prog[i] = 0; 811 812 /* add kernel prefix for kernel messages */ 813 if (flags & ISKERNEL) { 814 snprintf(buf, sizeof(buf), "%s: %s", 815 use_bootfile ? bootfile : "kernel", msg); 816 msg = buf; 817 msglen = strlen(buf); 818 } 819 820 /* log the message to the particular outputs */ 821 if (!Initialized) { 822 f = &consfile; 823 f->f_file = open(ctty, O_WRONLY, 0); 824 825 if (f->f_file >= 0) { 826 fprintlog(f, flags, msg); 827 (void)close(f->f_file); 828 } 829 (void)sigsetmask(omask); 830 return; 831 } 832 for (f = Files; f; f = f->f_next) { 833 /* skip messages that are incorrect priority */ 834 if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev)) 835 ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev)) 836 ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev)) 837 ) 838 || f->f_pmask[fac] == INTERNAL_NOPRI) 839 continue; 840 /* skip messages with the incorrect hostname */ 841 if (f->f_host) 842 switch (f->f_host[0]) { 843 case '+': 844 if (strcmp(from, f->f_host + 1) != 0) 845 continue; 846 break; 847 case '-': 848 if (strcmp(from, f->f_host + 1) == 0) 849 continue; 850 break; 851 } 852 853 /* skip messages with the incorrect program name */ 854 if (f->f_program) 855 if (strcmp(prog, f->f_program) != 0) 856 continue; 857 858 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 859 continue; 860 861 /* don't output marks to recently written files */ 862 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 863 continue; 864 865 /* 866 * suppress duplicate lines to this file 867 */ 868 if ((flags & MARK) == 0 && msglen == f->f_prevlen && 869 !strcmp(msg, f->f_prevline) && 870 !strcasecmp(from, f->f_prevhost)) { 871 (void)strlcpy(f->f_lasttime, timestamp, 16); 872 f->f_prevcount++; 873 dprintf("msg repeated %d times, %ld sec of %d\n", 874 f->f_prevcount, (long)(now - f->f_time), 875 repeatinterval[f->f_repeatcount]); 876 /* 877 * If domark would have logged this by now, 878 * flush it now (so we don't hold isolated messages), 879 * but back off so we'll flush less often 880 * in the future. 881 */ 882 if (now > REPEATTIME(f)) { 883 fprintlog(f, flags, (char *)NULL); 884 BACKOFF(f); 885 } 886 } else { 887 /* new line, save it */ 888 if (f->f_prevcount) 889 fprintlog(f, 0, (char *)NULL); 890 f->f_repeatcount = 0; 891 f->f_prevpri = pri; 892 (void)strlcpy(f->f_lasttime, timestamp, 16); 893 (void)strlcpy(f->f_prevhost, from, 894 sizeof(f->f_prevhost)); 895 if (msglen < MAXSVLINE) { 896 f->f_prevlen = msglen; 897 (void)strlcpy(f->f_prevline, msg, sizeof(f->f_prevline)); 898 fprintlog(f, flags, (char *)NULL); 899 } else { 900 f->f_prevline[0] = 0; 901 f->f_prevlen = 0; 902 fprintlog(f, flags, msg); 903 } 904 } 905 } 906 (void)sigsetmask(omask); 907 } 908 909 void 910 fprintlog(f, flags, msg) 911 struct filed *f; 912 int flags; 913 const char *msg; 914 { 915 struct iovec iov[7]; 916 struct iovec *v; 917 struct addrinfo *r; 918 int i, l, lsent = 0; 919 char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg; 920 const char *msgret; 921 922 v = iov; 923 if (f->f_type == F_WALL) { 924 v->iov_base = greetings; 925 v->iov_len = snprintf(greetings, sizeof greetings, 926 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 927 f->f_prevhost, ctime(&now)); 928 if (v->iov_len > 0) 929 v++; 930 v->iov_base = ""; 931 v->iov_len = 0; 932 v++; 933 } else { 934 v->iov_base = f->f_lasttime; 935 v->iov_len = 15; 936 v++; 937 v->iov_base = " "; 938 v->iov_len = 1; 939 v++; 940 } 941 942 if (LogFacPri) { 943 static char fp_buf[30]; /* Hollow laugh */ 944 int fac = f->f_prevpri & LOG_FACMASK; 945 int pri = LOG_PRI(f->f_prevpri); 946 const char *f_s = NULL; 947 char f_n[5]; /* Hollow laugh */ 948 const char *p_s = NULL; 949 char p_n[5]; /* Hollow laugh */ 950 951 if (LogFacPri > 1) { 952 CODE *c; 953 954 for (c = facilitynames; c->c_name; c++) { 955 if (c->c_val == fac) { 956 f_s = c->c_name; 957 break; 958 } 959 } 960 for (c = prioritynames; c->c_name; c++) { 961 if (c->c_val == pri) { 962 p_s = c->c_name; 963 break; 964 } 965 } 966 } 967 if (!f_s) { 968 snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac)); 969 f_s = f_n; 970 } 971 if (!p_s) { 972 snprintf(p_n, sizeof p_n, "%d", pri); 973 p_s = p_n; 974 } 975 snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s); 976 v->iov_base = fp_buf; 977 v->iov_len = strlen(fp_buf); 978 } else { 979 v->iov_base=""; 980 v->iov_len = 0; 981 } 982 v++; 983 984 v->iov_base = f->f_prevhost; 985 v->iov_len = strlen(v->iov_base); 986 v++; 987 v->iov_base = " "; 988 v->iov_len = 1; 989 v++; 990 991 if (msg) { 992 wmsg = strdup(msg); /* XXX iov_base needs a `const' sibling. */ 993 if (wmsg == NULL) { 994 logerror("strdup"); 995 exit(1); 996 } 997 v->iov_base = wmsg; 998 v->iov_len = strlen(msg); 999 } else if (f->f_prevcount > 1) { 1000 v->iov_base = repbuf; 1001 v->iov_len = snprintf(repbuf, sizeof repbuf, 1002 "last message repeated %d times", f->f_prevcount); 1003 } else { 1004 v->iov_base = f->f_prevline; 1005 v->iov_len = f->f_prevlen; 1006 } 1007 v++; 1008 1009 dprintf("Logging to %s", TypeNames[f->f_type]); 1010 f->f_time = now; 1011 1012 switch (f->f_type) { 1013 case F_UNUSED: 1014 dprintf("\n"); 1015 break; 1016 1017 case F_FORW: 1018 dprintf(" %s\n", f->f_un.f_forw.f_hname); 1019 /* check for local vs remote messages */ 1020 if (strcasecmp(f->f_prevhost, LocalHostName)) 1021 l = snprintf(line, sizeof line - 1, 1022 "<%d>%.15s Forwarded from %s: %s", 1023 f->f_prevpri, iov[0].iov_base, f->f_prevhost, 1024 iov[5].iov_base); 1025 else 1026 l = snprintf(line, sizeof line - 1, "<%d>%.15s %s", 1027 f->f_prevpri, iov[0].iov_base, iov[5].iov_base); 1028 if (l < 0) 1029 l = 0; 1030 else if (l > MAXLINE) 1031 l = MAXLINE; 1032 1033 if (finet) { 1034 for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) { 1035 for (i = 0; i < *finet; i++) { 1036 #if 0 1037 /* 1038 * should we check AF first, or just 1039 * trial and error? FWD 1040 */ 1041 if (r->ai_family == 1042 address_family_of(finet[i+1])) 1043 #endif 1044 lsent = sendto(finet[i+1], line, l, 0, 1045 r->ai_addr, r->ai_addrlen); 1046 if (lsent == l) 1047 break; 1048 } 1049 if (lsent == l && !send_to_all) 1050 break; 1051 } 1052 if (lsent != l) { 1053 int e = errno; 1054 (void)close(f->f_file); 1055 errno = e; 1056 f->f_type = F_UNUSED; 1057 logerror("sendto"); 1058 } 1059 } 1060 break; 1061 1062 case F_FILE: 1063 dprintf(" %s\n", f->f_un.f_fname); 1064 v->iov_base = "\n"; 1065 v->iov_len = 1; 1066 if (writev(f->f_file, iov, 7) < 0) { 1067 int e = errno; 1068 (void)close(f->f_file); 1069 f->f_type = F_UNUSED; 1070 errno = e; 1071 logerror(f->f_un.f_fname); 1072 } else if (flags & SYNC_FILE) 1073 (void)fsync(f->f_file); 1074 break; 1075 1076 case F_PIPE: 1077 dprintf(" %s\n", f->f_un.f_pipe.f_pname); 1078 v->iov_base = "\n"; 1079 v->iov_len = 1; 1080 if (f->f_un.f_pipe.f_pid == 0) { 1081 if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, 1082 &f->f_un.f_pipe.f_pid)) < 0) { 1083 f->f_type = F_UNUSED; 1084 logerror(f->f_un.f_pipe.f_pname); 1085 break; 1086 } 1087 } 1088 if (writev(f->f_file, iov, 7) < 0) { 1089 int e = errno; 1090 (void)close(f->f_file); 1091 if (f->f_un.f_pipe.f_pid > 0) 1092 deadq_enter(f->f_un.f_pipe.f_pid, 1093 f->f_un.f_pipe.f_pname); 1094 f->f_un.f_pipe.f_pid = 0; 1095 errno = e; 1096 logerror(f->f_un.f_pipe.f_pname); 1097 } 1098 break; 1099 1100 case F_CONSOLE: 1101 if (flags & IGN_CONS) { 1102 dprintf(" (ignored)\n"); 1103 break; 1104 } 1105 /* FALLTHROUGH */ 1106 1107 case F_TTY: 1108 dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname); 1109 v->iov_base = "\r\n"; 1110 v->iov_len = 2; 1111 1112 errno = 0; /* ttymsg() only sometimes returns an errno */ 1113 if ((msgret = ttymsg(iov, 7, f->f_un.f_fname, 10))) { 1114 f->f_type = F_UNUSED; 1115 logerror(msgret); 1116 } 1117 break; 1118 1119 case F_USERS: 1120 case F_WALL: 1121 dprintf("\n"); 1122 v->iov_base = "\r\n"; 1123 v->iov_len = 2; 1124 wallmsg(f, iov); 1125 break; 1126 } 1127 f->f_prevcount = 0; 1128 if (msg) 1129 free(wmsg); 1130 } 1131 1132 /* 1133 * WALLMSG -- Write a message to the world at large 1134 * 1135 * Write the specified message to either the entire 1136 * world, or a list of approved users. 1137 */ 1138 void 1139 wallmsg(f, iov) 1140 struct filed *f; 1141 struct iovec *iov; 1142 { 1143 static int reenter; /* avoid calling ourselves */ 1144 FILE *uf; 1145 struct utmp ut; 1146 int i; 1147 const char *p; 1148 char line[sizeof(ut.ut_line) + 1]; 1149 1150 if (reenter++) 1151 return; 1152 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) { 1153 logerror(_PATH_UTMP); 1154 reenter = 0; 1155 return; 1156 } 1157 /* NOSTRICT */ 1158 while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) { 1159 if (ut.ut_name[0] == '\0') 1160 continue; 1161 (void)strlcpy(line, ut.ut_line, sizeof(line)); 1162 if (f->f_type == F_WALL) { 1163 if ((p = ttymsg(iov, 7, line, TTYMSGTIME)) != NULL) { 1164 errno = 0; /* already in msg */ 1165 logerror(p); 1166 } 1167 continue; 1168 } 1169 /* should we send the message to this user? */ 1170 for (i = 0; i < MAXUNAMES; i++) { 1171 if (!f->f_un.f_uname[i][0]) 1172 break; 1173 if (!strncmp(f->f_un.f_uname[i], ut.ut_name, 1174 UT_NAMESIZE)) { 1175 if ((p = ttymsg(iov, 7, line, TTYMSGTIME)) 1176 != NULL) { 1177 errno = 0; /* already in msg */ 1178 logerror(p); 1179 } 1180 break; 1181 } 1182 } 1183 } 1184 (void)fclose(uf); 1185 reenter = 0; 1186 } 1187 1188 void 1189 reapchild(signo) 1190 int signo __unused; 1191 { 1192 int status; 1193 pid_t pid; 1194 struct filed *f; 1195 1196 while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { 1197 if (!Initialized) 1198 /* Don't tell while we are initting. */ 1199 continue; 1200 1201 /* First, look if it's a process from the dead queue. */ 1202 if (deadq_remove(pid)) 1203 goto oncemore; 1204 1205 /* Now, look in list of active processes. */ 1206 for (f = Files; f; f = f->f_next) 1207 if (f->f_type == F_PIPE && 1208 f->f_un.f_pipe.f_pid == pid) { 1209 (void)close(f->f_file); 1210 f->f_un.f_pipe.f_pid = 0; 1211 log_deadchild(pid, status, 1212 f->f_un.f_pipe.f_pname); 1213 break; 1214 } 1215 oncemore: 1216 continue; 1217 } 1218 } 1219 1220 /* 1221 * Return a printable representation of a host address. 1222 */ 1223 const char * 1224 cvthname(f) 1225 struct sockaddr *f; 1226 { 1227 int error; 1228 sigset_t omask, nmask; 1229 char *p; 1230 static char hname[NI_MAXHOST], ip[NI_MAXHOST]; 1231 1232 error = getnameinfo((struct sockaddr *)f, 1233 ((struct sockaddr *)f)->sa_len, 1234 ip, sizeof ip, NULL, 0, 1235 NI_NUMERICHOST | withscopeid); 1236 dprintf("cvthname(%s)\n", ip); 1237 1238 if (error) { 1239 dprintf("Malformed from address %s\n", gai_strerror(error)); 1240 return ("???"); 1241 } 1242 if (!resolve) 1243 return (ip); 1244 1245 sigemptyset(&nmask); 1246 sigaddset(&nmask, SIGHUP); 1247 sigprocmask(SIG_BLOCK, &nmask, &omask); 1248 error = getnameinfo((struct sockaddr *)f, 1249 ((struct sockaddr *)f)->sa_len, 1250 hname, sizeof hname, NULL, 0, 1251 NI_NAMEREQD | withscopeid); 1252 sigprocmask(SIG_SETMASK, &omask, NULL); 1253 if (error) { 1254 dprintf("Host name for your address (%s) unknown\n", ip); 1255 return (ip); 1256 } 1257 /* XXX Not quite correct, but close enough for government work. */ 1258 if ((p = strchr(hname, '.')) && strcasecmp(p + 1, LocalDomain) == 0) 1259 *p = '\0'; 1260 return (hname); 1261 } 1262 1263 void 1264 dodie(signo) 1265 int signo; 1266 { 1267 1268 WantDie = signo; 1269 } 1270 1271 void 1272 domark(signo) 1273 int signo __unused; 1274 { 1275 1276 MarkSet = 1; 1277 } 1278 1279 /* 1280 * Print syslogd errors some place. 1281 */ 1282 void 1283 logerror(type) 1284 const char *type; 1285 { 1286 char buf[512]; 1287 1288 if (errno) 1289 (void)snprintf(buf, 1290 sizeof buf, "syslogd: %s: %s", type, strerror(errno)); 1291 else 1292 (void)snprintf(buf, sizeof buf, "syslogd: %s", type); 1293 errno = 0; 1294 dprintf("%s\n", buf); 1295 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 1296 } 1297 1298 void 1299 die(signo) 1300 int signo; 1301 { 1302 struct filed *f; 1303 int was_initialized; 1304 char buf[100]; 1305 int i; 1306 1307 was_initialized = Initialized; 1308 Initialized = 0; /* Don't log SIGCHLDs. */ 1309 for (f = Files; f != NULL; f = f->f_next) { 1310 /* flush any pending output */ 1311 if (f->f_prevcount) 1312 fprintlog(f, 0, (char *)NULL); 1313 if (f->f_type == F_PIPE) 1314 (void)close(f->f_file); 1315 } 1316 Initialized = was_initialized; 1317 if (signo) { 1318 dprintf("syslogd: exiting on signal %d\n", signo); 1319 (void)snprintf(buf, sizeof(buf), "exiting on signal %d", signo); 1320 errno = 0; 1321 logerror(buf); 1322 } 1323 for (i = 0; i < nfunix; i++) 1324 if (funixn[i] && funix[i] != -1) 1325 (void)unlink(funixn[i]); 1326 exit(1); 1327 } 1328 1329 /* 1330 * INIT -- Initialize syslogd from configuration table 1331 */ 1332 void 1333 init(signo) 1334 int signo; 1335 { 1336 int i; 1337 FILE *cf; 1338 struct filed *f, *next, **nextp; 1339 char *p; 1340 char cline[LINE_MAX]; 1341 char prog[NAME_MAX+1]; 1342 char host[MAXHOSTNAMELEN]; 1343 char oldLocalHostName[MAXHOSTNAMELEN]; 1344 char hostMsg[2*MAXHOSTNAMELEN+40]; 1345 char bootfileMsg[LINE_MAX]; 1346 1347 dprintf("init\n"); 1348 1349 /* 1350 * Load hostname (may have changed). 1351 */ 1352 if (signo != 0) 1353 (void)strlcpy(oldLocalHostName, LocalHostName, 1354 sizeof(oldLocalHostName)); 1355 if (gethostname(LocalHostName, sizeof(LocalHostName))) 1356 err(EX_OSERR, "gethostname() failed"); 1357 if ((p = strchr(LocalHostName, '.')) != NULL) { 1358 *p++ = '\0'; 1359 LocalDomain = p; 1360 } else 1361 LocalDomain = ""; 1362 1363 /* 1364 * Close all open log files. 1365 */ 1366 Initialized = 0; 1367 for (f = Files; f != NULL; f = next) { 1368 /* flush any pending output */ 1369 if (f->f_prevcount) 1370 fprintlog(f, 0, (char *)NULL); 1371 1372 switch (f->f_type) { 1373 case F_FILE: 1374 case F_FORW: 1375 case F_CONSOLE: 1376 case F_TTY: 1377 (void)close(f->f_file); 1378 break; 1379 case F_PIPE: 1380 (void)close(f->f_file); 1381 if (f->f_un.f_pipe.f_pid > 0) 1382 deadq_enter(f->f_un.f_pipe.f_pid, 1383 f->f_un.f_pipe.f_pname); 1384 f->f_un.f_pipe.f_pid = 0; 1385 break; 1386 } 1387 next = f->f_next; 1388 if (f->f_program) free(f->f_program); 1389 if (f->f_host) free(f->f_host); 1390 free((char *)f); 1391 } 1392 Files = NULL; 1393 nextp = &Files; 1394 1395 /* open the configuration file */ 1396 if ((cf = fopen(ConfFile, "r")) == NULL) { 1397 dprintf("cannot open %s\n", ConfFile); 1398 *nextp = (struct filed *)calloc(1, sizeof(*f)); 1399 if (*nextp == NULL) { 1400 logerror("calloc"); 1401 exit(1); 1402 } 1403 cfline("*.ERR\t/dev/console", *nextp, "*", "*"); 1404 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 1405 if ((*nextp)->f_next == NULL) { 1406 logerror("calloc"); 1407 exit(1); 1408 } 1409 cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*"); 1410 Initialized = 1; 1411 return; 1412 } 1413 1414 /* 1415 * Foreach line in the conf table, open that file. 1416 */ 1417 f = NULL; 1418 (void)strlcpy(host, "*", sizeof(host)); 1419 (void)strlcpy(prog, "*", sizeof(prog)); 1420 while (fgets(cline, sizeof(cline), cf) != NULL) { 1421 /* 1422 * check for end-of-section, comments, strip off trailing 1423 * spaces and newline character. #!prog is treated specially: 1424 * following lines apply only to that program. 1425 */ 1426 for (p = cline; isspace(*p); ++p) 1427 continue; 1428 if (*p == 0) 1429 continue; 1430 if (*p == '#') { 1431 p++; 1432 if (*p != '!' && *p != '+' && *p != '-') 1433 continue; 1434 } 1435 if (*p == '+' || *p == '-') { 1436 host[0] = *p++; 1437 while (isspace(*p)) 1438 p++; 1439 if ((!*p) || (*p == '*')) { 1440 (void)strlcpy(host, "*", sizeof(host)); 1441 continue; 1442 } 1443 if (*p == '@') 1444 p = LocalHostName; 1445 for (i = 1; i < MAXHOSTNAMELEN - 1; i++) { 1446 if (!isalnum(*p) && *p != '.' && *p != '-') 1447 break; 1448 host[i] = *p++; 1449 } 1450 host[i] = '\0'; 1451 continue; 1452 } 1453 if (*p == '!') { 1454 p++; 1455 while (isspace(*p)) p++; 1456 if ((!*p) || (*p == '*')) { 1457 (void)strlcpy(prog, "*", sizeof(prog)); 1458 continue; 1459 } 1460 for (i = 0; i < NAME_MAX; i++) { 1461 if (!isalnum(p[i])) 1462 break; 1463 prog[i] = p[i]; 1464 } 1465 prog[i] = 0; 1466 continue; 1467 } 1468 for (p = strchr(cline, '\0'); isspace(*--p);) 1469 continue; 1470 *++p = '\0'; 1471 f = (struct filed *)calloc(1, sizeof(*f)); 1472 if (f == NULL) { 1473 logerror("calloc"); 1474 exit(1); 1475 } 1476 *nextp = f; 1477 nextp = &f->f_next; 1478 cfline(cline, f, prog, host); 1479 } 1480 1481 /* close the configuration file */ 1482 (void)fclose(cf); 1483 1484 Initialized = 1; 1485 1486 if (Debug) { 1487 for (f = Files; f; f = f->f_next) { 1488 for (i = 0; i <= LOG_NFACILITIES; i++) 1489 if (f->f_pmask[i] == INTERNAL_NOPRI) 1490 printf("X "); 1491 else 1492 printf("%d ", f->f_pmask[i]); 1493 printf("%s: ", TypeNames[f->f_type]); 1494 switch (f->f_type) { 1495 case F_FILE: 1496 printf("%s", f->f_un.f_fname); 1497 break; 1498 1499 case F_CONSOLE: 1500 case F_TTY: 1501 printf("%s%s", _PATH_DEV, f->f_un.f_fname); 1502 break; 1503 1504 case F_FORW: 1505 printf("%s", f->f_un.f_forw.f_hname); 1506 break; 1507 1508 case F_PIPE: 1509 printf("%s", f->f_un.f_pipe.f_pname); 1510 break; 1511 1512 case F_USERS: 1513 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1514 printf("%s, ", f->f_un.f_uname[i]); 1515 break; 1516 } 1517 if (f->f_program) 1518 printf(" (%s)", f->f_program); 1519 printf("\n"); 1520 } 1521 } 1522 1523 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1524 dprintf("syslogd: restarted\n"); 1525 /* 1526 * Log a change in hostname, but only on a restart. 1527 */ 1528 if (signo != 0 && strcmp(oldLocalHostName, LocalHostName) != 0) { 1529 (void)snprintf(hostMsg, sizeof(hostMsg), 1530 "syslogd: hostname changed, \"%s\" to \"%s\"", 1531 oldLocalHostName, LocalHostName); 1532 logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE); 1533 dprintf("%s\n", hostMsg); 1534 } 1535 /* 1536 * Log the kernel boot file if we aren't going to use it as 1537 * the prefix, and if this is *not* a restart. 1538 */ 1539 if (signo == 0 && !use_bootfile) { 1540 (void)snprintf(bootfileMsg, sizeof(bootfileMsg), 1541 "syslogd: kernel boot file is %s", bootfile); 1542 logmsg(LOG_KERN|LOG_INFO, bootfileMsg, LocalHostName, ADDDATE); 1543 dprintf("%s\n", bootfileMsg); 1544 } 1545 } 1546 1547 /* 1548 * Crack a configuration file line 1549 */ 1550 void 1551 cfline(line, f, prog, host) 1552 const char *line; 1553 struct filed *f; 1554 const char *prog; 1555 const char *host; 1556 { 1557 struct addrinfo hints, *res; 1558 int error, i, pri; 1559 const char *p, *q; 1560 char *bp; 1561 char buf[MAXLINE], ebuf[100]; 1562 1563 dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host); 1564 1565 errno = 0; /* keep strerror() stuff out of logerror messages */ 1566 1567 /* clear out file entry */ 1568 memset(f, 0, sizeof(*f)); 1569 for (i = 0; i <= LOG_NFACILITIES; i++) 1570 f->f_pmask[i] = INTERNAL_NOPRI; 1571 1572 /* save hostname if any */ 1573 if (host && *host == '*') 1574 host = NULL; 1575 if (host) { 1576 int hl, dl; 1577 1578 f->f_host = strdup(host); 1579 if (f->f_host == NULL) { 1580 logerror("strdup"); 1581 exit(1); 1582 } 1583 hl = strlen(f->f_host); 1584 if (f->f_host[hl-1] == '.') 1585 f->f_host[--hl] = '\0'; 1586 dl = strlen(LocalDomain) + 1; 1587 if (hl > dl && f->f_host[hl-dl] == '.' && 1588 strcasecmp(f->f_host + hl - dl + 1, LocalDomain) == 0) 1589 f->f_host[hl-dl] = '\0'; 1590 } 1591 1592 /* save program name if any */ 1593 if (prog && *prog == '*') 1594 prog = NULL; 1595 if (prog) { 1596 f->f_program = strdup(prog); 1597 if (f->f_program == NULL) { 1598 logerror("strdup"); 1599 exit(1); 1600 } 1601 } 1602 1603 /* scan through the list of selectors */ 1604 for (p = line; *p && *p != '\t' && *p != ' ';) { 1605 int pri_done; 1606 int pri_cmp; 1607 1608 /* find the end of this facility name list */ 1609 for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; ) 1610 continue; 1611 1612 /* get the priority comparison */ 1613 pri_cmp = 0; 1614 pri_done = 0; 1615 while (!pri_done) { 1616 switch (*q) { 1617 case '<': 1618 pri_cmp |= PRI_LT; 1619 q++; 1620 break; 1621 case '=': 1622 pri_cmp |= PRI_EQ; 1623 q++; 1624 break; 1625 case '>': 1626 pri_cmp |= PRI_GT; 1627 q++; 1628 break; 1629 default: 1630 pri_done++; 1631 break; 1632 } 1633 } 1634 if (!pri_cmp) 1635 pri_cmp = (UniquePriority) 1636 ? (PRI_EQ) 1637 : (PRI_EQ | PRI_GT) 1638 ; 1639 1640 /* collect priority name */ 1641 for (bp = buf; *q && !strchr("\t,; ", *q); ) 1642 *bp++ = *q++; 1643 *bp = '\0'; 1644 1645 /* skip cruft */ 1646 while (strchr(",;", *q)) 1647 q++; 1648 1649 /* decode priority name */ 1650 if (*buf == '*') 1651 pri = LOG_PRIMASK + 1; 1652 else { 1653 pri = decode(buf, prioritynames); 1654 if (pri < 0) { 1655 (void)snprintf(ebuf, sizeof ebuf, 1656 "unknown priority name \"%s\"", buf); 1657 logerror(ebuf); 1658 return; 1659 } 1660 } 1661 1662 /* scan facilities */ 1663 while (*p && !strchr("\t.; ", *p)) { 1664 for (bp = buf; *p && !strchr("\t,;. ", *p); ) 1665 *bp++ = *p++; 1666 *bp = '\0'; 1667 1668 if (*buf == '*') 1669 for (i = 0; i < LOG_NFACILITIES; i++) { 1670 f->f_pmask[i] = pri; 1671 f->f_pcmp[i] = pri_cmp; 1672 } 1673 else { 1674 i = decode(buf, facilitynames); 1675 if (i < 0) { 1676 (void)snprintf(ebuf, sizeof ebuf, 1677 "unknown facility name \"%s\"", 1678 buf); 1679 logerror(ebuf); 1680 return; 1681 } 1682 f->f_pmask[i >> 3] = pri; 1683 f->f_pcmp[i >> 3] = pri_cmp; 1684 } 1685 while (*p == ',' || *p == ' ') 1686 p++; 1687 } 1688 1689 p = q; 1690 } 1691 1692 /* skip to action part */ 1693 while (*p == '\t' || *p == ' ') 1694 p++; 1695 1696 switch (*p) 1697 { 1698 case '@': 1699 (void)strlcpy(f->f_un.f_forw.f_hname, ++p, 1700 sizeof(f->f_un.f_forw.f_hname)); 1701 memset(&hints, 0, sizeof(hints)); 1702 hints.ai_family = family; 1703 hints.ai_socktype = SOCK_DGRAM; 1704 error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, 1705 &res); 1706 if (error) { 1707 logerror(gai_strerror(error)); 1708 break; 1709 } 1710 f->f_un.f_forw.f_addr = res; 1711 f->f_type = F_FORW; 1712 break; 1713 1714 case '/': 1715 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { 1716 f->f_type = F_UNUSED; 1717 logerror(p); 1718 break; 1719 } 1720 if (isatty(f->f_file)) { 1721 if (strcmp(p, ctty) == 0) 1722 f->f_type = F_CONSOLE; 1723 else 1724 f->f_type = F_TTY; 1725 (void)strlcpy(f->f_un.f_fname, p + sizeof(_PATH_DEV) - 1, 1726 sizeof(f->f_un.f_fname)); 1727 } else { 1728 (void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname)); 1729 f->f_type = F_FILE; 1730 } 1731 break; 1732 1733 case '|': 1734 f->f_un.f_pipe.f_pid = 0; 1735 (void)strlcpy(f->f_un.f_fname, p + 1, sizeof(f->f_un.f_fname)); 1736 f->f_type = F_PIPE; 1737 break; 1738 1739 case '*': 1740 f->f_type = F_WALL; 1741 break; 1742 1743 default: 1744 for (i = 0; i < MAXUNAMES && *p; i++) { 1745 for (q = p; *q && *q != ','; ) 1746 q++; 1747 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1748 if ((q - p) > UT_NAMESIZE) 1749 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1750 else 1751 f->f_un.f_uname[i][q - p] = '\0'; 1752 while (*q == ',' || *q == ' ') 1753 q++; 1754 p = q; 1755 } 1756 f->f_type = F_USERS; 1757 break; 1758 } 1759 } 1760 1761 1762 /* 1763 * Decode a symbolic name to a numeric value 1764 */ 1765 int 1766 decode(name, codetab) 1767 const char *name; 1768 CODE *codetab; 1769 { 1770 CODE *c; 1771 char *p, buf[40]; 1772 1773 if (isdigit(*name)) 1774 return (atoi(name)); 1775 1776 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1777 if (isupper(*name)) 1778 *p = tolower(*name); 1779 else 1780 *p = *name; 1781 } 1782 *p = '\0'; 1783 for (c = codetab; c->c_name; c++) 1784 if (!strcmp(buf, c->c_name)) 1785 return (c->c_val); 1786 1787 return (-1); 1788 } 1789 1790 void 1791 markit(void) 1792 { 1793 struct filed *f; 1794 dq_t q; 1795 1796 now = time((time_t *)NULL); 1797 MarkSeq += TIMERINTVL; 1798 if (MarkSeq >= MarkInterval) { 1799 logmsg(LOG_INFO, "-- MARK --", 1800 LocalHostName, ADDDATE|MARK); 1801 MarkSeq = 0; 1802 } 1803 1804 for (f = Files; f; f = f->f_next) { 1805 if (f->f_prevcount && now >= REPEATTIME(f)) { 1806 dprintf("flush %s: repeated %d times, %d sec.\n", 1807 TypeNames[f->f_type], f->f_prevcount, 1808 repeatinterval[f->f_repeatcount]); 1809 fprintlog(f, 0, (char *)NULL); 1810 BACKOFF(f); 1811 } 1812 } 1813 1814 /* Walk the dead queue, and see if we should signal somebody. */ 1815 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries)) 1816 switch (q->dq_timeout) { 1817 case 0: 1818 /* Already signalled once, try harder now. */ 1819 if (kill(q->dq_pid, SIGKILL) != 0) 1820 (void)deadq_remove(q->dq_pid); 1821 break; 1822 1823 case 1: 1824 /* 1825 * Timed out on dead queue, send terminate 1826 * signal. Note that we leave the removal 1827 * from the dead queue to reapchild(), which 1828 * will also log the event (unless the process 1829 * didn't even really exist, in case we simply 1830 * drop it from the dead queue). 1831 */ 1832 if (kill(q->dq_pid, SIGTERM) != 0) 1833 (void)deadq_remove(q->dq_pid); 1834 /* FALLTHROUGH */ 1835 1836 default: 1837 q->dq_timeout--; 1838 } 1839 MarkSet = 0; 1840 (void)alarm(TIMERINTVL); 1841 } 1842 1843 /* 1844 * fork off and become a daemon, but wait for the child to come online 1845 * before returing to the parent, or we get disk thrashing at boot etc. 1846 * Set a timer so we don't hang forever if it wedges. 1847 */ 1848 int 1849 waitdaemon(nochdir, noclose, maxwait) 1850 int nochdir, noclose, maxwait; 1851 { 1852 int fd; 1853 int status; 1854 pid_t pid, childpid; 1855 1856 switch (childpid = fork()) { 1857 case -1: 1858 return (-1); 1859 case 0: 1860 break; 1861 default: 1862 signal(SIGALRM, timedout); 1863 alarm(maxwait); 1864 while ((pid = wait3(&status, 0, NULL)) != -1) { 1865 if (WIFEXITED(status)) 1866 errx(1, "child pid %d exited with return code %d", 1867 pid, WEXITSTATUS(status)); 1868 if (WIFSIGNALED(status)) 1869 errx(1, "child pid %d exited on signal %d%s", 1870 pid, WTERMSIG(status), 1871 WCOREDUMP(status) ? " (core dumped)" : 1872 ""); 1873 if (pid == childpid) /* it's gone... */ 1874 break; 1875 } 1876 exit(0); 1877 } 1878 1879 if (setsid() == -1) 1880 return (-1); 1881 1882 if (!nochdir) 1883 (void)chdir("/"); 1884 1885 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1886 (void)dup2(fd, STDIN_FILENO); 1887 (void)dup2(fd, STDOUT_FILENO); 1888 (void)dup2(fd, STDERR_FILENO); 1889 if (fd > 2) 1890 (void)close (fd); 1891 } 1892 return (getppid()); 1893 } 1894 1895 /* 1896 * We get a SIGALRM from the child when it's running and finished doing it's 1897 * fsync()'s or O_SYNC writes for all the boot messages. 1898 * 1899 * We also get a signal from the kernel if the timer expires, so check to 1900 * see what happened. 1901 */ 1902 void 1903 timedout(sig) 1904 int sig __unused; 1905 { 1906 int left; 1907 left = alarm(0); 1908 signal(SIGALRM, SIG_DFL); 1909 if (left == 0) 1910 errx(1, "timed out waiting for child"); 1911 else 1912 _exit(0); 1913 } 1914 1915 /* 1916 * Add `s' to the list of allowable peer addresses to accept messages 1917 * from. 1918 * 1919 * `s' is a string in the form: 1920 * 1921 * [*]domainname[:{servicename|portnumber|*}] 1922 * 1923 * or 1924 * 1925 * netaddr/maskbits[:{servicename|portnumber|*}] 1926 * 1927 * Returns -1 on error, 0 if the argument was valid. 1928 */ 1929 int 1930 allowaddr(s) 1931 char *s; 1932 { 1933 char *cp1, *cp2; 1934 struct allowedpeer ap; 1935 struct servent *se; 1936 int masklen = -1, i; 1937 struct addrinfo hints, *res; 1938 struct in_addr *addrp, *maskp; 1939 u_int32_t *addr6p, *mask6p; 1940 char ip[NI_MAXHOST]; 1941 1942 #ifdef INET6 1943 if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL) 1944 #endif 1945 cp1 = s; 1946 if ((cp1 = strrchr(cp1, ':'))) { 1947 /* service/port provided */ 1948 *cp1++ = '\0'; 1949 if (strlen(cp1) == 1 && *cp1 == '*') 1950 /* any port allowed */ 1951 ap.port = 0; 1952 else if ((se = getservbyname(cp1, "udp"))) 1953 ap.port = ntohs(se->s_port); 1954 else { 1955 ap.port = strtol(cp1, &cp2, 0); 1956 if (*cp2 != '\0') 1957 return -1; /* port not numeric */ 1958 } 1959 } else { 1960 if ((se = getservbyname("syslog", "udp"))) 1961 ap.port = ntohs(se->s_port); 1962 else 1963 /* sanity, should not happen */ 1964 ap.port = 514; 1965 } 1966 1967 if ((cp1 = strchr(s, '/')) != NULL && 1968 strspn(cp1 + 1, "0123456789") == strlen(cp1 + 1)) { 1969 *cp1 = '\0'; 1970 if ((masklen = atoi(cp1 + 1)) < 0) 1971 return -1; 1972 } 1973 #ifdef INET6 1974 if (*s == '[') { 1975 cp2 = s + strlen(s) - 1; 1976 if (*cp2 == ']') { 1977 ++s; 1978 *cp2 = '\0'; 1979 } else 1980 cp2 = NULL; 1981 } else 1982 cp2 = NULL; 1983 #endif 1984 memset(&hints, 0, sizeof(hints)); 1985 hints.ai_family = PF_UNSPEC; 1986 hints.ai_socktype = SOCK_DGRAM; 1987 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 1988 if (getaddrinfo(s, NULL, &hints, &res) == 0) { 1989 ap.isnumeric = 1; 1990 memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen); 1991 memset(&ap.a_mask, 0, sizeof(ap.a_mask)); 1992 ap.a_mask.ss_family = res->ai_family; 1993 if (res->ai_family == AF_INET) { 1994 ap.a_mask.ss_len = sizeof(struct sockaddr_in); 1995 maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr; 1996 addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr; 1997 if (masklen < 0) { 1998 /* use default netmask */ 1999 if (IN_CLASSA(ntohl(addrp->s_addr))) 2000 maskp->s_addr = htonl(IN_CLASSA_NET); 2001 else if (IN_CLASSB(ntohl(addrp->s_addr))) 2002 maskp->s_addr = htonl(IN_CLASSB_NET); 2003 else 2004 maskp->s_addr = htonl(IN_CLASSC_NET); 2005 } else if (masklen <= 32) { 2006 /* convert masklen to netmask */ 2007 if (masklen == 0) 2008 maskp->s_addr = 0; 2009 else 2010 maskp->s_addr = htonl(~((1 << (32 - masklen)) - 1)); 2011 } else { 2012 freeaddrinfo(res); 2013 return -1; 2014 } 2015 /* Lose any host bits in the network number. */ 2016 addrp->s_addr &= maskp->s_addr; 2017 } 2018 #ifdef INET6 2019 else if (res->ai_family == AF_INET6 && masklen <= 128) { 2020 ap.a_mask.ss_len = sizeof(struct sockaddr_in6); 2021 if (masklen < 0) 2022 masklen = 128; 2023 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2024 /* convert masklen to netmask */ 2025 while (masklen > 0) { 2026 if (masklen < 32) { 2027 *mask6p = htonl(~(0xffffffff >> masklen)); 2028 break; 2029 } 2030 *mask6p++ = 0xffffffff; 2031 masklen -= 32; 2032 } 2033 /* Lose any host bits in the network number. */ 2034 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2035 addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr; 2036 for (i = 0; i < 4; i++) 2037 addr6p[i] &= mask6p[i]; 2038 } 2039 #endif 2040 else { 2041 freeaddrinfo(res); 2042 return -1; 2043 } 2044 freeaddrinfo(res); 2045 } else { 2046 /* arg `s' is domain name */ 2047 ap.isnumeric = 0; 2048 ap.a_name = s; 2049 if (cp1) 2050 *cp1 = '/'; 2051 #ifdef INET6 2052 if (cp2) { 2053 *cp2 = ']'; 2054 --s; 2055 } 2056 #endif 2057 } 2058 2059 if (Debug) { 2060 printf("allowaddr: rule %d: ", NumAllowed); 2061 if (ap.isnumeric) { 2062 printf("numeric, "); 2063 getnameinfo((struct sockaddr *)&ap.a_addr, 2064 ((struct sockaddr *)&ap.a_addr)->sa_len, 2065 ip, sizeof ip, NULL, 0, 2066 NI_NUMERICHOST | withscopeid); 2067 printf("addr = %s, ", ip); 2068 getnameinfo((struct sockaddr *)&ap.a_mask, 2069 ((struct sockaddr *)&ap.a_mask)->sa_len, 2070 ip, sizeof ip, NULL, 0, 2071 NI_NUMERICHOST | withscopeid); 2072 printf("mask = %s; ", ip); 2073 } else 2074 printf("domainname = %s; ", ap.a_name); 2075 printf("port = %d\n", ap.port); 2076 } 2077 2078 if ((AllowedPeers = realloc(AllowedPeers, 2079 ++NumAllowed * sizeof(struct allowedpeer))) 2080 == NULL) { 2081 logerror("realloc"); 2082 exit(1); 2083 } 2084 memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer)); 2085 return 0; 2086 } 2087 2088 /* 2089 * Validate that the remote peer has permission to log to us. 2090 */ 2091 int 2092 validate(sa, hname) 2093 struct sockaddr *sa; 2094 const char *hname; 2095 { 2096 int i, j, reject; 2097 size_t l1, l2; 2098 char *cp, name[NI_MAXHOST], ip[NI_MAXHOST], port[NI_MAXSERV]; 2099 struct allowedpeer *ap; 2100 struct sockaddr_in *sin4, *a4p = NULL, *m4p = NULL; 2101 struct sockaddr_in6 *sin6, *a6p = NULL, *m6p = NULL; 2102 struct addrinfo hints, *res; 2103 u_short sport; 2104 2105 if (NumAllowed == 0) 2106 /* traditional behaviour, allow everything */ 2107 return 1; 2108 2109 (void)strlcpy(name, hname, sizeof(name)); 2110 memset(&hints, 0, sizeof(hints)); 2111 hints.ai_family = PF_UNSPEC; 2112 hints.ai_socktype = SOCK_DGRAM; 2113 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 2114 if (getaddrinfo(name, NULL, &hints, &res) == 0) 2115 freeaddrinfo(res); 2116 else if (strchr(name, '.') == NULL) { 2117 strlcat(name, ".", sizeof name); 2118 strlcat(name, LocalDomain, sizeof name); 2119 } 2120 if (getnameinfo(sa, sa->sa_len, ip, sizeof ip, port, sizeof port, 2121 NI_NUMERICHOST | withscopeid | NI_NUMERICSERV) != 0) 2122 return 0; /* for safety, should not occur */ 2123 dprintf("validate: dgram from IP %s, port %s, name %s;\n", 2124 ip, port, name); 2125 sport = atoi(port); 2126 2127 /* now, walk down the list */ 2128 for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) { 2129 if (ap->port != 0 && ap->port != sport) { 2130 dprintf("rejected in rule %d due to port mismatch.\n", i); 2131 continue; 2132 } 2133 2134 if (ap->isnumeric) { 2135 if (ap->a_addr.ss_family != sa->sa_family) { 2136 dprintf("rejected in rule %d due to address family mismatch.\n", i); 2137 continue; 2138 } 2139 if (ap->a_addr.ss_family == AF_INET) { 2140 sin4 = (struct sockaddr_in *)sa; 2141 a4p = (struct sockaddr_in *)&ap->a_addr; 2142 m4p = (struct sockaddr_in *)&ap->a_mask; 2143 if ((sin4->sin_addr.s_addr & m4p->sin_addr.s_addr) 2144 != a4p->sin_addr.s_addr) { 2145 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2146 continue; 2147 } 2148 } 2149 #ifdef INET6 2150 else if (ap->a_addr.ss_family == AF_INET6) { 2151 sin6 = (struct sockaddr_in6 *)sa; 2152 a6p = (struct sockaddr_in6 *)&ap->a_addr; 2153 m6p = (struct sockaddr_in6 *)&ap->a_mask; 2154 #ifdef NI_WITHSCOPEID 2155 if (a6p->sin6_scope_id != 0 && 2156 sin6->sin6_scope_id != a6p->sin6_scope_id) { 2157 dprintf("rejected in rule %d due to scope mismatch.\n", i); 2158 continue; 2159 } 2160 #endif 2161 reject = 0; 2162 for (j = 0; j < 16; j += 4) { 2163 if ((*(u_int32_t *)&sin6->sin6_addr.s6_addr[j] & *(u_int32_t *)&m6p->sin6_addr.s6_addr[j]) 2164 != *(u_int32_t *)&a6p->sin6_addr.s6_addr[j]) { 2165 ++reject; 2166 break; 2167 } 2168 } 2169 if (reject) { 2170 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2171 continue; 2172 } 2173 } 2174 #endif 2175 else 2176 continue; 2177 } else { 2178 cp = ap->a_name; 2179 l1 = strlen(name); 2180 if (*cp == '*') { 2181 /* allow wildmatch */ 2182 cp++; 2183 l2 = strlen(cp); 2184 if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) { 2185 dprintf("rejected in rule %d due to name mismatch.\n", i); 2186 continue; 2187 } 2188 } else { 2189 /* exact match */ 2190 l2 = strlen(cp); 2191 if (l2 != l1 || memcmp(cp, name, l1) != 0) { 2192 dprintf("rejected in rule %d due to name mismatch.\n", i); 2193 continue; 2194 } 2195 } 2196 } 2197 dprintf("accepted in rule %d.\n", i); 2198 return 1; /* hooray! */ 2199 } 2200 return 0; 2201 } 2202 2203 /* 2204 * Fairly similar to popen(3), but returns an open descriptor, as 2205 * opposed to a FILE *. 2206 */ 2207 int 2208 p_open(prog, pid) 2209 const char *prog; 2210 pid_t *pid; 2211 { 2212 int pfd[2], nulldesc, i; 2213 sigset_t omask, mask; 2214 char *argv[4]; /* sh -c cmd NULL */ 2215 char errmsg[200]; 2216 2217 if (pipe(pfd) == -1) 2218 return -1; 2219 if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) 2220 /* we are royally screwed anyway */ 2221 return -1; 2222 2223 sigemptyset(&mask); 2224 sigaddset(&mask, SIGALRM); 2225 sigaddset(&mask, SIGHUP); 2226 sigprocmask(SIG_BLOCK, &mask, &omask); 2227 switch ((*pid = fork())) { 2228 case -1: 2229 sigprocmask(SIG_SETMASK, &omask, 0); 2230 close(nulldesc); 2231 return -1; 2232 2233 case 0: 2234 /* XXX should check for NULL return */ 2235 argv[0] = strdup("sh"); 2236 argv[1] = strdup("-c"); 2237 argv[2] = strdup(prog); 2238 argv[3] = NULL; 2239 if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { 2240 logerror("strdup"); 2241 exit(1); 2242 } 2243 2244 alarm(0); 2245 (void)setsid(); /* Avoid catching SIGHUPs. */ 2246 2247 /* 2248 * Throw away pending signals, and reset signal 2249 * behaviour to standard values. 2250 */ 2251 signal(SIGALRM, SIG_IGN); 2252 signal(SIGHUP, SIG_IGN); 2253 sigprocmask(SIG_SETMASK, &omask, 0); 2254 signal(SIGPIPE, SIG_DFL); 2255 signal(SIGQUIT, SIG_DFL); 2256 signal(SIGALRM, SIG_DFL); 2257 signal(SIGHUP, SIG_DFL); 2258 2259 dup2(pfd[0], STDIN_FILENO); 2260 dup2(nulldesc, STDOUT_FILENO); 2261 dup2(nulldesc, STDERR_FILENO); 2262 for (i = getdtablesize(); i > 2; i--) 2263 (void) close(i); 2264 2265 (void) execvp(_PATH_BSHELL, argv); 2266 _exit(255); 2267 } 2268 2269 sigprocmask(SIG_SETMASK, &omask, 0); 2270 close(nulldesc); 2271 close(pfd[0]); 2272 /* 2273 * Avoid blocking on a hung pipe. With O_NONBLOCK, we are 2274 * supposed to get an EWOULDBLOCK on writev(2), which is 2275 * caught by the logic above anyway, which will in turn close 2276 * the pipe, and fork a new logging subprocess if necessary. 2277 * The stale subprocess will be killed some time later unless 2278 * it terminated itself due to closing its input pipe (so we 2279 * get rid of really dead puppies). 2280 */ 2281 if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { 2282 /* This is bad. */ 2283 (void)snprintf(errmsg, sizeof errmsg, 2284 "Warning: cannot change pipe to PID %d to " 2285 "non-blocking behaviour.", 2286 (int)*pid); 2287 logerror(errmsg); 2288 } 2289 return pfd[1]; 2290 } 2291 2292 void 2293 deadq_enter(pid, name) 2294 pid_t pid; 2295 const char *name; 2296 { 2297 dq_t p; 2298 int status; 2299 2300 /* 2301 * Be paranoid, if we can't signal the process, don't enter it 2302 * into the dead queue (perhaps it's already dead). If possible, 2303 * we try to fetch and log the child's status. 2304 */ 2305 if (kill(pid, 0) != 0) { 2306 if (waitpid(pid, &status, WNOHANG) > 0) 2307 log_deadchild(pid, status, name); 2308 return; 2309 } 2310 2311 p = malloc(sizeof(struct deadq_entry)); 2312 if (p == NULL) { 2313 logerror("malloc"); 2314 exit(1); 2315 } 2316 2317 p->dq_pid = pid; 2318 p->dq_timeout = DQ_TIMO_INIT; 2319 TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); 2320 } 2321 2322 int 2323 deadq_remove(pid) 2324 pid_t pid; 2325 { 2326 dq_t q; 2327 2328 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = TAILQ_NEXT(q, dq_entries)) 2329 if (q->dq_pid == pid) { 2330 TAILQ_REMOVE(&deadq_head, q, dq_entries); 2331 free(q); 2332 return 1; 2333 } 2334 2335 return 0; 2336 } 2337 2338 void 2339 log_deadchild(pid, status, name) 2340 pid_t pid; 2341 int status; 2342 const char *name; 2343 { 2344 int code; 2345 char buf[256]; 2346 const char *reason; 2347 2348 errno = 0; /* Keep strerror() stuff out of logerror messages. */ 2349 if (WIFSIGNALED(status)) { 2350 reason = "due to signal"; 2351 code = WTERMSIG(status); 2352 } else { 2353 reason = "with status"; 2354 code = WEXITSTATUS(status); 2355 if (code == 0) 2356 return; 2357 } 2358 (void)snprintf(buf, sizeof buf, 2359 "Logging subprocess %d (%s) exited %s %d.", 2360 pid, name, reason, code); 2361 logerror(buf); 2362 } 2363 2364 int * 2365 socksetup(af, bindhostname) 2366 int af; 2367 const char *bindhostname; 2368 { 2369 struct addrinfo hints, *res, *r; 2370 int error, maxs, *s, *socks; 2371 2372 memset(&hints, 0, sizeof(hints)); 2373 hints.ai_flags = AI_PASSIVE; 2374 hints.ai_family = af; 2375 hints.ai_socktype = SOCK_DGRAM; 2376 error = getaddrinfo(bindhostname, "syslog", &hints, &res); 2377 if (error) { 2378 logerror(gai_strerror(error)); 2379 errno = 0; 2380 die(0); 2381 } 2382 2383 /* Count max number of sockets we may open */ 2384 for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 2385 socks = malloc((maxs+1) * sizeof(int)); 2386 if (socks == NULL) { 2387 logerror("couldn't allocate memory for sockets"); 2388 die(0); 2389 } 2390 2391 *socks = 0; /* num of sockets counter at start of array */ 2392 s = socks + 1; 2393 for (r = res; r; r = r->ai_next) { 2394 *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 2395 if (*s < 0) { 2396 logerror("socket"); 2397 continue; 2398 } 2399 #ifdef IPV6_BINDV6ONLY 2400 if (r->ai_family == AF_INET6) { 2401 int on = 1; 2402 if (setsockopt(*s, IPPROTO_IPV6, IPV6_BINDV6ONLY, 2403 (char *)&on, sizeof (on)) < 0) { 2404 logerror("setsockopt"); 2405 close(*s); 2406 continue; 2407 } 2408 } 2409 #endif 2410 if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 2411 close(*s); 2412 logerror("bind"); 2413 continue; 2414 } 2415 2416 (*socks)++; 2417 s++; 2418 } 2419 2420 if (*socks == 0) { 2421 free(socks); 2422 if (Debug) 2423 return(NULL); 2424 else 2425 die(0); 2426 } 2427 if (res) 2428 freeaddrinfo(res); 2429 2430 return(socks); 2431 } 2432