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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; 39 #endif 40 #endif /* not lint */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 /* 46 * syslogd -- log system messages 47 * 48 * This program implements a system log. It takes a series of lines. 49 * Each line may have a priority, signified as "<n>" as 50 * the first characters of the line. If this is 51 * not present, a default priority is used. 52 * 53 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will 54 * cause it to reread its configuration file. 55 * 56 * Defined Constants: 57 * 58 * MAXLINE -- the maximum line length that can be handled. 59 * DEFUPRI -- the default priority for user messages 60 * DEFSPRI -- the default priority for kernel messages 61 * 62 * Author: Eric Allman 63 * extensive changes by Ralph Campbell 64 * more extensive changes by Eric Allman (again) 65 * Extension to log by program name as well as facility and priority 66 * by Peter da Silva. 67 * -u and -v by Harlan Stenn. 68 * Priority comparison code by Harlan Stenn. 69 */ 70 71 #define MAXLINE 1024 /* maximum line length */ 72 #define MAXSVLINE MAXLINE /* maximum saved line length */ 73 #define DEFUPRI (LOG_USER|LOG_NOTICE) 74 #define DEFSPRI (LOG_KERN|LOG_CRIT) 75 #define TIMERINTVL 30 /* interval for checking flush, mark */ 76 #define TTYMSGTIME 1 /* timeout passed to ttymsg */ 77 #define RCVBUF_MINSIZE (80 * 1024) /* minimum size of dgram rcv buffer */ 78 79 #include <sys/param.h> 80 #include <sys/ioctl.h> 81 #include <sys/mman.h> 82 #include <sys/stat.h> 83 #include <sys/wait.h> 84 #include <sys/socket.h> 85 #include <sys/queue.h> 86 #include <sys/uio.h> 87 #include <sys/un.h> 88 #include <sys/time.h> 89 #include <sys/resource.h> 90 #include <sys/syslimits.h> 91 92 #include <netinet/in.h> 93 #include <netdb.h> 94 #include <arpa/inet.h> 95 96 #include <ctype.h> 97 #include <dirent.h> 98 #include <err.h> 99 #include <errno.h> 100 #include <fcntl.h> 101 #include <libutil.h> 102 #include <limits.h> 103 #include <paths.h> 104 #include <signal.h> 105 #include <stdio.h> 106 #include <stdlib.h> 107 #include <string.h> 108 #include <sysexits.h> 109 #include <unistd.h> 110 #include <utmpx.h> 111 112 #include "pathnames.h" 113 #include "ttymsg.h" 114 115 #define SYSLOG_NAMES 116 #include <sys/syslog.h> 117 118 static const char *ConfFile = _PATH_LOGCONF; 119 static const char *PidFile = _PATH_LOGPID; 120 static const char ctty[] = _PATH_CONSOLE; 121 static const char include_str[] = "include"; 122 static const char include_ext[] = ".conf"; 123 124 #define dprintf if (Debug) printf 125 126 #define MAXUNAMES 20 /* maximum number of user names */ 127 128 #define sstosa(ss) ((struct sockaddr *)(ss)) 129 #define satosin6(sa) ((struct sockaddr_in6 *)(void *)(sa)) 130 #define satosin(sa) ((struct sockaddr_in *)(void *)(sa)) 131 #define s6_addr32 __u6_addr.__u6_addr32 132 #define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 133 (((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \ 134 (((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \ 135 (((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \ 136 (((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 ) 137 /* 138 * List of peers and sockets for binding. 139 */ 140 struct peer { 141 const char *pe_name; 142 const char *pe_serv; 143 mode_t pe_mode; 144 STAILQ_ENTRY(peer) next; 145 }; 146 struct socklist { 147 struct sockaddr_storage sl_ss; 148 int sl_socket; 149 struct peer *sl_peer; 150 STAILQ_ENTRY(socklist) next; 151 }; 152 static STAILQ_HEAD(, socklist) shead = STAILQ_HEAD_INITIALIZER(shead); 153 154 static struct peer funix_secure = { 155 .pe_name = _PATH_LOG_PRIV, 156 .pe_mode = S_IRUSR | S_IWUSR, 157 .next = {NULL}, 158 }; 159 static struct peer funix_default = { 160 .pe_name = _PATH_LOG, 161 .pe_mode = DEFFILEMODE, 162 .next = {&funix_secure}, 163 }; 164 static STAILQ_HEAD(, peer) pqueue = { 165 &funix_default, 166 &funix_secure.next.stqe_next, 167 }; 168 169 /* 170 * Flags to logmsg(). 171 */ 172 173 #define IGN_CONS 0x001 /* don't print on console */ 174 #define SYNC_FILE 0x002 /* do fsync on file after printing */ 175 #define ADDDATE 0x004 /* add a date to the message */ 176 #define MARK 0x008 /* this message is a mark */ 177 #define ISKERNEL 0x010 /* kernel generated message */ 178 179 /* 180 * This structure represents the files that will have log 181 * copies printed. 182 * We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY 183 * or if f_type if F_PIPE and f_pid > 0. 184 */ 185 186 struct filed { 187 STAILQ_ENTRY(filed) next; /* next in linked list */ 188 short f_type; /* entry type, see below */ 189 short f_file; /* file descriptor */ 190 time_t f_time; /* time this was last written */ 191 char *f_host; /* host from which to recd. */ 192 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ 193 u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ 194 #define PRI_LT 0x1 195 #define PRI_EQ 0x2 196 #define PRI_GT 0x4 197 char *f_program; /* program this applies to */ 198 union { 199 char f_uname[MAXUNAMES][MAXLOGNAME]; 200 struct { 201 char f_hname[MAXHOSTNAMELEN]; 202 struct addrinfo *f_addr; 203 204 } f_forw; /* forwarding address */ 205 char f_fname[MAXPATHLEN]; 206 struct { 207 char f_pname[MAXPATHLEN]; 208 pid_t f_pid; 209 } f_pipe; 210 } f_un; 211 #define fu_uname f_un.f_uname 212 #define fu_forw_hname f_un.f_forw.f_hname 213 #define fu_forw_addr f_un.f_forw.f_addr 214 #define fu_fname f_un.f_fname 215 #define fu_pipe_pname f_un.f_pipe.f_pname 216 #define fu_pipe_pid f_un.f_pipe.f_pid 217 char f_prevline[MAXSVLINE]; /* last message logged */ 218 char f_lasttime[16]; /* time of last occurrence */ 219 char f_prevhost[MAXHOSTNAMELEN]; /* host from which recd. */ 220 int f_prevpri; /* pri of f_prevline */ 221 int f_prevlen; /* length of f_prevline */ 222 int f_prevcount; /* repetition cnt of prevline */ 223 u_int f_repeatcount; /* number of "repeated" msgs */ 224 int f_flags; /* file-specific flags */ 225 #define FFLAG_SYNC 0x01 226 #define FFLAG_NEEDSYNC 0x02 227 }; 228 229 /* 230 * Queue of about-to-be dead processes we should watch out for. 231 */ 232 struct deadq_entry { 233 pid_t dq_pid; 234 int dq_timeout; 235 TAILQ_ENTRY(deadq_entry) dq_entries; 236 }; 237 static TAILQ_HEAD(, deadq_entry) deadq_head; 238 239 /* 240 * The timeout to apply to processes waiting on the dead queue. Unit 241 * of measure is `mark intervals', i.e. 20 minutes by default. 242 * Processes on the dead queue will be terminated after that time. 243 */ 244 245 #define DQ_TIMO_INIT 2 246 247 typedef struct deadq_entry *dq_t; 248 249 250 /* 251 * Struct to hold records of network addresses that are allowed to log 252 * to us. 253 */ 254 struct allowedpeer { 255 int isnumeric; 256 u_short port; 257 union { 258 struct { 259 struct sockaddr_storage addr; 260 struct sockaddr_storage mask; 261 } numeric; 262 char *name; 263 } u; 264 #define a_addr u.numeric.addr 265 #define a_mask u.numeric.mask 266 #define a_name u.name 267 STAILQ_ENTRY(allowedpeer) next; 268 }; 269 static STAILQ_HEAD(, allowedpeer) aphead = STAILQ_HEAD_INITIALIZER(aphead); 270 271 272 /* 273 * Intervals at which we flush out "message repeated" messages, 274 * in seconds after previous message is logged. After each flush, 275 * we move to the next interval until we reach the largest. 276 */ 277 static int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 278 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 279 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 280 #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ 281 (f)->f_repeatcount = MAXREPEAT; \ 282 } 283 284 /* values for f_type */ 285 #define F_UNUSED 0 /* unused entry */ 286 #define F_FILE 1 /* regular file */ 287 #define F_TTY 2 /* terminal */ 288 #define F_CONSOLE 3 /* console terminal */ 289 #define F_FORW 4 /* remote machine */ 290 #define F_USERS 5 /* list of users */ 291 #define F_WALL 6 /* everyone logged on */ 292 #define F_PIPE 7 /* pipe to program */ 293 294 static const char *TypeNames[8] = { 295 "UNUSED", "FILE", "TTY", "CONSOLE", 296 "FORW", "USERS", "WALL", "PIPE" 297 }; 298 299 static STAILQ_HEAD(, filed) fhead = 300 STAILQ_HEAD_INITIALIZER(fhead); /* Log files that we write to */ 301 static struct filed consfile; /* Console */ 302 303 static int Debug; /* debug flag */ 304 static int Foreground = 0; /* Run in foreground, instead of daemonizing */ 305 static int resolve = 1; /* resolve hostname */ 306 static char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ 307 static const char *LocalDomain; /* our local domain name */ 308 static int fklog = -1; /* /dev/klog */ 309 static int Initialized; /* set when we have initialized ourselves */ 310 static int MarkInterval = 20 * 60; /* interval between marks in seconds */ 311 static int MarkSeq; /* mark sequence number */ 312 static int NoBind; /* don't bind() as suggested by RFC 3164 */ 313 static int SecureMode; /* when true, receive only unix domain socks */ 314 #ifdef INET6 315 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 316 #else 317 static int family = PF_INET; /* protocol family (IPv4 only) */ 318 #endif 319 static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */ 320 static int send_to_all; /* send message to all IPv4/IPv6 addresses */ 321 static int use_bootfile; /* log entire bootfile for every kern msg */ 322 static int no_compress; /* don't compress messages (1=pipes, 2=all) */ 323 static int logflags = O_WRONLY|O_APPEND; /* flags used to open log files */ 324 325 static char bootfile[MAXLINE+1]; /* booted kernel file */ 326 327 static int RemoteAddDate; /* Always set the date on remote messages */ 328 329 static int UniquePriority; /* Only log specified priority? */ 330 static int LogFacPri; /* Put facility and priority in log message: */ 331 /* 0=no, 1=numeric, 2=names */ 332 static int KeepKernFac; /* Keep remotely logged kernel facility */ 333 static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */ 334 static struct pidfh *pfh; 335 336 static volatile sig_atomic_t MarkSet, WantDie; 337 338 static int allowaddr(char *); 339 static void cfline(const char *, struct filed *, 340 const char *, const char *); 341 static const char *cvthname(struct sockaddr *); 342 static void deadq_enter(pid_t, const char *); 343 static int deadq_remove(pid_t); 344 static int decode(const char *, const CODE *); 345 static void die(int) __dead2; 346 static void dodie(int); 347 static void dofsync(void); 348 static void domark(int); 349 static void fprintlog(struct filed *, int, const char *); 350 static void init(int); 351 static void logerror(const char *); 352 static void logmsg(int, const char *, const char *, int); 353 static void log_deadchild(pid_t, int, const char *); 354 static void markit(void); 355 static int socksetup(struct peer *); 356 static int skip_message(const char *, const char *, int); 357 static void printline(const char *, char *, int); 358 static void printsys(char *); 359 static int p_open(const char *, pid_t *); 360 static void readklog(void); 361 static void reapchild(int); 362 static const char *ttymsg_check(struct iovec *, int, char *, int); 363 static void usage(void); 364 static int validate(struct sockaddr *, const char *); 365 static void unmapped(struct sockaddr *); 366 static void wallmsg(struct filed *, struct iovec *, const int iovlen); 367 static int waitdaemon(int, int, int); 368 static void timedout(int); 369 static void increase_rcvbuf(int); 370 371 static void 372 close_filed(struct filed *f) 373 { 374 375 if (f == NULL || f->f_file == -1) 376 return; 377 378 (void)close(f->f_file); 379 f->f_file = -1; 380 f->f_type = F_UNUSED; 381 } 382 383 int 384 main(int argc, char *argv[]) 385 { 386 int ch, i, fdsrmax = 0; 387 struct sockaddr_storage ss; 388 fd_set *fdsr = NULL; 389 char line[MAXLINE + 1]; 390 const char *hname; 391 struct timeval tv, *tvp; 392 struct sigaction sact; 393 struct peer *pe; 394 struct socklist *sl; 395 sigset_t mask; 396 pid_t ppid = 1, spid; 397 socklen_t sslen; 398 char *p; 399 400 if (madvise(NULL, 0, MADV_PROTECT) != 0) 401 dprintf("madvise() failed: %s\n", strerror(errno)); 402 403 while ((ch = getopt(argc, argv, "468Aa:b:cCdf:Fkl:m:nNop:P:sS:Tuv")) 404 != -1) 405 switch (ch) { 406 #ifdef INET 407 case '4': 408 family = PF_INET; 409 break; 410 #endif 411 #ifdef INET6 412 case '6': 413 family = PF_INET6; 414 break; 415 #endif 416 case '8': 417 mask_C1 = 0; 418 break; 419 case 'A': 420 send_to_all++; 421 break; 422 case 'a': /* allow specific network addresses only */ 423 if (allowaddr(optarg) == -1) 424 usage(); 425 break; 426 case 'b': 427 if ((pe = calloc(1, sizeof(*pe))) == NULL) 428 err(1, "malloc failed"); 429 if ((p = strchr(optarg, ':')) == NULL) { 430 /* A hostname or filename only. */ 431 pe->pe_name = optarg; 432 pe->pe_serv = "syslog"; 433 } else { 434 /* The case of "name:service". */ 435 *p++ = '\0'; 436 pe->pe_serv = p; 437 pe->pe_name = (strlen(optarg) == 0) ? 438 NULL : optarg; 439 } 440 STAILQ_INSERT_TAIL(&pqueue, pe, next); 441 break; 442 case 'c': 443 no_compress++; 444 break; 445 case 'C': 446 logflags |= O_CREAT; 447 break; 448 case 'd': /* debug */ 449 Debug++; 450 break; 451 case 'f': /* configuration file */ 452 ConfFile = optarg; 453 break; 454 case 'F': /* run in foreground instead of daemon */ 455 Foreground++; 456 break; 457 case 'k': /* keep remote kern fac */ 458 KeepKernFac = 1; 459 break; 460 case 'l': 461 { 462 long perml; 463 mode_t mode; 464 char *name, *ep; 465 466 if (optarg[0] == '/') { 467 mode = DEFFILEMODE; 468 name = optarg; 469 } else if ((name = strchr(optarg, ':')) != NULL) { 470 *name++ = '\0'; 471 if (name[0] != '/') 472 errx(1, "socket name must be absolute " 473 "path"); 474 if (isdigit(*optarg)) { 475 perml = strtol(optarg, &ep, 8); 476 if (*ep || perml < 0 || 477 perml & ~(S_IRWXU|S_IRWXG|S_IRWXO)) 478 errx(1, "invalid mode %s, exiting", 479 optarg); 480 mode = (mode_t )perml; 481 } else 482 errx(1, "invalid mode %s, exiting", 483 optarg); 484 } 485 if ((pe = calloc(1, sizeof(*pe))) == NULL) 486 err(1, "malloc failed"); 487 pe->pe_name = name; 488 pe->pe_mode = mode; 489 STAILQ_INSERT_TAIL(&pqueue, pe, next); 490 break; 491 } 492 case 'm': /* mark interval */ 493 MarkInterval = atoi(optarg) * 60; 494 break; 495 case 'N': 496 NoBind = 1; 497 SecureMode = 1; 498 break; 499 case 'n': 500 resolve = 0; 501 break; 502 case 'o': 503 use_bootfile = 1; 504 break; 505 case 'p': /* path */ 506 funix_default.pe_name = optarg; 507 break; 508 case 'P': /* path for alt. PID */ 509 PidFile = optarg; 510 break; 511 case 's': /* no network mode */ 512 SecureMode++; 513 break; 514 case 'S': /* path for privileged originator */ 515 funix_secure.pe_name = optarg; 516 break; 517 case 'T': 518 RemoteAddDate = 1; 519 break; 520 case 'u': /* only log specified priority */ 521 UniquePriority++; 522 break; 523 case 'v': /* log facility and priority */ 524 LogFacPri++; 525 break; 526 default: 527 usage(); 528 } 529 if ((argc -= optind) != 0) 530 usage(); 531 STAILQ_FOREACH(pe, &pqueue, next) 532 socksetup(pe); 533 534 pfh = pidfile_open(PidFile, 0600, &spid); 535 if (pfh == NULL) { 536 if (errno == EEXIST) 537 errx(1, "syslogd already running, pid: %d", spid); 538 warn("cannot open pid file"); 539 } 540 541 if ((!Foreground) && (!Debug)) { 542 ppid = waitdaemon(0, 0, 30); 543 if (ppid < 0) { 544 warn("could not become daemon"); 545 pidfile_remove(pfh); 546 exit(1); 547 } 548 } else if (Debug) 549 setlinebuf(stdout); 550 551 consfile.f_type = F_CONSOLE; 552 (void)strlcpy(consfile.fu_fname, ctty + sizeof _PATH_DEV - 1, 553 sizeof(consfile.fu_fname)); 554 (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); 555 (void)signal(SIGTERM, dodie); 556 (void)signal(SIGINT, Debug ? dodie : SIG_IGN); 557 (void)signal(SIGQUIT, Debug ? dodie : SIG_IGN); 558 /* 559 * We don't want the SIGCHLD and SIGHUP handlers to interfere 560 * with each other; they are likely candidates for being called 561 * simultaneously (SIGHUP closes pipe descriptor, process dies, 562 * SIGCHLD happens). 563 */ 564 sigemptyset(&mask); 565 sigaddset(&mask, SIGHUP); 566 sact.sa_handler = reapchild; 567 sact.sa_mask = mask; 568 sact.sa_flags = SA_RESTART; 569 (void)sigaction(SIGCHLD, &sact, NULL); 570 (void)signal(SIGALRM, domark); 571 (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ 572 (void)alarm(TIMERINTVL); 573 574 TAILQ_INIT(&deadq_head); 575 576 if ((fklog = open(_PATH_KLOG, O_RDONLY|O_NONBLOCK, 0)) < 0) 577 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); 578 579 /* tuck my process id away */ 580 pidfile_write(pfh); 581 582 dprintf("off & running....\n"); 583 584 init(0); 585 /* prevent SIGHUP and SIGCHLD handlers from running in parallel */ 586 sigemptyset(&mask); 587 sigaddset(&mask, SIGCHLD); 588 sact.sa_handler = init; 589 sact.sa_mask = mask; 590 sact.sa_flags = SA_RESTART; 591 (void)sigaction(SIGHUP, &sact, NULL); 592 593 tvp = &tv; 594 tv.tv_sec = tv.tv_usec = 0; 595 596 if (fklog != -1 && fklog > fdsrmax) 597 fdsrmax = fklog; 598 STAILQ_FOREACH(sl, &shead, next) { 599 if (sl->sl_socket > fdsrmax) 600 fdsrmax = sl->sl_socket; 601 } 602 603 fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS), 604 sizeof(fd_mask)); 605 if (fdsr == NULL) 606 errx(1, "calloc fd_set"); 607 608 for (;;) { 609 if (MarkSet) 610 markit(); 611 if (WantDie) 612 die(WantDie); 613 614 bzero(fdsr, howmany(fdsrmax+1, NFDBITS) * 615 sizeof(fd_mask)); 616 617 if (fklog != -1) 618 FD_SET(fklog, fdsr); 619 STAILQ_FOREACH(sl, &shead, next) { 620 if (sl->sl_socket != -1) 621 FD_SET(sl->sl_socket, fdsr); 622 } 623 i = select(fdsrmax + 1, fdsr, NULL, NULL, 624 needdofsync ? &tv : tvp); 625 switch (i) { 626 case 0: 627 dofsync(); 628 needdofsync = 0; 629 if (tvp) { 630 tvp = NULL; 631 if (ppid != 1) 632 kill(ppid, SIGALRM); 633 } 634 continue; 635 case -1: 636 if (errno != EINTR) 637 logerror("select"); 638 continue; 639 } 640 if (fklog != -1 && FD_ISSET(fklog, fdsr)) 641 readklog(); 642 STAILQ_FOREACH(sl, &shead, next) { 643 int date, len; 644 645 if (FD_ISSET(sl->sl_socket, fdsr)) { 646 sslen = sizeof(ss); 647 dprintf("sslen(1) = %d\n", sslen); 648 len = recvfrom(sl->sl_socket, line, 649 sizeof(line) - 1, 0, 650 sstosa(&ss), &sslen); 651 dprintf("sslen(2) = %d\n", sslen); 652 if (len == 0) 653 continue; 654 if (len < 0) { 655 if (errno != EINTR) 656 logerror("recvfrom"); 657 continue; 658 } 659 /* Received valid data. */ 660 line[len] = '\0'; 661 if (sl->sl_ss.ss_family == AF_LOCAL) { 662 hname = LocalHostName; 663 date = 0; 664 } else { 665 hname = cvthname(sstosa(&ss)); 666 unmapped(sstosa(&ss)); 667 if (validate(sstosa(&ss), hname) == 0) 668 hname = NULL; 669 date = RemoteAddDate ? ADDDATE : 0; 670 } 671 if (hname != NULL) 672 printline(hname, line, date); 673 else 674 dprintf("Invalid msg from " 675 "%s was ignored.", hname); 676 } 677 } 678 } 679 if (fdsr) 680 free(fdsr); 681 } 682 683 static void 684 unmapped(struct sockaddr *sa) 685 { 686 struct sockaddr_in6 *sin6; 687 struct sockaddr_in sin4; 688 689 if (sa == NULL || 690 sa->sa_family != AF_INET6 || 691 sa->sa_len != sizeof(*sin6)) 692 return; 693 sin6 = satosin6(sa); 694 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 695 return; 696 697 memset(&sin4, 0, sizeof(sin4)); 698 sin4.sin_family = AF_INET; 699 sin4.sin_len = sizeof(sin4); 700 memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12], 701 sizeof(sin4.sin_addr)); 702 sin4.sin_port = sin6->sin6_port; 703 704 memcpy(sa, &sin4, sin4.sin_len); 705 } 706 707 static void 708 usage(void) 709 { 710 711 fprintf(stderr, "%s\n%s\n%s\n%s\n", 712 "usage: syslogd [-468ACcdFknosTuv] [-a allowed_peer]", 713 " [-b bind_address] [-f config_file]", 714 " [-l [mode:]path] [-m mark_interval]", 715 " [-P pid_file] [-p log_socket]"); 716 exit(1); 717 } 718 719 /* 720 * Take a raw input line, decode the message, and print the message 721 * on the appropriate log files. 722 */ 723 static void 724 printline(const char *hname, char *msg, int flags) 725 { 726 char *p, *q; 727 long n; 728 int c, pri; 729 char line[MAXLINE + 1]; 730 731 /* test for special codes */ 732 p = msg; 733 pri = DEFUPRI; 734 if (*p == '<') { 735 errno = 0; 736 n = strtol(p + 1, &q, 10); 737 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 738 p = q + 1; 739 pri = n; 740 } 741 } 742 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 743 pri = DEFUPRI; 744 745 /* 746 * Don't allow users to log kernel messages. 747 * NOTE: since LOG_KERN == 0 this will also match 748 * messages with no facility specified. 749 */ 750 if ((pri & LOG_FACMASK) == LOG_KERN && !KeepKernFac) 751 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 752 753 q = line; 754 755 while ((c = (unsigned char)*p++) != '\0' && 756 q < &line[sizeof(line) - 4]) { 757 if (mask_C1 && (c & 0x80) && c < 0xA0) { 758 c &= 0x7F; 759 *q++ = 'M'; 760 *q++ = '-'; 761 } 762 if (isascii(c) && iscntrl(c)) { 763 if (c == '\n') { 764 *q++ = ' '; 765 } else if (c == '\t') { 766 *q++ = '\t'; 767 } else { 768 *q++ = '^'; 769 *q++ = c ^ 0100; 770 } 771 } else { 772 *q++ = c; 773 } 774 } 775 *q = '\0'; 776 777 logmsg(pri, line, hname, flags); 778 } 779 780 /* 781 * Read /dev/klog while data are available, split into lines. 782 */ 783 static void 784 readklog(void) 785 { 786 char *p, *q, line[MAXLINE + 1]; 787 int len, i; 788 789 len = 0; 790 for (;;) { 791 i = read(fklog, line + len, MAXLINE - 1 - len); 792 if (i > 0) { 793 line[i + len] = '\0'; 794 } else { 795 if (i < 0 && errno != EINTR && errno != EAGAIN) { 796 logerror("klog"); 797 fklog = -1; 798 } 799 break; 800 } 801 802 for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { 803 *q = '\0'; 804 printsys(p); 805 } 806 len = strlen(p); 807 if (len >= MAXLINE - 1) { 808 printsys(p); 809 len = 0; 810 } 811 if (len > 0) 812 memmove(line, p, len + 1); 813 } 814 if (len > 0) 815 printsys(line); 816 } 817 818 /* 819 * Take a raw input line from /dev/klog, format similar to syslog(). 820 */ 821 static void 822 printsys(char *msg) 823 { 824 char *p, *q; 825 long n; 826 int flags, isprintf, pri; 827 828 flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */ 829 p = msg; 830 pri = DEFSPRI; 831 isprintf = 1; 832 if (*p == '<') { 833 errno = 0; 834 n = strtol(p + 1, &q, 10); 835 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 836 p = q + 1; 837 pri = n; 838 isprintf = 0; 839 } 840 } 841 /* 842 * Kernel printf's and LOG_CONSOLE messages have been displayed 843 * on the console already. 844 */ 845 if (isprintf || (pri & LOG_FACMASK) == LOG_CONSOLE) 846 flags |= IGN_CONS; 847 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 848 pri = DEFSPRI; 849 logmsg(pri, p, LocalHostName, flags); 850 } 851 852 static time_t now; 853 854 /* 855 * Match a program or host name against a specification. 856 * Return a non-0 value if the message must be ignored 857 * based on the specification. 858 */ 859 static int 860 skip_message(const char *name, const char *spec, int checkcase) 861 { 862 const char *s; 863 char prev, next; 864 int exclude = 0; 865 /* Behaviour on explicit match */ 866 867 if (spec == NULL) 868 return 0; 869 switch (*spec) { 870 case '-': 871 exclude = 1; 872 /*FALLTHROUGH*/ 873 case '+': 874 spec++; 875 break; 876 default: 877 break; 878 } 879 if (checkcase) 880 s = strstr (spec, name); 881 else 882 s = strcasestr (spec, name); 883 884 if (s != NULL) { 885 prev = (s == spec ? ',' : *(s - 1)); 886 next = *(s + strlen (name)); 887 888 if (prev == ',' && (next == '\0' || next == ',')) 889 /* Explicit match: skip iff the spec is an 890 exclusive one. */ 891 return exclude; 892 } 893 894 /* No explicit match for this name: skip the message iff 895 the spec is an inclusive one. */ 896 return !exclude; 897 } 898 899 /* 900 * Log a message to the appropriate log files, users, etc. based on 901 * the priority. 902 */ 903 static void 904 logmsg(int pri, const char *msg, const char *from, int flags) 905 { 906 struct filed *f; 907 int i, fac, msglen, omask, prilev; 908 const char *timestamp; 909 char prog[NAME_MAX+1]; 910 char buf[MAXLINE+1]; 911 912 dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", 913 pri, flags, from, msg); 914 915 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 916 917 /* 918 * Check to see if msg looks non-standard. 919 */ 920 msglen = strlen(msg); 921 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || 922 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 923 flags |= ADDDATE; 924 925 (void)time(&now); 926 if (flags & ADDDATE) { 927 timestamp = ctime(&now) + 4; 928 } else { 929 timestamp = msg; 930 msg += 16; 931 msglen -= 16; 932 } 933 934 /* skip leading blanks */ 935 while (isspace(*msg)) { 936 msg++; 937 msglen--; 938 } 939 940 /* extract facility and priority level */ 941 if (flags & MARK) 942 fac = LOG_NFACILITIES; 943 else 944 fac = LOG_FAC(pri); 945 946 /* Check maximum facility number. */ 947 if (fac > LOG_NFACILITIES) { 948 (void)sigsetmask(omask); 949 return; 950 } 951 952 prilev = LOG_PRI(pri); 953 954 /* extract program name */ 955 for (i = 0; i < NAME_MAX; i++) { 956 if (!isprint(msg[i]) || msg[i] == ':' || msg[i] == '[' || 957 msg[i] == '/' || isspace(msg[i])) 958 break; 959 prog[i] = msg[i]; 960 } 961 prog[i] = 0; 962 963 /* add kernel prefix for kernel messages */ 964 if (flags & ISKERNEL) { 965 snprintf(buf, sizeof(buf), "%s: %s", 966 use_bootfile ? bootfile : "kernel", msg); 967 msg = buf; 968 msglen = strlen(buf); 969 } 970 971 /* log the message to the particular outputs */ 972 if (!Initialized) { 973 f = &consfile; 974 /* 975 * Open in non-blocking mode to avoid hangs during open 976 * and close(waiting for the port to drain). 977 */ 978 f->f_file = open(ctty, O_WRONLY | O_NONBLOCK, 0); 979 980 if (f->f_file >= 0) { 981 (void)strlcpy(f->f_lasttime, timestamp, 982 sizeof(f->f_lasttime)); 983 fprintlog(f, flags, msg); 984 close(f->f_file); 985 f->f_file = -1; 986 } 987 (void)sigsetmask(omask); 988 return; 989 } 990 STAILQ_FOREACH(f, &fhead, next) { 991 /* skip messages that are incorrect priority */ 992 if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev)) 993 ||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev)) 994 ||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev)) 995 ) 996 || f->f_pmask[fac] == INTERNAL_NOPRI) 997 continue; 998 999 /* skip messages with the incorrect hostname */ 1000 if (skip_message(from, f->f_host, 0)) 1001 continue; 1002 1003 /* skip messages with the incorrect program name */ 1004 if (skip_message(prog, f->f_program, 1)) 1005 continue; 1006 1007 /* skip message to console if it has already been printed */ 1008 if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) 1009 continue; 1010 1011 /* don't output marks to recently written files */ 1012 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) 1013 continue; 1014 1015 /* 1016 * suppress duplicate lines to this file 1017 */ 1018 if (no_compress - (f->f_type != F_PIPE) < 1 && 1019 (flags & MARK) == 0 && msglen == f->f_prevlen && 1020 !strcmp(msg, f->f_prevline) && 1021 !strcasecmp(from, f->f_prevhost)) { 1022 (void)strlcpy(f->f_lasttime, timestamp, 1023 sizeof(f->f_lasttime)); 1024 f->f_prevcount++; 1025 dprintf("msg repeated %d times, %ld sec of %d\n", 1026 f->f_prevcount, (long)(now - f->f_time), 1027 repeatinterval[f->f_repeatcount]); 1028 /* 1029 * If domark would have logged this by now, 1030 * flush it now (so we don't hold isolated messages), 1031 * but back off so we'll flush less often 1032 * in the future. 1033 */ 1034 if (now > REPEATTIME(f)) { 1035 fprintlog(f, flags, (char *)NULL); 1036 BACKOFF(f); 1037 } 1038 } else { 1039 /* new line, save it */ 1040 if (f->f_prevcount) 1041 fprintlog(f, 0, (char *)NULL); 1042 f->f_repeatcount = 0; 1043 f->f_prevpri = pri; 1044 (void)strlcpy(f->f_lasttime, timestamp, 1045 sizeof(f->f_lasttime)); 1046 (void)strlcpy(f->f_prevhost, from, 1047 sizeof(f->f_prevhost)); 1048 if (msglen < MAXSVLINE) { 1049 f->f_prevlen = msglen; 1050 (void)strlcpy(f->f_prevline, msg, sizeof(f->f_prevline)); 1051 fprintlog(f, flags, (char *)NULL); 1052 } else { 1053 f->f_prevline[0] = 0; 1054 f->f_prevlen = 0; 1055 fprintlog(f, flags, msg); 1056 } 1057 } 1058 } 1059 (void)sigsetmask(omask); 1060 } 1061 1062 static void 1063 dofsync(void) 1064 { 1065 struct filed *f; 1066 1067 STAILQ_FOREACH(f, &fhead, next) { 1068 if ((f->f_type == F_FILE) && 1069 (f->f_flags & FFLAG_NEEDSYNC)) { 1070 f->f_flags &= ~FFLAG_NEEDSYNC; 1071 (void)fsync(f->f_file); 1072 } 1073 } 1074 } 1075 1076 #define IOV_SIZE 7 1077 static void 1078 fprintlog(struct filed *f, int flags, const char *msg) 1079 { 1080 struct iovec iov[IOV_SIZE]; 1081 struct iovec *v; 1082 struct addrinfo *r; 1083 int l, lsent = 0; 1084 char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg = NULL; 1085 char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n"; 1086 const char *msgret; 1087 1088 v = iov; 1089 if (f->f_type == F_WALL) { 1090 v->iov_base = greetings; 1091 /* The time displayed is not synchornized with the other log 1092 * destinations (like messages). Following fragment was using 1093 * ctime(&now), which was updating the time every 30 sec. 1094 * With f_lasttime, time is synchronized correctly. 1095 */ 1096 v->iov_len = snprintf(greetings, sizeof greetings, 1097 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", 1098 f->f_prevhost, f->f_lasttime); 1099 if (v->iov_len >= sizeof greetings) 1100 v->iov_len = sizeof greetings - 1; 1101 v++; 1102 v->iov_base = nul; 1103 v->iov_len = 0; 1104 v++; 1105 } else { 1106 v->iov_base = f->f_lasttime; 1107 v->iov_len = strlen(f->f_lasttime); 1108 v++; 1109 v->iov_base = space; 1110 v->iov_len = 1; 1111 v++; 1112 } 1113 1114 if (LogFacPri) { 1115 static char fp_buf[30]; /* Hollow laugh */ 1116 int fac = f->f_prevpri & LOG_FACMASK; 1117 int pri = LOG_PRI(f->f_prevpri); 1118 const char *f_s = NULL; 1119 char f_n[5]; /* Hollow laugh */ 1120 const char *p_s = NULL; 1121 char p_n[5]; /* Hollow laugh */ 1122 1123 if (LogFacPri > 1) { 1124 const CODE *c; 1125 1126 for (c = facilitynames; c->c_name; c++) { 1127 if (c->c_val == fac) { 1128 f_s = c->c_name; 1129 break; 1130 } 1131 } 1132 for (c = prioritynames; c->c_name; c++) { 1133 if (c->c_val == pri) { 1134 p_s = c->c_name; 1135 break; 1136 } 1137 } 1138 } 1139 if (!f_s) { 1140 snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac)); 1141 f_s = f_n; 1142 } 1143 if (!p_s) { 1144 snprintf(p_n, sizeof p_n, "%d", pri); 1145 p_s = p_n; 1146 } 1147 snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s); 1148 v->iov_base = fp_buf; 1149 v->iov_len = strlen(fp_buf); 1150 } else { 1151 v->iov_base = nul; 1152 v->iov_len = 0; 1153 } 1154 v++; 1155 1156 v->iov_base = f->f_prevhost; 1157 v->iov_len = strlen(v->iov_base); 1158 v++; 1159 v->iov_base = space; 1160 v->iov_len = 1; 1161 v++; 1162 1163 if (msg) { 1164 wmsg = strdup(msg); /* XXX iov_base needs a `const' sibling. */ 1165 if (wmsg == NULL) { 1166 logerror("strdup"); 1167 exit(1); 1168 } 1169 v->iov_base = wmsg; 1170 v->iov_len = strlen(msg); 1171 } else if (f->f_prevcount > 1) { 1172 v->iov_base = repbuf; 1173 v->iov_len = snprintf(repbuf, sizeof repbuf, 1174 "last message repeated %d times", f->f_prevcount); 1175 } else { 1176 v->iov_base = f->f_prevline; 1177 v->iov_len = f->f_prevlen; 1178 } 1179 v++; 1180 1181 dprintf("Logging to %s", TypeNames[f->f_type]); 1182 f->f_time = now; 1183 1184 switch (f->f_type) { 1185 int port; 1186 case F_UNUSED: 1187 dprintf("\n"); 1188 break; 1189 1190 case F_FORW: 1191 port = ntohs(satosin(f->fu_forw_addr->ai_addr)->sin_port); 1192 if (port != 514) { 1193 dprintf(" %s:%d\n", f->fu_forw_hname, port); 1194 } else { 1195 dprintf(" %s\n", f->fu_forw_hname); 1196 } 1197 /* check for local vs remote messages */ 1198 if (strcasecmp(f->f_prevhost, LocalHostName)) 1199 l = snprintf(line, sizeof line - 1, 1200 "<%d>%.15s Forwarded from %s: %s", 1201 f->f_prevpri, (char *)iov[0].iov_base, 1202 f->f_prevhost, (char *)iov[5].iov_base); 1203 else 1204 l = snprintf(line, sizeof line - 1, "<%d>%.15s %s", 1205 f->f_prevpri, (char *)iov[0].iov_base, 1206 (char *)iov[5].iov_base); 1207 if (l < 0) 1208 l = 0; 1209 else if (l > MAXLINE) 1210 l = MAXLINE; 1211 1212 for (r = f->fu_forw_addr; r; r = r->ai_next) { 1213 struct socklist *sl; 1214 1215 STAILQ_FOREACH(sl, &shead, next) { 1216 if (sl->sl_ss.ss_family == AF_LOCAL) 1217 continue; 1218 lsent = sendto(sl->sl_socket, line, l, 0, 1219 r->ai_addr, r->ai_addrlen); 1220 if (lsent == l) 1221 break; 1222 } 1223 if (lsent == l && !send_to_all) 1224 break; 1225 } 1226 dprintf("lsent/l: %d/%d\n", lsent, l); 1227 if (lsent != l) { 1228 int e = errno; 1229 logerror("sendto"); 1230 errno = e; 1231 switch (errno) { 1232 case ENOBUFS: 1233 case ENETDOWN: 1234 case ENETUNREACH: 1235 case EHOSTUNREACH: 1236 case EHOSTDOWN: 1237 case EADDRNOTAVAIL: 1238 break; 1239 /* case EBADF: */ 1240 /* case EACCES: */ 1241 /* case ENOTSOCK: */ 1242 /* case EFAULT: */ 1243 /* case EMSGSIZE: */ 1244 /* case EAGAIN: */ 1245 /* case ENOBUFS: */ 1246 /* case ECONNREFUSED: */ 1247 default: 1248 dprintf("removing entry: errno=%d\n", e); 1249 f->f_type = F_UNUSED; 1250 break; 1251 } 1252 } 1253 break; 1254 1255 case F_FILE: 1256 dprintf(" %s\n", f->fu_fname); 1257 v->iov_base = lf; 1258 v->iov_len = 1; 1259 if (writev(f->f_file, iov, IOV_SIZE) < 0) { 1260 /* 1261 * If writev(2) fails for potentially transient errors 1262 * like the filesystem being full, ignore it. 1263 * Otherwise remove this logfile from the list. 1264 */ 1265 if (errno != ENOSPC) { 1266 int e = errno; 1267 close_filed(f); 1268 errno = e; 1269 logerror(f->fu_fname); 1270 } 1271 } else if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC)) { 1272 f->f_flags |= FFLAG_NEEDSYNC; 1273 needdofsync = 1; 1274 } 1275 break; 1276 1277 case F_PIPE: 1278 dprintf(" %s\n", f->fu_pipe_pname); 1279 v->iov_base = lf; 1280 v->iov_len = 1; 1281 if (f->fu_pipe_pid == 0) { 1282 if ((f->f_file = p_open(f->fu_pipe_pname, 1283 &f->fu_pipe_pid)) < 0) { 1284 f->f_type = F_UNUSED; 1285 logerror(f->fu_pipe_pname); 1286 break; 1287 } 1288 } 1289 if (writev(f->f_file, iov, IOV_SIZE) < 0) { 1290 int e = errno; 1291 close_filed(f); 1292 if (f->fu_pipe_pid > 0) 1293 deadq_enter(f->fu_pipe_pid, 1294 f->fu_pipe_pname); 1295 f->fu_pipe_pid = 0; 1296 errno = e; 1297 logerror(f->fu_pipe_pname); 1298 } 1299 break; 1300 1301 case F_CONSOLE: 1302 if (flags & IGN_CONS) { 1303 dprintf(" (ignored)\n"); 1304 break; 1305 } 1306 /* FALLTHROUGH */ 1307 1308 case F_TTY: 1309 dprintf(" %s%s\n", _PATH_DEV, f->fu_fname); 1310 v->iov_base = crlf; 1311 v->iov_len = 2; 1312 1313 errno = 0; /* ttymsg() only sometimes returns an errno */ 1314 if ((msgret = ttymsg(iov, IOV_SIZE, f->fu_fname, 10))) { 1315 f->f_type = F_UNUSED; 1316 logerror(msgret); 1317 } 1318 break; 1319 1320 case F_USERS: 1321 case F_WALL: 1322 dprintf("\n"); 1323 v->iov_base = crlf; 1324 v->iov_len = 2; 1325 wallmsg(f, iov, IOV_SIZE); 1326 break; 1327 } 1328 f->f_prevcount = 0; 1329 free(wmsg); 1330 } 1331 1332 /* 1333 * WALLMSG -- Write a message to the world at large 1334 * 1335 * Write the specified message to either the entire 1336 * world, or a list of approved users. 1337 */ 1338 static void 1339 wallmsg(struct filed *f, struct iovec *iov, const int iovlen) 1340 { 1341 static int reenter; /* avoid calling ourselves */ 1342 struct utmpx *ut; 1343 int i; 1344 const char *p; 1345 1346 if (reenter++) 1347 return; 1348 setutxent(); 1349 /* NOSTRICT */ 1350 while ((ut = getutxent()) != NULL) { 1351 if (ut->ut_type != USER_PROCESS) 1352 continue; 1353 if (f->f_type == F_WALL) { 1354 if ((p = ttymsg(iov, iovlen, ut->ut_line, 1355 TTYMSGTIME)) != NULL) { 1356 errno = 0; /* already in msg */ 1357 logerror(p); 1358 } 1359 continue; 1360 } 1361 /* should we send the message to this user? */ 1362 for (i = 0; i < MAXUNAMES; i++) { 1363 if (!f->fu_uname[i][0]) 1364 break; 1365 if (!strcmp(f->fu_uname[i], ut->ut_user)) { 1366 if ((p = ttymsg_check(iov, iovlen, ut->ut_line, 1367 TTYMSGTIME)) != NULL) { 1368 errno = 0; /* already in msg */ 1369 logerror(p); 1370 } 1371 break; 1372 } 1373 } 1374 } 1375 endutxent(); 1376 reenter = 0; 1377 } 1378 1379 /* 1380 * Wrapper routine for ttymsg() that checks the terminal for messages enabled. 1381 */ 1382 static const char * 1383 ttymsg_check(struct iovec *iov, int iovcnt, char *line, int tmout) 1384 { 1385 static char device[1024]; 1386 static char errbuf[1024]; 1387 struct stat sb; 1388 1389 (void) snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line); 1390 1391 if (stat(device, &sb) < 0) { 1392 (void) snprintf(errbuf, sizeof(errbuf), 1393 "%s: %s", device, strerror(errno)); 1394 return (errbuf); 1395 } 1396 if ((sb.st_mode & S_IWGRP) == 0) 1397 /* Messages disabled. */ 1398 return (NULL); 1399 return ttymsg(iov, iovcnt, line, tmout); 1400 } 1401 1402 static void 1403 reapchild(int signo __unused) 1404 { 1405 int status; 1406 pid_t pid; 1407 struct filed *f; 1408 1409 while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { 1410 if (!Initialized) 1411 /* Don't tell while we are initting. */ 1412 continue; 1413 1414 /* First, look if it's a process from the dead queue. */ 1415 if (deadq_remove(pid)) 1416 continue; 1417 1418 /* Now, look in list of active processes. */ 1419 STAILQ_FOREACH(f, &fhead, next) { 1420 if (f->f_type == F_PIPE && 1421 f->fu_pipe_pid == pid) { 1422 close_filed(f); 1423 f->fu_pipe_pid = 0; 1424 log_deadchild(pid, status, f->fu_pipe_pname); 1425 break; 1426 } 1427 } 1428 } 1429 } 1430 1431 /* 1432 * Return a printable representation of a host address. 1433 */ 1434 static const char * 1435 cvthname(struct sockaddr *f) 1436 { 1437 int error, hl; 1438 sigset_t omask, nmask; 1439 static char hname[NI_MAXHOST], ip[NI_MAXHOST]; 1440 1441 dprintf("cvthname(%d) len = %d, %zu\n", f->sa_family, f->sa_len, sizeof(struct sockaddr_in6)); 1442 error = getnameinfo(f, f->sa_len, ip, sizeof(ip), NULL, 0, 1443 NI_NUMERICHOST); 1444 dprintf("cvthname(%s)\n", ip); 1445 1446 if (error) { 1447 dprintf("Malformed from address %s\n", gai_strerror(error)); 1448 return ("???"); 1449 } 1450 if (!resolve) 1451 return (ip); 1452 1453 sigemptyset(&nmask); 1454 sigaddset(&nmask, SIGHUP); 1455 sigprocmask(SIG_BLOCK, &nmask, &omask); 1456 error = getnameinfo((struct sockaddr *)f, 1457 ((struct sockaddr *)f)->sa_len, 1458 hname, sizeof hname, NULL, 0, NI_NAMEREQD); 1459 sigprocmask(SIG_SETMASK, &omask, NULL); 1460 if (error) { 1461 dprintf("Host name for your address (%s) unknown\n", ip); 1462 return (ip); 1463 } 1464 hl = strlen(hname); 1465 if (hl > 0 && hname[hl-1] == '.') 1466 hname[--hl] = '\0'; 1467 trimdomain(hname, hl); 1468 return (hname); 1469 } 1470 1471 static void 1472 dodie(int signo) 1473 { 1474 1475 WantDie = signo; 1476 } 1477 1478 static void 1479 domark(int signo __unused) 1480 { 1481 1482 MarkSet = 1; 1483 } 1484 1485 /* 1486 * Print syslogd errors some place. 1487 */ 1488 static void 1489 logerror(const char *type) 1490 { 1491 char buf[512]; 1492 static int recursed = 0; 1493 1494 /* If there's an error while trying to log an error, give up. */ 1495 if (recursed) 1496 return; 1497 recursed++; 1498 if (errno) 1499 (void)snprintf(buf, 1500 sizeof buf, "syslogd: %s: %s", type, strerror(errno)); 1501 else 1502 (void)snprintf(buf, sizeof buf, "syslogd: %s", type); 1503 errno = 0; 1504 dprintf("%s\n", buf); 1505 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); 1506 recursed--; 1507 } 1508 1509 static void 1510 die(int signo) 1511 { 1512 struct filed *f; 1513 struct socklist *sl; 1514 int was_initialized; 1515 char buf[100]; 1516 1517 was_initialized = Initialized; 1518 Initialized = 0; /* Don't log SIGCHLDs. */ 1519 STAILQ_FOREACH(f, &fhead, next) { 1520 /* flush any pending output */ 1521 if (f->f_prevcount) 1522 fprintlog(f, 0, (char *)NULL); 1523 if (f->f_type == F_PIPE && f->fu_pipe_pid > 0) { 1524 close_filed(f); 1525 f->fu_pipe_pid = 0; 1526 } 1527 } 1528 Initialized = was_initialized; 1529 if (signo) { 1530 dprintf("syslogd: exiting on signal %d\n", signo); 1531 (void)snprintf(buf, sizeof(buf), "exiting on signal %d", signo); 1532 errno = 0; 1533 logerror(buf); 1534 } 1535 STAILQ_FOREACH(sl, &shead, next) { 1536 if (sl->sl_ss.ss_family == AF_LOCAL) 1537 unlink(sl->sl_peer->pe_name); 1538 } 1539 pidfile_remove(pfh); 1540 1541 exit(1); 1542 } 1543 1544 static int 1545 configfiles(const struct dirent *dp) 1546 { 1547 const char *p; 1548 size_t ext_len; 1549 1550 if (dp->d_name[0] == '.') 1551 return (0); 1552 1553 ext_len = sizeof(include_ext) -1; 1554 1555 if (dp->d_namlen <= ext_len) 1556 return (0); 1557 1558 p = &dp->d_name[dp->d_namlen - ext_len]; 1559 if (strcmp(p, include_ext) != 0) 1560 return (0); 1561 1562 return (1); 1563 } 1564 1565 static void 1566 readconfigfile(FILE *cf, int allow_includes) 1567 { 1568 FILE *cf2; 1569 struct filed *f; 1570 struct dirent **ent; 1571 char cline[LINE_MAX]; 1572 char host[MAXHOSTNAMELEN]; 1573 char prog[LINE_MAX]; 1574 char file[MAXPATHLEN]; 1575 char *p, *tmp; 1576 int i, nents; 1577 size_t include_len; 1578 1579 /* 1580 * Foreach line in the conf table, open that file. 1581 */ 1582 f = NULL; 1583 include_len = sizeof(include_str) -1; 1584 (void)strlcpy(host, "*", sizeof(host)); 1585 (void)strlcpy(prog, "*", sizeof(prog)); 1586 while (fgets(cline, sizeof(cline), cf) != NULL) { 1587 /* 1588 * check for end-of-section, comments, strip off trailing 1589 * spaces and newline character. #!prog is treated specially: 1590 * following lines apply only to that program. 1591 */ 1592 for (p = cline; isspace(*p); ++p) 1593 continue; 1594 if (*p == 0) 1595 continue; 1596 if (allow_includes && 1597 strncmp(p, include_str, include_len) == 0 && 1598 isspace(p[include_len])) { 1599 p += include_len; 1600 while (isspace(*p)) 1601 p++; 1602 tmp = p; 1603 while (*tmp != '\0' && !isspace(*tmp)) 1604 tmp++; 1605 *tmp = '\0'; 1606 dprintf("Trying to include files in '%s'\n", p); 1607 nents = scandir(p, &ent, configfiles, alphasort); 1608 if (nents == -1) { 1609 dprintf("Unable to open '%s': %s\n", p, 1610 strerror(errno)); 1611 continue; 1612 } 1613 for (i = 0; i < nents; i++) { 1614 if (snprintf(file, sizeof(file), "%s/%s", p, 1615 ent[i]->d_name) >= (int)sizeof(file)) { 1616 dprintf("ignoring path too long: " 1617 "'%s/%s'\n", p, ent[i]->d_name); 1618 free(ent[i]); 1619 continue; 1620 } 1621 free(ent[i]); 1622 cf2 = fopen(file, "r"); 1623 if (cf2 == NULL) 1624 continue; 1625 dprintf("reading %s\n", file); 1626 readconfigfile(cf2, 0); 1627 fclose(cf2); 1628 } 1629 free(ent); 1630 continue; 1631 } 1632 if (*p == '#') { 1633 p++; 1634 if (*p != '!' && *p != '+' && *p != '-') 1635 continue; 1636 } 1637 if (*p == '+' || *p == '-') { 1638 host[0] = *p++; 1639 while (isspace(*p)) 1640 p++; 1641 if ((!*p) || (*p == '*')) { 1642 (void)strlcpy(host, "*", sizeof(host)); 1643 continue; 1644 } 1645 if (*p == '@') 1646 p = LocalHostName; 1647 for (i = 1; i < MAXHOSTNAMELEN - 1; i++) { 1648 if (!isalnum(*p) && *p != '.' && *p != '-' 1649 && *p != ',' && *p != ':' && *p != '%') 1650 break; 1651 host[i] = *p++; 1652 } 1653 host[i] = '\0'; 1654 continue; 1655 } 1656 if (*p == '!') { 1657 p++; 1658 while (isspace(*p)) p++; 1659 if ((!*p) || (*p == '*')) { 1660 (void)strlcpy(prog, "*", sizeof(prog)); 1661 continue; 1662 } 1663 for (i = 0; i < LINE_MAX - 1; i++) { 1664 if (!isprint(p[i]) || isspace(p[i])) 1665 break; 1666 prog[i] = p[i]; 1667 } 1668 prog[i] = 0; 1669 continue; 1670 } 1671 for (p = cline + 1; *p != '\0'; p++) { 1672 if (*p != '#') 1673 continue; 1674 if (*(p - 1) == '\\') { 1675 strcpy(p - 1, p); 1676 p--; 1677 continue; 1678 } 1679 *p = '\0'; 1680 break; 1681 } 1682 for (i = strlen(cline) - 1; i >= 0 && isspace(cline[i]); i--) 1683 cline[i] = '\0'; 1684 f = (struct filed *)calloc(1, sizeof(*f)); 1685 if (f == NULL) { 1686 logerror("calloc"); 1687 exit(1); 1688 } 1689 STAILQ_INSERT_TAIL(&fhead, f, next); 1690 cfline(cline, f, prog, host); 1691 } 1692 } 1693 1694 /* 1695 * INIT -- Initialize syslogd from configuration table 1696 */ 1697 static void 1698 init(int signo) 1699 { 1700 int i; 1701 FILE *cf; 1702 struct filed *f; 1703 char *p; 1704 char oldLocalHostName[MAXHOSTNAMELEN]; 1705 char hostMsg[2*MAXHOSTNAMELEN+40]; 1706 char bootfileMsg[LINE_MAX]; 1707 1708 dprintf("init\n"); 1709 1710 /* 1711 * Load hostname (may have changed). 1712 */ 1713 if (signo != 0) 1714 (void)strlcpy(oldLocalHostName, LocalHostName, 1715 sizeof(oldLocalHostName)); 1716 if (gethostname(LocalHostName, sizeof(LocalHostName))) 1717 err(EX_OSERR, "gethostname() failed"); 1718 if ((p = strchr(LocalHostName, '.')) != NULL) { 1719 *p++ = '\0'; 1720 LocalDomain = p; 1721 } else { 1722 LocalDomain = ""; 1723 } 1724 1725 /* 1726 * Load / reload timezone data (in case it changed). 1727 * 1728 * Just calling tzset() again does not work, the timezone code 1729 * caches the result. However, by setting the TZ variable, one 1730 * can defeat the caching and have the timezone code really 1731 * reload the timezone data. Respect any initial setting of 1732 * TZ, in case the system is configured specially. 1733 */ 1734 dprintf("loading timezone data via tzset()\n"); 1735 if (getenv("TZ")) { 1736 tzset(); 1737 } else { 1738 setenv("TZ", ":/etc/localtime", 1); 1739 tzset(); 1740 unsetenv("TZ"); 1741 } 1742 1743 /* 1744 * Close all open log files. 1745 */ 1746 Initialized = 0; 1747 STAILQ_FOREACH(f, &fhead, next) { 1748 /* flush any pending output */ 1749 if (f->f_prevcount) 1750 fprintlog(f, 0, (char *)NULL); 1751 1752 switch (f->f_type) { 1753 case F_FILE: 1754 case F_FORW: 1755 case F_CONSOLE: 1756 case F_TTY: 1757 close_filed(f); 1758 break; 1759 case F_PIPE: 1760 if (f->fu_pipe_pid > 0) { 1761 close_filed(f); 1762 deadq_enter(f->fu_pipe_pid, 1763 f->fu_pipe_pname); 1764 } 1765 f->fu_pipe_pid = 0; 1766 break; 1767 } 1768 } 1769 while(!STAILQ_EMPTY(&fhead)) { 1770 f = STAILQ_FIRST(&fhead); 1771 STAILQ_REMOVE_HEAD(&fhead, next); 1772 free(f->f_program); 1773 free(f->f_host); 1774 free(f); 1775 } 1776 1777 /* open the configuration file */ 1778 if ((cf = fopen(ConfFile, "r")) == NULL) { 1779 dprintf("cannot open %s\n", ConfFile); 1780 f = calloc(1, sizeof(*f)); 1781 if (f == NULL) { 1782 logerror("calloc"); 1783 exit(1); 1784 } 1785 cfline("*.ERR\t/dev/console", f, "*", "*"); 1786 STAILQ_INSERT_TAIL(&fhead, f, next); 1787 1788 f = calloc(1, sizeof(*f)); 1789 if (f == NULL) { 1790 logerror("calloc"); 1791 exit(1); 1792 } 1793 cfline("*.PANIC\t*", f, "*", "*"); 1794 STAILQ_INSERT_TAIL(&fhead, f, next); 1795 1796 Initialized = 1; 1797 return; 1798 } 1799 1800 readconfigfile(cf, 1); 1801 1802 /* close the configuration file */ 1803 (void)fclose(cf); 1804 1805 Initialized = 1; 1806 1807 if (Debug) { 1808 int port; 1809 STAILQ_FOREACH(f, &fhead, next) { 1810 for (i = 0; i <= LOG_NFACILITIES; i++) 1811 if (f->f_pmask[i] == INTERNAL_NOPRI) 1812 printf("X "); 1813 else 1814 printf("%d ", f->f_pmask[i]); 1815 printf("%s: ", TypeNames[f->f_type]); 1816 switch (f->f_type) { 1817 case F_FILE: 1818 printf("%s", f->fu_fname); 1819 break; 1820 1821 case F_CONSOLE: 1822 case F_TTY: 1823 printf("%s%s", _PATH_DEV, f->fu_fname); 1824 break; 1825 1826 case F_FORW: 1827 port = ntohs(satosin(f->fu_forw_addr->ai_addr)->sin_port); 1828 if (port != 514) { 1829 printf("%s:%d", 1830 f->fu_forw_hname, port); 1831 } else { 1832 printf("%s", f->fu_forw_hname); 1833 } 1834 break; 1835 1836 case F_PIPE: 1837 printf("%s", f->fu_pipe_pname); 1838 break; 1839 1840 case F_USERS: 1841 for (i = 0; i < MAXUNAMES && *f->fu_uname[i]; i++) 1842 printf("%s, ", f->fu_uname[i]); 1843 break; 1844 } 1845 if (f->f_program) 1846 printf(" (%s)", f->f_program); 1847 printf("\n"); 1848 } 1849 } 1850 1851 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); 1852 dprintf("syslogd: restarted\n"); 1853 /* 1854 * Log a change in hostname, but only on a restart. 1855 */ 1856 if (signo != 0 && strcmp(oldLocalHostName, LocalHostName) != 0) { 1857 (void)snprintf(hostMsg, sizeof(hostMsg), 1858 "syslogd: hostname changed, \"%s\" to \"%s\"", 1859 oldLocalHostName, LocalHostName); 1860 logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE); 1861 dprintf("%s\n", hostMsg); 1862 } 1863 /* 1864 * Log the kernel boot file if we aren't going to use it as 1865 * the prefix, and if this is *not* a restart. 1866 */ 1867 if (signo == 0 && !use_bootfile) { 1868 (void)snprintf(bootfileMsg, sizeof(bootfileMsg), 1869 "syslogd: kernel boot file is %s", bootfile); 1870 logmsg(LOG_KERN|LOG_INFO, bootfileMsg, LocalHostName, ADDDATE); 1871 dprintf("%s\n", bootfileMsg); 1872 } 1873 } 1874 1875 /* 1876 * Crack a configuration file line 1877 */ 1878 static void 1879 cfline(const char *line, struct filed *f, const char *prog, const char *host) 1880 { 1881 struct addrinfo hints, *res; 1882 int error, i, pri, syncfile; 1883 const char *p, *q; 1884 char *bp; 1885 char buf[MAXLINE], ebuf[100]; 1886 1887 dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host); 1888 1889 errno = 0; /* keep strerror() stuff out of logerror messages */ 1890 1891 /* clear out file entry */ 1892 memset(f, 0, sizeof(*f)); 1893 for (i = 0; i <= LOG_NFACILITIES; i++) 1894 f->f_pmask[i] = INTERNAL_NOPRI; 1895 1896 /* save hostname if any */ 1897 if (host && *host == '*') 1898 host = NULL; 1899 if (host) { 1900 int hl; 1901 1902 f->f_host = strdup(host); 1903 if (f->f_host == NULL) { 1904 logerror("strdup"); 1905 exit(1); 1906 } 1907 hl = strlen(f->f_host); 1908 if (hl > 0 && f->f_host[hl-1] == '.') 1909 f->f_host[--hl] = '\0'; 1910 trimdomain(f->f_host, hl); 1911 } 1912 1913 /* save program name if any */ 1914 if (prog && *prog == '*') 1915 prog = NULL; 1916 if (prog) { 1917 f->f_program = strdup(prog); 1918 if (f->f_program == NULL) { 1919 logerror("strdup"); 1920 exit(1); 1921 } 1922 } 1923 1924 /* scan through the list of selectors */ 1925 for (p = line; *p && *p != '\t' && *p != ' ';) { 1926 int pri_done; 1927 int pri_cmp; 1928 int pri_invert; 1929 1930 /* find the end of this facility name list */ 1931 for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; ) 1932 continue; 1933 1934 /* get the priority comparison */ 1935 pri_cmp = 0; 1936 pri_done = 0; 1937 pri_invert = 0; 1938 if (*q == '!') { 1939 pri_invert = 1; 1940 q++; 1941 } 1942 while (!pri_done) { 1943 switch (*q) { 1944 case '<': 1945 pri_cmp |= PRI_LT; 1946 q++; 1947 break; 1948 case '=': 1949 pri_cmp |= PRI_EQ; 1950 q++; 1951 break; 1952 case '>': 1953 pri_cmp |= PRI_GT; 1954 q++; 1955 break; 1956 default: 1957 pri_done++; 1958 break; 1959 } 1960 } 1961 1962 /* collect priority name */ 1963 for (bp = buf; *q && !strchr("\t,; ", *q); ) 1964 *bp++ = *q++; 1965 *bp = '\0'; 1966 1967 /* skip cruft */ 1968 while (strchr(",;", *q)) 1969 q++; 1970 1971 /* decode priority name */ 1972 if (*buf == '*') { 1973 pri = LOG_PRIMASK; 1974 pri_cmp = PRI_LT | PRI_EQ | PRI_GT; 1975 } else { 1976 /* Ignore trailing spaces. */ 1977 for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--) 1978 buf[i] = '\0'; 1979 1980 pri = decode(buf, prioritynames); 1981 if (pri < 0) { 1982 errno = 0; 1983 (void)snprintf(ebuf, sizeof ebuf, 1984 "unknown priority name \"%s\"", buf); 1985 logerror(ebuf); 1986 return; 1987 } 1988 } 1989 if (!pri_cmp) 1990 pri_cmp = (UniquePriority) 1991 ? (PRI_EQ) 1992 : (PRI_EQ | PRI_GT) 1993 ; 1994 if (pri_invert) 1995 pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT; 1996 1997 /* scan facilities */ 1998 while (*p && !strchr("\t.; ", *p)) { 1999 for (bp = buf; *p && !strchr("\t,;. ", *p); ) 2000 *bp++ = *p++; 2001 *bp = '\0'; 2002 2003 if (*buf == '*') { 2004 for (i = 0; i < LOG_NFACILITIES; i++) { 2005 f->f_pmask[i] = pri; 2006 f->f_pcmp[i] = pri_cmp; 2007 } 2008 } else { 2009 i = decode(buf, facilitynames); 2010 if (i < 0) { 2011 errno = 0; 2012 (void)snprintf(ebuf, sizeof ebuf, 2013 "unknown facility name \"%s\"", 2014 buf); 2015 logerror(ebuf); 2016 return; 2017 } 2018 f->f_pmask[i >> 3] = pri; 2019 f->f_pcmp[i >> 3] = pri_cmp; 2020 } 2021 while (*p == ',' || *p == ' ') 2022 p++; 2023 } 2024 2025 p = q; 2026 } 2027 2028 /* skip to action part */ 2029 while (*p == '\t' || *p == ' ') 2030 p++; 2031 2032 if (*p == '-') { 2033 syncfile = 0; 2034 p++; 2035 } else 2036 syncfile = 1; 2037 2038 switch (*p) { 2039 case '@': 2040 { 2041 char *tp; 2042 char endkey = ':'; 2043 /* 2044 * scan forward to see if there is a port defined. 2045 * so we can't use strlcpy.. 2046 */ 2047 i = sizeof(f->fu_forw_hname); 2048 tp = f->fu_forw_hname; 2049 p++; 2050 2051 /* 2052 * an ipv6 address should start with a '[' in that case 2053 * we should scan for a ']' 2054 */ 2055 if (*p == '[') { 2056 p++; 2057 endkey = ']'; 2058 } 2059 while (*p && (*p != endkey) && (i-- > 0)) { 2060 *tp++ = *p++; 2061 } 2062 if (endkey == ']' && *p == endkey) 2063 p++; 2064 *tp = '\0'; 2065 } 2066 /* See if we copied a domain and have a port */ 2067 if (*p == ':') 2068 p++; 2069 else 2070 p = NULL; 2071 2072 memset(&hints, 0, sizeof(hints)); 2073 hints.ai_family = family; 2074 hints.ai_socktype = SOCK_DGRAM; 2075 error = getaddrinfo(f->fu_forw_hname, 2076 p ? p : "syslog", &hints, &res); 2077 if (error) { 2078 logerror(gai_strerror(error)); 2079 break; 2080 } 2081 f->fu_forw_addr = res; 2082 f->f_type = F_FORW; 2083 break; 2084 2085 case '/': 2086 if ((f->f_file = open(p, logflags, 0600)) < 0) { 2087 f->f_type = F_UNUSED; 2088 logerror(p); 2089 break; 2090 } 2091 if (syncfile) 2092 f->f_flags |= FFLAG_SYNC; 2093 if (isatty(f->f_file)) { 2094 if (strcmp(p, ctty) == 0) 2095 f->f_type = F_CONSOLE; 2096 else 2097 f->f_type = F_TTY; 2098 (void)strlcpy(f->fu_fname, p + sizeof(_PATH_DEV) - 1, 2099 sizeof(f->fu_fname)); 2100 } else { 2101 (void)strlcpy(f->fu_fname, p, sizeof(f->fu_fname)); 2102 f->f_type = F_FILE; 2103 } 2104 break; 2105 2106 case '|': 2107 f->fu_pipe_pid = 0; 2108 (void)strlcpy(f->fu_pipe_pname, p + 1, 2109 sizeof(f->fu_pipe_pname)); 2110 f->f_type = F_PIPE; 2111 break; 2112 2113 case '*': 2114 f->f_type = F_WALL; 2115 break; 2116 2117 default: 2118 for (i = 0; i < MAXUNAMES && *p; i++) { 2119 for (q = p; *q && *q != ','; ) 2120 q++; 2121 (void)strncpy(f->fu_uname[i], p, MAXLOGNAME - 1); 2122 if ((q - p) >= MAXLOGNAME) 2123 f->fu_uname[i][MAXLOGNAME - 1] = '\0'; 2124 else 2125 f->fu_uname[i][q - p] = '\0'; 2126 while (*q == ',' || *q == ' ') 2127 q++; 2128 p = q; 2129 } 2130 f->f_type = F_USERS; 2131 break; 2132 } 2133 } 2134 2135 2136 /* 2137 * Decode a symbolic name to a numeric value 2138 */ 2139 static int 2140 decode(const char *name, const CODE *codetab) 2141 { 2142 const CODE *c; 2143 char *p, buf[40]; 2144 2145 if (isdigit(*name)) 2146 return (atoi(name)); 2147 2148 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 2149 if (isupper(*name)) 2150 *p = tolower(*name); 2151 else 2152 *p = *name; 2153 } 2154 *p = '\0'; 2155 for (c = codetab; c->c_name; c++) 2156 if (!strcmp(buf, c->c_name)) 2157 return (c->c_val); 2158 2159 return (-1); 2160 } 2161 2162 static void 2163 markit(void) 2164 { 2165 struct filed *f; 2166 dq_t q, next; 2167 2168 now = time((time_t *)NULL); 2169 MarkSeq += TIMERINTVL; 2170 if (MarkSeq >= MarkInterval) { 2171 logmsg(LOG_INFO, "-- MARK --", 2172 LocalHostName, ADDDATE|MARK); 2173 MarkSeq = 0; 2174 } 2175 2176 STAILQ_FOREACH(f, &fhead, next) { 2177 if (f->f_prevcount && now >= REPEATTIME(f)) { 2178 dprintf("flush %s: repeated %d times, %d sec.\n", 2179 TypeNames[f->f_type], f->f_prevcount, 2180 repeatinterval[f->f_repeatcount]); 2181 fprintlog(f, 0, (char *)NULL); 2182 BACKOFF(f); 2183 } 2184 } 2185 2186 /* Walk the dead queue, and see if we should signal somebody. */ 2187 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = next) { 2188 next = TAILQ_NEXT(q, dq_entries); 2189 2190 switch (q->dq_timeout) { 2191 case 0: 2192 /* Already signalled once, try harder now. */ 2193 if (kill(q->dq_pid, SIGKILL) != 0) 2194 (void)deadq_remove(q->dq_pid); 2195 break; 2196 2197 case 1: 2198 /* 2199 * Timed out on dead queue, send terminate 2200 * signal. Note that we leave the removal 2201 * from the dead queue to reapchild(), which 2202 * will also log the event (unless the process 2203 * didn't even really exist, in case we simply 2204 * drop it from the dead queue). 2205 */ 2206 if (kill(q->dq_pid, SIGTERM) != 0) 2207 (void)deadq_remove(q->dq_pid); 2208 /* FALLTHROUGH */ 2209 2210 default: 2211 q->dq_timeout--; 2212 } 2213 } 2214 MarkSet = 0; 2215 (void)alarm(TIMERINTVL); 2216 } 2217 2218 /* 2219 * fork off and become a daemon, but wait for the child to come online 2220 * before returing to the parent, or we get disk thrashing at boot etc. 2221 * Set a timer so we don't hang forever if it wedges. 2222 */ 2223 static int 2224 waitdaemon(int nochdir, int noclose, int maxwait) 2225 { 2226 int fd; 2227 int status; 2228 pid_t pid, childpid; 2229 2230 switch (childpid = fork()) { 2231 case -1: 2232 return (-1); 2233 case 0: 2234 break; 2235 default: 2236 signal(SIGALRM, timedout); 2237 alarm(maxwait); 2238 while ((pid = wait3(&status, 0, NULL)) != -1) { 2239 if (WIFEXITED(status)) 2240 errx(1, "child pid %d exited with return code %d", 2241 pid, WEXITSTATUS(status)); 2242 if (WIFSIGNALED(status)) 2243 errx(1, "child pid %d exited on signal %d%s", 2244 pid, WTERMSIG(status), 2245 WCOREDUMP(status) ? " (core dumped)" : 2246 ""); 2247 if (pid == childpid) /* it's gone... */ 2248 break; 2249 } 2250 exit(0); 2251 } 2252 2253 if (setsid() == -1) 2254 return (-1); 2255 2256 if (!nochdir) 2257 (void)chdir("/"); 2258 2259 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 2260 (void)dup2(fd, STDIN_FILENO); 2261 (void)dup2(fd, STDOUT_FILENO); 2262 (void)dup2(fd, STDERR_FILENO); 2263 if (fd > 2) 2264 (void)close (fd); 2265 } 2266 return (getppid()); 2267 } 2268 2269 /* 2270 * We get a SIGALRM from the child when it's running and finished doing it's 2271 * fsync()'s or O_SYNC writes for all the boot messages. 2272 * 2273 * We also get a signal from the kernel if the timer expires, so check to 2274 * see what happened. 2275 */ 2276 static void 2277 timedout(int sig __unused) 2278 { 2279 int left; 2280 left = alarm(0); 2281 signal(SIGALRM, SIG_DFL); 2282 if (left == 0) 2283 errx(1, "timed out waiting for child"); 2284 else 2285 _exit(0); 2286 } 2287 2288 /* 2289 * Add `s' to the list of allowable peer addresses to accept messages 2290 * from. 2291 * 2292 * `s' is a string in the form: 2293 * 2294 * [*]domainname[:{servicename|portnumber|*}] 2295 * 2296 * or 2297 * 2298 * netaddr/maskbits[:{servicename|portnumber|*}] 2299 * 2300 * Returns -1 on error, 0 if the argument was valid. 2301 */ 2302 static int 2303 allowaddr(char *s) 2304 { 2305 char *cp1, *cp2; 2306 struct allowedpeer ap; 2307 struct servent *se; 2308 int masklen = -1; 2309 struct addrinfo hints, *res; 2310 struct in_addr *addrp, *maskp; 2311 #ifdef INET6 2312 int i; 2313 u_int32_t *addr6p, *mask6p; 2314 #endif 2315 char ip[NI_MAXHOST]; 2316 2317 #ifdef INET6 2318 if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL) 2319 #endif 2320 cp1 = s; 2321 if ((cp1 = strrchr(cp1, ':'))) { 2322 /* service/port provided */ 2323 *cp1++ = '\0'; 2324 if (strlen(cp1) == 1 && *cp1 == '*') 2325 /* any port allowed */ 2326 ap.port = 0; 2327 else if ((se = getservbyname(cp1, "udp"))) { 2328 ap.port = ntohs(se->s_port); 2329 } else { 2330 ap.port = strtol(cp1, &cp2, 0); 2331 if (*cp2 != '\0') 2332 return (-1); /* port not numeric */ 2333 } 2334 } else { 2335 if ((se = getservbyname("syslog", "udp"))) 2336 ap.port = ntohs(se->s_port); 2337 else 2338 /* sanity, should not happen */ 2339 ap.port = 514; 2340 } 2341 2342 if ((cp1 = strchr(s, '/')) != NULL && 2343 strspn(cp1 + 1, "0123456789") == strlen(cp1 + 1)) { 2344 *cp1 = '\0'; 2345 if ((masklen = atoi(cp1 + 1)) < 0) 2346 return (-1); 2347 } 2348 #ifdef INET6 2349 if (*s == '[') { 2350 cp2 = s + strlen(s) - 1; 2351 if (*cp2 == ']') { 2352 ++s; 2353 *cp2 = '\0'; 2354 } else { 2355 cp2 = NULL; 2356 } 2357 } else { 2358 cp2 = NULL; 2359 } 2360 #endif 2361 memset(&hints, 0, sizeof(hints)); 2362 hints.ai_family = PF_UNSPEC; 2363 hints.ai_socktype = SOCK_DGRAM; 2364 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 2365 if (getaddrinfo(s, NULL, &hints, &res) == 0) { 2366 ap.isnumeric = 1; 2367 memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen); 2368 memset(&ap.a_mask, 0, sizeof(ap.a_mask)); 2369 ap.a_mask.ss_family = res->ai_family; 2370 if (res->ai_family == AF_INET) { 2371 ap.a_mask.ss_len = sizeof(struct sockaddr_in); 2372 maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr; 2373 addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr; 2374 if (masklen < 0) { 2375 /* use default netmask */ 2376 if (IN_CLASSA(ntohl(addrp->s_addr))) 2377 maskp->s_addr = htonl(IN_CLASSA_NET); 2378 else if (IN_CLASSB(ntohl(addrp->s_addr))) 2379 maskp->s_addr = htonl(IN_CLASSB_NET); 2380 else 2381 maskp->s_addr = htonl(IN_CLASSC_NET); 2382 } else if (masklen <= 32) { 2383 /* convert masklen to netmask */ 2384 if (masklen == 0) 2385 maskp->s_addr = 0; 2386 else 2387 maskp->s_addr = htonl(~((1 << (32 - masklen)) - 1)); 2388 } else { 2389 freeaddrinfo(res); 2390 return (-1); 2391 } 2392 /* Lose any host bits in the network number. */ 2393 addrp->s_addr &= maskp->s_addr; 2394 } 2395 #ifdef INET6 2396 else if (res->ai_family == AF_INET6 && masklen <= 128) { 2397 ap.a_mask.ss_len = sizeof(struct sockaddr_in6); 2398 if (masklen < 0) 2399 masklen = 128; 2400 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2401 /* convert masklen to netmask */ 2402 while (masklen > 0) { 2403 if (masklen < 32) { 2404 *mask6p = htonl(~(0xffffffff >> masklen)); 2405 break; 2406 } 2407 *mask6p++ = 0xffffffff; 2408 masklen -= 32; 2409 } 2410 /* Lose any host bits in the network number. */ 2411 mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; 2412 addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr; 2413 for (i = 0; i < 4; i++) 2414 addr6p[i] &= mask6p[i]; 2415 } 2416 #endif 2417 else { 2418 freeaddrinfo(res); 2419 return (-1); 2420 } 2421 freeaddrinfo(res); 2422 } else { 2423 /* arg `s' is domain name */ 2424 ap.isnumeric = 0; 2425 ap.a_name = s; 2426 if (cp1) 2427 *cp1 = '/'; 2428 #ifdef INET6 2429 if (cp2) { 2430 *cp2 = ']'; 2431 --s; 2432 } 2433 #endif 2434 } 2435 2436 if (Debug) { 2437 printf("allowaddr: rule "); 2438 if (ap.isnumeric) { 2439 printf("numeric, "); 2440 getnameinfo((struct sockaddr *)&ap.a_addr, 2441 ((struct sockaddr *)&ap.a_addr)->sa_len, 2442 ip, sizeof ip, NULL, 0, NI_NUMERICHOST); 2443 printf("addr = %s, ", ip); 2444 getnameinfo((struct sockaddr *)&ap.a_mask, 2445 ((struct sockaddr *)&ap.a_mask)->sa_len, 2446 ip, sizeof ip, NULL, 0, NI_NUMERICHOST); 2447 printf("mask = %s; ", ip); 2448 } else { 2449 printf("domainname = %s; ", ap.a_name); 2450 } 2451 printf("port = %d\n", ap.port); 2452 } 2453 return (0); 2454 } 2455 2456 /* 2457 * Validate that the remote peer has permission to log to us. 2458 */ 2459 static int 2460 validate(struct sockaddr *sa, const char *hname) 2461 { 2462 int i; 2463 size_t l1, l2; 2464 char *cp, name[NI_MAXHOST], ip[NI_MAXHOST], port[NI_MAXSERV]; 2465 struct allowedpeer *ap; 2466 struct sockaddr_in *sin4, *a4p = NULL, *m4p = NULL; 2467 #ifdef INET6 2468 struct sockaddr_in6 *sin6, *a6p = NULL, *m6p = NULL; 2469 #endif 2470 struct addrinfo hints, *res; 2471 u_short sport; 2472 int num = 0; 2473 2474 STAILQ_FOREACH(ap, &aphead, next) { 2475 num++; 2476 } 2477 dprintf("# of validation rule: %d\n", num); 2478 if (num == 0) 2479 /* traditional behaviour, allow everything */ 2480 return (1); 2481 2482 (void)strlcpy(name, hname, sizeof(name)); 2483 memset(&hints, 0, sizeof(hints)); 2484 hints.ai_family = PF_UNSPEC; 2485 hints.ai_socktype = SOCK_DGRAM; 2486 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 2487 if (getaddrinfo(name, NULL, &hints, &res) == 0) 2488 freeaddrinfo(res); 2489 else if (strchr(name, '.') == NULL) { 2490 strlcat(name, ".", sizeof name); 2491 strlcat(name, LocalDomain, sizeof name); 2492 } 2493 if (getnameinfo(sa, sa->sa_len, ip, sizeof(ip), port, sizeof(port), 2494 NI_NUMERICHOST | NI_NUMERICSERV) != 0) 2495 return (0); /* for safety, should not occur */ 2496 dprintf("validate: dgram from IP %s, port %s, name %s;\n", 2497 ip, port, name); 2498 sport = atoi(port); 2499 2500 /* now, walk down the list */ 2501 i = 0; 2502 STAILQ_FOREACH(ap, &aphead, next) { 2503 i++; 2504 if (ap->port != 0 && ap->port != sport) { 2505 dprintf("rejected in rule %d due to port mismatch.\n", 2506 i); 2507 continue; 2508 } 2509 2510 if (ap->isnumeric) { 2511 if (ap->a_addr.ss_family != sa->sa_family) { 2512 dprintf("rejected in rule %d due to address family mismatch.\n", i); 2513 continue; 2514 } 2515 if (ap->a_addr.ss_family == AF_INET) { 2516 sin4 = satosin(sa); 2517 a4p = satosin(&ap->a_addr); 2518 m4p = satosin(&ap->a_mask); 2519 if ((sin4->sin_addr.s_addr & m4p->sin_addr.s_addr) 2520 != a4p->sin_addr.s_addr) { 2521 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2522 continue; 2523 } 2524 } 2525 #ifdef INET6 2526 else if (ap->a_addr.ss_family == AF_INET6) { 2527 sin6 = satosin6(sa); 2528 a6p = satosin6(&ap->a_addr); 2529 m6p = satosin6(&ap->a_mask); 2530 if (a6p->sin6_scope_id != 0 && 2531 sin6->sin6_scope_id != a6p->sin6_scope_id) { 2532 dprintf("rejected in rule %d due to scope mismatch.\n", i); 2533 continue; 2534 } 2535 if (IN6_ARE_MASKED_ADDR_EQUAL(&sin6->sin6_addr, 2536 &a6p->sin6_addr, &m6p->sin6_addr) != 0) { 2537 dprintf("rejected in rule %d due to IP mismatch.\n", i); 2538 continue; 2539 } 2540 } 2541 #endif 2542 else 2543 continue; 2544 } else { 2545 cp = ap->a_name; 2546 l1 = strlen(name); 2547 if (*cp == '*') { 2548 /* allow wildmatch */ 2549 cp++; 2550 l2 = strlen(cp); 2551 if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) { 2552 dprintf("rejected in rule %d due to name mismatch.\n", i); 2553 continue; 2554 } 2555 } else { 2556 /* exact match */ 2557 l2 = strlen(cp); 2558 if (l2 != l1 || memcmp(cp, name, l1) != 0) { 2559 dprintf("rejected in rule %d due to name mismatch.\n", i); 2560 continue; 2561 } 2562 } 2563 } 2564 dprintf("accepted in rule %d.\n", i); 2565 return (1); /* hooray! */ 2566 } 2567 return (0); 2568 } 2569 2570 /* 2571 * Fairly similar to popen(3), but returns an open descriptor, as 2572 * opposed to a FILE *. 2573 */ 2574 static int 2575 p_open(const char *prog, pid_t *rpid) 2576 { 2577 int pfd[2], nulldesc; 2578 pid_t pid; 2579 sigset_t omask, mask; 2580 char *argv[4]; /* sh -c cmd NULL */ 2581 char errmsg[200]; 2582 2583 if (pipe(pfd) == -1) 2584 return (-1); 2585 if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) 2586 /* we are royally screwed anyway */ 2587 return (-1); 2588 2589 sigemptyset(&mask); 2590 sigaddset(&mask, SIGALRM); 2591 sigaddset(&mask, SIGHUP); 2592 sigprocmask(SIG_BLOCK, &mask, &omask); 2593 switch ((pid = fork())) { 2594 case -1: 2595 sigprocmask(SIG_SETMASK, &omask, 0); 2596 close(nulldesc); 2597 return (-1); 2598 2599 case 0: 2600 argv[0] = strdup("sh"); 2601 argv[1] = strdup("-c"); 2602 argv[2] = strdup(prog); 2603 argv[3] = NULL; 2604 if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { 2605 logerror("strdup"); 2606 exit(1); 2607 } 2608 2609 alarm(0); 2610 (void)setsid(); /* Avoid catching SIGHUPs. */ 2611 2612 /* 2613 * Throw away pending signals, and reset signal 2614 * behaviour to standard values. 2615 */ 2616 signal(SIGALRM, SIG_IGN); 2617 signal(SIGHUP, SIG_IGN); 2618 sigprocmask(SIG_SETMASK, &omask, 0); 2619 signal(SIGPIPE, SIG_DFL); 2620 signal(SIGQUIT, SIG_DFL); 2621 signal(SIGALRM, SIG_DFL); 2622 signal(SIGHUP, SIG_DFL); 2623 2624 dup2(pfd[0], STDIN_FILENO); 2625 dup2(nulldesc, STDOUT_FILENO); 2626 dup2(nulldesc, STDERR_FILENO); 2627 closefrom(3); 2628 2629 (void)execvp(_PATH_BSHELL, argv); 2630 _exit(255); 2631 } 2632 2633 sigprocmask(SIG_SETMASK, &omask, 0); 2634 close(nulldesc); 2635 close(pfd[0]); 2636 /* 2637 * Avoid blocking on a hung pipe. With O_NONBLOCK, we are 2638 * supposed to get an EWOULDBLOCK on writev(2), which is 2639 * caught by the logic above anyway, which will in turn close 2640 * the pipe, and fork a new logging subprocess if necessary. 2641 * The stale subprocess will be killed some time later unless 2642 * it terminated itself due to closing its input pipe (so we 2643 * get rid of really dead puppies). 2644 */ 2645 if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { 2646 /* This is bad. */ 2647 (void)snprintf(errmsg, sizeof errmsg, 2648 "Warning: cannot change pipe to PID %d to " 2649 "non-blocking behaviour.", 2650 (int)pid); 2651 logerror(errmsg); 2652 } 2653 *rpid = pid; 2654 return (pfd[1]); 2655 } 2656 2657 static void 2658 deadq_enter(pid_t pid, const char *name) 2659 { 2660 dq_t p; 2661 int status; 2662 2663 /* 2664 * Be paranoid, if we can't signal the process, don't enter it 2665 * into the dead queue (perhaps it's already dead). If possible, 2666 * we try to fetch and log the child's status. 2667 */ 2668 if (kill(pid, 0) != 0) { 2669 if (waitpid(pid, &status, WNOHANG) > 0) 2670 log_deadchild(pid, status, name); 2671 return; 2672 } 2673 2674 p = malloc(sizeof(struct deadq_entry)); 2675 if (p == NULL) { 2676 logerror("malloc"); 2677 exit(1); 2678 } 2679 2680 p->dq_pid = pid; 2681 p->dq_timeout = DQ_TIMO_INIT; 2682 TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); 2683 } 2684 2685 static int 2686 deadq_remove(pid_t pid) 2687 { 2688 dq_t q; 2689 2690 TAILQ_FOREACH(q, &deadq_head, dq_entries) { 2691 if (q->dq_pid == pid) 2692 break; 2693 } 2694 if (q != NULL) { 2695 TAILQ_REMOVE(&deadq_head, q, dq_entries); 2696 free(q); 2697 return (1); 2698 } 2699 2700 return (0); 2701 } 2702 2703 static void 2704 log_deadchild(pid_t pid, int status, const char *name) 2705 { 2706 int code; 2707 char buf[256]; 2708 const char *reason; 2709 2710 errno = 0; /* Keep strerror() stuff out of logerror messages. */ 2711 if (WIFSIGNALED(status)) { 2712 reason = "due to signal"; 2713 code = WTERMSIG(status); 2714 } else { 2715 reason = "with status"; 2716 code = WEXITSTATUS(status); 2717 if (code == 0) 2718 return; 2719 } 2720 (void)snprintf(buf, sizeof buf, 2721 "Logging subprocess %d (%s) exited %s %d.", 2722 pid, name, reason, code); 2723 logerror(buf); 2724 } 2725 2726 static int 2727 socksetup(struct peer *pe) 2728 { 2729 struct addrinfo hints, *res, *res0; 2730 struct socklist *sl; 2731 int error; 2732 char *cp; 2733 /* 2734 * We have to handle this case for backwards compatibility: 2735 * If there are two (or more) colons but no '[' and ']', 2736 * assume this is an inet6 address without a service. 2737 */ 2738 if (pe->pe_name != NULL) { 2739 #ifdef INET6 2740 if (pe->pe_name[0] == '[' && 2741 (cp = strchr(pe->pe_name + 1, ']')) != NULL) { 2742 pe->pe_name = &pe->pe_name[1]; 2743 *cp = '\0'; 2744 if (cp[1] == ':' && cp[2] != '\0') 2745 pe->pe_serv = cp + 2; 2746 } else { 2747 #endif 2748 cp = strchr(pe->pe_name, ':'); 2749 if (cp != NULL && strchr(cp + 1, ':') == NULL) { 2750 *cp = '\0'; 2751 if (cp[1] != '\0') 2752 pe->pe_serv = cp + 1; 2753 if (cp == pe->pe_name) 2754 pe->pe_name = NULL; 2755 } 2756 #ifdef INET6 2757 } 2758 #endif 2759 } 2760 memset(&hints, 0, sizeof(hints)); 2761 hints.ai_family = AF_UNSPEC; 2762 hints.ai_socktype = SOCK_DGRAM; 2763 hints.ai_flags = AI_PASSIVE; 2764 2765 dprintf("Try %s\n", pe->pe_name); 2766 if (pe->pe_serv == NULL) 2767 pe->pe_serv = "syslog"; 2768 error = getaddrinfo(pe->pe_name, pe->pe_serv, &hints, &res0); 2769 if (error) { 2770 logerror(gai_strerror(error)); 2771 errno = 0; 2772 die(0); 2773 } 2774 for (res = res0; res != NULL; res = res->ai_next) { 2775 int s; 2776 int on = 1; 2777 2778 if (res->ai_family == AF_LOCAL) 2779 unlink(pe->pe_name); 2780 else if (SecureMode > 1) { 2781 /* Only AF_LOCAL in secure mode. */ 2782 continue; 2783 } 2784 if (family != AF_UNSPEC && res->ai_family != family) 2785 continue; 2786 2787 s = socket(res->ai_family, res->ai_socktype, 2788 res->ai_protocol); 2789 if (s < 0) { 2790 logerror("socket"); 2791 error++; 2792 continue; 2793 } 2794 #ifdef INET6 2795 if (res->ai_family == AF_INET6) { 2796 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, 2797 (char *)&on, sizeof(on)) < 0) { 2798 logerror("setsockopt(IPV6_V6ONLY)"); 2799 close(s); 2800 error++; 2801 continue; 2802 } 2803 } 2804 #endif 2805 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 2806 (char *)&on, sizeof(on)) < 0) { 2807 logerror("setsockopt(SO_REUSEADDR)"); 2808 close(s); 2809 error++; 2810 continue; 2811 } 2812 /* 2813 * RFC 3164 recommends that client side message 2814 * should come from the privileged syslogd port. 2815 * 2816 * If the system administrator choose not to obey 2817 * this, we can skip the bind() step so that the 2818 * system will choose a port for us. 2819 */ 2820 if (NoBind == 0) { 2821 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) { 2822 logerror("bind"); 2823 close(s); 2824 error++; 2825 continue; 2826 } 2827 if (SecureMode == 0) 2828 increase_rcvbuf(s); 2829 } 2830 if (res->ai_family == AF_LOCAL && 2831 chmod(pe->pe_name, pe->pe_mode) < 0) { 2832 dprintf("chmod %s: %s\n", pe->pe_name, 2833 strerror(errno)); 2834 close(s); 2835 error++; 2836 continue; 2837 } 2838 dprintf("new socket fd is %d\n", s); 2839 listen(s, 5); 2840 dprintf("shutdown\n"); 2841 if (SecureMode) { 2842 /* Forbid communication in secure mode. */ 2843 if (shutdown(s, SHUT_RD) < 0 && 2844 errno != ENOTCONN) { 2845 logerror("shutdown"); 2846 if (!Debug) 2847 die(0); 2848 } 2849 dprintf("listening on inet socket\n"); 2850 } else 2851 dprintf("sending on inet socket\n"); 2852 sl = calloc(1, sizeof(*sl)); 2853 if (sl == NULL) 2854 err(1, "malloc failed"); 2855 sl->sl_socket = s; 2856 memcpy(&sl->sl_ss, res->ai_addr, res->ai_addrlen); 2857 sl->sl_peer = pe; 2858 STAILQ_INSERT_TAIL(&shead, sl, next); 2859 } 2860 freeaddrinfo(res0); 2861 2862 return(error); 2863 } 2864 2865 static void 2866 increase_rcvbuf(int fd) 2867 { 2868 socklen_t len, slen; 2869 2870 slen = sizeof(len); 2871 2872 if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, &slen) == 0) { 2873 if (len < RCVBUF_MINSIZE) { 2874 len = RCVBUF_MINSIZE; 2875 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); 2876 } 2877 } 2878 } 2879