1 /* $NetBSD: syslogd.c,v 1.63 2003/10/17 01:39:25 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 41 #else 42 __RCSID("$NetBSD: syslogd.c,v 1.63 2003/10/17 01:39:25 lukem Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 /* 47 * syslogd -- log system messages 48 * 49 * This program implements a system log. It takes a series of lines. 50 * Each line may have a priority, signified as "<n>" as 51 * the first characters of the line. If this is 52 * not present, a default priority is used. 53 * 54 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will 55 * cause it to reread its configuration file. 56 * 57 * Defined Constants: 58 * 59 * MAXLINE -- the maximimum line length that can be handled. 60 * DEFUPRI -- the default priority for user messages 61 * DEFSPRI -- the default priority for kernel messages 62 * 63 * Author: Eric Allman 64 * extensive changes by Ralph Campbell 65 * more extensive changes by Eric Allman (again) 66 */ 67 68 #define MAXLINE 1024 /* maximum line length */ 69 #define MAXSVLINE 120 /* maximum saved line length */ 70 #define DEFUPRI (LOG_USER|LOG_NOTICE) 71 #define DEFSPRI (LOG_KERN|LOG_CRIT) 72 #define TIMERINTVL 30 /* interval for checking flush, mark */ 73 #define TTYMSGTIME 1 /* timeout passed to ttymsg */ 74 75 #include <sys/param.h> 76 #include <sys/socket.h> 77 #include <sys/sysctl.h> 78 #include <sys/types.h> 79 #include <sys/un.h> 80 #include <sys/wait.h> 81 82 #include <netinet/in.h> 83 84 #include <ctype.h> 85 #include <errno.h> 86 #include <fcntl.h> 87 #include <grp.h> 88 #include <locale.h> 89 #include <netdb.h> 90 #include <poll.h> 91 #include <pwd.h> 92 #include <signal.h> 93 #include <stdarg.h> 94 #include <stdio.h> 95 #include <stdlib.h> 96 #include <string.h> 97 #include <unistd.h> 98 #include <util.h> 99 100 #include "utmpentry.h" 101 #include "pathnames.h" 102 103 #define SYSLOG_NAMES 104 #include <sys/syslog.h> 105 106 #ifdef LIBWRAP 107 #include <tcpd.h> 108 109 int allow_severity = LOG_AUTH|LOG_INFO; 110 int deny_severity = LOG_AUTH|LOG_WARNING; 111 #endif 112 113 char *ConfFile = _PATH_LOGCONF; 114 char ctty[] = _PATH_CONSOLE; 115 116 #define FDMASK(fd) (1 << (fd)) 117 118 #define dprintf if (Debug) printf 119 120 #define MAXUNAMES 20 /* maximum number of user names */ 121 122 /* 123 * Flags to logmsg(). 124 */ 125 126 #define IGN_CONS 0x001 /* don't print on console */ 127 #define SYNC_FILE 0x002 /* do fsync on file after printing */ 128 #define ADDDATE 0x004 /* add a date to the message */ 129 #define MARK 0x008 /* this message is a mark */ 130 131 /* 132 * This structure represents the files that will have log 133 * copies printed. 134 */ 135 136 struct filed { 137 struct filed *f_next; /* next in linked list */ 138 short f_type; /* entry type, see below */ 139 short f_file; /* file descriptor */ 140 time_t f_time; /* time this was last written */ 141 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 142 union { 143 char f_uname[MAXUNAMES][UT_NAMESIZE+1]; 144 struct { 145 char f_hname[MAXHOSTNAMELEN+1]; 146 struct addrinfo *f_addr; 147 } f_forw; /* forwarding address */ 148 char f_fname[MAXPATHLEN]; 149 } f_un; 150 char f_prevline[MAXSVLINE]; /* last message logged */ 151 char f_lasttime[16]; /* time of last occurrence */ 152 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */ 153 int f_prevpri; /* pri of f_prevline */ 154 int f_prevlen; /* length of f_prevline */ 155 int f_prevcount; /* repetition cnt of prevline */ 156 int f_repeatcount; /* number of "repeated" msgs */ 157 }; 158 159 /* 160 * Intervals at which we flush out "message repeated" messages, 161 * in seconds after previous message is logged. After each flush, 162 * we move to the next interval until we reach the largest. 163 */ 164 int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 165 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 166 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 167 #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 168 (f)->f_repeatcount = MAXREPEAT; \ 169 } 170 171 /* values for f_type */ 172 #define F_UNUSED 0 /* unused entry */ 173 #define F_FILE 1 /* regular file */ 174 #define F_TTY 2 /* terminal */ 175 #define F_CONSOLE 3 /* console terminal */ 176 #define F_FORW 4 /* remote machine */ 177 #define F_USERS 5 /* list of users */ 178 #define F_WALL 6 /* everyone logged on */ 179 180 char *TypeNames[7] = { 181 "UNUSED", "FILE", "TTY", "CONSOLE", 182 "FORW", "USERS", "WALL" 183 }; 184 185 struct filed *Files; 186 struct filed consfile; 187 188 int Debug; /* debug flag */ 189 int daemonized = 0; /* we are not daemonized yet */ 190 char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */ 191 char *LocalDomain; /* our local domain name */ 192 int *finet = NULL; /* Internet datagram sockets */ 193 int Initialized = 0; /* set when we have initialized ourselves */ 194 int MarkInterval = 20 * 60; /* interval between marks in seconds */ 195 int MarkSeq = 0; /* mark sequence number */ 196 int SecureMode = 0; /* listen only on unix domain socks */ 197 int UseNameService = 1; /* make domain name queries */ 198 int NumForwards = 0; /* number of forwarding actions in conf file */ 199 char **LogPaths; /* array of pathnames to read messages from */ 200 int NoRepeat = 0; /* disable "repeated"; log always */ 201 202 void cfline(char *, struct filed *); 203 char *cvthname(struct sockaddr_storage *); 204 int decode(const char *, CODE *); 205 void die(int); 206 void domark(int); 207 void fprintlog(struct filed *, int, char *); 208 int getmsgbufsize(void); 209 int* socksetup(int); 210 void init(int); 211 void logerror(const char *, ...); 212 void logmsg(int, char *, char *, int); 213 void printline(char *, char *); 214 void printsys(char *); 215 void reapchild(int); 216 void usage(void); 217 void wallmsg(struct filed *, struct iovec *); 218 int main(int, char *[]); 219 void logpath_add(char ***, int *, int *, char *); 220 void logpath_fileadd(char ***, int *, int *, char *); 221 222 int 223 main(int argc, char *argv[]) 224 { 225 int ch, *funix, i, j, fklog, len, linesize; 226 int *nfinetix, nfklogix, nfunixbaseix, nfds; 227 int funixsize = 0, funixmaxsize = 0; 228 struct sockaddr_un sunx, fromunix; 229 struct sockaddr_storage frominet; 230 char *p, *line, **pp; 231 struct pollfd *readfds; 232 uid_t uid = 0; 233 gid_t gid = 0; 234 char *user = NULL; 235 char *group = NULL; 236 char *root = "/"; 237 char *endp; 238 struct group *gr; 239 struct passwd *pw; 240 unsigned long l; 241 242 (void)setlocale(LC_ALL, ""); 243 244 while ((ch = getopt(argc, argv, "dnsf:m:p:P:ru:g:t:")) != -1) 245 switch(ch) { 246 case 'd': /* debug */ 247 Debug++; 248 break; 249 case 'f': /* configuration file */ 250 ConfFile = optarg; 251 break; 252 case 'g': 253 group = optarg; 254 if (*group == '\0') 255 usage(); 256 break; 257 case 'm': /* mark interval */ 258 MarkInterval = atoi(optarg) * 60; 259 break; 260 case 'n': /* turn off DNS queries */ 261 UseNameService = 0; 262 break; 263 case 'p': /* path */ 264 logpath_add(&LogPaths, &funixsize, 265 &funixmaxsize, optarg); 266 break; 267 case 'P': /* file of paths */ 268 logpath_fileadd(&LogPaths, &funixsize, 269 &funixmaxsize, optarg); 270 break; 271 case 'r': /* disable "repeated" compression */ 272 NoRepeat++; 273 break; 274 case 's': /* no network listen mode */ 275 SecureMode++; 276 break; 277 case 't': 278 root = optarg; 279 if (*root == '\0') 280 usage(); 281 break; 282 case 'u': 283 user = optarg; 284 if (*user == '\0') 285 usage(); 286 break; 287 default: 288 usage(); 289 } 290 if ((argc -= optind) != 0) 291 usage(); 292 293 setlinebuf(stdout); 294 295 if (user != NULL) { 296 if (isdigit((unsigned char)*user)) { 297 errno = 0; 298 endp = NULL; 299 l = strtoul(user, &endp, 0); 300 if (errno || *endp != '\0') 301 goto getuser; 302 uid = (uid_t)l; 303 if (uid != l) { 304 errno = 0; 305 logerror("UID out of range"); 306 die(0); 307 } 308 } else { 309 getuser: 310 if ((pw = getpwnam(user)) != NULL) { 311 uid = pw->pw_uid; 312 } else { 313 errno = 0; 314 logerror("Cannot find user `%s'", user); 315 die (0); 316 } 317 } 318 } 319 320 if (group != NULL) { 321 if (isdigit((unsigned char)*group)) { 322 errno = 0; 323 endp = NULL; 324 l = strtoul(group, &endp, 0); 325 if (errno || *endp != '\0') 326 goto getgroup; 327 gid = (gid_t)l; 328 if (gid != l) { 329 errno = 0; 330 logerror("GID out of range"); 331 die(0); 332 } 333 } else { 334 getgroup: 335 if ((gr = getgrnam(group)) != NULL) { 336 gid = gr->gr_gid; 337 } else { 338 errno = 0; 339 logerror("Cannot find group `%s'", group); 340 die(0); 341 } 342 } 343 } 344 345 if (access (root, F_OK | R_OK)) { 346 logerror("Cannot access `%s'", root); 347 die (0); 348 } 349 350 consfile.f_type = F_CONSOLE; 351 (void)strlcpy(consfile.f_un.f_fname, ctty, 352 sizeof(consfile.f_un.f_fname)); 353 (void)gethostname(LocalHostName, sizeof(LocalHostName)); 354 LocalHostName[sizeof(LocalHostName) - 1] = '\0'; 355 if ((p = strchr(LocalHostName, '.')) != NULL) { 356 *p++ = '\0'; 357 LocalDomain = p; 358 } else 359 LocalDomain = ""; 360 linesize = getmsgbufsize(); 361 if (linesize < MAXLINE) 362 linesize = MAXLINE; 363 linesize++; 364 line = malloc(linesize); 365 if (line == NULL) { 366 logerror("Couldn't allocate line buffer"); 367 die(0); 368 } 369 (void)signal(SIGTERM, die); 370 (void)signal(SIGINT, Debug ? die : SIG_IGN); 371 (void)signal(SIGQUIT, Debug ? die : SIG_IGN); 372 (void)signal(SIGCHLD, reapchild); 373 (void)signal(SIGALRM, domark); 374 (void)alarm(TIMERINTVL); 375 376 #ifndef SUN_LEN 377 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 378 #endif 379 if (funixsize == 0) 380 logpath_add(&LogPaths, &funixsize, 381 &funixmaxsize, _PATH_LOG); 382 funix = (int *)malloc(sizeof(int) * funixsize); 383 if (funix == NULL) { 384 logerror("Couldn't allocate funix descriptors"); 385 die(0); 386 } 387 for (j = 0, pp = LogPaths; *pp; pp++, j++) { 388 dprintf("Making unix dgram socket `%s'\n", *pp); 389 unlink(*pp); 390 memset(&sunx, 0, sizeof(sunx)); 391 sunx.sun_family = AF_LOCAL; 392 (void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path)); 393 funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0); 394 if (funix[j] < 0 || bind(funix[j], 395 (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || 396 chmod(*pp, 0666) < 0) { 397 logerror("Cannot create `%s'", *pp); 398 die(0); 399 } 400 dprintf("Listening on unix dgram socket `%s'\n", *pp); 401 } 402 403 init(0); 404 405 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) { 406 dprintf("Can't open `%s' (%d)\n", _PATH_KLOG, errno); 407 } else { 408 dprintf("Listening on kernel log `%s'\n", _PATH_KLOG); 409 } 410 411 dprintf("Off & running....\n"); 412 413 (void)signal(SIGHUP, init); 414 415 /* setup pollfd set. */ 416 readfds = (struct pollfd *)malloc(sizeof(struct pollfd) * 417 (funixsize + (finet ? *finet : 0) + 1)); 418 if (readfds == NULL) { 419 logerror("Couldn't allocate pollfds"); 420 die(0); 421 } 422 nfds = 0; 423 if (fklog >= 0) { 424 nfklogix = nfds++; 425 readfds[nfklogix].fd = fklog; 426 readfds[nfklogix].events = POLLIN | POLLPRI; 427 } 428 if (finet && !SecureMode) { 429 nfinetix = malloc(*finet * sizeof(*nfinetix)); 430 for (j = 0; j < *finet; j++) { 431 nfinetix[j] = nfds++; 432 readfds[nfinetix[j]].fd = finet[j+1]; 433 readfds[nfinetix[j]].events = POLLIN | POLLPRI; 434 } 435 } 436 nfunixbaseix = nfds; 437 for (j = 0, pp = LogPaths; *pp; pp++) { 438 readfds[nfds].fd = funix[j++]; 439 readfds[nfds++].events = POLLIN | POLLPRI; 440 } 441 442 /* 443 * All files are open, we can drop privileges and chroot 444 */ 445 dprintf("Attempt to chroot to `%s'\n", root); 446 if (chroot(root)) { 447 logerror("Failed to chroot to `%s'", root); 448 die(0); 449 } 450 dprintf("Attempt to set GID/EGID to `%d'\n", gid); 451 if (setgid(gid) || setegid(gid)) { 452 logerror("Failed to set gid to `%d'", gid); 453 die(0); 454 } 455 dprintf("Attempt to set UID/EUID to `%d'\n", uid); 456 if (setuid(uid) || seteuid(uid)) { 457 logerror("Failed to set uid to `%d'", uid); 458 die(0); 459 } 460 461 /* 462 * We cannot detach from the terminal before we are sure we won't 463 * have a fatal error, because error message would not go to the 464 * terminal and would not be logged because syslogd dies. 465 * All die() calls are behind us, we can call daemon() 466 */ 467 if (!Debug) { 468 (void)daemon(0, 0); 469 daemonized = 1; 470 471 /* tuck my process id away, if i'm not in debug mode */ 472 pidfile(NULL); 473 } 474 475 for (;;) { 476 int rv; 477 478 rv = poll(readfds, nfds, INFTIM); 479 if (rv == 0) 480 continue; 481 if (rv < 0) { 482 if (errno != EINTR) 483 logerror("poll() failed"); 484 continue; 485 } 486 dprintf("Got a message (%d)\n", rv); 487 if (fklog >= 0 && 488 (readfds[nfklogix].revents & (POLLIN | POLLPRI))) { 489 dprintf("Kernel log active\n"); 490 i = read(fklog, line, linesize - 1); 491 if (i > 0) { 492 line[i] = '\0'; 493 printsys(line); 494 } else if (i < 0 && errno != EINTR) { 495 logerror("klog failed"); 496 fklog = -1; 497 } 498 } 499 for (j = 0, pp = LogPaths; *pp; pp++, j++) { 500 if ((readfds[nfunixbaseix + j].revents & 501 (POLLIN | POLLPRI)) == 0) 502 continue; 503 504 dprintf("Unix socket (%s) active\n", *pp); 505 len = sizeof(fromunix); 506 i = recvfrom(funix[j], line, MAXLINE, 0, 507 (struct sockaddr *)&fromunix, &len); 508 if (i > 0) { 509 line[i] = '\0'; 510 printline(LocalHostName, line); 511 } else if (i < 0 && errno != EINTR) { 512 logerror("recvfrom() unix `%s'", *pp); 513 } 514 } 515 if (finet && !SecureMode) { 516 for (j = 0; j < *finet; j++) { 517 if (readfds[nfinetix[j]].revents & 518 (POLLIN | POLLPRI)) { 519 #ifdef LIBWRAP 520 struct request_info req; 521 #endif 522 int reject = 0; 523 524 dprintf("inet socket active\n"); 525 526 #ifdef LIBWRAP 527 request_init(&req, RQ_DAEMON, "syslogd", 528 RQ_FILE, finet[j + 1], NULL); 529 fromhost(&req); 530 reject = !hosts_access(&req); 531 if (reject) 532 dprintf("access denied\n"); 533 #endif 534 535 len = sizeof(frominet); 536 i = recvfrom(finet[j+1], line, MAXLINE, 537 0, (struct sockaddr *)&frominet, 538 &len); 539 if (i == 0 || (i < 0 && errno == EINTR)) 540 continue; 541 else if (i < 0) { 542 logerror("recvfrom inet"); 543 continue; 544 } 545 546 line[i] = '\0'; 547 if (!reject) 548 printline(cvthname(&frominet), 549 line); 550 } 551 } 552 } 553 } 554 } 555 556 void 557 usage(void) 558 { 559 560 (void)fprintf(stderr, 561 "usage: %s [-dnrs] [-f config_file] [-g group] [-m mark_interval]\n" 562 "\t[-P file_list] [-p log_socket [-p log_socket2 ...]]\n" 563 "\t[-t chroot_dir] [-u user]\n", getprogname()); 564 exit(1); 565 } 566 567 /* 568 * given a pointer to an array of char *'s, a pointer to it's current 569 * size and current allocated max size, and a new char * to add, add 570 * it, update everything as necessary, possibly allocating a new array 571 */ 572 void 573 logpath_add(char ***lp, int *szp, int *maxszp, char *new) 574 { 575 char **nlp; 576 int newmaxsz; 577 578 dprintf("Adding `%s' to the %p logpath list\n", new, *lp); 579 if (*szp == *maxszp) { 580 if (*maxszp == 0) { 581 newmaxsz = 4; /* start of with enough for now */ 582 *lp = NULL; 583 } else 584 newmaxsz = *maxszp * 2; 585 nlp = realloc(*lp, sizeof(char *) * (newmaxsz + 1)); 586 if (nlp == NULL) { 587 logerror("Couldn't allocate line buffer"); 588 die(0); 589 } 590 *lp = nlp; 591 *maxszp = newmaxsz; 592 } 593 if (((*lp)[(*szp)++] = strdup(new)) == NULL) { 594 logerror("Couldn't allocate logpath"); 595 die(0); 596 } 597 (*lp)[(*szp)] = NULL; /* always keep it NULL terminated */ 598 } 599 600 /* do a file of log sockets */ 601 void 602 logpath_fileadd(char ***lp, int *szp, int *maxszp, char *file) 603 { 604 FILE *fp; 605 char *line; 606 size_t len; 607 608 fp = fopen(file, "r"); 609 if (fp == NULL) { 610 logerror("Could not open socket file list `%s'", file); 611 die(0); 612 } 613 614 while ((line = fgetln(fp, &len))) { 615 line[len - 1] = 0; 616 logpath_add(lp, szp, maxszp, line); 617 } 618 fclose(fp); 619 } 620 621 /* 622 * Take a raw input line, decode the message, and print the message 623 * on the appropriate log files. 624 */ 625 void 626 printline(char *hname, char *msg) 627 { 628 int c, pri; 629 char *p, *q, line[MAXLINE + 1]; 630 631 /* test for special codes */ 632 pri = DEFUPRI; 633 p = msg; 634 if (*p == '<') { 635 pri = 0; 636 while (isdigit(*++p)) 637 pri = 10 * pri + (*p - '0'); 638 if (*p == '>') 639 ++p; 640 } 641 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 642 pri = DEFUPRI; 643 644 /* don't allow users to log kernel messages */ 645 if (LOG_FAC(pri) == LOG_KERN) 646 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 647 648 q = line; 649 650 while ((c = *p++) != '\0' && 651 q < &line[sizeof(line) - 2]) { 652 c &= 0177; 653 if (iscntrl(c)) 654 if (c == '\n') 655 *q++ = ' '; 656 else if (c == '\t') 657 *q++ = '\t'; 658 else { 659 *q++ = '^'; 660 *q++ = c ^ 0100; 661 } 662 else 663 *q++ = c; 664 } 665 *q = '\0'; 666 667 logmsg(pri, line, hname, 0); 668 } 669 670 /* 671 * Take a raw input line from /dev/klog, split and format similar to syslog(). 672 */ 673 void 674 printsys(char *msg) 675 { 676 int c, pri, flags; 677 char *lp, *p, *q, line[MAXLINE + 1]; 678 679 (void)strlcpy(line, _PATH_UNIX, sizeof(line)); 680 (void)strlcat(line, ": ", sizeof(line)); 681 lp = line + strlen(line); 682 for (p = msg; *p != '\0'; ) { 683 flags = SYNC_FILE | ADDDATE; /* fsync file after write */ 684 pri = DEFSPRI; 685 if (*p == '<') { 686 pri = 0; 687 while (isdigit(*++p)) 688 pri = 10 * pri + (*p - '0'); 689 if (*p == '>') 690 ++p; 691 } else { 692 /* kernel printf's come out on console */ 693 flags |= IGN_CONS; 694 } 695 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 696 pri = DEFSPRI; 697 q = lp; 698 while (*p != '\0' && (c = *p++) != '\n' && 699 q < &line[MAXLINE]) 700 *q++ = c; 701 *q = '\0'; 702 logmsg(pri, line, LocalHostName, flags); 703 } 704 } 705 706 time_t now; 707 708 /* 709 * Log a message to the appropriate log files, users, etc. based on 710 * the priority. 711 */ 712 void 713 logmsg(int pri, char *msg, char *from, int flags) 714 { 715 struct filed *f; 716 int fac, msglen, omask, prilev; 717 char *timestamp; 718 719 dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n", 720 pri, flags, from, msg); 721 722 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 723 724 /* 725 * Check to see if msg looks non-standard. 726 */ 727 msglen = strlen(msg); 728 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 729 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 730 flags |= ADDDATE; 731 732 (void)time(&now); 733 if (flags & ADDDATE) 734 timestamp = ctime(&now) + 4; 735 else { 736 timestamp = msg; 737 msg += 16; 738 msglen -= 16; 739 } 740 741 /* extract facility and priority level */ 742 if (flags & MARK) 743 fac = LOG_NFACILITIES; 744 else 745 fac = LOG_FAC(pri); 746 prilev = LOG_PRI(pri); 747 748 /* log the message to the particular outputs */ 749 if (!Initialized) { 750 f = &consfile; 751 f->f_file = open(ctty, O_WRONLY, 0); 752 753 if (f->f_file >= 0) { 754 fprintlog(f, flags, msg); 755 (void)close(f->f_file); 756 } 757 (void)sigsetmask(omask); 758 return; 759 } 760 for (f = Files; f; f = f->f_next) { 761 /* skip messages that are incorrect priority */ 762 if (f->f_pmask[fac] < prilev || 763 f->f_pmask[fac] == INTERNAL_NOPRI) 764 continue; 765 766 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 767 continue; 768 769 /* don't output marks to recently written files */ 770 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 771 continue; 772 773 /* 774 * suppress duplicate lines to this file unless NoRepeat 775 */ 776 if ((flags & MARK) == 0 && msglen == f->f_prevlen && 777 !NoRepeat && 778 !strcmp(msg, f->f_prevline) && 779 !strcmp(from, f->f_prevhost)) { 780 (void)strncpy(f->f_lasttime, timestamp, 15); 781 f->f_prevcount++; 782 dprintf("Msg repeated %d times, %ld sec of %d\n", 783 f->f_prevcount, (long)(now - f->f_time), 784 repeatinterval[f->f_repeatcount]); 785 /* 786 * If domark would have logged this by now, 787 * flush it now (so we don't hold isolated messages), 788 * but back off so we'll flush less often 789 * in the future. 790 */ 791 if (now > REPEATTIME(f)) { 792 fprintlog(f, flags, (char *)NULL); 793 BACKOFF(f); 794 } 795 } else { 796 /* new line, save it */ 797 if (f->f_prevcount) 798 fprintlog(f, 0, (char *)NULL); 799 f->f_repeatcount = 0; 800 f->f_prevpri = pri; 801 (void)strncpy(f->f_lasttime, timestamp, 15); 802 (void)strncpy(f->f_prevhost, from, 803 sizeof(f->f_prevhost)); 804 if (msglen < MAXSVLINE) { 805 f->f_prevlen = msglen; 806 (void)strlcpy(f->f_prevline, msg, 807 sizeof(f->f_prevline)); 808 fprintlog(f, flags, (char *)NULL); 809 } else { 810 f->f_prevline[0] = 0; 811 f->f_prevlen = 0; 812 fprintlog(f, flags, msg); 813 } 814 } 815 } 816 (void)sigsetmask(omask); 817 } 818 819 void 820 fprintlog(struct filed *f, int flags, char *msg) 821 { 822 struct iovec iov[6]; 823 struct iovec *v; 824 struct addrinfo *r; 825 int j, l, lsent; 826 char line[MAXLINE + 1], repbuf[80], greetings[200]; 827 828 v = iov; 829 if (f->f_type == F_WALL) { 830 v->iov_base = greetings; 831 v->iov_len = snprintf(greetings, sizeof greetings, 832 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 833 f->f_prevhost, ctime(&now)); 834 v++; 835 v->iov_base = ""; 836 v->iov_len = 0; 837 v++; 838 } else { 839 v->iov_base = f->f_lasttime; 840 v->iov_len = 15; 841 v++; 842 v->iov_base = " "; 843 v->iov_len = 1; 844 v++; 845 } 846 v->iov_base = f->f_prevhost; 847 v->iov_len = strlen(v->iov_base); 848 v++; 849 v->iov_base = " "; 850 v->iov_len = 1; 851 v++; 852 853 if (msg) { 854 v->iov_base = msg; 855 v->iov_len = strlen(msg); 856 } else if (f->f_prevcount > 1) { 857 v->iov_base = repbuf; 858 v->iov_len = snprintf(repbuf, sizeof repbuf, 859 "last message repeated %d times", f->f_prevcount); 860 } else { 861 v->iov_base = f->f_prevline; 862 v->iov_len = f->f_prevlen; 863 } 864 v++; 865 866 dprintf("Logging to %s", TypeNames[f->f_type]); 867 f->f_time = now; 868 869 switch (f->f_type) { 870 case F_UNUSED: 871 dprintf("\n"); 872 break; 873 874 case F_FORW: 875 dprintf(" %s\n", f->f_un.f_forw.f_hname); 876 /* 877 * check for local vs remote messages 878 * (from FreeBSD PR#bin/7055) 879 */ 880 if (strcmp(f->f_prevhost, LocalHostName)) { 881 l = snprintf(line, sizeof(line) - 1, 882 "<%d>%.15s [%s]: %s", 883 f->f_prevpri, (char *) iov[0].iov_base, 884 f->f_prevhost, (char *) iov[4].iov_base); 885 } else { 886 l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s", 887 f->f_prevpri, (char *) iov[0].iov_base, 888 (char *) iov[4].iov_base); 889 } 890 if (l > MAXLINE) 891 l = MAXLINE; 892 if (finet) { 893 for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) { 894 for (j = 0; j < *finet; j++) { 895 #if 0 896 /* 897 * should we check AF first, or just 898 * trial and error? FWD 899 */ 900 if (r->ai_family == 901 address_family_of(finet[j+1])) 902 #endif 903 lsent = sendto(finet[j+1], line, l, 0, 904 r->ai_addr, r->ai_addrlen); 905 if (lsent == l) 906 break; 907 } 908 } 909 if (lsent != l) { 910 f->f_type = F_UNUSED; 911 logerror("sendto() failed"); 912 } 913 } 914 break; 915 916 case F_CONSOLE: 917 if (flags & IGN_CONS) { 918 dprintf(" (ignored)\n"); 919 break; 920 } 921 /* FALLTHROUGH */ 922 923 case F_TTY: 924 case F_FILE: 925 dprintf(" %s\n", f->f_un.f_fname); 926 if (f->f_type != F_FILE) { 927 v->iov_base = "\r\n"; 928 v->iov_len = 2; 929 } else { 930 v->iov_base = "\n"; 931 v->iov_len = 1; 932 } 933 again: 934 if (writev(f->f_file, iov, 6) < 0) { 935 int e = errno; 936 (void)close(f->f_file); 937 /* 938 * Check for errors on TTY's due to loss of tty 939 */ 940 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) { 941 f->f_file = open(f->f_un.f_fname, 942 O_WRONLY|O_APPEND, 0); 943 if (f->f_file < 0) { 944 f->f_type = F_UNUSED; 945 logerror(f->f_un.f_fname); 946 } else 947 goto again; 948 } else { 949 f->f_type = F_UNUSED; 950 errno = e; 951 logerror(f->f_un.f_fname); 952 } 953 } else if (flags & SYNC_FILE) 954 (void)fsync(f->f_file); 955 break; 956 957 case F_USERS: 958 case F_WALL: 959 dprintf("\n"); 960 v->iov_base = "\r\n"; 961 v->iov_len = 2; 962 wallmsg(f, iov); 963 break; 964 } 965 f->f_prevcount = 0; 966 } 967 968 /* 969 * WALLMSG -- Write a message to the world at large 970 * 971 * Write the specified message to either the entire 972 * world, or a list of approved users. 973 */ 974 void 975 wallmsg(struct filed *f, struct iovec *iov) 976 { 977 static int reenter; /* avoid calling ourselves */ 978 int i; 979 char *p; 980 static struct utmpentry *ohead = NULL; 981 struct utmpentry *ep; 982 983 if (reenter++) 984 return; 985 986 (void)getutentries(NULL, &ep); 987 if (ep != ohead) { 988 freeutentries(ohead); 989 ohead = ep; 990 } 991 /* NOSTRICT */ 992 for (; ep; ep = ep->next) { 993 if (f->f_type == F_WALL) { 994 if ((p = ttymsg(iov, 6, ep->line, TTYMSGTIME)) 995 != NULL) { 996 errno = 0; /* already in msg */ 997 logerror(p); 998 } 999 continue; 1000 } 1001 /* should we send the message to this user? */ 1002 for (i = 0; i < MAXUNAMES; i++) { 1003 if (!f->f_un.f_uname[i][0]) 1004 break; 1005 if (strcmp(f->f_un.f_uname[i], ep->name) == 0) { 1006 if ((p = ttymsg(iov, 6, ep->line, TTYMSGTIME)) 1007 != NULL) { 1008 errno = 0; /* already in msg */ 1009 logerror(p); 1010 } 1011 break; 1012 } 1013 } 1014 } 1015 reenter = 0; 1016 } 1017 1018 void 1019 reapchild(int signo) 1020 { 1021 union wait status; 1022 1023 while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0) 1024 ; 1025 } 1026 1027 /* 1028 * Return a printable representation of a host address. 1029 */ 1030 char * 1031 cvthname(struct sockaddr_storage *f) 1032 { 1033 int error; 1034 char *p; 1035 const int niflag = NI_DGRAM; 1036 static char host[NI_MAXHOST], ip[NI_MAXHOST]; 1037 1038 error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len, 1039 ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag); 1040 1041 dprintf("cvthname(%s)\n", ip); 1042 1043 if (error) { 1044 dprintf("Malformed from address %s\n", gai_strerror(error)); 1045 return ("???"); 1046 } 1047 1048 if (!UseNameService) 1049 return (ip); 1050 1051 error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len, 1052 host, sizeof host, NULL, 0, niflag); 1053 if (error) { 1054 dprintf("Host name for your address (%s) unknown\n", ip); 1055 return (ip); 1056 } 1057 if ((p = strchr(host, '.')) && strcmp(p + 1, LocalDomain) == 0) 1058 *p = '\0'; 1059 return (host); 1060 } 1061 1062 void 1063 domark(int signo) 1064 { 1065 struct filed *f; 1066 1067 now = time((time_t *)NULL); 1068 MarkSeq += TIMERINTVL; 1069 if (MarkSeq >= MarkInterval) { 1070 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); 1071 MarkSeq = 0; 1072 } 1073 1074 for (f = Files; f; f = f->f_next) { 1075 if (f->f_prevcount && now >= REPEATTIME(f)) { 1076 dprintf("Flush %s: repeated %d times, %d sec.\n", 1077 TypeNames[f->f_type], f->f_prevcount, 1078 repeatinterval[f->f_repeatcount]); 1079 fprintlog(f, 0, (char *)NULL); 1080 BACKOFF(f); 1081 } 1082 } 1083 (void)alarm(TIMERINTVL); 1084 } 1085 1086 /* 1087 * Print syslogd errors some place. 1088 */ 1089 void 1090 logerror(const char *fmt, ...) 1091 { 1092 va_list ap; 1093 char tmpbuf[BUFSIZ]; 1094 char buf[BUFSIZ]; 1095 1096 va_start(ap, fmt); 1097 1098 (void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap); 1099 1100 va_end(ap); 1101 1102 if (errno) 1103 (void)snprintf(buf, sizeof(buf), "syslogd: %s: %s", 1104 tmpbuf, strerror(errno)); 1105 else 1106 (void)snprintf(buf, sizeof(buf), "syslogd: %s", tmpbuf); 1107 1108 if (daemonized) 1109 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 1110 if (!daemonized && Debug) 1111 dprintf("%s\n", buf); 1112 if (!daemonized && !Debug) 1113 printf("%s\n", buf); 1114 1115 return; 1116 } 1117 1118 void 1119 die(int signo) 1120 { 1121 struct filed *f; 1122 char **p; 1123 1124 for (f = Files; f != NULL; f = f->f_next) { 1125 /* flush any pending output */ 1126 if (f->f_prevcount) 1127 fprintlog(f, 0, (char *)NULL); 1128 } 1129 errno = 0; 1130 if (signo) 1131 logerror("Exiting on signal %d", signo); 1132 else 1133 logerror("Fatal error, exiting"); 1134 for (p = LogPaths; p && *p; p++) 1135 unlink(*p); 1136 exit(0); 1137 } 1138 1139 /* 1140 * INIT -- Initialize syslogd from configuration table 1141 */ 1142 void 1143 init(int signo) 1144 { 1145 int i; 1146 FILE *cf; 1147 struct filed *f, *next, **nextp; 1148 char *p; 1149 char cline[LINE_MAX]; 1150 1151 dprintf("init\n"); 1152 1153 /* 1154 * Close all open log files. 1155 */ 1156 Initialized = 0; 1157 for (f = Files; f != NULL; f = next) { 1158 /* flush any pending output */ 1159 if (f->f_prevcount) 1160 fprintlog(f, 0, (char *)NULL); 1161 1162 switch (f->f_type) { 1163 case F_FILE: 1164 case F_TTY: 1165 case F_CONSOLE: 1166 (void)close(f->f_file); 1167 break; 1168 case F_FORW: 1169 if (f->f_un.f_forw.f_addr) 1170 freeaddrinfo(f->f_un.f_forw.f_addr); 1171 break; 1172 } 1173 next = f->f_next; 1174 free((char *)f); 1175 } 1176 Files = NULL; 1177 nextp = &Files; 1178 1179 /* 1180 * Close all open sockets 1181 */ 1182 1183 if (finet) { 1184 for (i = 0; i < *finet; i++) { 1185 if (close(finet[i+1]) < 0) { 1186 logerror("close() failed"); 1187 die(0); 1188 } 1189 } 1190 } 1191 1192 /* 1193 * Reset counter of forwarding actions 1194 */ 1195 1196 NumForwards=0; 1197 1198 /* open the configuration file */ 1199 if ((cf = fopen(ConfFile, "r")) == NULL) { 1200 dprintf("Cannot open `%s'\n", ConfFile); 1201 *nextp = (struct filed *)calloc(1, sizeof(*f)); 1202 cfline("*.ERR\t/dev/console", *nextp); 1203 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 1204 cfline("*.PANIC\t*", (*nextp)->f_next); 1205 Initialized = 1; 1206 return; 1207 } 1208 1209 /* 1210 * Foreach line in the conf table, open that file. 1211 */ 1212 f = NULL; 1213 while (fgets(cline, sizeof(cline), cf) != NULL) { 1214 /* 1215 * check for end-of-section, comments, strip off trailing 1216 * spaces and newline character. 1217 */ 1218 for (p = cline; isspace(*p); ++p) 1219 continue; 1220 if (*p == '\0' || *p == '#') 1221 continue; 1222 for (p = strchr(cline, '\0'); isspace(*--p);) 1223 continue; 1224 *++p = '\0'; 1225 f = (struct filed *)calloc(1, sizeof(*f)); 1226 *nextp = f; 1227 nextp = &f->f_next; 1228 cfline(cline, f); 1229 } 1230 1231 /* close the configuration file */ 1232 (void)fclose(cf); 1233 1234 Initialized = 1; 1235 1236 if (Debug) { 1237 for (f = Files; f; f = f->f_next) { 1238 for (i = 0; i <= LOG_NFACILITIES; i++) 1239 if (f->f_pmask[i] == INTERNAL_NOPRI) 1240 printf("X "); 1241 else 1242 printf("%d ", f->f_pmask[i]); 1243 printf("%s: ", TypeNames[f->f_type]); 1244 switch (f->f_type) { 1245 case F_FILE: 1246 case F_TTY: 1247 case F_CONSOLE: 1248 printf("%s", f->f_un.f_fname); 1249 break; 1250 1251 case F_FORW: 1252 printf("%s", f->f_un.f_forw.f_hname); 1253 break; 1254 1255 case F_USERS: 1256 for (i = 0; 1257 i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 1258 printf("%s, ", f->f_un.f_uname[i]); 1259 break; 1260 } 1261 printf("\n"); 1262 } 1263 } 1264 1265 finet = socksetup(PF_UNSPEC); 1266 if (finet) { 1267 if (SecureMode) { 1268 for (i = 0; i < *finet; i++) { 1269 if (shutdown(finet[i+1], SHUT_RD) < 0) { 1270 logerror("shutdown() failed"); 1271 die(0); 1272 } 1273 } 1274 } else 1275 dprintf("Listening on inet and/or inet6 socket\n"); 1276 dprintf("Sending on inet and/or inet6 socket\n"); 1277 } 1278 1279 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1280 dprintf("syslogd: restarted\n"); 1281 } 1282 1283 /* 1284 * Crack a configuration file line 1285 */ 1286 void 1287 cfline(char *line, struct filed *f) 1288 { 1289 struct addrinfo hints, *res; 1290 int error, i, pri; 1291 char *bp, *p, *q; 1292 char buf[MAXLINE]; 1293 int sp_err; 1294 1295 dprintf("cfline(%s)\n", line); 1296 1297 errno = 0; /* keep strerror() stuff out of logerror messages */ 1298 1299 /* clear out file entry */ 1300 memset(f, 0, sizeof(*f)); 1301 for (i = 0; i <= LOG_NFACILITIES; i++) 1302 f->f_pmask[i] = INTERNAL_NOPRI; 1303 1304 /* 1305 * There should not be any space before the log facility. 1306 * Check this is okay, complain and fix if it is not. 1307 */ 1308 q = line; 1309 if (isblank((unsigned char)*line)) { 1310 errno = 0; 1311 logerror( 1312 "Warning: `%s' space or tab before the log facility", 1313 line); 1314 /* Fix: strip all spaces/tabs before the log facility */ 1315 while (*q++ && isblank((unsigned char)*q)); 1316 line = q; 1317 } 1318 1319 /* 1320 * q is now at the first char of the log facility 1321 * There should be at least one tab after the log facility 1322 * Check this is okay, and complain and fix if it is not. 1323 */ 1324 q = line + strlen(line); 1325 while (!isblank((unsigned char)*q) && (q != line)) 1326 q--; 1327 if ((q == line) && strlen(line)) { 1328 /* No tabs or space in a non empty line: complain */ 1329 errno = 0; 1330 logerror( 1331 "Error: `%s' log facility or log target missing", 1332 line); 1333 } 1334 1335 /* q is at the end of the blank between the two fields */ 1336 sp_err = 0; 1337 while (isblank((unsigned char)*q) && (q != line)) 1338 if (*q-- == ' ') 1339 sp_err = 1; 1340 1341 if (sp_err) { 1342 /* 1343 * A space somewhere between the log facility 1344 * and the log target: complain 1345 */ 1346 errno = 0; 1347 logerror( 1348 "Warning: `%s' space found where tab is expected", 1349 line); 1350 /* ... and fix the problem: replace all spaces by tabs */ 1351 while (*++q && isblank((unsigned char)*q)) 1352 if (*q == ' ') 1353 *q='\t'; 1354 } 1355 1356 /* scan through the list of selectors */ 1357 for (p = line; *p && *p != '\t';) { 1358 1359 /* find the end of this facility name list */ 1360 for (q = p; *q && *q != '\t' && *q++ != '.'; ) 1361 continue; 1362 1363 /* collect priority name */ 1364 for (bp = buf; *q && !strchr("\t,;", *q); ) 1365 *bp++ = *q++; 1366 *bp = '\0'; 1367 1368 /* skip cruft */ 1369 while (strchr(", ;", *q)) 1370 q++; 1371 1372 /* decode priority name */ 1373 if (*buf == '*') 1374 pri = LOG_PRIMASK + 1; 1375 else { 1376 pri = decode(buf, prioritynames); 1377 if (pri < 0) { 1378 errno = 0; 1379 logerror("Unknown priority name `%s'", buf); 1380 return; 1381 } 1382 } 1383 1384 /* scan facilities */ 1385 while (*p && !strchr("\t.;", *p)) { 1386 for (bp = buf; *p && !strchr("\t,;.", *p); ) 1387 *bp++ = *p++; 1388 *bp = '\0'; 1389 if (*buf == '*') 1390 for (i = 0; i < LOG_NFACILITIES; i++) 1391 f->f_pmask[i] = pri; 1392 else { 1393 i = decode(buf, facilitynames); 1394 if (i < 0) { 1395 errno = 0; 1396 logerror("Unknown facility name `%s'", 1397 buf); 1398 return; 1399 } 1400 f->f_pmask[i >> 3] = pri; 1401 } 1402 while (*p == ',' || *p == ' ') 1403 p++; 1404 } 1405 1406 p = q; 1407 } 1408 1409 /* skip to action part */ 1410 sp_err = 0; 1411 while ((*p == '\t') || (*p == ' ')) 1412 p++; 1413 1414 switch (*p) 1415 { 1416 case '@': 1417 (void)strlcpy(f->f_un.f_forw.f_hname, ++p, 1418 sizeof(f->f_un.f_forw.f_hname)); 1419 memset(&hints, 0, sizeof(hints)); 1420 hints.ai_family = AF_UNSPEC; 1421 hints.ai_socktype = SOCK_DGRAM; 1422 hints.ai_protocol = 0; 1423 error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, 1424 &res); 1425 if (error) { 1426 logerror(gai_strerror(error)); 1427 break; 1428 } 1429 f->f_un.f_forw.f_addr = res; 1430 f->f_type = F_FORW; 1431 NumForwards++; 1432 break; 1433 1434 case '/': 1435 (void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname)); 1436 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { 1437 f->f_type = F_UNUSED; 1438 logerror(p); 1439 break; 1440 } 1441 if (isatty(f->f_file)) 1442 f->f_type = F_TTY; 1443 else 1444 f->f_type = F_FILE; 1445 if (strcmp(p, ctty) == 0) 1446 f->f_type = F_CONSOLE; 1447 break; 1448 1449 case '*': 1450 f->f_type = F_WALL; 1451 break; 1452 1453 default: 1454 for (i = 0; i < MAXUNAMES && *p; i++) { 1455 for (q = p; *q && *q != ','; ) 1456 q++; 1457 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 1458 if ((q - p) > UT_NAMESIZE) 1459 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 1460 else 1461 f->f_un.f_uname[i][q - p] = '\0'; 1462 while (*q == ',' || *q == ' ') 1463 q++; 1464 p = q; 1465 } 1466 f->f_type = F_USERS; 1467 break; 1468 } 1469 } 1470 1471 1472 /* 1473 * Decode a symbolic name to a numeric value 1474 */ 1475 int 1476 decode(const char *name, CODE *codetab) 1477 { 1478 CODE *c; 1479 char *p, buf[40]; 1480 1481 if (isdigit(*name)) 1482 return (atoi(name)); 1483 1484 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 1485 if (isupper(*name)) 1486 *p = tolower(*name); 1487 else 1488 *p = *name; 1489 } 1490 *p = '\0'; 1491 for (c = codetab; c->c_name; c++) 1492 if (!strcmp(buf, c->c_name)) 1493 return (c->c_val); 1494 1495 return (-1); 1496 } 1497 1498 /* 1499 * Retrieve the size of the kernel message buffer, via sysctl. 1500 */ 1501 int 1502 getmsgbufsize(void) 1503 { 1504 int msgbufsize, mib[2]; 1505 size_t size; 1506 1507 mib[0] = CTL_KERN; 1508 mib[1] = KERN_MSGBUFSIZE; 1509 size = sizeof msgbufsize; 1510 if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) { 1511 dprintf("Couldn't get kern.msgbufsize\n"); 1512 return (0); 1513 } 1514 return (msgbufsize); 1515 } 1516 1517 int * 1518 socksetup(int af) 1519 { 1520 struct addrinfo hints, *res, *r; 1521 int error, maxs, *s, *socks; 1522 const int on = 1; 1523 1524 if(SecureMode && !NumForwards) 1525 return(NULL); 1526 1527 memset(&hints, 0, sizeof(hints)); 1528 hints.ai_flags = AI_PASSIVE; 1529 hints.ai_family = af; 1530 hints.ai_socktype = SOCK_DGRAM; 1531 error = getaddrinfo(NULL, "syslog", &hints, &res); 1532 if (error) { 1533 logerror(gai_strerror(error)); 1534 errno = 0; 1535 die(0); 1536 } 1537 1538 /* Count max number of sockets we may open */ 1539 for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 1540 continue; 1541 socks = malloc((maxs+1) * sizeof(int)); 1542 if (!socks) { 1543 logerror("Couldn't allocate memory for sockets"); 1544 die(0); 1545 } 1546 1547 *socks = 0; /* num of sockets counter at start of array */ 1548 s = socks + 1; 1549 for (r = res; r; r = r->ai_next) { 1550 *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 1551 if (*s < 0) { 1552 logerror("socket() failed"); 1553 continue; 1554 } 1555 if (r->ai_family == AF_INET6 && setsockopt(*s, IPPROTO_IPV6, 1556 IPV6_V6ONLY, &on, sizeof(on)) < 0) { 1557 logerror("setsockopt(IPV6_V6ONLY) failed"); 1558 close(*s); 1559 continue; 1560 } 1561 if (!SecureMode && bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 1562 logerror("bind() failed"); 1563 close (*s); 1564 continue; 1565 } 1566 1567 *socks = *socks + 1; 1568 s++; 1569 } 1570 1571 if (*socks == 0) { 1572 free (socks); 1573 if(Debug) 1574 return(NULL); 1575 else 1576 die(0); 1577 } 1578 if (res) 1579 freeaddrinfo(res); 1580 1581 return(socks); 1582 } 1583