1 /* $NetBSD: syslogd.c,v 1.121 2015/02/10 20:38:15 christos 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\ 35 The Regents of the University of California. All rights reserved."); 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.121 2015/02/10 20:38:15 christos 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 * Extension to log by program name as well as facility and priority 67 * by Peter da Silva. 68 * -U and -v by Harlan Stenn. 69 * Priority comparison code by Harlan Stenn. 70 * TLS, syslog-protocol, and syslog-sign code by Martin Schuette. 71 */ 72 #define SYSLOG_NAMES 73 #include <sys/stat.h> 74 #include <poll.h> 75 #include "syslogd.h" 76 #include "extern.h" 77 78 #ifndef DISABLE_SIGN 79 #include "sign.h" 80 struct sign_global_t GlobalSign = { 81 .rsid = 0, 82 .sig2_delims = STAILQ_HEAD_INITIALIZER(GlobalSign.sig2_delims) 83 }; 84 #endif /* !DISABLE_SIGN */ 85 86 #ifndef DISABLE_TLS 87 #include "tls.h" 88 #endif /* !DISABLE_TLS */ 89 90 #ifdef LIBWRAP 91 int allow_severity = LOG_AUTH|LOG_INFO; 92 int deny_severity = LOG_AUTH|LOG_WARNING; 93 #endif 94 95 const char *ConfFile = _PATH_LOGCONF; 96 char ctty[] = _PATH_CONSOLE; 97 98 /* 99 * Queue of about-to-be-dead processes we should watch out for. 100 */ 101 TAILQ_HEAD(, deadq_entry) deadq_head = TAILQ_HEAD_INITIALIZER(deadq_head); 102 103 typedef struct deadq_entry { 104 pid_t dq_pid; 105 int dq_timeout; 106 TAILQ_ENTRY(deadq_entry) dq_entries; 107 } *dq_t; 108 109 /* 110 * The timeout to apply to processes waiting on the dead queue. Unit 111 * of measure is "mark intervals", i.e. 20 minutes by default. 112 * Processes on the dead queue will be terminated after that time. 113 */ 114 #define DQ_TIMO_INIT 2 115 116 /* 117 * Intervals at which we flush out "message repeated" messages, 118 * in seconds after previous message is logged. After each flush, 119 * we move to the next interval until we reach the largest. 120 */ 121 int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ 122 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) 123 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) 124 #define BACKOFF(f) { if ((size_t)(++(f)->f_repeatcount) > MAXREPEAT) \ 125 (f)->f_repeatcount = MAXREPEAT; \ 126 } 127 128 /* values for f_type */ 129 #define F_UNUSED 0 /* unused entry */ 130 #define F_FILE 1 /* regular file */ 131 #define F_TTY 2 /* terminal */ 132 #define F_CONSOLE 3 /* console terminal */ 133 #define F_FORW 4 /* remote machine */ 134 #define F_USERS 5 /* list of users */ 135 #define F_WALL 6 /* everyone logged on */ 136 #define F_PIPE 7 /* pipe to program */ 137 #define F_FIFO 8 /* mkfifo(2) file */ 138 #define F_TLS 9 139 140 struct TypeInfo { 141 const char *name; 142 char *queue_length_string; 143 const char *default_length_string; 144 char *queue_size_string; 145 const char *default_size_string; 146 int64_t queue_length; 147 int64_t queue_size; 148 int max_msg_length; 149 } TypeInfo[] = { 150 /* numeric values are set in init() 151 * -1 in length/size or max_msg_length means infinite */ 152 {"UNUSED", NULL, "0", NULL, "0", 0, 0, 0}, 153 {"FILE", NULL, "1024", NULL, "1M", 0, 0, 16384}, 154 {"TTY", NULL, "0", NULL, "0", 0, 0, 1024}, 155 {"CONSOLE", NULL, "0", NULL, "0", 0, 0, 1024}, 156 {"FORW", NULL, "0", NULL, "1M", 0, 0, 16384}, 157 {"USERS", NULL, "0", NULL, "0", 0, 0, 1024}, 158 {"WALL", NULL, "0", NULL, "0", 0, 0, 1024}, 159 {"PIPE", NULL, "1024", NULL, "1M", 0, 0, 16384}, 160 {"FIFO", NULL, "1024", NULL, "1M", 0, 0, 16384}, 161 #ifndef DISABLE_TLS 162 {"TLS", NULL, "-1", NULL, "16M", 0, 0, 16384} 163 #endif /* !DISABLE_TLS */ 164 }; 165 166 struct filed *Files = NULL; 167 struct filed consfile; 168 169 time_t now; 170 int Debug = D_NONE; /* debug flag */ 171 int daemonized = 0; /* we are not daemonized yet */ 172 char *LocalFQDN = NULL; /* our FQDN */ 173 char *oldLocalFQDN = NULL; /* our previous FQDN */ 174 char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ 175 struct socketEvent *finet; /* Internet datagram sockets and events */ 176 int *funix; /* Unix domain datagram sockets */ 177 #ifndef DISABLE_TLS 178 struct socketEvent *TLS_Listen_Set; /* TLS/TCP sockets and events */ 179 #endif /* !DISABLE_TLS */ 180 int Initialized = 0; /* set when we have initialized ourselves */ 181 int ShuttingDown; /* set when we die() */ 182 int MarkInterval = 20 * 60; /* interval between marks in seconds */ 183 int MarkSeq = 0; /* mark sequence number */ 184 int SecureMode = 0; /* listen only on unix domain socks */ 185 int UseNameService = 1; /* make domain name queries */ 186 int NumForwards = 0; /* number of forwarding actions in conf file */ 187 char **LogPaths; /* array of pathnames to read messages from */ 188 int NoRepeat = 0; /* disable "repeated"; log always */ 189 int RemoteAddDate = 0; /* always add date to messages from network */ 190 int SyncKernel = 0; /* write kernel messages synchronously */ 191 int UniquePriority = 0; /* only log specified priority */ 192 int LogFacPri = 0; /* put facility and priority in log messages: */ 193 /* 0=no, 1=numeric, 2=names */ 194 bool BSDOutputFormat = true; /* if true emit traditional BSD Syslog lines, 195 * otherwise new syslog-protocol lines 196 * 197 * Open Issue: having a global flag is the 198 * easiest solution. If we get a more detailed 199 * config file this could/should be changed 200 * into a destination-specific flag. 201 * Most output code should be ready to handle 202 * this, it will only break some syslog-sign 203 * configurations (e.g. with SG="0"). 204 */ 205 char appname[] = "syslogd";/* the APPNAME for own messages */ 206 char *include_pid = NULL; /* include PID in own messages */ 207 208 209 /* init and setup */ 210 void usage(void) __attribute__((__noreturn__)); 211 void logpath_add(char ***, int *, int *, const char *); 212 void logpath_fileadd(char ***, int *, int *, const char *); 213 void init(int fd, short event, void *ev); /* SIGHUP kevent dispatch routine */ 214 struct socketEvent* 215 socksetup(int, const char *); 216 int getmsgbufsize(void); 217 char *getLocalFQDN(void); 218 void trim_anydomain(char *); 219 /* pipe & subprocess handling */ 220 int p_open(char *, pid_t *); 221 void deadq_enter(pid_t, const char *); 222 int deadq_remove(pid_t); 223 void log_deadchild(pid_t, int, const char *); 224 void reapchild(int fd, short event, void *ev); /* SIGCHLD kevent dispatch routine */ 225 /* input message parsing & formatting */ 226 const char *cvthname(struct sockaddr_storage *); 227 void printsys(char *); 228 struct buf_msg *printline_syslogprotocol(const char*, char*, int, int); 229 struct buf_msg *printline_bsdsyslog(const char*, char*, int, int); 230 struct buf_msg *printline_kernelprintf(const char*, char*, int, int); 231 size_t check_timestamp(unsigned char *, char **, bool, bool); 232 char *copy_utf8_ascii(char*, size_t); 233 uint_fast32_t get_utf8_value(const char*); 234 unsigned valid_utf8(const char *); 235 static unsigned check_sd(char*); 236 static unsigned check_msgid(char *); 237 /* event handling */ 238 static void dispatch_read_klog(int fd, short event, void *ev); 239 static void dispatch_read_finet(int fd, short event, void *ev); 240 static void dispatch_read_funix(int fd, short event, void *ev); 241 static void domark(int fd, short event, void *ev); /* timer kevent dispatch routine */ 242 /* log messages */ 243 void logmsg_async(int, const char *, const char *, int); 244 void logmsg(struct buf_msg *); 245 int matches_spec(const char *, const char *, 246 char *(*)(const char *, const char *)); 247 void udp_send(struct filed *, char *, size_t); 248 void wallmsg(struct filed *, struct iovec *, size_t); 249 /* buffer & queue functions */ 250 size_t message_queue_purge(struct filed *f, size_t, int); 251 size_t message_allqueues_check(void); 252 static struct buf_queue * 253 find_qentry_to_delete(const struct buf_queue_head *, int, bool); 254 struct buf_queue * 255 message_queue_add(struct filed *, struct buf_msg *); 256 size_t buf_queue_obj_size(struct buf_queue*); 257 /* configuration & parsing */ 258 void cfline(size_t, const char *, struct filed *, const char *, 259 const char *); 260 void read_config_file(FILE*, struct filed**); 261 void store_sign_delim_sg2(char*); 262 int decode(const char *, CODE *); 263 bool copy_config_value(const char *, char **, const char **, 264 const char *, int); 265 bool copy_config_value_word(char **, const char **); 266 267 /* config parsing */ 268 #ifndef DISABLE_TLS 269 void free_cred_SLIST(struct peer_cred_head *); 270 static inline void 271 free_incoming_tls_sockets(void); 272 #endif /* !DISABLE_TLS */ 273 static int writev1(int, struct iovec *, size_t); 274 275 /* for make_timestamp() */ 276 char timestamp[MAX_TIMESTAMPLEN + 1]; 277 /* 278 * Global line buffer. Since we only process one event at a time, 279 * a global one will do. But for klog, we use own buffer so that 280 * partial line at the end of buffer can be deferred. 281 */ 282 char *linebuf, *klog_linebuf; 283 size_t linebufsize, klog_linebufoff; 284 285 static const char *bindhostname = NULL; 286 287 #ifndef DISABLE_TLS 288 struct TLS_Incoming TLS_Incoming_Head = \ 289 SLIST_HEAD_INITIALIZER(TLS_Incoming_Head); 290 extern char *SSL_ERRCODE[]; 291 struct tls_global_options_t tls_opt; 292 #endif /* !DISABLE_TLS */ 293 294 int 295 main(int argc, char *argv[]) 296 { 297 int ch, j, fklog; 298 int funixsize = 0, funixmaxsize = 0; 299 struct sockaddr_un sunx; 300 char **pp; 301 struct event *ev; 302 uid_t uid = 0; 303 gid_t gid = 0; 304 char *user = NULL; 305 char *group = NULL; 306 const char *root = "/"; 307 char *endp; 308 struct group *gr; 309 struct passwd *pw; 310 unsigned long l; 311 312 /* should we set LC_TIME="C" to ensure correct timestamps&parsing? */ 313 (void)setlocale(LC_ALL, ""); 314 315 while ((ch = getopt(argc, argv, "b:dnsSf:m:o:p:P:ru:g:t:TUv")) != -1) 316 switch(ch) { 317 case 'b': 318 bindhostname = optarg; 319 break; 320 case 'd': /* debug */ 321 Debug = D_DEFAULT; 322 /* is there a way to read the integer value 323 * for Debug as an optional argument? */ 324 break; 325 case 'f': /* configuration file */ 326 ConfFile = optarg; 327 break; 328 case 'g': 329 group = optarg; 330 if (*group == '\0') 331 usage(); 332 break; 333 case 'm': /* mark interval */ 334 MarkInterval = atoi(optarg) * 60; 335 break; 336 case 'n': /* turn off DNS queries */ 337 UseNameService = 0; 338 break; 339 case 'o': /* message format */ 340 #define EQ(a) (strncmp(optarg, # a, sizeof(# a) - 1) == 0) 341 if (EQ(bsd) || EQ(rfc3264)) 342 BSDOutputFormat = true; 343 else if (EQ(syslog) || EQ(rfc5424)) 344 BSDOutputFormat = false; 345 else 346 usage(); 347 /* TODO: implement additional output option "osyslog" 348 * for old syslogd behaviour as introduced after 349 * FreeBSD PR#bin/7055. 350 */ 351 break; 352 case 'p': /* path */ 353 logpath_add(&LogPaths, &funixsize, 354 &funixmaxsize, optarg); 355 break; 356 case 'P': /* file of paths */ 357 logpath_fileadd(&LogPaths, &funixsize, 358 &funixmaxsize, optarg); 359 break; 360 case 'r': /* disable "repeated" compression */ 361 NoRepeat++; 362 break; 363 case 's': /* no network listen mode */ 364 SecureMode++; 365 break; 366 case 'S': 367 SyncKernel = 1; 368 break; 369 case 't': 370 root = optarg; 371 if (*root == '\0') 372 usage(); 373 break; 374 case 'T': 375 RemoteAddDate = 1; 376 break; 377 case 'u': 378 user = optarg; 379 if (*user == '\0') 380 usage(); 381 break; 382 case 'U': /* only log specified priority */ 383 UniquePriority = 1; 384 break; 385 case 'v': /* log facility and priority */ 386 if (LogFacPri < 2) 387 LogFacPri++; 388 break; 389 default: 390 usage(); 391 } 392 if ((argc -= optind) != 0) 393 usage(); 394 395 setlinebuf(stdout); 396 tzset(); /* init TZ information for localtime. */ 397 398 if (user != NULL) { 399 if (isdigit((unsigned char)*user)) { 400 errno = 0; 401 endp = NULL; 402 l = strtoul(user, &endp, 0); 403 if (errno || *endp != '\0') 404 goto getuser; 405 uid = (uid_t)l; 406 if (uid != l) {/* TODO: never executed */ 407 errno = 0; 408 logerror("UID out of range"); 409 die(0, 0, NULL); 410 } 411 } else { 412 getuser: 413 if ((pw = getpwnam(user)) != NULL) { 414 uid = pw->pw_uid; 415 } else { 416 errno = 0; 417 logerror("Cannot find user `%s'", user); 418 die(0, 0, NULL); 419 } 420 } 421 } 422 423 if (group != NULL) { 424 if (isdigit((unsigned char)*group)) { 425 errno = 0; 426 endp = NULL; 427 l = strtoul(group, &endp, 0); 428 if (errno || *endp != '\0') 429 goto getgroup; 430 gid = (gid_t)l; 431 if (gid != l) {/* TODO: never executed */ 432 errno = 0; 433 logerror("GID out of range"); 434 die(0, 0, NULL); 435 } 436 } else { 437 getgroup: 438 if ((gr = getgrnam(group)) != NULL) { 439 gid = gr->gr_gid; 440 } else { 441 errno = 0; 442 logerror("Cannot find group `%s'", group); 443 die(0, 0, NULL); 444 } 445 } 446 } 447 448 if (access(root, F_OK | R_OK)) { 449 logerror("Cannot access `%s'", root); 450 die(0, 0, NULL); 451 } 452 453 consfile.f_type = F_CONSOLE; 454 (void)strlcpy(consfile.f_un.f_fname, ctty, 455 sizeof(consfile.f_un.f_fname)); 456 linebufsize = getmsgbufsize(); 457 if (linebufsize < MAXLINE) 458 linebufsize = MAXLINE; 459 linebufsize++; 460 461 if (!(linebuf = malloc(linebufsize))) { 462 logerror("Couldn't allocate buffer"); 463 die(0, 0, NULL); 464 } 465 if (!(klog_linebuf = malloc(linebufsize))) { 466 logerror("Couldn't allocate buffer for klog"); 467 die(0, 0, NULL); 468 } 469 470 471 #ifndef SUN_LEN 472 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 473 #endif 474 if (funixsize == 0) 475 logpath_add(&LogPaths, &funixsize, 476 &funixmaxsize, _PATH_LOG); 477 funix = malloc(sizeof(*funix) * funixsize); 478 if (funix == NULL) { 479 logerror("Couldn't allocate funix descriptors"); 480 die(0, 0, NULL); 481 } 482 for (j = 0, pp = LogPaths; *pp; pp++, j++) { 483 DPRINTF(D_NET, "Making unix dgram socket `%s'\n", *pp); 484 unlink(*pp); 485 memset(&sunx, 0, sizeof(sunx)); 486 sunx.sun_family = AF_LOCAL; 487 (void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path)); 488 funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0); 489 if (funix[j] < 0 || bind(funix[j], 490 (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || 491 chmod(*pp, 0666) < 0) { 492 logerror("Cannot create `%s'", *pp); 493 die(0, 0, NULL); 494 } 495 DPRINTF(D_NET, "Listening on unix dgram socket `%s'\n", *pp); 496 } 497 498 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) { 499 DPRINTF(D_FILE, "Can't open `%s' (%d)\n", _PATH_KLOG, errno); 500 } else { 501 DPRINTF(D_FILE, "Listening on kernel log `%s' with fd %d\n", 502 _PATH_KLOG, fklog); 503 } 504 505 #if (!defined(DISABLE_TLS) && !defined(DISABLE_SIGN)) 506 /* basic OpenSSL init */ 507 SSL_load_error_strings(); 508 (void) SSL_library_init(); 509 OpenSSL_add_all_digests(); 510 /* OpenSSL PRNG needs /dev/urandom, thus initialize before chroot() */ 511 if (!RAND_status()) { 512 errno = 0; 513 logerror("Unable to initialize OpenSSL PRNG"); 514 } else { 515 DPRINTF(D_TLS, "Initializing PRNG\n"); 516 } 517 #endif /* (!defined(DISABLE_TLS) && !defined(DISABLE_SIGN)) */ 518 #ifndef DISABLE_SIGN 519 /* initialize rsid -- we will use that later to determine 520 * whether sign_global_init() was already called */ 521 GlobalSign.rsid = 0; 522 #endif /* !DISABLE_SIGN */ 523 #if (IETF_NUM_PRIVALUES != (LOG_NFACILITIES<<3)) 524 logerror("Warning: system defines %d priority values, but " 525 "syslog-protocol/syslog-sign specify %d values", 526 LOG_NFACILITIES, SIGN_NUM_PRIVALS); 527 #endif 528 529 /* 530 * All files are open, we can drop privileges and chroot 531 */ 532 DPRINTF(D_MISC, "Attempt to chroot to `%s'\n", root); 533 if (chroot(root) == -1) { 534 logerror("Failed to chroot to `%s'", root); 535 die(0, 0, NULL); 536 } 537 DPRINTF(D_MISC, "Attempt to set GID/EGID to `%d'\n", gid); 538 if (setgid(gid) || setegid(gid)) { 539 logerror("Failed to set gid to `%d'", gid); 540 die(0, 0, NULL); 541 } 542 DPRINTF(D_MISC, "Attempt to set UID/EUID to `%d'\n", uid); 543 if (setuid(uid) || seteuid(uid)) { 544 logerror("Failed to set uid to `%d'", uid); 545 die(0, 0, NULL); 546 } 547 /* 548 * We cannot detach from the terminal before we are sure we won't 549 * have a fatal error, because error message would not go to the 550 * terminal and would not be logged because syslogd dies. 551 * All die() calls are behind us, we can call daemon() 552 */ 553 if (!Debug) { 554 (void)daemon(0, 0); 555 daemonized = 1; 556 /* tuck my process id away, if i'm not in debug mode */ 557 #ifdef __NetBSD_Version__ 558 pidfile(NULL); 559 #endif /* __NetBSD_Version__ */ 560 } 561 562 #define MAX_PID_LEN 5 563 include_pid = malloc(MAX_PID_LEN+1); 564 snprintf(include_pid, MAX_PID_LEN+1, "%d", getpid()); 565 566 /* 567 * Create the global kernel event descriptor. 568 * 569 * NOTE: We MUST do this after daemon(), bacause the kqueue() 570 * API dictates that kqueue descriptors are not inherited 571 * across forks (lame!). 572 */ 573 (void)event_init(); 574 575 /* 576 * We must read the configuration file for the first time 577 * after the kqueue descriptor is created, because we install 578 * events during this process. 579 */ 580 init(0, 0, NULL); 581 582 /* 583 * Always exit on SIGTERM. Also exit on SIGINT and SIGQUIT 584 * if we're debugging. 585 */ 586 (void)signal(SIGTERM, SIG_IGN); 587 (void)signal(SIGINT, SIG_IGN); 588 (void)signal(SIGQUIT, SIG_IGN); 589 590 ev = allocev(); 591 signal_set(ev, SIGTERM, die, ev); 592 EVENT_ADD(ev); 593 594 if (Debug) { 595 ev = allocev(); 596 signal_set(ev, SIGINT, die, ev); 597 EVENT_ADD(ev); 598 ev = allocev(); 599 signal_set(ev, SIGQUIT, die, ev); 600 EVENT_ADD(ev); 601 } 602 603 ev = allocev(); 604 signal_set(ev, SIGCHLD, reapchild, ev); 605 EVENT_ADD(ev); 606 607 ev = allocev(); 608 schedule_event(&ev, 609 &((struct timeval){TIMERINTVL, 0}), 610 domark, ev); 611 612 (void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ 613 614 /* Re-read configuration on SIGHUP. */ 615 (void) signal(SIGHUP, SIG_IGN); 616 ev = allocev(); 617 signal_set(ev, SIGHUP, init, ev); 618 EVENT_ADD(ev); 619 620 #ifndef DISABLE_TLS 621 ev = allocev(); 622 signal_set(ev, SIGUSR1, dispatch_force_tls_reconnect, ev); 623 EVENT_ADD(ev); 624 #endif /* !DISABLE_TLS */ 625 626 if (fklog >= 0) { 627 ev = allocev(); 628 DPRINTF(D_EVENT, 629 "register klog for fd %d with ev@%p\n", fklog, ev); 630 event_set(ev, fklog, EV_READ | EV_PERSIST, 631 dispatch_read_klog, ev); 632 EVENT_ADD(ev); 633 } 634 for (j = 0, pp = LogPaths; *pp; pp++, j++) { 635 ev = allocev(); 636 event_set(ev, funix[j], EV_READ | EV_PERSIST, 637 dispatch_read_funix, ev); 638 EVENT_ADD(ev); 639 } 640 641 DPRINTF(D_MISC, "Off & running....\n"); 642 643 j = event_dispatch(); 644 /* normal termination via die(), reaching this is an error */ 645 DPRINTF(D_MISC, "event_dispatch() returned %d\n", j); 646 die(0, 0, NULL); 647 /*NOTREACHED*/ 648 return 0; 649 } 650 651 void 652 usage(void) 653 { 654 655 (void)fprintf(stderr, 656 "usage: %s [-dnrSsTUv] [-b bind_address] [-f config_file] [-g group]\n" 657 "\t[-m mark_interval] [-P file_list] [-p log_socket\n" 658 "\t[-p log_socket2 ...]] [-t chroot_dir] [-u user]\n", 659 getprogname()); 660 exit(1); 661 } 662 663 /* 664 * Dispatch routine for reading /dev/klog 665 * 666 * Note: slightly different semantic in dispatch_read functions: 667 * - read_klog() might give multiple messages in linebuf and 668 * leaves the task of splitting them to printsys() 669 * - all other read functions receive one message and 670 * then call printline() with one buffer. 671 */ 672 static void 673 dispatch_read_klog(int fd, short event, void *ev) 674 { 675 ssize_t rv; 676 size_t resid = linebufsize - klog_linebufoff; 677 678 DPRINTF((D_CALL|D_EVENT), "Kernel log active (%d, %d, %p)" 679 " with linebuf@%p, length %zu)\n", fd, event, ev, 680 klog_linebuf, linebufsize); 681 682 rv = read(fd, &klog_linebuf[klog_linebufoff], resid - 1); 683 if (rv > 0) { 684 klog_linebuf[klog_linebufoff + rv] = '\0'; 685 printsys(klog_linebuf); 686 } else if (rv < 0 && errno != EINTR) { 687 /* 688 * /dev/klog has croaked. Disable the event 689 * so it won't bother us again. 690 */ 691 logerror("klog failed"); 692 event_del(ev); 693 } 694 } 695 696 /* 697 * Dispatch routine for reading Unix domain sockets. 698 */ 699 static void 700 dispatch_read_funix(int fd, short event, void *ev) 701 { 702 struct sockaddr_un myname, fromunix; 703 ssize_t rv; 704 socklen_t sunlen; 705 706 sunlen = sizeof(myname); 707 if (getsockname(fd, (struct sockaddr *)&myname, &sunlen) != 0) { 708 /* 709 * This should never happen, so ensure that it doesn't 710 * happen again. 711 */ 712 logerror("getsockname() unix failed"); 713 event_del(ev); 714 return; 715 } 716 717 DPRINTF((D_CALL|D_EVENT|D_NET), "Unix socket (%.*s) active (%d, %d %p)" 718 " with linebuf@%p, size %zu)\n", (int)(myname.sun_len 719 - sizeof(myname.sun_len) - sizeof(myname.sun_family)), 720 myname.sun_path, fd, event, ev, linebuf, linebufsize-1); 721 722 sunlen = sizeof(fromunix); 723 rv = recvfrom(fd, linebuf, linebufsize-1, 0, 724 (struct sockaddr *)&fromunix, &sunlen); 725 if (rv > 0) { 726 linebuf[rv] = '\0'; 727 printline(LocalFQDN, linebuf, 0); 728 } else if (rv < 0 && errno != EINTR) { 729 logerror("recvfrom() unix `%.*s'", 730 myname.sun_len, myname.sun_path); 731 } 732 } 733 734 /* 735 * Dispatch routine for reading Internet sockets. 736 */ 737 static void 738 dispatch_read_finet(int fd, short event, void *ev) 739 { 740 #ifdef LIBWRAP 741 struct request_info req; 742 #endif 743 struct sockaddr_storage frominet; 744 ssize_t rv; 745 socklen_t len; 746 int reject = 0; 747 748 DPRINTF((D_CALL|D_EVENT|D_NET), "inet socket active (%d, %d %p) " 749 " with linebuf@%p, size %zu)\n", 750 fd, event, ev, linebuf, linebufsize-1); 751 752 #ifdef LIBWRAP 753 request_init(&req, RQ_DAEMON, appname, RQ_FILE, fd, NULL); 754 fromhost(&req); 755 reject = !hosts_access(&req); 756 if (reject) 757 DPRINTF(D_NET, "access denied\n"); 758 #endif 759 760 len = sizeof(frominet); 761 rv = recvfrom(fd, linebuf, linebufsize-1, 0, 762 (struct sockaddr *)&frominet, &len); 763 if (rv == 0 || (rv < 0 && errno == EINTR)) 764 return; 765 else if (rv < 0) { 766 logerror("recvfrom inet"); 767 return; 768 } 769 770 linebuf[rv] = '\0'; 771 if (!reject) 772 printline(cvthname(&frominet), linebuf, 773 RemoteAddDate ? ADDDATE : 0); 774 } 775 776 /* 777 * given a pointer to an array of char *'s, a pointer to its current 778 * size and current allocated max size, and a new char * to add, add 779 * it, update everything as necessary, possibly allocating a new array 780 */ 781 void 782 logpath_add(char ***lp, int *szp, int *maxszp, const char *new) 783 { 784 char **nlp; 785 int newmaxsz; 786 787 DPRINTF(D_FILE, "Adding `%s' to the %p logpath list\n", new, *lp); 788 if (*szp == *maxszp) { 789 if (*maxszp == 0) { 790 newmaxsz = 4; /* start of with enough for now */ 791 *lp = NULL; 792 } else 793 newmaxsz = *maxszp * 2; 794 nlp = realloc(*lp, sizeof(char *) * (newmaxsz + 1)); 795 if (nlp == NULL) { 796 logerror("Couldn't allocate line buffer"); 797 die(0, 0, NULL); 798 } 799 *lp = nlp; 800 *maxszp = newmaxsz; 801 } 802 if (((*lp)[(*szp)++] = strdup(new)) == NULL) { 803 logerror("Couldn't allocate logpath"); 804 die(0, 0, NULL); 805 } 806 (*lp)[(*szp)] = NULL; /* always keep it NULL terminated */ 807 } 808 809 /* do a file of log sockets */ 810 void 811 logpath_fileadd(char ***lp, int *szp, int *maxszp, const char *file) 812 { 813 FILE *fp; 814 char *line; 815 size_t len; 816 817 fp = fopen(file, "r"); 818 if (fp == NULL) { 819 logerror("Could not open socket file list `%s'", file); 820 die(0, 0, NULL); 821 } 822 823 while ((line = fgetln(fp, &len)) != NULL) { 824 line[len - 1] = 0; 825 logpath_add(lp, szp, maxszp, line); 826 } 827 fclose(fp); 828 } 829 830 /* 831 * checks UTF-8 codepoint 832 * returns either its length in bytes or 0 if *input is invalid 833 */ 834 unsigned 835 valid_utf8(const char *c) { 836 unsigned rc, nb; 837 838 /* first byte gives sequence length */ 839 if ((*c & 0x80) == 0x00) return 1; /* 0bbbbbbb -- ASCII */ 840 else if ((*c & 0xc0) == 0x80) return 0; /* 10bbbbbb -- trailing byte */ 841 else if ((*c & 0xe0) == 0xc0) nb = 2; /* 110bbbbb */ 842 else if ((*c & 0xf0) == 0xe0) nb = 3; /* 1110bbbb */ 843 else if ((*c & 0xf8) == 0xf0) nb = 4; /* 11110bbb */ 844 else return 0; /* UTF-8 allows only up to 4 bytes */ 845 846 /* catch overlong encodings */ 847 if ((*c & 0xfe) == 0xc0) 848 return 0; /* 1100000b ... */ 849 else if (((*c & 0xff) == 0xe0) && ((*(c+1) & 0xe0) == 0x80)) 850 return 0; /* 11100000 100bbbbb ... */ 851 else if (((*c & 0xff) == 0xf0) && ((*(c+1) & 0xf0) == 0x80)) 852 return 0; /* 11110000 1000bbbb ... ... */ 853 854 /* and also filter UTF-16 surrogates (=invalid in UTF-8) */ 855 if (((*c & 0xff) == 0xed) && ((*(c+1) & 0xe0) == 0xa0)) 856 return 0; /* 11101101 101bbbbb ... */ 857 858 rc = nb; 859 /* check trailing bytes */ 860 switch (nb) { 861 default: return 0; 862 case 4: if ((*(c+3) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/ 863 case 3: if ((*(c+2) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/ 864 case 2: if ((*(c+1) & 0xc0) != 0x80) return 0; /*FALLTHROUGH*/ 865 } 866 return rc; 867 } 868 #define UTF8CHARMAX 4 869 870 /* 871 * read UTF-8 value 872 * returns a the codepoint number 873 */ 874 uint_fast32_t 875 get_utf8_value(const char *c) { 876 uint_fast32_t sum; 877 unsigned nb, i; 878 879 /* first byte gives sequence length */ 880 if ((*c & 0x80) == 0x00) return *c;/* 0bbbbbbb -- ASCII */ 881 else if ((*c & 0xc0) == 0x80) return 0; /* 10bbbbbb -- trailing byte */ 882 else if ((*c & 0xe0) == 0xc0) { /* 110bbbbb */ 883 nb = 2; 884 sum = (*c & ~0xe0) & 0xff; 885 } else if ((*c & 0xf0) == 0xe0) { /* 1110bbbb */ 886 nb = 3; 887 sum = (*c & ~0xf0) & 0xff; 888 } else if ((*c & 0xf8) == 0xf0) { /* 11110bbb */ 889 nb = 4; 890 sum = (*c & ~0xf8) & 0xff; 891 } else return 0; /* UTF-8 allows only up to 4 bytes */ 892 893 /* check trailing bytes -- 10bbbbbb */ 894 i = 1; 895 while (i < nb) { 896 sum <<= 6; 897 sum |= ((*(c+i) & ~0xc0) & 0xff); 898 i++; 899 } 900 return sum; 901 } 902 903 /* note previous versions transscribe 904 * control characters, e.g. \007 --> "^G" 905 * did anyone rely on that? 906 * 907 * this new version works on only one buffer and 908 * replaces control characters with a space 909 */ 910 #define NEXTFIELD(ptr) if (*(p) == ' ') (p)++; /* SP */ \ 911 else { \ 912 DPRINTF(D_DATA, "format error\n"); \ 913 if (*(p) == '\0') start = (p); \ 914 goto all_syslog_msg; \ 915 } 916 #define FORCE2ASCII(c) ((iscntrl((unsigned char)(c)) && (c) != '\t') \ 917 ? ((c) == '\n' ? ' ' : '?') \ 918 : (c) & 0177) 919 920 /* following syslog-protocol */ 921 #define printusascii(ch) (ch >= 33 && ch <= 126) 922 #define sdname(ch) (ch != '=' && ch != ' ' \ 923 && ch != ']' && ch != '"' \ 924 && printusascii(ch)) 925 926 /* checks whether the first word of string p can be interpreted as 927 * a syslog-protocol MSGID and if so returns its length. 928 * 929 * otherwise returns 0 930 */ 931 static unsigned 932 check_msgid(char *p) 933 { 934 char *q = p; 935 936 /* consider the NILVALUE to be valid */ 937 if (*q == '-' && *(q+1) == ' ') 938 return 1; 939 940 for (;;) { 941 if (*q == ' ') 942 return q - p; 943 else if (*q == '\0' || !printusascii(*q) || q - p >= MSGID_MAX) 944 return 0; 945 else 946 q++; 947 } 948 } 949 950 /* 951 * returns number of chars found in SD at beginning of string p 952 * thus returns 0 if no valid SD is found 953 * 954 * if ascii == true then substitute all non-ASCII chars 955 * otherwise use syslog-protocol rules to allow UTF-8 in values 956 * note: one pass for filtering and scanning, so a found SD 957 * is always filtered, but an invalid one could be partially 958 * filtered up to the format error. 959 */ 960 static unsigned 961 check_sd(char* p) 962 { 963 char *q = p; 964 bool esc = false; 965 966 /* consider the NILVALUE to be valid */ 967 if (*q == '-' && (*(q+1) == ' ' || *(q+1) == '\0')) 968 return 1; 969 970 for(;;) { /* SD-ELEMENT */ 971 if (*q++ != '[') return 0; 972 /* SD-ID */ 973 if (!sdname(*q)) return 0; 974 while (sdname(*q)) { 975 *q = FORCE2ASCII(*q); 976 q++; 977 } 978 for(;;) { /* SD-PARAM */ 979 if (*q == ']') { 980 q++; 981 if (*q == ' ' || *q == '\0') return q - p; 982 else if (*q == '[') break; 983 } else if (*q++ != ' ') return 0; 984 985 /* PARAM-NAME */ 986 if (!sdname(*q)) return 0; 987 while (sdname(*q)) { 988 *q = FORCE2ASCII(*q); 989 q++; 990 } 991 992 if (*q++ != '=') return 0; 993 if (*q++ != '"') return 0; 994 995 for(;;) { /* PARAM-VALUE */ 996 if (esc) { 997 esc = false; 998 if (*q == '\\' || *q == '"' || 999 *q == ']') { 1000 q++; 1001 continue; 1002 } 1003 /* no else because invalid 1004 * escape sequences are accepted */ 1005 } 1006 else if (*q == '"') break; 1007 else if (*q == '\0' || *q == ']') return 0; 1008 else if (*q == '\\') esc = true; 1009 else { 1010 int i; 1011 i = valid_utf8(q); 1012 if (i == 0) 1013 *q = '?'; 1014 else if (i == 1) 1015 *q = FORCE2ASCII(*q); 1016 else /* multi byte char */ 1017 q += (i-1); 1018 } 1019 q++; 1020 } 1021 q++; 1022 } 1023 } 1024 } 1025 1026 struct buf_msg * 1027 printline_syslogprotocol(const char *hname, char *msg, 1028 int flags, int pri) 1029 { 1030 struct buf_msg *buffer; 1031 char *p, *start; 1032 unsigned sdlen = 0, i = 0; 1033 bool utf8allowed = false; /* for some fields */ 1034 1035 DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_syslogprotocol(" 1036 "\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri); 1037 1038 buffer = buf_msg_new(0); 1039 p = msg; 1040 p += check_timestamp((unsigned char*) p, 1041 &buffer->timestamp, true, !BSDOutputFormat); 1042 DPRINTF(D_DATA, "Got timestamp \"%s\"\n", buffer->timestamp); 1043 1044 if (flags & ADDDATE) { 1045 FREEPTR(buffer->timestamp); 1046 buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0); 1047 } 1048 1049 start = p; 1050 NEXTFIELD(p); 1051 /* extract host */ 1052 for (start = p;; p++) { 1053 if ((*p == ' ' || *p == '\0') 1054 && start == p-1 && *(p-1) == '-') { 1055 /* NILVALUE */ 1056 break; 1057 } else if ((*p == ' ' || *p == '\0') 1058 && (start != p-1 || *(p-1) != '-')) { 1059 buffer->host = strndup(start, p - start); 1060 break; 1061 } else { 1062 *p = FORCE2ASCII(*p); 1063 } 1064 } 1065 /* p @ SP after host */ 1066 DPRINTF(D_DATA, "Got host \"%s\"\n", buffer->host); 1067 1068 /* extract app-name */ 1069 NEXTFIELD(p); 1070 for (start = p;; p++) { 1071 if ((*p == ' ' || *p == '\0') 1072 && start == p-1 && *(p-1) == '-') { 1073 /* NILVALUE */ 1074 break; 1075 } else if ((*p == ' ' || *p == '\0') 1076 && (start != p-1 || *(p-1) != '-')) { 1077 buffer->prog = strndup(start, p - start); 1078 break; 1079 } else { 1080 *p = FORCE2ASCII(*p); 1081 } 1082 } 1083 DPRINTF(D_DATA, "Got prog \"%s\"\n", buffer->prog); 1084 1085 /* extract procid */ 1086 NEXTFIELD(p); 1087 for (start = p;; p++) { 1088 if ((*p == ' ' || *p == '\0') 1089 && start == p-1 && *(p-1) == '-') { 1090 /* NILVALUE */ 1091 break; 1092 } else if ((*p == ' ' || *p == '\0') 1093 && (start != p-1 || *(p-1) != '-')) { 1094 buffer->pid = strndup(start, p - start); 1095 start = p; 1096 break; 1097 } else { 1098 *p = FORCE2ASCII(*p); 1099 } 1100 } 1101 DPRINTF(D_DATA, "Got pid \"%s\"\n", buffer->pid); 1102 1103 /* extract msgid */ 1104 NEXTFIELD(p); 1105 for (start = p;; p++) { 1106 if ((*p == ' ' || *p == '\0') 1107 && start == p-1 && *(p-1) == '-') { 1108 /* NILVALUE */ 1109 start = p+1; 1110 break; 1111 } else if ((*p == ' ' || *p == '\0') 1112 && (start != p-1 || *(p-1) != '-')) { 1113 buffer->msgid = strndup(start, p - start); 1114 start = p+1; 1115 break; 1116 } else { 1117 *p = FORCE2ASCII(*p); 1118 } 1119 } 1120 DPRINTF(D_DATA, "Got msgid \"%s\"\n", buffer->msgid); 1121 1122 /* extract SD */ 1123 NEXTFIELD(p); 1124 start = p; 1125 sdlen = check_sd(p); 1126 DPRINTF(D_DATA, "check_sd(\"%s\") returned %d\n", p, sdlen); 1127 1128 if (sdlen == 1 && *p == '-') { 1129 /* NILVALUE */ 1130 p++; 1131 } else if (sdlen > 1) { 1132 buffer->sd = strndup(p, sdlen); 1133 p += sdlen; 1134 } else { 1135 DPRINTF(D_DATA, "format error\n"); 1136 } 1137 if (*p == '\0') start = p; 1138 else if (*p == ' ') start = ++p; /* SP */ 1139 DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd); 1140 1141 /* and now the message itself 1142 * note: move back to last start to check for BOM 1143 */ 1144 all_syslog_msg: 1145 p = start; 1146 1147 /* check for UTF-8-BOM */ 1148 if (IS_BOM(p)) { 1149 DPRINTF(D_DATA, "UTF-8 BOM\n"); 1150 utf8allowed = true; 1151 p += 3; 1152 } 1153 1154 if (*p != '\0' && !utf8allowed) { 1155 size_t msglen; 1156 1157 msglen = strlen(p); 1158 assert(!buffer->msg); 1159 buffer->msg = copy_utf8_ascii(p, msglen); 1160 buffer->msgorig = buffer->msg; 1161 buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1; 1162 } else if (*p != '\0' && utf8allowed) { 1163 while (*p != '\0') { 1164 i = valid_utf8(p); 1165 if (i == 0) 1166 *p++ = '?'; 1167 else if (i == 1) 1168 *p = FORCE2ASCII(*p); 1169 p += i; 1170 } 1171 assert(p != start); 1172 assert(!buffer->msg); 1173 buffer->msg = strndup(start, p - start); 1174 buffer->msgorig = buffer->msg; 1175 buffer->msglen = buffer->msgsize = 1 + p - start; 1176 } 1177 DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg); 1178 1179 buffer->recvhost = strdup(hname); 1180 buffer->pri = pri; 1181 buffer->flags = flags; 1182 1183 return buffer; 1184 } 1185 1186 /* copies an input into a new ASCII buffer 1187 * ASCII controls are converted to format "^X" 1188 * multi-byte UTF-8 chars are converted to format "<ab><cd>" 1189 */ 1190 #define INIT_BUFSIZE 512 1191 char * 1192 copy_utf8_ascii(char *p, size_t p_len) 1193 { 1194 size_t idst = 0, isrc = 0, dstsize = INIT_BUFSIZE, i; 1195 char *dst, *tmp_dst; 1196 1197 MALLOC(dst, dstsize); 1198 while (isrc < p_len) { 1199 if (dstsize < idst + 10) { 1200 /* check for enough space for \0 and a UTF-8 1201 * conversion; longest possible is <U+123456> */ 1202 tmp_dst = realloc(dst, dstsize + INIT_BUFSIZE); 1203 if (!tmp_dst) 1204 break; 1205 dst = tmp_dst; 1206 dstsize += INIT_BUFSIZE; 1207 } 1208 1209 i = valid_utf8(&p[isrc]); 1210 if (i == 0) { /* invalid encoding */ 1211 dst[idst++] = '?'; 1212 isrc++; 1213 } else if (i == 1) { /* check printable */ 1214 if (iscntrl((unsigned char)p[isrc]) 1215 && p[isrc] != '\t') { 1216 if (p[isrc] == '\n') { 1217 dst[idst++] = ' '; 1218 isrc++; 1219 } else { 1220 dst[idst++] = '^'; 1221 dst[idst++] = p[isrc++] ^ 0100; 1222 } 1223 } else 1224 dst[idst++] = p[isrc++]; 1225 } else { /* convert UTF-8 to ASCII */ 1226 dst[idst++] = '<'; 1227 idst += snprintf(&dst[idst], dstsize - idst, "U+%x", 1228 get_utf8_value(&p[isrc])); 1229 isrc += i; 1230 dst[idst++] = '>'; 1231 } 1232 } 1233 dst[idst] = '\0'; 1234 1235 /* shrink buffer to right size */ 1236 tmp_dst = realloc(dst, idst+1); 1237 if (tmp_dst) 1238 return tmp_dst; 1239 else 1240 return dst; 1241 } 1242 1243 struct buf_msg * 1244 printline_bsdsyslog(const char *hname, char *msg, 1245 int flags, int pri) 1246 { 1247 struct buf_msg *buffer; 1248 char *p, *start; 1249 unsigned msgidlen = 0, sdlen = 0; 1250 1251 DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_bsdsyslog(" 1252 "\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri); 1253 1254 buffer = buf_msg_new(0); 1255 p = msg; 1256 p += check_timestamp((unsigned char*) p, 1257 &buffer->timestamp, false, !BSDOutputFormat); 1258 DPRINTF(D_DATA, "Got timestamp \"%s\"\n", buffer->timestamp); 1259 1260 if (flags & ADDDATE || !buffer->timestamp) { 1261 FREEPTR(buffer->timestamp); 1262 buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0); 1263 } 1264 1265 if (*p == ' ') p++; /* SP */ 1266 else goto all_bsd_msg; 1267 /* in any error case we skip header parsing and 1268 * treat all following data as message content */ 1269 1270 /* extract host */ 1271 for (start = p;; p++) { 1272 if (*p == ' ' || *p == '\0') { 1273 buffer->host = strndup(start, p - start); 1274 break; 1275 } else if (*p == '[' || (*p == ':' 1276 && (*(p+1) == ' ' || *(p+1) == '\0'))) { 1277 /* no host in message */ 1278 buffer->host = LocalFQDN; 1279 buffer->prog = strndup(start, p - start); 1280 break; 1281 } else { 1282 *p = FORCE2ASCII(*p); 1283 } 1284 } 1285 DPRINTF(D_DATA, "Got host \"%s\"\n", buffer->host); 1286 /* p @ SP after host, or @ :/[ after prog */ 1287 1288 /* extract program */ 1289 if (!buffer->prog) { 1290 if (*p == ' ') p++; /* SP */ 1291 else goto all_bsd_msg; 1292 1293 for (start = p;; p++) { 1294 if (*p == ' ' || *p == '\0') { /* error */ 1295 goto all_bsd_msg; 1296 } else if (*p == '[' || (*p == ':' 1297 && (*(p+1) == ' ' || *(p+1) == '\0'))) { 1298 buffer->prog = strndup(start, p - start); 1299 break; 1300 } else { 1301 *p = FORCE2ASCII(*p); 1302 } 1303 } 1304 } 1305 DPRINTF(D_DATA, "Got prog \"%s\"\n", buffer->prog); 1306 start = p; 1307 1308 /* p @ :/[ after prog */ 1309 if (*p == '[') { 1310 p++; 1311 if (*p == ' ') p++; /* SP */ 1312 for (start = p;; p++) { 1313 if (*p == ' ' || *p == '\0') { /* error */ 1314 goto all_bsd_msg; 1315 } else if (*p == ']') { 1316 buffer->pid = strndup(start, p - start); 1317 break; 1318 } else { 1319 *p = FORCE2ASCII(*p); 1320 } 1321 } 1322 } 1323 DPRINTF(D_DATA, "Got pid \"%s\"\n", buffer->pid); 1324 1325 if (*p == ']') p++; 1326 if (*p == ':') p++; 1327 if (*p == ' ') p++; 1328 1329 /* p @ msgid, @ opening [ of SD or @ first byte of message 1330 * accept either case and try to detect MSGID and SD fields 1331 * 1332 * only limitation: we do not accept UTF-8 data in 1333 * BSD Syslog messages -- so all SD values are ASCII-filtered 1334 * 1335 * I have found one scenario with 'unexpected' behaviour: 1336 * if there is only a SD intended, but a) it is short enough 1337 * to be a MSGID and b) the first word of the message can also 1338 * be parsed as an SD. 1339 * example: 1340 * "<35>Jul 6 12:39:08 tag[123]: [exampleSDID@0] - hello" 1341 * --> parsed as 1342 * MSGID = "[exampleSDID@0]" 1343 * SD = "-" 1344 * MSG = "hello" 1345 */ 1346 start = p; 1347 msgidlen = check_msgid(p); 1348 if (msgidlen) /* check for SD in 2nd field */ 1349 sdlen = check_sd(p+msgidlen+1); 1350 1351 if (msgidlen && sdlen) { 1352 /* MSGID in 1st and SD in 2nd field 1353 * now check for NILVALUEs and copy */ 1354 if (msgidlen == 1 && *p == '-') { 1355 p++; /* - */ 1356 p++; /* SP */ 1357 DPRINTF(D_DATA, "Got MSGID \"-\"\n"); 1358 } else { 1359 /* only has ASCII chars after check_msgid() */ 1360 buffer->msgid = strndup(p, msgidlen); 1361 p += msgidlen; 1362 p++; /* SP */ 1363 DPRINTF(D_DATA, "Got MSGID \"%s\"\n", 1364 buffer->msgid); 1365 } 1366 } else { 1367 /* either no msgid or no SD in 2nd field 1368 * --> check 1st field for SD */ 1369 DPRINTF(D_DATA, "No MSGID\n"); 1370 sdlen = check_sd(p); 1371 } 1372 1373 if (sdlen == 0) { 1374 DPRINTF(D_DATA, "No SD\n"); 1375 } else if (sdlen > 1) { 1376 buffer->sd = copy_utf8_ascii(p, sdlen); 1377 DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd); 1378 } else if (sdlen == 1 && *p == '-') { 1379 p++; 1380 DPRINTF(D_DATA, "Got SD \"-\"\n"); 1381 } else { 1382 DPRINTF(D_DATA, "Error\n"); 1383 } 1384 1385 if (*p == ' ') p++; 1386 start = p; 1387 /* and now the message itself 1388 * note: do not reset start, because we might come here 1389 * by goto and want to have the incomplete field as part 1390 * of the msg 1391 */ 1392 all_bsd_msg: 1393 if (*p != '\0') { 1394 size_t msglen = strlen(p); 1395 buffer->msg = copy_utf8_ascii(p, msglen); 1396 buffer->msgorig = buffer->msg; 1397 buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1; 1398 } 1399 DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg); 1400 1401 buffer->recvhost = strdup(hname); 1402 buffer->pri = pri; 1403 buffer->flags = flags | BSDSYSLOG; 1404 1405 return buffer; 1406 } 1407 1408 struct buf_msg * 1409 printline_kernelprintf(const char *hname, char *msg, 1410 int flags, int pri) 1411 { 1412 struct buf_msg *buffer; 1413 char *p; 1414 unsigned sdlen = 0; 1415 1416 DPRINTF((D_CALL|D_BUFFER|D_DATA), "printline_kernelprintf(" 1417 "\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri); 1418 1419 buffer = buf_msg_new(0); 1420 buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0); 1421 buffer->pri = pri; 1422 buffer->flags = flags; 1423 1424 /* assume there is no MSGID but there might be SD */ 1425 p = msg; 1426 sdlen = check_sd(p); 1427 1428 if (sdlen == 0) { 1429 DPRINTF(D_DATA, "No SD\n"); 1430 } else if (sdlen > 1) { 1431 buffer->sd = copy_utf8_ascii(p, sdlen); 1432 DPRINTF(D_DATA, "Got SD \"%s\"\n", buffer->sd); 1433 } else if (sdlen == 1 && *p == '-') { 1434 p++; 1435 DPRINTF(D_DATA, "Got SD \"-\"\n"); 1436 } else { 1437 DPRINTF(D_DATA, "Error\n"); 1438 } 1439 1440 if (*p == ' ') p++; 1441 if (*p != '\0') { 1442 size_t msglen = strlen(p); 1443 buffer->msg = copy_utf8_ascii(p, msglen); 1444 buffer->msgorig = buffer->msg; 1445 buffer->msglen = buffer->msgsize = strlen(buffer->msg)+1; 1446 } 1447 DPRINTF(D_DATA, "Got msg \"%s\"\n", buffer->msg); 1448 1449 return buffer; 1450 } 1451 1452 /* 1453 * Take a raw input line, read priority and version, call the 1454 * right message parsing function, then call logmsg(). 1455 */ 1456 void 1457 printline(const char *hname, char *msg, int flags) 1458 { 1459 struct buf_msg *buffer; 1460 int pri; 1461 char *p, *q; 1462 long n; 1463 bool bsdsyslog = true; 1464 1465 DPRINTF((D_CALL|D_BUFFER|D_DATA), 1466 "printline(\"%s\", \"%s\", %d)\n", hname, msg, flags); 1467 1468 /* test for special codes */ 1469 pri = DEFUPRI; 1470 p = msg; 1471 if (*p == '<') { 1472 errno = 0; 1473 n = strtol(p + 1, &q, 10); 1474 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 1475 p = q + 1; 1476 pri = (int)n; 1477 /* check for syslog-protocol version */ 1478 if (*p == '1' && p[1] == ' ') { 1479 p += 2; /* skip version and space */ 1480 bsdsyslog = false; 1481 } else { 1482 bsdsyslog = true; 1483 } 1484 } 1485 } 1486 if (pri & ~(LOG_FACMASK|LOG_PRIMASK)) 1487 pri = DEFUPRI; 1488 1489 /* 1490 * Don't allow users to log kernel messages. 1491 * NOTE: Since LOG_KERN == 0, this will also match 1492 * messages with no facility specified. 1493 */ 1494 if ((pri & LOG_FACMASK) == LOG_KERN) 1495 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); 1496 1497 if (bsdsyslog) { 1498 buffer = printline_bsdsyslog(hname, p, flags, pri); 1499 } else { 1500 buffer = printline_syslogprotocol(hname, p, flags, pri); 1501 } 1502 logmsg(buffer); 1503 DELREF(buffer); 1504 } 1505 1506 /* 1507 * Take a raw input line from /dev/klog, split and format similar to syslog(). 1508 */ 1509 void 1510 printsys(char *msg) 1511 { 1512 int n, is_printf, pri, flags; 1513 char *p, *q; 1514 struct buf_msg *buffer; 1515 1516 klog_linebufoff = 0; 1517 for (p = msg; *p != '\0'; ) { 1518 bool bsdsyslog = true; 1519 1520 is_printf = 1; 1521 flags = ISKERNEL | ADDDATE | BSDSYSLOG; 1522 if (SyncKernel) 1523 flags |= SYNC_FILE; 1524 if (is_printf) /* kernel printf's come out on console */ 1525 flags |= IGN_CONS; 1526 pri = DEFSPRI; 1527 1528 if (*p == '<') { 1529 errno = 0; 1530 n = (int)strtol(p + 1, &q, 10); 1531 if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { 1532 p = q + 1; 1533 is_printf = 0; 1534 pri = n; 1535 if (*p == '1') { /* syslog-protocol version */ 1536 p += 2; /* skip version and space */ 1537 bsdsyslog = false; 1538 } else { 1539 bsdsyslog = true; 1540 } 1541 } 1542 } 1543 for (q = p; *q != '\0' && *q != '\n'; q++) 1544 /* look for end of line; no further checks. 1545 * trust the kernel to send ASCII only */; 1546 if (*q != '\0') 1547 *q++ = '\0'; 1548 else { 1549 memcpy(linebuf, p, klog_linebufoff = q - p); 1550 break; 1551 } 1552 1553 if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) 1554 pri = DEFSPRI; 1555 1556 /* allow all kinds of input from kernel */ 1557 if (is_printf) 1558 buffer = printline_kernelprintf( 1559 LocalFQDN, p, flags, pri); 1560 else { 1561 if (bsdsyslog) 1562 buffer = printline_bsdsyslog( 1563 LocalFQDN, p, flags, pri); 1564 else 1565 buffer = printline_syslogprotocol( 1566 LocalFQDN, p, flags, pri); 1567 } 1568 1569 /* set fields left open */ 1570 if (!buffer->prog) 1571 buffer->prog = strdup(_PATH_UNIX); 1572 if (!buffer->host) 1573 buffer->host = LocalFQDN; 1574 if (!buffer->recvhost) 1575 buffer->recvhost = LocalFQDN; 1576 1577 logmsg(buffer); 1578 DELREF(buffer); 1579 p = q; 1580 } 1581 } 1582 1583 /* 1584 * Check to see if `name' matches the provided specification, using the 1585 * specified strstr function. 1586 */ 1587 int 1588 matches_spec(const char *name, const char *spec, 1589 char *(*check)(const char *, const char *)) 1590 { 1591 const char *s; 1592 const char *cursor; 1593 char prev, next; 1594 size_t len; 1595 1596 if (name[0] == '\0') 1597 return 0; 1598 1599 if (strchr(name, ',')) /* sanity */ 1600 return 0; 1601 1602 len = strlen(name); 1603 cursor = spec; 1604 while ((s = (*check)(cursor, name)) != NULL) { 1605 prev = s == spec ? ',' : *(s - 1); 1606 cursor = s + len; 1607 next = *cursor; 1608 1609 if (prev == ',' && (next == '\0' || next == ',')) 1610 return 1; 1611 } 1612 1613 return 0; 1614 } 1615 1616 /* 1617 * wrapper with old function signature, 1618 * keeps calling code shorter and hides buffer allocation 1619 */ 1620 void 1621 logmsg_async(int pri, const char *sd, const char *msg, int flags) 1622 { 1623 struct buf_msg *buffer; 1624 size_t msglen; 1625 1626 DPRINTF((D_CALL|D_DATA), "logmsg_async(%d, \"%s\", \"%s\", %d)\n", 1627 pri, sd, msg, flags); 1628 1629 if (msg) { 1630 msglen = strlen(msg); 1631 msglen++; /* adds \0 */ 1632 buffer = buf_msg_new(msglen); 1633 buffer->msglen = strlcpy(buffer->msg, msg, msglen) + 1; 1634 } else { 1635 buffer = buf_msg_new(0); 1636 } 1637 if (sd) buffer->sd = strdup(sd); 1638 buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0); 1639 buffer->prog = appname; 1640 buffer->pid = include_pid; 1641 buffer->recvhost = buffer->host = LocalFQDN; 1642 buffer->pri = pri; 1643 buffer->flags = flags; 1644 1645 logmsg(buffer); 1646 DELREF(buffer); 1647 } 1648 1649 /* read timestamp in from_buf, convert into a timestamp in to_buf 1650 * 1651 * returns length of timestamp found in from_buf (= number of bytes consumed) 1652 */ 1653 size_t 1654 check_timestamp(unsigned char *from_buf, char **to_buf, 1655 bool from_iso, bool to_iso) 1656 { 1657 unsigned char *q; 1658 int p; 1659 bool found_ts = false; 1660 1661 DPRINTF((D_CALL|D_DATA), "check_timestamp(%p = \"%s\", from_iso=%d, " 1662 "to_iso=%d)\n", from_buf, from_buf, from_iso, to_iso); 1663 1664 if (!from_buf) return 0; 1665 /* 1666 * Check to see if msg looks non-standard. 1667 * looks at every char because we do not have a msg length yet 1668 */ 1669 /* detailed checking adapted from Albert Mietus' sl_timestamp.c */ 1670 if (from_iso) { 1671 if (from_buf[4] == '-' && from_buf[7] == '-' 1672 && from_buf[10] == 'T' && from_buf[13] == ':' 1673 && from_buf[16] == ':' 1674 && isdigit(from_buf[0]) && isdigit(from_buf[1]) 1675 && isdigit(from_buf[2]) && isdigit(from_buf[3]) /* YYYY */ 1676 && isdigit(from_buf[5]) && isdigit(from_buf[6]) 1677 && isdigit(from_buf[8]) && isdigit(from_buf[9]) /* mm dd */ 1678 && isdigit(from_buf[11]) && isdigit(from_buf[12]) /* HH */ 1679 && isdigit(from_buf[14]) && isdigit(from_buf[15]) /* MM */ 1680 && isdigit(from_buf[17]) && isdigit(from_buf[18]) /* SS */ 1681 ) { 1682 /* time-secfrac */ 1683 if (from_buf[19] == '.') 1684 for (p=20; isdigit(from_buf[p]); p++) /* NOP*/; 1685 else 1686 p = 19; 1687 /* time-offset */ 1688 if (from_buf[p] == 'Z' 1689 || ((from_buf[p] == '+' || from_buf[p] == '-') 1690 && from_buf[p+3] == ':' 1691 && isdigit(from_buf[p+1]) && isdigit(from_buf[p+2]) 1692 && isdigit(from_buf[p+4]) && isdigit(from_buf[p+5]) 1693 )) 1694 found_ts = true; 1695 } 1696 } else { 1697 if (from_buf[3] == ' ' && from_buf[6] == ' ' 1698 && from_buf[9] == ':' && from_buf[12] == ':' 1699 && (from_buf[4] == ' ' || isdigit(from_buf[4])) 1700 && isdigit(from_buf[5]) /* dd */ 1701 && isdigit(from_buf[7]) && isdigit(from_buf[8]) /* HH */ 1702 && isdigit(from_buf[10]) && isdigit(from_buf[11]) /* MM */ 1703 && isdigit(from_buf[13]) && isdigit(from_buf[14]) /* SS */ 1704 && isupper(from_buf[0]) && islower(from_buf[1]) /* month */ 1705 && islower(from_buf[2])) 1706 found_ts = true; 1707 } 1708 if (!found_ts) { 1709 if (from_buf[0] == '-' && from_buf[1] == ' ') { 1710 /* NILVALUE */ 1711 if (to_iso) { 1712 /* with ISO = syslog-protocol output leave 1713 * it as is, because it is better to have 1714 * no timestamp than a wrong one. 1715 */ 1716 *to_buf = strdup("-"); 1717 } else { 1718 /* with BSD Syslog the field is reqired 1719 * so replace it with current time 1720 */ 1721 *to_buf = make_timestamp(NULL, false, 0); 1722 } 1723 return 2; 1724 } 1725 *to_buf = make_timestamp(NULL, false, 0); 1726 return 0; 1727 } 1728 1729 if (!from_iso && !to_iso) { 1730 /* copy BSD timestamp */ 1731 DPRINTF(D_CALL, "check_timestamp(): copy BSD timestamp\n"); 1732 *to_buf = strndup((char *)from_buf, BSD_TIMESTAMPLEN); 1733 return BSD_TIMESTAMPLEN; 1734 } else if (from_iso && to_iso) { 1735 /* copy ISO timestamp */ 1736 DPRINTF(D_CALL, "check_timestamp(): copy ISO timestamp\n"); 1737 if (!(q = (unsigned char *) strchr((char *)from_buf, ' '))) 1738 q = from_buf + strlen((char *)from_buf); 1739 *to_buf = strndup((char *)from_buf, q - from_buf); 1740 return q - from_buf; 1741 } else if (from_iso && !to_iso) { 1742 /* convert ISO->BSD */ 1743 struct tm parsed; 1744 time_t timeval; 1745 char tsbuf[MAX_TIMESTAMPLEN]; 1746 int i = 0; 1747 1748 DPRINTF(D_CALL, "check_timestamp(): convert ISO->BSD\n"); 1749 for(i = 0; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0' 1750 && from_buf[i] != '.' && from_buf[i] != ' '; i++) 1751 tsbuf[i] = from_buf[i]; /* copy date & time */ 1752 for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0' 1753 && from_buf[i] != '+' && from_buf[i] != '-' 1754 && from_buf[i] != 'Z' && from_buf[i] != ' '; i++) 1755 ; /* skip fraction digits */ 1756 for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0' 1757 && from_buf[i] != ':' && from_buf[i] != ' ' ; i++) 1758 tsbuf[i] = from_buf[i]; /* copy TZ */ 1759 if (from_buf[i] == ':') i++; /* skip colon */ 1760 for(; i < MAX_TIMESTAMPLEN && from_buf[i] != '\0' 1761 && from_buf[i] != ' ' ; i++) 1762 tsbuf[i] = from_buf[i]; /* copy TZ */ 1763 1764 (void)memset(&parsed, 0, sizeof(parsed)); 1765 parsed.tm_isdst = -1; 1766 (void)strptime(tsbuf, "%FT%T%z", &parsed); 1767 timeval = mktime(&parsed); 1768 1769 *to_buf = make_timestamp(&timeval, false, BSD_TIMESTAMPLEN); 1770 return i; 1771 } else if (!from_iso && to_iso) { 1772 /* convert BSD->ISO */ 1773 struct tm parsed; 1774 struct tm *current; 1775 time_t timeval; 1776 1777 (void)memset(&parsed, 0, sizeof(parsed)); 1778 parsed.tm_isdst = -1; 1779 DPRINTF(D_CALL, "check_timestamp(): convert BSD->ISO\n"); 1780 strptime((char *)from_buf, "%b %d %T", &parsed); 1781 current = gmtime(&now); 1782 1783 /* use current year and timezone */ 1784 parsed.tm_isdst = current->tm_isdst; 1785 parsed.tm_gmtoff = current->tm_gmtoff; 1786 parsed.tm_year = current->tm_year; 1787 if (current->tm_mon == 0 && parsed.tm_mon == 11) 1788 parsed.tm_year--; 1789 1790 timeval = mktime(&parsed); 1791 *to_buf = make_timestamp(&timeval, true, MAX_TIMESTAMPLEN - 1); 1792 1793 return BSD_TIMESTAMPLEN; 1794 } else { 1795 DPRINTF(D_MISC, 1796 "Executing unreachable code in check_timestamp()\n"); 1797 return 0; 1798 } 1799 } 1800 1801 /* 1802 * Log a message to the appropriate log files, users, etc. based on 1803 * the priority. 1804 */ 1805 void 1806 logmsg(struct buf_msg *buffer) 1807 { 1808 struct filed *f; 1809 int fac, omask, prilev; 1810 1811 DPRINTF((D_CALL|D_BUFFER), "logmsg: buffer@%p, pri 0%o/%d, flags 0x%x," 1812 " timestamp \"%s\", from \"%s\", sd \"%s\", msg \"%s\"\n", 1813 buffer, buffer->pri, buffer->pri, buffer->flags, 1814 buffer->timestamp, buffer->recvhost, buffer->sd, buffer->msg); 1815 1816 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); 1817 1818 /* sanity check */ 1819 assert(buffer->refcount == 1); 1820 assert(buffer->msglen <= buffer->msgsize); 1821 assert(buffer->msgorig <= buffer->msg); 1822 assert((buffer->msg && buffer->msglen == strlen(buffer->msg)+1) 1823 || (!buffer->msg && !buffer->msglen)); 1824 if (!buffer->msg && !buffer->sd && !buffer->msgid) 1825 DPRINTF(D_BUFFER, "Empty message?\n"); 1826 1827 /* extract facility and priority level */ 1828 if (buffer->flags & MARK) 1829 fac = LOG_NFACILITIES; 1830 else 1831 fac = LOG_FAC(buffer->pri); 1832 prilev = LOG_PRI(buffer->pri); 1833 1834 /* log the message to the particular outputs */ 1835 if (!Initialized) { 1836 f = &consfile; 1837 f->f_file = open(ctty, O_WRONLY | O_NDELAY, 0); 1838 1839 if (f->f_file >= 0) { 1840 DELREF(f->f_prevmsg); 1841 f->f_prevmsg = NEWREF(buffer); 1842 fprintlog(f, NEWREF(buffer), NULL); 1843 DELREF(buffer); 1844 (void)close(f->f_file); 1845 } 1846 (void)sigsetmask(omask); 1847 return; 1848 } 1849 1850 for (f = Files; f; f = f->f_next) { 1851 char *h; /* host to use for comparing */ 1852 1853 /* skip messages that are incorrect priority */ 1854 if (!MATCH_PRI(f, fac, prilev) 1855 || f->f_pmask[fac] == INTERNAL_NOPRI) 1856 continue; 1857 1858 /* skip messages with the incorrect host name */ 1859 /* compare with host (which is supposedly more correct), */ 1860 /* but fallback to recvhost if host is NULL */ 1861 h = (buffer->host != NULL) ? buffer->host : buffer->recvhost; 1862 if (f->f_host != NULL && h != NULL) { 1863 char shost[MAXHOSTNAMELEN + 1]; 1864 1865 if (BSDOutputFormat) { 1866 (void)strlcpy(shost, h, sizeof(shost)); 1867 trim_anydomain(shost); 1868 h = shost; 1869 } 1870 switch (f->f_host[0]) { 1871 case '+': 1872 if (! matches_spec(h, f->f_host + 1, 1873 strcasestr)) 1874 continue; 1875 break; 1876 case '-': 1877 if (matches_spec(h, f->f_host + 1, 1878 strcasestr)) 1879 continue; 1880 break; 1881 } 1882 } 1883 1884 /* skip messages with the incorrect program name */ 1885 if (f->f_program != NULL && buffer->prog != NULL) { 1886 switch (f->f_program[0]) { 1887 case '+': 1888 if (!matches_spec(buffer->prog, 1889 f->f_program + 1, strstr)) 1890 continue; 1891 break; 1892 case '-': 1893 if (matches_spec(buffer->prog, 1894 f->f_program + 1, strstr)) 1895 continue; 1896 break; 1897 default: 1898 if (!matches_spec(buffer->prog, 1899 f->f_program, strstr)) 1900 continue; 1901 break; 1902 } 1903 } 1904 1905 if (f->f_type == F_CONSOLE && (buffer->flags & IGN_CONS)) 1906 continue; 1907 1908 /* don't output marks to recently written files */ 1909 if ((buffer->flags & MARK) 1910 && (now - f->f_time) < MarkInterval / 2) 1911 continue; 1912 1913 /* 1914 * suppress duplicate lines to this file unless NoRepeat 1915 */ 1916 #define MSG_FIELD_EQ(x) ((!buffer->x && !f->f_prevmsg->x) || \ 1917 (buffer->x && f->f_prevmsg->x && !strcmp(buffer->x, f->f_prevmsg->x))) 1918 1919 if ((buffer->flags & MARK) == 0 && 1920 f->f_prevmsg && 1921 buffer->msglen == f->f_prevmsg->msglen && 1922 !NoRepeat && 1923 MSG_FIELD_EQ(host) && 1924 MSG_FIELD_EQ(sd) && 1925 MSG_FIELD_EQ(msg) 1926 ) { 1927 f->f_prevcount++; 1928 DPRINTF(D_DATA, "Msg repeated %d times, %ld sec of %d\n", 1929 f->f_prevcount, (long)(now - f->f_time), 1930 repeatinterval[f->f_repeatcount]); 1931 /* 1932 * If domark would have logged this by now, 1933 * flush it now (so we don't hold isolated messages), 1934 * but back off so we'll flush less often 1935 * in the future. 1936 */ 1937 if (now > REPEATTIME(f)) { 1938 fprintlog(f, NEWREF(buffer), NULL); 1939 DELREF(buffer); 1940 BACKOFF(f); 1941 } 1942 } else { 1943 /* new line, save it */ 1944 if (f->f_prevcount) 1945 fprintlog(f, NULL, NULL); 1946 f->f_repeatcount = 0; 1947 DELREF(f->f_prevmsg); 1948 f->f_prevmsg = NEWREF(buffer); 1949 fprintlog(f, NEWREF(buffer), NULL); 1950 DELREF(buffer); 1951 } 1952 } 1953 (void)sigsetmask(omask); 1954 } 1955 1956 /* 1957 * format one buffer into output format given by flag BSDOutputFormat 1958 * line is allocated and has to be free()d by caller 1959 * size_t pointers are optional, if not NULL then they will return 1960 * different lenghts used for formatting and output 1961 */ 1962 #define OUT(x) ((x)?(x):"-") 1963 bool 1964 format_buffer(struct buf_msg *buffer, char **line, size_t *ptr_linelen, 1965 size_t *ptr_msglen, size_t *ptr_tlsprefixlen, size_t *ptr_prilen) 1966 { 1967 #define FPBUFSIZE 30 1968 static char ascii_empty[] = ""; 1969 char fp_buf[FPBUFSIZE] = "\0"; 1970 char *hostname, *shorthostname = NULL; 1971 char *ascii_sd = ascii_empty; 1972 char *ascii_msg = ascii_empty; 1973 size_t linelen, msglen, tlsprefixlen, prilen, j; 1974 1975 DPRINTF(D_CALL, "format_buffer(%p)\n", buffer); 1976 if (!buffer) return false; 1977 1978 /* All buffer fields are set with strdup(). To avoid problems 1979 * on memory exhaustion we allow them to be empty and replace 1980 * the essential fields with already allocated generic values. 1981 */ 1982 if (!buffer->timestamp) 1983 buffer->timestamp = timestamp; 1984 if (!buffer->host && !buffer->recvhost) 1985 buffer->host = LocalFQDN; 1986 1987 if (LogFacPri) { 1988 const char *f_s = NULL, *p_s = NULL; 1989 int fac = buffer->pri & LOG_FACMASK; 1990 int pri = LOG_PRI(buffer->pri); 1991 char f_n[5], p_n[5]; 1992 1993 if (LogFacPri > 1) { 1994 CODE *c; 1995 1996 for (c = facilitynames; c->c_name != NULL; c++) { 1997 if (c->c_val == fac) { 1998 f_s = c->c_name; 1999 break; 2000 } 2001 } 2002 for (c = prioritynames; c->c_name != NULL; c++) { 2003 if (c->c_val == pri) { 2004 p_s = c->c_name; 2005 break; 2006 } 2007 } 2008 } 2009 if (f_s == NULL) { 2010 snprintf(f_n, sizeof(f_n), "%d", LOG_FAC(fac)); 2011 f_s = f_n; 2012 } 2013 if (p_s == NULL) { 2014 snprintf(p_n, sizeof(p_n), "%d", pri); 2015 p_s = p_n; 2016 } 2017 snprintf(fp_buf, sizeof(fp_buf), "<%s.%s>", f_s, p_s); 2018 } 2019 2020 /* hostname or FQDN */ 2021 hostname = (buffer->host ? buffer->host : buffer->recvhost); 2022 if (BSDOutputFormat 2023 && (shorthostname = strdup(hostname))) { 2024 /* if the previous BSD output format with "host [recvhost]:" 2025 * gets implemented, this is the right place to distinguish 2026 * between buffer->host and buffer->recvhost 2027 */ 2028 trim_anydomain(shorthostname); 2029 hostname = shorthostname; 2030 } 2031 2032 /* new message formatting: 2033 * instead of using iov always assemble one complete TLS-ready line 2034 * with length and priority (depending on BSDOutputFormat either in 2035 * BSD Syslog or syslog-protocol format) 2036 * 2037 * additionally save the length of the prefixes, 2038 * so UDP destinations can skip the length prefix and 2039 * file/pipe/wall destinations can omit length and priority 2040 */ 2041 /* first determine required space */ 2042 if (BSDOutputFormat) { 2043 /* only output ASCII chars */ 2044 if (buffer->sd) 2045 ascii_sd = copy_utf8_ascii(buffer->sd, 2046 strlen(buffer->sd)); 2047 if (buffer->msg) { 2048 if (IS_BOM(buffer->msg)) 2049 ascii_msg = copy_utf8_ascii(buffer->msg, 2050 buffer->msglen - 1); 2051 else /* assume already converted at input */ 2052 ascii_msg = buffer->msg; 2053 } 2054 msglen = snprintf(NULL, 0, "<%d>%s%.15s %s %s%s%s%s: %s%s%s", 2055 buffer->pri, fp_buf, buffer->timestamp, 2056 hostname, OUT(buffer->prog), 2057 buffer->pid ? "[" : "", 2058 buffer->pid ? buffer->pid : "", 2059 buffer->pid ? "]" : "", ascii_sd, 2060 (buffer->sd && buffer->msg ? " ": ""), ascii_msg); 2061 } else 2062 msglen = snprintf(NULL, 0, "<%d>1 %s%s %s %s %s %s %s%s%s", 2063 buffer->pri, fp_buf, buffer->timestamp, 2064 hostname, OUT(buffer->prog), OUT(buffer->pid), 2065 OUT(buffer->msgid), OUT(buffer->sd), 2066 (buffer->msg ? " ": ""), 2067 (buffer->msg ? buffer->msg: "")); 2068 /* add space for length prefix */ 2069 tlsprefixlen = 0; 2070 for (j = msglen; j; j /= 10) 2071 tlsprefixlen++; 2072 /* one more for the space */ 2073 tlsprefixlen++; 2074 2075 prilen = snprintf(NULL, 0, "<%d>", buffer->pri); 2076 if (!BSDOutputFormat) 2077 prilen += 2; /* version char and space */ 2078 MALLOC(*line, msglen + tlsprefixlen + 1); 2079 if (BSDOutputFormat) 2080 linelen = snprintf(*line, 2081 msglen + tlsprefixlen + 1, 2082 "%zu <%d>%s%.15s %s %s%s%s%s: %s%s%s", 2083 msglen, buffer->pri, fp_buf, buffer->timestamp, 2084 hostname, OUT(buffer->prog), 2085 (buffer->pid ? "[" : ""), 2086 (buffer->pid ? buffer->pid : ""), 2087 (buffer->pid ? "]" : ""), ascii_sd, 2088 (buffer->sd && buffer->msg ? " ": ""), ascii_msg); 2089 else 2090 linelen = snprintf(*line, 2091 msglen + tlsprefixlen + 1, 2092 "%zu <%d>1 %s%s %s %s %s %s %s%s%s", 2093 msglen, buffer->pri, fp_buf, buffer->timestamp, 2094 hostname, OUT(buffer->prog), OUT(buffer->pid), 2095 OUT(buffer->msgid), OUT(buffer->sd), 2096 (buffer->msg ? " ": ""), 2097 (buffer->msg ? buffer->msg: "")); 2098 DPRINTF(D_DATA, "formatted %zu octets to: '%.*s' (linelen %zu, " 2099 "msglen %zu, tlsprefixlen %zu, prilen %zu)\n", linelen, 2100 (int)linelen, *line, linelen, msglen, tlsprefixlen, prilen); 2101 2102 FREEPTR(shorthostname); 2103 if (ascii_sd != ascii_empty) 2104 FREEPTR(ascii_sd); 2105 if (ascii_msg != ascii_empty && ascii_msg != buffer->msg) 2106 FREEPTR(ascii_msg); 2107 2108 if (ptr_linelen) *ptr_linelen = linelen; 2109 if (ptr_msglen) *ptr_msglen = msglen; 2110 if (ptr_tlsprefixlen) *ptr_tlsprefixlen = tlsprefixlen; 2111 if (ptr_prilen) *ptr_prilen = prilen; 2112 return true; 2113 } 2114 2115 /* 2116 * if qentry == NULL: new message, if temporarily undeliverable it will be enqueued 2117 * if qentry != NULL: a temporarily undeliverable message will not be enqueued, 2118 * but after delivery be removed from the queue 2119 */ 2120 void 2121 fprintlog(struct filed *f, struct buf_msg *passedbuffer, struct buf_queue *qentry) 2122 { 2123 static char crnl[] = "\r\n"; 2124 struct buf_msg *buffer = passedbuffer; 2125 struct iovec iov[4]; 2126 struct iovec *v = iov; 2127 bool error = false; 2128 int e = 0, len = 0; 2129 size_t msglen, linelen, tlsprefixlen, prilen; 2130 char *p, *line = NULL, *lineptr = NULL; 2131 #ifndef DISABLE_SIGN 2132 bool newhash = false; 2133 #endif 2134 #define REPBUFSIZE 80 2135 char greetings[200]; 2136 #define ADDEV() do { v++; assert((size_t)(v - iov) < A_CNT(iov)); } while(/*CONSTCOND*/0) 2137 2138 DPRINTF(D_CALL, "fprintlog(%p, %p, %p)\n", f, buffer, qentry); 2139 2140 f->f_time = now; 2141 2142 /* increase refcount here and lower again at return. 2143 * this enables the buffer in the else branch to be freed 2144 * --> every branch needs one NEWREF() or buf_msg_new()! */ 2145 if (buffer) { 2146 (void)NEWREF(buffer); 2147 } else { 2148 if (f->f_prevcount > 1) { 2149 /* possible syslog-sign incompatibility: 2150 * assume destinations f1 and f2 share one SG and 2151 * get the same message sequence. 2152 * 2153 * now both f1 and f2 generate "repeated" messages 2154 * "repeated" messages are different due to different 2155 * timestamps 2156 * the SG will get hashes for the two "repeated" messages 2157 * 2158 * now both f1 and f2 are just fine, but a verification 2159 * will report that each 'lost' a message, i.e. the 2160 * other's "repeated" message 2161 * 2162 * conditions for 'safe configurations': 2163 * - use NoRepeat option, 2164 * - use SG 3, or 2165 * - have exactly one destination for every PRI 2166 */ 2167 buffer = buf_msg_new(REPBUFSIZE); 2168 buffer->msglen = snprintf(buffer->msg, REPBUFSIZE, 2169 "last message repeated %d times", f->f_prevcount); 2170 buffer->timestamp = make_timestamp(NULL, 2171 !BSDOutputFormat, 0); 2172 buffer->pri = f->f_prevmsg->pri; 2173 buffer->host = LocalFQDN; 2174 buffer->prog = appname; 2175 buffer->pid = include_pid; 2176 2177 } else { 2178 buffer = NEWREF(f->f_prevmsg); 2179 } 2180 } 2181 2182 /* no syslog-sign messages to tty/console/... */ 2183 if ((buffer->flags & SIGN_MSG) 2184 && ((f->f_type == F_UNUSED) 2185 || (f->f_type == F_TTY) 2186 || (f->f_type == F_CONSOLE) 2187 || (f->f_type == F_USERS) 2188 || (f->f_type == F_WALL) 2189 || (f->f_type == F_FIFO))) { 2190 DELREF(buffer); 2191 return; 2192 } 2193 2194 /* buffering works only for few types */ 2195 if (qentry 2196 && (f->f_type != F_TLS) 2197 && (f->f_type != F_PIPE) 2198 && (f->f_type != F_FILE) 2199 && (f->f_type != F_FIFO)) { 2200 errno = 0; 2201 logerror("Warning: unexpected message type %d in buffer", 2202 f->f_type); 2203 DELREF(buffer); 2204 return; 2205 } 2206 2207 if (!format_buffer(buffer, &line, 2208 &linelen, &msglen, &tlsprefixlen, &prilen)) { 2209 DPRINTF(D_CALL, "format_buffer() failed, skip message\n"); 2210 DELREF(buffer); 2211 return; 2212 } 2213 /* assert maximum message length */ 2214 if (TypeInfo[f->f_type].max_msg_length != -1 2215 && (size_t)TypeInfo[f->f_type].max_msg_length 2216 < linelen - tlsprefixlen - prilen) { 2217 linelen = TypeInfo[f->f_type].max_msg_length 2218 + tlsprefixlen + prilen; 2219 DPRINTF(D_DATA, "truncating oversized message to %zu octets\n", 2220 linelen); 2221 } 2222 2223 #ifndef DISABLE_SIGN 2224 /* keep state between appending the hash (before buffer is sent) 2225 * and possibly sending a SB (after buffer is sent): */ 2226 /* get hash */ 2227 if (!(buffer->flags & SIGN_MSG) && !qentry) { 2228 char *hash = NULL; 2229 struct signature_group_t *sg; 2230 2231 if ((sg = sign_get_sg(buffer->pri, f)) != NULL) { 2232 if (sign_msg_hash(line + tlsprefixlen, &hash)) 2233 newhash = sign_append_hash(hash, sg); 2234 else 2235 DPRINTF(D_SIGN, 2236 "Unable to hash line \"%s\"\n", line); 2237 } 2238 } 2239 #endif /* !DISABLE_SIGN */ 2240 2241 /* set start and length of buffer and/or fill iovec */ 2242 switch (f->f_type) { 2243 case F_UNUSED: 2244 /* nothing */ 2245 break; 2246 case F_TLS: 2247 /* nothing, as TLS uses whole buffer to send */ 2248 lineptr = line; 2249 len = linelen; 2250 break; 2251 case F_FORW: 2252 lineptr = line + tlsprefixlen; 2253 len = linelen - tlsprefixlen; 2254 break; 2255 case F_PIPE: 2256 case F_FIFO: 2257 case F_FILE: /* fallthrough */ 2258 if (f->f_flags & FFLAG_FULL) { 2259 v->iov_base = line + tlsprefixlen; 2260 v->iov_len = linelen - tlsprefixlen; 2261 } else { 2262 v->iov_base = line + tlsprefixlen + prilen; 2263 v->iov_len = linelen - tlsprefixlen - prilen; 2264 } 2265 ADDEV(); 2266 v->iov_base = &crnl[1]; 2267 v->iov_len = 1; 2268 ADDEV(); 2269 break; 2270 case F_CONSOLE: 2271 case F_TTY: 2272 /* filter non-ASCII */ 2273 p = line; 2274 while (*p) { 2275 *p = FORCE2ASCII(*p); 2276 p++; 2277 } 2278 v->iov_base = line + tlsprefixlen + prilen; 2279 v->iov_len = linelen - tlsprefixlen - prilen; 2280 ADDEV(); 2281 v->iov_base = crnl; 2282 v->iov_len = 2; 2283 ADDEV(); 2284 break; 2285 case F_WALL: 2286 v->iov_base = greetings; 2287 v->iov_len = snprintf(greetings, sizeof(greetings), 2288 "\r\n\7Message from syslogd@%s at %s ...\r\n", 2289 (buffer->host ? buffer->host : buffer->recvhost), 2290 buffer->timestamp); 2291 ADDEV(); 2292 case F_USERS: /* fallthrough */ 2293 /* filter non-ASCII */ 2294 p = line; 2295 while (*p) { 2296 *p = FORCE2ASCII(*p); 2297 p++; 2298 } 2299 v->iov_base = line + tlsprefixlen + prilen; 2300 v->iov_len = linelen - tlsprefixlen - prilen; 2301 ADDEV(); 2302 v->iov_base = &crnl[1]; 2303 v->iov_len = 1; 2304 ADDEV(); 2305 break; 2306 } 2307 2308 /* send */ 2309 switch (f->f_type) { 2310 case F_UNUSED: 2311 DPRINTF(D_MISC, "Logging to %s\n", TypeInfo[f->f_type].name); 2312 break; 2313 2314 case F_FORW: 2315 DPRINTF(D_MISC, "Logging to %s %s\n", 2316 TypeInfo[f->f_type].name, f->f_un.f_forw.f_hname); 2317 udp_send(f, lineptr, len); 2318 break; 2319 2320 #ifndef DISABLE_TLS 2321 case F_TLS: 2322 DPRINTF(D_MISC, "Logging to %s %s\n", 2323 TypeInfo[f->f_type].name, 2324 f->f_un.f_tls.tls_conn->hostname); 2325 /* make sure every message gets queued once 2326 * it will be removed when sendmsg is sent and free()d */ 2327 if (!qentry) 2328 qentry = message_queue_add(f, NEWREF(buffer)); 2329 (void)tls_send(f, lineptr, len, qentry); 2330 break; 2331 #endif /* !DISABLE_TLS */ 2332 2333 case F_PIPE: 2334 DPRINTF(D_MISC, "Logging to %s %s\n", 2335 TypeInfo[f->f_type].name, f->f_un.f_pipe.f_pname); 2336 if (f->f_un.f_pipe.f_pid == 0) { 2337 /* (re-)open */ 2338 if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, 2339 &f->f_un.f_pipe.f_pid)) < 0) { 2340 f->f_type = F_UNUSED; 2341 logerror("%s", f->f_un.f_pipe.f_pname); 2342 message_queue_freeall(f); 2343 break; 2344 } else if (!qentry) /* prevent recursion */ 2345 SEND_QUEUE(f); 2346 } 2347 if (writev(f->f_file, iov, v - iov) < 0) { 2348 e = errno; 2349 if (f->f_un.f_pipe.f_pid > 0) { 2350 (void) close(f->f_file); 2351 deadq_enter(f->f_un.f_pipe.f_pid, 2352 f->f_un.f_pipe.f_pname); 2353 } 2354 f->f_un.f_pipe.f_pid = 0; 2355 /* 2356 * If the error was EPIPE, then what is likely 2357 * has happened is we have a command that is 2358 * designed to take a single message line and 2359 * then exit, but we tried to feed it another 2360 * one before we reaped the child and thus 2361 * reset our state. 2362 * 2363 * Well, now we've reset our state, so try opening 2364 * the pipe and sending the message again if EPIPE 2365 * was the error. 2366 */ 2367 if (e == EPIPE) { 2368 if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, 2369 &f->f_un.f_pipe.f_pid)) < 0) { 2370 f->f_type = F_UNUSED; 2371 logerror("%s", f->f_un.f_pipe.f_pname); 2372 message_queue_freeall(f); 2373 break; 2374 } 2375 if (writev(f->f_file, iov, v - iov) < 0) { 2376 e = errno; 2377 if (f->f_un.f_pipe.f_pid > 0) { 2378 (void) close(f->f_file); 2379 deadq_enter(f->f_un.f_pipe.f_pid, 2380 f->f_un.f_pipe.f_pname); 2381 } 2382 f->f_un.f_pipe.f_pid = 0; 2383 error = true; /* enqueue on return */ 2384 } else 2385 e = 0; 2386 } 2387 if (e != 0 && !error) { 2388 errno = e; 2389 logerror("%s", f->f_un.f_pipe.f_pname); 2390 } 2391 } 2392 if (e == 0 && qentry) { /* sent buffered msg */ 2393 message_queue_remove(f, qentry); 2394 } 2395 break; 2396 2397 case F_CONSOLE: 2398 if (buffer->flags & IGN_CONS) { 2399 DPRINTF(D_MISC, "Logging to %s (ignored)\n", 2400 TypeInfo[f->f_type].name); 2401 break; 2402 } 2403 /* FALLTHROUGH */ 2404 2405 case F_TTY: 2406 case F_FILE: 2407 DPRINTF(D_MISC, "Logging to %s %s\n", 2408 TypeInfo[f->f_type].name, f->f_un.f_fname); 2409 again: 2410 if ((f->f_type == F_FILE ? writev(f->f_file, iov, v - iov) : 2411 writev1(f->f_file, iov, v - iov)) < 0) { 2412 e = errno; 2413 if (f->f_type == F_FILE && e == ENOSPC) { 2414 int lasterror = f->f_lasterror; 2415 f->f_lasterror = e; 2416 if (lasterror != e) 2417 logerror("%s", f->f_un.f_fname); 2418 error = true; /* enqueue on return */ 2419 } 2420 (void)close(f->f_file); 2421 /* 2422 * Check for errors on TTY's due to loss of tty 2423 */ 2424 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) { 2425 f->f_file = open(f->f_un.f_fname, 2426 O_WRONLY|O_APPEND|O_NONBLOCK, 0); 2427 if (f->f_file < 0) { 2428 f->f_type = F_UNUSED; 2429 logerror("%s", f->f_un.f_fname); 2430 message_queue_freeall(f); 2431 } else 2432 goto again; 2433 } else { 2434 f->f_type = F_UNUSED; 2435 errno = e; 2436 f->f_lasterror = e; 2437 logerror("%s", f->f_un.f_fname); 2438 message_queue_freeall(f); 2439 } 2440 } else { 2441 f->f_lasterror = 0; 2442 if ((buffer->flags & SYNC_FILE) 2443 && (f->f_flags & FFLAG_SYNC)) 2444 (void)fsync(f->f_file); 2445 /* Problem with files: We cannot check beforehand if 2446 * they would be writeable and call send_queue() first. 2447 * So we call send_queue() after a successful write, 2448 * which means the first message will be out of order. 2449 */ 2450 if (!qentry) /* prevent recursion */ 2451 SEND_QUEUE(f); 2452 else if (qentry) /* sent buffered msg */ 2453 message_queue_remove(f, qentry); 2454 } 2455 break; 2456 2457 case F_FIFO: 2458 DPRINTF(D_MISC, "Logging to %s %s\n", 2459 TypeInfo[f->f_type].name, f->f_un.f_fname); 2460 if (f->f_file < 0) { 2461 f->f_file = 2462 open(f->f_un.f_fname, O_WRONLY|O_NONBLOCK, 0); 2463 e = errno; 2464 if (f->f_file < 0 && e == ENXIO) { 2465 /* Drop messages with no reader */ 2466 if (qentry) 2467 message_queue_remove(f, qentry); 2468 break; 2469 } 2470 } 2471 2472 if (f->f_file >= 0 && writev(f->f_file, iov, v - iov) < 0) { 2473 e = errno; 2474 2475 /* Enqueue if the fifo buffer is full */ 2476 if (e == EAGAIN) { 2477 if (f->f_lasterror != e) 2478 logerror("%s", f->f_un.f_fname); 2479 f->f_lasterror = e; 2480 error = true; /* enqueue on return */ 2481 break; 2482 } 2483 2484 close(f->f_file); 2485 f->f_file = -1; 2486 2487 /* Drop messages with no reader */ 2488 if (e == EPIPE) { 2489 if (qentry) 2490 message_queue_remove(f, qentry); 2491 break; 2492 } 2493 } 2494 2495 if (f->f_file < 0) { 2496 f->f_type = F_UNUSED; 2497 errno = e; 2498 f->f_lasterror = e; 2499 logerror("%s", f->f_un.f_fname); 2500 message_queue_freeall(f); 2501 break; 2502 } 2503 2504 f->f_lasterror = 0; 2505 if (!qentry) /* prevent recursion (see comment for F_FILE) */ 2506 SEND_QUEUE(f); 2507 if (qentry) /* sent buffered msg */ 2508 message_queue_remove(f, qentry); 2509 break; 2510 2511 case F_USERS: 2512 case F_WALL: 2513 DPRINTF(D_MISC, "Logging to %s\n", TypeInfo[f->f_type].name); 2514 wallmsg(f, iov, v - iov); 2515 break; 2516 } 2517 f->f_prevcount = 0; 2518 2519 if (error && !qentry) 2520 message_queue_add(f, NEWREF(buffer)); 2521 #ifndef DISABLE_SIGN 2522 if (newhash) { 2523 struct signature_group_t *sg; 2524 sg = sign_get_sg(buffer->pri, f); 2525 (void)sign_send_signature_block(sg, false); 2526 } 2527 #endif /* !DISABLE_SIGN */ 2528 /* this belongs to the ad-hoc buffer at the first if(buffer) */ 2529 DELREF(buffer); 2530 /* TLS frees on its own */ 2531 if (f->f_type != F_TLS) 2532 FREEPTR(line); 2533 } 2534 2535 /* send one line by UDP */ 2536 void 2537 udp_send(struct filed *f, char *line, size_t len) 2538 { 2539 int lsent, fail, retry, j; 2540 struct addrinfo *r; 2541 2542 DPRINTF((D_NET|D_CALL), "udp_send(f=%p, line=\"%s\", " 2543 "len=%zu) to dest.\n", f, line, len); 2544 2545 if (!finet) 2546 return; 2547 2548 lsent = -1; 2549 fail = 0; 2550 assert(f->f_type == F_FORW); 2551 for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) { 2552 retry = 0; 2553 for (j = 0; j < finet->fd; j++) { 2554 if (finet[j+1].af != r->ai_family) 2555 continue; 2556 sendagain: 2557 lsent = sendto(finet[j+1].fd, line, len, 0, 2558 r->ai_addr, r->ai_addrlen); 2559 if (lsent == -1) { 2560 switch (errno) { 2561 case ENOBUFS: 2562 /* wait/retry/drop */ 2563 if (++retry < 5) { 2564 usleep(1000); 2565 goto sendagain; 2566 } 2567 break; 2568 case EHOSTDOWN: 2569 case EHOSTUNREACH: 2570 case ENETDOWN: 2571 /* drop */ 2572 break; 2573 default: 2574 /* busted */ 2575 fail++; 2576 break; 2577 } 2578 } else if ((size_t)lsent == len) 2579 break; 2580 } 2581 if ((size_t)lsent != len && fail) { 2582 f->f_type = F_UNUSED; 2583 logerror("sendto() failed"); 2584 } 2585 } 2586 } 2587 2588 /* 2589 * WALLMSG -- Write a message to the world at large 2590 * 2591 * Write the specified message to either the entire 2592 * world, or a list of approved users. 2593 */ 2594 void 2595 wallmsg(struct filed *f, struct iovec *iov, size_t iovcnt) 2596 { 2597 #ifdef __NetBSD_Version__ 2598 static int reenter; /* avoid calling ourselves */ 2599 int i; 2600 char *p; 2601 struct utmpentry *ep; 2602 2603 if (reenter++) 2604 return; 2605 2606 (void)getutentries(NULL, &ep); 2607 /* NOSTRICT */ 2608 for (; ep; ep = ep->next) { 2609 if (f->f_type == F_WALL) { 2610 if ((p = ttymsg(iov, iovcnt, ep->line, TTYMSGTIME)) 2611 != NULL) { 2612 errno = 0; /* already in msg */ 2613 logerror("%s", p); 2614 } 2615 continue; 2616 } 2617 /* should we send the message to this user? */ 2618 for (i = 0; i < MAXUNAMES; i++) { 2619 if (!f->f_un.f_uname[i][0]) 2620 break; 2621 if (strcmp(f->f_un.f_uname[i], ep->name) == 0) { 2622 struct stat st; 2623 char tty[MAXPATHLEN]; 2624 snprintf(tty, sizeof(tty), "%s/%s", _PATH_DEV, 2625 ep->line); 2626 if (stat(tty, &st) != -1 && 2627 (st.st_mode & S_IWGRP) == 0) 2628 break; 2629 2630 if ((p = ttymsg(iov, iovcnt, ep->line, 2631 TTYMSGTIME)) != NULL) { 2632 errno = 0; /* already in msg */ 2633 logerror("%s", p); 2634 } 2635 break; 2636 } 2637 } 2638 } 2639 reenter = 0; 2640 #endif /* __NetBSD_Version__ */ 2641 } 2642 2643 void 2644 /*ARGSUSED*/ 2645 reapchild(int fd, short event, void *ev) 2646 { 2647 int status; 2648 pid_t pid; 2649 struct filed *f; 2650 2651 while ((pid = wait3(&status, WNOHANG, NULL)) > 0) { 2652 if (!Initialized || ShuttingDown) { 2653 /* 2654 * Be silent while we are initializing or 2655 * shutting down. 2656 */ 2657 continue; 2658 } 2659 2660 if (deadq_remove(pid)) 2661 continue; 2662 2663 /* Now, look in the list of active processes. */ 2664 for (f = Files; f != NULL; f = f->f_next) { 2665 if (f->f_type == F_PIPE && 2666 f->f_un.f_pipe.f_pid == pid) { 2667 (void) close(f->f_file); 2668 f->f_un.f_pipe.f_pid = 0; 2669 log_deadchild(pid, status, 2670 f->f_un.f_pipe.f_pname); 2671 break; 2672 } 2673 } 2674 } 2675 } 2676 2677 /* 2678 * Return a printable representation of a host address (FQDN if available) 2679 */ 2680 const char * 2681 cvthname(struct sockaddr_storage *f) 2682 { 2683 int error; 2684 int niflag = NI_DGRAM; 2685 static char host[NI_MAXHOST], ip[NI_MAXHOST]; 2686 2687 error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len, 2688 ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag); 2689 2690 DPRINTF(D_CALL, "cvthname(%s)\n", ip); 2691 2692 if (error) { 2693 DPRINTF(D_NET, "Malformed from address %s\n", 2694 gai_strerror(error)); 2695 return "???"; 2696 } 2697 2698 if (!UseNameService) 2699 return ip; 2700 2701 error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len, 2702 host, sizeof host, NULL, 0, niflag); 2703 if (error) { 2704 DPRINTF(D_NET, "Host name for your address (%s) unknown\n", ip); 2705 return ip; 2706 } 2707 2708 return host; 2709 } 2710 2711 void 2712 trim_anydomain(char *host) 2713 { 2714 bool onlydigits = true; 2715 int i; 2716 2717 if (!BSDOutputFormat) 2718 return; 2719 2720 /* if non-digits found, then assume hostname and cut at first dot (this 2721 * case also covers IPv6 addresses which should not contain dots), 2722 * if only digits then assume IPv4 address and do not cut at all */ 2723 for (i = 0; host[i]; i++) { 2724 if (host[i] == '.' && !onlydigits) 2725 host[i] = '\0'; 2726 else if (!isdigit((unsigned char)host[i]) && host[i] != '.') 2727 onlydigits = false; 2728 } 2729 } 2730 2731 static void 2732 /*ARGSUSED*/ 2733 domark(int fd, short event, void *ev) 2734 { 2735 struct event *ev_pass = (struct event *)ev; 2736 struct filed *f; 2737 dq_t q, nextq; 2738 sigset_t newmask, omask; 2739 2740 schedule_event(&ev_pass, 2741 &((struct timeval){TIMERINTVL, 0}), 2742 domark, ev_pass); 2743 DPRINTF((D_CALL|D_EVENT), "domark()\n"); 2744 2745 BLOCK_SIGNALS(omask, newmask); 2746 now = time(NULL); 2747 MarkSeq += TIMERINTVL; 2748 if (MarkSeq >= MarkInterval) { 2749 logmsg_async(LOG_INFO, NULL, "-- MARK --", ADDDATE|MARK); 2750 MarkSeq = 0; 2751 } 2752 2753 for (f = Files; f; f = f->f_next) { 2754 if (f->f_prevcount && now >= REPEATTIME(f)) { 2755 DPRINTF(D_DATA, "Flush %s: repeated %d times, %d sec.\n", 2756 TypeInfo[f->f_type].name, f->f_prevcount, 2757 repeatinterval[f->f_repeatcount]); 2758 fprintlog(f, NULL, NULL); 2759 BACKOFF(f); 2760 } 2761 } 2762 message_allqueues_check(); 2763 RESTORE_SIGNALS(omask); 2764 2765 /* Walk the dead queue, and see if we should signal somebody. */ 2766 for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = nextq) { 2767 nextq = TAILQ_NEXT(q, dq_entries); 2768 switch (q->dq_timeout) { 2769 case 0: 2770 /* Already signalled once, try harder now. */ 2771 if (kill(q->dq_pid, SIGKILL) != 0) 2772 (void) deadq_remove(q->dq_pid); 2773 break; 2774 2775 case 1: 2776 /* 2777 * Timed out on the dead queue, send terminate 2778 * signal. Note that we leave the removal from 2779 * the dead queue to reapchild(), which will 2780 * also log the event (unless the process 2781 * didn't even really exist, in case we simply 2782 * drop it from the dead queue). 2783 */ 2784 if (kill(q->dq_pid, SIGTERM) != 0) { 2785 (void) deadq_remove(q->dq_pid); 2786 break; 2787 } 2788 /* FALLTHROUGH */ 2789 2790 default: 2791 q->dq_timeout--; 2792 } 2793 } 2794 #ifndef DISABLE_SIGN 2795 if (GlobalSign.rsid) { /* check if initialized */ 2796 struct signature_group_t *sg; 2797 STAILQ_FOREACH(sg, &GlobalSign.SigGroups, entries) { 2798 sign_send_certificate_block(sg); 2799 } 2800 } 2801 #endif /* !DISABLE_SIGN */ 2802 } 2803 2804 /* 2805 * Print syslogd errors some place. 2806 */ 2807 void 2808 logerror(const char *fmt, ...) 2809 { 2810 static int logerror_running; 2811 va_list ap; 2812 char tmpbuf[BUFSIZ]; 2813 char buf[BUFSIZ]; 2814 char *outbuf; 2815 2816 /* If there's an error while trying to log an error, give up. */ 2817 if (logerror_running) 2818 return; 2819 logerror_running = 1; 2820 2821 va_start(ap, fmt); 2822 (void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap); 2823 va_end(ap); 2824 2825 if (errno) { 2826 (void)snprintf(buf, sizeof(buf), "%s: %s", 2827 tmpbuf, strerror(errno)); 2828 outbuf = buf; 2829 } else { 2830 (void)snprintf(buf, sizeof(buf), "%s", tmpbuf); 2831 outbuf = tmpbuf; 2832 } 2833 2834 if (daemonized) 2835 logmsg_async(LOG_SYSLOG|LOG_ERR, NULL, outbuf, ADDDATE); 2836 if (!daemonized && Debug) 2837 DPRINTF(D_MISC, "%s\n", outbuf); 2838 if (!daemonized && !Debug) 2839 printf("%s\n", outbuf); 2840 2841 logerror_running = 0; 2842 } 2843 2844 /* 2845 * Print syslogd info some place. 2846 */ 2847 void 2848 loginfo(const char *fmt, ...) 2849 { 2850 va_list ap; 2851 char buf[BUFSIZ]; 2852 2853 va_start(ap, fmt); 2854 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2855 va_end(ap); 2856 2857 DPRINTF(D_MISC, "%s\n", buf); 2858 logmsg_async(LOG_SYSLOG|LOG_INFO, NULL, buf, ADDDATE); 2859 } 2860 2861 #ifndef DISABLE_TLS 2862 static inline void 2863 free_incoming_tls_sockets(void) 2864 { 2865 struct TLS_Incoming_Conn *tls_in; 2866 int i; 2867 2868 /* 2869 * close all listening and connected TLS sockets 2870 */ 2871 if (TLS_Listen_Set) 2872 for (i = 0; i < TLS_Listen_Set->fd; i++) { 2873 if (close(TLS_Listen_Set[i+1].fd) == -1) 2874 logerror("close() failed"); 2875 DEL_EVENT(TLS_Listen_Set[i+1].ev); 2876 FREEPTR(TLS_Listen_Set[i+1].ev); 2877 } 2878 FREEPTR(TLS_Listen_Set); 2879 /* close/free incoming TLS connections */ 2880 while (!SLIST_EMPTY(&TLS_Incoming_Head)) { 2881 tls_in = SLIST_FIRST(&TLS_Incoming_Head); 2882 SLIST_REMOVE_HEAD(&TLS_Incoming_Head, entries); 2883 FREEPTR(tls_in->inbuf); 2884 free_tls_conn(tls_in->tls_conn); 2885 free(tls_in); 2886 } 2887 } 2888 #endif /* !DISABLE_TLS */ 2889 2890 void 2891 /*ARGSUSED*/ 2892 die(int fd, short event, void *ev) 2893 { 2894 struct filed *f, *next; 2895 char **p; 2896 sigset_t newmask, omask; 2897 int i; 2898 size_t j; 2899 2900 ShuttingDown = 1; /* Don't log SIGCHLDs. */ 2901 /* prevent recursive signals */ 2902 BLOCK_SIGNALS(omask, newmask); 2903 2904 errno = 0; 2905 if (ev != NULL) 2906 logerror("Exiting on signal %d", fd); 2907 else 2908 logerror("Fatal error, exiting"); 2909 2910 /* 2911 * flush any pending output 2912 */ 2913 for (f = Files; f != NULL; f = f->f_next) { 2914 /* flush any pending output */ 2915 if (f->f_prevcount) 2916 fprintlog(f, NULL, NULL); 2917 SEND_QUEUE(f); 2918 } 2919 2920 #ifndef DISABLE_TLS 2921 free_incoming_tls_sockets(); 2922 #endif /* !DISABLE_TLS */ 2923 #ifndef DISABLE_SIGN 2924 sign_global_free(); 2925 #endif /* !DISABLE_SIGN */ 2926 2927 /* 2928 * Close all open log files. 2929 */ 2930 for (f = Files; f != NULL; f = next) { 2931 message_queue_freeall(f); 2932 2933 switch (f->f_type) { 2934 case F_FILE: 2935 case F_TTY: 2936 case F_CONSOLE: 2937 case F_FIFO: 2938 if (f->f_file >= 0) 2939 (void)close(f->f_file); 2940 break; 2941 case F_PIPE: 2942 if (f->f_un.f_pipe.f_pid > 0) { 2943 (void)close(f->f_file); 2944 } 2945 f->f_un.f_pipe.f_pid = 0; 2946 break; 2947 case F_FORW: 2948 if (f->f_un.f_forw.f_addr) 2949 freeaddrinfo(f->f_un.f_forw.f_addr); 2950 break; 2951 #ifndef DISABLE_TLS 2952 case F_TLS: 2953 free_tls_conn(f->f_un.f_tls.tls_conn); 2954 break; 2955 #endif /* !DISABLE_TLS */ 2956 } 2957 next = f->f_next; 2958 DELREF(f->f_prevmsg); 2959 FREEPTR(f->f_program); 2960 FREEPTR(f->f_host); 2961 DEL_EVENT(f->f_sq_event); 2962 free((char *)f); 2963 } 2964 2965 /* 2966 * Close all open UDP sockets 2967 */ 2968 if (finet) { 2969 for (i = 0; i < finet->fd; i++) { 2970 if (close(finet[i+1].fd) < 0) { 2971 logerror("close() failed"); 2972 die(0, 0, NULL); 2973 } 2974 DEL_EVENT(finet[i+1].ev); 2975 FREEPTR(finet[i+1].ev); 2976 } 2977 FREEPTR(finet); 2978 } 2979 2980 /* free config options */ 2981 for (j = 0; j < A_CNT(TypeInfo); j++) { 2982 FREEPTR(TypeInfo[j].queue_length_string); 2983 FREEPTR(TypeInfo[j].queue_size_string); 2984 } 2985 2986 #ifndef DISABLE_TLS 2987 FREEPTR(tls_opt.CAdir); 2988 FREEPTR(tls_opt.CAfile); 2989 FREEPTR(tls_opt.keyfile); 2990 FREEPTR(tls_opt.certfile); 2991 FREEPTR(tls_opt.x509verify); 2992 FREEPTR(tls_opt.bindhost); 2993 FREEPTR(tls_opt.bindport); 2994 FREEPTR(tls_opt.server); 2995 FREEPTR(tls_opt.gen_cert); 2996 free_cred_SLIST(&tls_opt.cert_head); 2997 free_cred_SLIST(&tls_opt.fprint_head); 2998 FREE_SSL_CTX(tls_opt.global_TLS_CTX); 2999 #endif /* !DISABLE_TLS */ 3000 3001 FREEPTR(funix); 3002 for (p = LogPaths; p && *p; p++) 3003 unlink(*p); 3004 exit(0); 3005 } 3006 3007 #ifndef DISABLE_SIGN 3008 /* 3009 * get one "sign_delim_sg2" item, convert and store in ordered queue 3010 */ 3011 void 3012 store_sign_delim_sg2(char *tmp_buf) 3013 { 3014 struct string_queue *sqentry, *sqe1, *sqe2; 3015 3016 if(!(sqentry = malloc(sizeof(*sqentry)))) { 3017 logerror("Unable to allocate memory"); 3018 return; 3019 } 3020 /*LINTED constcond/null effect */ 3021 assert(sizeof(int64_t) == sizeof(uint_fast64_t)); 3022 if (dehumanize_number(tmp_buf, (int64_t*) &(sqentry->key)) == -1 3023 || sqentry->key > (LOG_NFACILITIES<<3)) { 3024 DPRINTF(D_PARSE, "invalid sign_delim_sg2: %s\n", tmp_buf); 3025 free(sqentry); 3026 FREEPTR(tmp_buf); 3027 return; 3028 } 3029 sqentry->data = tmp_buf; 3030 3031 if (STAILQ_EMPTY(&GlobalSign.sig2_delims)) { 3032 STAILQ_INSERT_HEAD(&GlobalSign.sig2_delims, 3033 sqentry, entries); 3034 return; 3035 } 3036 3037 /* keep delimiters sorted */ 3038 sqe1 = sqe2 = STAILQ_FIRST(&GlobalSign.sig2_delims); 3039 if (sqe1->key > sqentry->key) { 3040 STAILQ_INSERT_HEAD(&GlobalSign.sig2_delims, 3041 sqentry, entries); 3042 return; 3043 } 3044 3045 while ((sqe1 = sqe2) 3046 && (sqe2 = STAILQ_NEXT(sqe1, entries))) { 3047 if (sqe2->key > sqentry->key) { 3048 break; 3049 } else if (sqe2->key == sqentry->key) { 3050 DPRINTF(D_PARSE, "duplicate sign_delim_sg2: %s\n", 3051 tmp_buf); 3052 FREEPTR(sqentry); 3053 FREEPTR(tmp_buf); 3054 return; 3055 } 3056 } 3057 STAILQ_INSERT_AFTER(&GlobalSign.sig2_delims, sqe1, sqentry, entries); 3058 } 3059 #endif /* !DISABLE_SIGN */ 3060 3061 /* 3062 * read syslog.conf 3063 */ 3064 void 3065 read_config_file(FILE *cf, struct filed **f_ptr) 3066 { 3067 size_t linenum = 0; 3068 size_t i; 3069 struct filed *f, **nextp; 3070 char cline[LINE_MAX]; 3071 char prog[NAME_MAX + 1]; 3072 char host[MAXHOSTNAMELEN]; 3073 const char *p; 3074 char *q; 3075 bool found_keyword; 3076 #ifndef DISABLE_TLS 3077 struct peer_cred *cred = NULL; 3078 struct peer_cred_head *credhead = NULL; 3079 #endif /* !DISABLE_TLS */ 3080 #ifndef DISABLE_SIGN 3081 char *sign_sg_str = NULL; 3082 #endif /* !DISABLE_SIGN */ 3083 #if (!defined(DISABLE_TLS) || !defined(DISABLE_SIGN)) 3084 char *tmp_buf = NULL; 3085 #endif /* (!defined(DISABLE_TLS) || !defined(DISABLE_SIGN)) */ 3086 /* central list of recognized configuration keywords 3087 * and an address for their values as strings */ 3088 const struct config_keywords { 3089 const char *keyword; 3090 char **variable; 3091 } config_keywords[] = { 3092 #ifndef DISABLE_TLS 3093 /* TLS settings */ 3094 {"tls_ca", &tls_opt.CAfile}, 3095 {"tls_cadir", &tls_opt.CAdir}, 3096 {"tls_cert", &tls_opt.certfile}, 3097 {"tls_key", &tls_opt.keyfile}, 3098 {"tls_verify", &tls_opt.x509verify}, 3099 {"tls_bindport", &tls_opt.bindport}, 3100 {"tls_bindhost", &tls_opt.bindhost}, 3101 {"tls_server", &tls_opt.server}, 3102 {"tls_gen_cert", &tls_opt.gen_cert}, 3103 /* special cases in parsing */ 3104 {"tls_allow_fingerprints",&tmp_buf}, 3105 {"tls_allow_clientcerts", &tmp_buf}, 3106 /* buffer settings */ 3107 {"tls_queue_length", &TypeInfo[F_TLS].queue_length_string}, 3108 {"tls_queue_size", &TypeInfo[F_TLS].queue_size_string}, 3109 #endif /* !DISABLE_TLS */ 3110 {"file_queue_length", &TypeInfo[F_FILE].queue_length_string}, 3111 {"pipe_queue_length", &TypeInfo[F_PIPE].queue_length_string}, 3112 {"fifo_queue_length", &TypeInfo[F_FIFO].queue_length_string}, 3113 {"file_queue_size", &TypeInfo[F_FILE].queue_size_string}, 3114 {"pipe_queue_size", &TypeInfo[F_PIPE].queue_size_string}, 3115 {"fifo_queue_size", &TypeInfo[F_FIFO].queue_size_string}, 3116 #ifndef DISABLE_SIGN 3117 /* syslog-sign setting */ 3118 {"sign_sg", &sign_sg_str}, 3119 /* also special case in parsing */ 3120 {"sign_delim_sg2", &tmp_buf}, 3121 #endif /* !DISABLE_SIGN */ 3122 }; 3123 3124 DPRINTF(D_CALL, "read_config_file()\n"); 3125 3126 /* free all previous config options */ 3127 for (i = 0; i < A_CNT(TypeInfo); i++) { 3128 if (TypeInfo[i].queue_length_string 3129 && TypeInfo[i].queue_length_string 3130 != TypeInfo[i].default_length_string) { 3131 FREEPTR(TypeInfo[i].queue_length_string); 3132 TypeInfo[i].queue_length_string = 3133 strdup(TypeInfo[i].default_length_string); 3134 } 3135 if (TypeInfo[i].queue_size_string 3136 && TypeInfo[i].queue_size_string 3137 != TypeInfo[i].default_size_string) { 3138 FREEPTR(TypeInfo[i].queue_size_string); 3139 TypeInfo[i].queue_size_string = 3140 strdup(TypeInfo[i].default_size_string); 3141 } 3142 } 3143 for (i = 0; i < A_CNT(config_keywords); i++) 3144 FREEPTR(*config_keywords[i].variable); 3145 /* 3146 * global settings 3147 */ 3148 while (fgets(cline, sizeof(cline), cf) != NULL) { 3149 linenum++; 3150 for (p = cline; isspace((unsigned char)*p); ++p) 3151 continue; 3152 if ((*p == '\0') || (*p == '#')) 3153 continue; 3154 3155 for (i = 0; i < A_CNT(config_keywords); i++) { 3156 if (copy_config_value(config_keywords[i].keyword, 3157 config_keywords[i].variable, &p, ConfFile, 3158 linenum)) { 3159 DPRINTF((D_PARSE|D_MEM), 3160 "found option %s, saved @%p\n", 3161 config_keywords[i].keyword, 3162 *config_keywords[i].variable); 3163 #ifndef DISABLE_SIGN 3164 if (!strcmp("sign_delim_sg2", 3165 config_keywords[i].keyword)) 3166 do { 3167 store_sign_delim_sg2(tmp_buf); 3168 } while (copy_config_value_word( 3169 &tmp_buf, &p)); 3170 3171 #endif /* !DISABLE_SIGN */ 3172 3173 #ifndef DISABLE_TLS 3174 /* special cases with multiple parameters */ 3175 if (!strcmp("tls_allow_fingerprints", 3176 config_keywords[i].keyword)) 3177 credhead = &tls_opt.fprint_head; 3178 else if (!strcmp("tls_allow_clientcerts", 3179 config_keywords[i].keyword)) 3180 credhead = &tls_opt.cert_head; 3181 3182 if (credhead) do { 3183 if(!(cred = malloc(sizeof(*cred)))) { 3184 logerror("Unable to " 3185 "allocate memory"); 3186 break; 3187 } 3188 cred->data = tmp_buf; 3189 tmp_buf = NULL; 3190 SLIST_INSERT_HEAD(credhead, 3191 cred, entries); 3192 } while /* additional values? */ 3193 (copy_config_value_word(&tmp_buf, &p)); 3194 credhead = NULL; 3195 break; 3196 #endif /* !DISABLE_TLS */ 3197 } 3198 } 3199 } 3200 /* convert strings to integer values */ 3201 for (i = 0; i < A_CNT(TypeInfo); i++) { 3202 if (!TypeInfo[i].queue_length_string 3203 || dehumanize_number(TypeInfo[i].queue_length_string, 3204 &TypeInfo[i].queue_length) == -1) 3205 if (dehumanize_number(TypeInfo[i].default_length_string, 3206 &TypeInfo[i].queue_length) == -1) 3207 abort(); 3208 if (!TypeInfo[i].queue_size_string 3209 || dehumanize_number(TypeInfo[i].queue_size_string, 3210 &TypeInfo[i].queue_size) == -1) 3211 if (dehumanize_number(TypeInfo[i].default_size_string, 3212 &TypeInfo[i].queue_size) == -1) 3213 abort(); 3214 } 3215 3216 #ifndef DISABLE_SIGN 3217 if (sign_sg_str) { 3218 if (sign_sg_str[1] == '\0' 3219 && (sign_sg_str[0] == '0' || sign_sg_str[0] == '1' 3220 || sign_sg_str[0] == '2' || sign_sg_str[0] == '3')) 3221 GlobalSign.sg = sign_sg_str[0] - '0'; 3222 else { 3223 GlobalSign.sg = SIGN_SG; 3224 DPRINTF(D_MISC, "Invalid sign_sg value `%s', " 3225 "use default value `%d'\n", 3226 sign_sg_str, GlobalSign.sg); 3227 } 3228 } else /* disable syslog-sign */ 3229 GlobalSign.sg = -1; 3230 #endif /* !DISABLE_SIGN */ 3231 3232 rewind(cf); 3233 linenum = 0; 3234 /* 3235 * Foreach line in the conf table, open that file. 3236 */ 3237 f = NULL; 3238 nextp = &f; 3239 3240 strcpy(prog, "*"); 3241 strcpy(host, "*"); 3242 while (fgets(cline, sizeof(cline), cf) != NULL) { 3243 linenum++; 3244 found_keyword = false; 3245 /* 3246 * check for end-of-section, comments, strip off trailing 3247 * spaces and newline character. #!prog is treated specially: 3248 * following lines apply only to that program. 3249 */ 3250 for (p = cline; isspace((unsigned char)*p); ++p) 3251 continue; 3252 if (*p == '\0') 3253 continue; 3254 if (*p == '#') { 3255 p++; 3256 if (*p != '!' && *p != '+' && *p != '-') 3257 continue; 3258 } 3259 3260 for (i = 0; i < A_CNT(config_keywords); i++) { 3261 if (!strncasecmp(p, config_keywords[i].keyword, 3262 strlen(config_keywords[i].keyword))) { 3263 DPRINTF(D_PARSE, 3264 "skip cline %zu with keyword %s\n", 3265 linenum, config_keywords[i].keyword); 3266 found_keyword = true; 3267 } 3268 } 3269 if (found_keyword) 3270 continue; 3271 3272 if (*p == '+' || *p == '-') { 3273 host[0] = *p++; 3274 while (isspace((unsigned char)*p)) 3275 p++; 3276 if (*p == '\0' || *p == '*') { 3277 strcpy(host, "*"); 3278 continue; 3279 } 3280 /* the +hostname expression will continue 3281 * to use the LocalHostName, not the FQDN */ 3282 for (i = 1; i < MAXHOSTNAMELEN - 1; i++) { 3283 if (*p == '@') { 3284 (void)strncpy(&host[i], LocalHostName, 3285 sizeof(host) - 1 - i); 3286 host[sizeof(host) - 1] = '\0'; 3287 i = strlen(host) - 1; 3288 p++; 3289 continue; 3290 } 3291 if (!isalnum((unsigned char)*p) && 3292 *p != '.' && *p != '-' && *p != ',') 3293 break; 3294 host[i] = *p++; 3295 } 3296 host[i] = '\0'; 3297 continue; 3298 } 3299 if (*p == '!') { 3300 p++; 3301 while (isspace((unsigned char)*p)) 3302 p++; 3303 if (*p == '\0' || *p == '*') { 3304 strcpy(prog, "*"); 3305 continue; 3306 } 3307 for (i = 0; i < NAME_MAX; i++) { 3308 if (!isprint((unsigned char)p[i])) 3309 break; 3310 prog[i] = p[i]; 3311 } 3312 prog[i] = '\0'; 3313 continue; 3314 } 3315 for (q = strchr(cline, '\0'); isspace((unsigned char)*--q);) 3316 continue; 3317 *++q = '\0'; 3318 if ((f = calloc(1, sizeof(*f))) == NULL) { 3319 logerror("alloc failed"); 3320 die(0, 0, NULL); 3321 } 3322 if (!*f_ptr) *f_ptr = f; /* return first node */ 3323 *nextp = f; 3324 nextp = &f->f_next; 3325 cfline(linenum, cline, f, prog, host); 3326 } 3327 } 3328 3329 /* 3330 * INIT -- Initialize syslogd from configuration table 3331 */ 3332 void 3333 /*ARGSUSED*/ 3334 init(int fd, short event, void *ev) 3335 { 3336 FILE *cf; 3337 int i; 3338 struct filed *f, *newf, **nextp, *f2; 3339 char *p; 3340 sigset_t newmask, omask; 3341 #ifndef DISABLE_TLS 3342 char *tls_status_msg = NULL; 3343 struct peer_cred *cred = NULL; 3344 #endif /* !DISABLE_TLS */ 3345 3346 /* prevent recursive signals */ 3347 BLOCK_SIGNALS(omask, newmask); 3348 3349 DPRINTF((D_EVENT|D_CALL), "init\n"); 3350 3351 /* 3352 * be careful about dependencies and order of actions: 3353 * 1. flush buffer queues 3354 * 2. flush -sign SBs 3355 * 3. flush/delete buffer queue again, in case an SB got there 3356 * 4. close files/connections 3357 */ 3358 3359 /* 3360 * flush any pending output 3361 */ 3362 for (f = Files; f != NULL; f = f->f_next) { 3363 /* flush any pending output */ 3364 if (f->f_prevcount) 3365 fprintlog(f, NULL, NULL); 3366 SEND_QUEUE(f); 3367 } 3368 /* some actions only on SIGHUP and not on first start */ 3369 if (Initialized) { 3370 #ifndef DISABLE_SIGN 3371 sign_global_free(); 3372 #endif /* !DISABLE_SIGN */ 3373 #ifndef DISABLE_TLS 3374 free_incoming_tls_sockets(); 3375 #endif /* !DISABLE_TLS */ 3376 Initialized = 0; 3377 } 3378 /* 3379 * Close all open log files. 3380 */ 3381 for (f = Files; f != NULL; f = f->f_next) { 3382 switch (f->f_type) { 3383 case F_FILE: 3384 case F_TTY: 3385 case F_CONSOLE: 3386 (void)close(f->f_file); 3387 break; 3388 case F_PIPE: 3389 if (f->f_un.f_pipe.f_pid > 0) { 3390 (void)close(f->f_file); 3391 deadq_enter(f->f_un.f_pipe.f_pid, 3392 f->f_un.f_pipe.f_pname); 3393 } 3394 f->f_un.f_pipe.f_pid = 0; 3395 break; 3396 case F_FORW: 3397 if (f->f_un.f_forw.f_addr) 3398 freeaddrinfo(f->f_un.f_forw.f_addr); 3399 break; 3400 #ifndef DISABLE_TLS 3401 case F_TLS: 3402 free_tls_sslptr(f->f_un.f_tls.tls_conn); 3403 break; 3404 #endif /* !DISABLE_TLS */ 3405 } 3406 } 3407 3408 /* 3409 * Close all open UDP sockets 3410 */ 3411 if (finet) { 3412 for (i = 0; i < finet->fd; i++) { 3413 if (close(finet[i+1].fd) < 0) { 3414 logerror("close() failed"); 3415 die(0, 0, NULL); 3416 } 3417 DEL_EVENT(finet[i+1].ev); 3418 FREEPTR(finet[i+1].ev); 3419 } 3420 FREEPTR(finet); 3421 } 3422 3423 /* get FQDN and hostname/domain */ 3424 FREEPTR(oldLocalFQDN); 3425 oldLocalFQDN = LocalFQDN; 3426 LocalFQDN = getLocalFQDN(); 3427 if ((p = strchr(LocalFQDN, '.')) != NULL) 3428 (void)strlcpy(LocalHostName, LocalFQDN, 1+p-LocalFQDN); 3429 else 3430 (void)strlcpy(LocalHostName, LocalFQDN, sizeof(LocalHostName)); 3431 3432 /* 3433 * Reset counter of forwarding actions 3434 */ 3435 3436 NumForwards=0; 3437 3438 /* new destination list to replace Files */ 3439 newf = NULL; 3440 nextp = &newf; 3441 3442 /* open the configuration file */ 3443 if ((cf = fopen(ConfFile, "r")) == NULL) { 3444 DPRINTF(D_FILE, "Cannot open `%s'\n", ConfFile); 3445 *nextp = (struct filed *)calloc(1, sizeof(*f)); 3446 cfline(0, "*.ERR\t/dev/console", *nextp, "*", "*"); 3447 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); 3448 cfline(0, "*.PANIC\t*", (*nextp)->f_next, "*", "*"); 3449 Initialized = 1; 3450 RESTORE_SIGNALS(omask); 3451 return; 3452 } 3453 3454 #ifndef DISABLE_TLS 3455 /* init with new TLS_CTX 3456 * as far as I see one cannot change the cert/key of an existing CTX 3457 */ 3458 FREE_SSL_CTX(tls_opt.global_TLS_CTX); 3459 3460 free_cred_SLIST(&tls_opt.cert_head); 3461 free_cred_SLIST(&tls_opt.fprint_head); 3462 #endif /* !DISABLE_TLS */ 3463 3464 /* read and close configuration file */ 3465 read_config_file(cf, &newf); 3466 newf = *nextp; 3467 (void)fclose(cf); 3468 DPRINTF(D_MISC, "read_config_file() returned newf=%p\n", newf); 3469 3470 #define MOVE_QUEUE(dst, src) do { \ 3471 struct buf_queue *buf; \ 3472 STAILQ_CONCAT(&dst->f_qhead, &src->f_qhead); \ 3473 STAILQ_FOREACH(buf, &dst->f_qhead, entries) { \ 3474 dst->f_qelements++; \ 3475 dst->f_qsize += buf_queue_obj_size(buf); \ 3476 } \ 3477 src->f_qsize = 0; \ 3478 src->f_qelements = 0; \ 3479 } while (/*CONSTCOND*/0) 3480 3481 /* 3482 * Free old log files. 3483 */ 3484 for (f = Files; f != NULL;) { 3485 struct filed *ftmp; 3486 3487 /* check if a new logfile is equal, if so pass the queue */ 3488 for (f2 = newf; f2 != NULL; f2 = f2->f_next) { 3489 if (f->f_type == f2->f_type 3490 && ((f->f_type == F_PIPE 3491 && !strcmp(f->f_un.f_pipe.f_pname, 3492 f2->f_un.f_pipe.f_pname)) 3493 #ifndef DISABLE_TLS 3494 || (f->f_type == F_TLS 3495 && !strcmp(f->f_un.f_tls.tls_conn->hostname, 3496 f2->f_un.f_tls.tls_conn->hostname) 3497 && !strcmp(f->f_un.f_tls.tls_conn->port, 3498 f2->f_un.f_tls.tls_conn->port)) 3499 #endif /* !DISABLE_TLS */ 3500 || (f->f_type == F_FORW 3501 && !strcmp(f->f_un.f_forw.f_hname, 3502 f2->f_un.f_forw.f_hname)))) { 3503 DPRINTF(D_BUFFER, "move queue from f@%p " 3504 "to f2@%p\n", f, f2); 3505 MOVE_QUEUE(f2, f); 3506 } 3507 } 3508 message_queue_freeall(f); 3509 DELREF(f->f_prevmsg); 3510 #ifndef DISABLE_TLS 3511 if (f->f_type == F_TLS) 3512 free_tls_conn(f->f_un.f_tls.tls_conn); 3513 #endif /* !DISABLE_TLS */ 3514 FREEPTR(f->f_program); 3515 FREEPTR(f->f_host); 3516 DEL_EVENT(f->f_sq_event); 3517 3518 ftmp = f->f_next; 3519 free((char *)f); 3520 f = ftmp; 3521 } 3522 Files = newf; 3523 Initialized = 1; 3524 3525 if (Debug) { 3526 for (f = Files; f; f = f->f_next) { 3527 for (i = 0; i <= LOG_NFACILITIES; i++) 3528 if (f->f_pmask[i] == INTERNAL_NOPRI) 3529 printf("X "); 3530 else 3531 printf("%d ", f->f_pmask[i]); 3532 printf("%s: ", TypeInfo[f->f_type].name); 3533 switch (f->f_type) { 3534 case F_FILE: 3535 case F_TTY: 3536 case F_CONSOLE: 3537 case F_FIFO: 3538 printf("%s", f->f_un.f_fname); 3539 break; 3540 3541 case F_FORW: 3542 printf("%s", f->f_un.f_forw.f_hname); 3543 break; 3544 #ifndef DISABLE_TLS 3545 case F_TLS: 3546 printf("[%s]", f->f_un.f_tls.tls_conn->hostname); 3547 break; 3548 #endif /* !DISABLE_TLS */ 3549 case F_PIPE: 3550 printf("%s", f->f_un.f_pipe.f_pname); 3551 break; 3552 3553 case F_USERS: 3554 for (i = 0; 3555 i < MAXUNAMES && *f->f_un.f_uname[i]; i++) 3556 printf("%s, ", f->f_un.f_uname[i]); 3557 break; 3558 } 3559 if (f->f_program != NULL) 3560 printf(" (%s)", f->f_program); 3561 printf("\n"); 3562 } 3563 } 3564 3565 finet = socksetup(PF_UNSPEC, bindhostname); 3566 if (finet) { 3567 if (SecureMode) { 3568 for (i = 0; i < finet->fd; i++) { 3569 if (shutdown(finet[i+1].fd, SHUT_RD) < 0) { 3570 logerror("shutdown() failed"); 3571 die(0, 0, NULL); 3572 } 3573 } 3574 } else 3575 DPRINTF(D_NET, "Listening on inet and/or inet6 socket\n"); 3576 DPRINTF(D_NET, "Sending on inet and/or inet6 socket\n"); 3577 } 3578 3579 #ifndef DISABLE_TLS 3580 /* TLS setup -- after all local destinations opened */ 3581 DPRINTF(D_PARSE, "Parsed options: tls_ca: %s, tls_cadir: %s, " 3582 "tls_cert: %s, tls_key: %s, tls_verify: %s, " 3583 "bind: %s:%s, max. queue_lengths: %" 3584 PRId64 ", %" PRId64 ", %" PRId64 ", " 3585 "max. queue_sizes: %" 3586 PRId64 ", %" PRId64 ", %" PRId64 "\n", 3587 tls_opt.CAfile, tls_opt.CAdir, 3588 tls_opt.certfile, tls_opt.keyfile, tls_opt.x509verify, 3589 tls_opt.bindhost, tls_opt.bindport, 3590 TypeInfo[F_TLS].queue_length, TypeInfo[F_FILE].queue_length, 3591 TypeInfo[F_PIPE].queue_length, 3592 TypeInfo[F_TLS].queue_size, TypeInfo[F_FILE].queue_size, 3593 TypeInfo[F_PIPE].queue_size); 3594 SLIST_FOREACH(cred, &tls_opt.cert_head, entries) { 3595 DPRINTF(D_PARSE, "Accepting peer certificate " 3596 "from file: \"%s\"\n", cred->data); 3597 } 3598 SLIST_FOREACH(cred, &tls_opt.fprint_head, entries) { 3599 DPRINTF(D_PARSE, "Accepting peer certificate with " 3600 "fingerprint: \"%s\"\n", cred->data); 3601 } 3602 3603 /* Note: The order of initialization is important because syslog-sign 3604 * should use the TLS cert for signing. -- So we check first if TLS 3605 * will be used and initialize it before starting -sign. 3606 * 3607 * This means that if we are a client without TLS destinations TLS 3608 * will not be initialized and syslog-sign will generate a new key. 3609 * -- Even if the user has set a usable tls_cert. 3610 * Is this the expected behaviour? The alternative would be to always 3611 * initialize the TLS structures, even if they will not be needed 3612 * (or only needed to read the DSA key for -sign). 3613 */ 3614 3615 /* Initialize TLS only if used */ 3616 if (tls_opt.server) 3617 tls_status_msg = init_global_TLS_CTX(); 3618 else 3619 for (f = Files; f; f = f->f_next) { 3620 if (f->f_type != F_TLS) 3621 continue; 3622 tls_status_msg = init_global_TLS_CTX(); 3623 break; 3624 } 3625 3626 #endif /* !DISABLE_TLS */ 3627 3628 #ifndef DISABLE_SIGN 3629 /* only initialize -sign if actually used */ 3630 if (GlobalSign.sg == 0 || GlobalSign.sg == 1 || GlobalSign.sg == 2) 3631 (void)sign_global_init(Files); 3632 else if (GlobalSign.sg == 3) 3633 for (f = Files; f; f = f->f_next) 3634 if (f->f_flags & FFLAG_SIGN) { 3635 (void)sign_global_init(Files); 3636 break; 3637 } 3638 #endif /* !DISABLE_SIGN */ 3639 3640 #ifndef DISABLE_TLS 3641 if (tls_status_msg) { 3642 loginfo("%s", tls_status_msg); 3643 free(tls_status_msg); 3644 } 3645 DPRINTF((D_NET|D_TLS), "Preparing sockets for TLS\n"); 3646 TLS_Listen_Set = 3647 socksetup_tls(PF_UNSPEC, tls_opt.bindhost, tls_opt.bindport); 3648 3649 for (f = Files; f; f = f->f_next) { 3650 if (f->f_type != F_TLS) 3651 continue; 3652 if (!tls_connect(f->f_un.f_tls.tls_conn)) { 3653 logerror("Unable to connect to TLS server %s", 3654 f->f_un.f_tls.tls_conn->hostname); 3655 /* Reconnect after x seconds */ 3656 schedule_event(&f->f_un.f_tls.tls_conn->event, 3657 &((struct timeval){TLS_RECONNECT_SEC, 0}), 3658 tls_reconnect, f->f_un.f_tls.tls_conn); 3659 } 3660 } 3661 #endif /* !DISABLE_TLS */ 3662 3663 loginfo("restart"); 3664 /* 3665 * Log a change in hostname, but only on a restart (we detect this 3666 * by checking to see if we're passed a kevent). 3667 */ 3668 if (oldLocalFQDN && strcmp(oldLocalFQDN, LocalFQDN) != 0) 3669 loginfo("host name changed, \"%s\" to \"%s\"", 3670 oldLocalFQDN, LocalFQDN); 3671 3672 RESTORE_SIGNALS(omask); 3673 } 3674 3675 /* 3676 * Crack a configuration file line 3677 */ 3678 void 3679 cfline(size_t linenum, const char *line, struct filed *f, const char *prog, 3680 const char *host) 3681 { 3682 struct addrinfo hints, *res; 3683 int error, i, pri, syncfile; 3684 const char *p, *q; 3685 char *bp; 3686 char buf[MAXLINE]; 3687 struct stat sb; 3688 3689 DPRINTF((D_CALL|D_PARSE), 3690 "cfline(%zu, \"%s\", f, \"%s\", \"%s\")\n", 3691 linenum, line, prog, host); 3692 3693 errno = 0; /* keep strerror() stuff out of logerror messages */ 3694 3695 /* clear out file entry */ 3696 memset(f, 0, sizeof(*f)); 3697 for (i = 0; i <= LOG_NFACILITIES; i++) 3698 f->f_pmask[i] = INTERNAL_NOPRI; 3699 STAILQ_INIT(&f->f_qhead); 3700 3701 /* 3702 * There should not be any space before the log facility. 3703 * Check this is okay, complain and fix if it is not. 3704 */ 3705 q = line; 3706 if (isblank((unsigned char)*line)) { 3707 errno = 0; 3708 logerror("Warning: `%s' space or tab before the log facility", 3709 line); 3710 /* Fix: strip all spaces/tabs before the log facility */ 3711 while (*q++ && isblank((unsigned char)*q)) 3712 /* skip blanks */; 3713 line = q; 3714 } 3715 3716 /* 3717 * q is now at the first char of the log facility 3718 * There should be at least one tab after the log facility 3719 * Check this is okay, and complain and fix if it is not. 3720 */ 3721 q = line + strlen(line); 3722 while (!isblank((unsigned char)*q) && (q != line)) 3723 q--; 3724 if ((q == line) && strlen(line)) { 3725 /* No tabs or space in a non empty line: complain */ 3726 errno = 0; 3727 logerror( 3728 "Error: `%s' log facility or log target missing", 3729 line); 3730 return; 3731 } 3732 3733 /* save host name, if any */ 3734 if (*host == '*') 3735 f->f_host = NULL; 3736 else { 3737 f->f_host = strdup(host); 3738 trim_anydomain(&f->f_host[1]); /* skip +/- at beginning */ 3739 } 3740 3741 /* save program name, if any */ 3742 if (*prog == '*') 3743 f->f_program = NULL; 3744 else 3745 f->f_program = strdup(prog); 3746 3747 /* scan through the list of selectors */ 3748 for (p = line; *p && !isblank((unsigned char)*p);) { 3749 int pri_done, pri_cmp, pri_invert; 3750 3751 /* find the end of this facility name list */ 3752 for (q = p; *q && !isblank((unsigned char)*q) && *q++ != '.'; ) 3753 continue; 3754 3755 /* get the priority comparison */ 3756 pri_cmp = 0; 3757 pri_done = 0; 3758 pri_invert = 0; 3759 if (*q == '!') { 3760 pri_invert = 1; 3761 q++; 3762 } 3763 while (! pri_done) { 3764 switch (*q) { 3765 case '<': 3766 pri_cmp = PRI_LT; 3767 q++; 3768 break; 3769 case '=': 3770 pri_cmp = PRI_EQ; 3771 q++; 3772 break; 3773 case '>': 3774 pri_cmp = PRI_GT; 3775 q++; 3776 break; 3777 default: 3778 pri_done = 1; 3779 break; 3780 } 3781 } 3782 3783 /* collect priority name */ 3784 for (bp = buf; *q && !strchr("\t ,;", *q); ) 3785 *bp++ = *q++; 3786 *bp = '\0'; 3787 3788 /* skip cruft */ 3789 while (strchr(",;", *q)) 3790 q++; 3791 3792 /* decode priority name */ 3793 if (*buf == '*') { 3794 pri = LOG_PRIMASK + 1; 3795 pri_cmp = PRI_LT | PRI_EQ | PRI_GT; 3796 } else { 3797 pri = decode(buf, prioritynames); 3798 if (pri < 0) { 3799 errno = 0; 3800 logerror("Unknown priority name `%s'", buf); 3801 return; 3802 } 3803 } 3804 if (pri_cmp == 0) 3805 pri_cmp = UniquePriority ? PRI_EQ 3806 : PRI_EQ | PRI_GT; 3807 if (pri_invert) 3808 pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT; 3809 3810 /* scan facilities */ 3811 while (*p && !strchr("\t .;", *p)) { 3812 for (bp = buf; *p && !strchr("\t ,;.", *p); ) 3813 *bp++ = *p++; 3814 *bp = '\0'; 3815 if (*buf == '*') 3816 for (i = 0; i < LOG_NFACILITIES; i++) { 3817 f->f_pmask[i] = pri; 3818 f->f_pcmp[i] = pri_cmp; 3819 } 3820 else { 3821 i = decode(buf, facilitynames); 3822 if (i < 0) { 3823 errno = 0; 3824 logerror("Unknown facility name `%s'", 3825 buf); 3826 return; 3827 } 3828 f->f_pmask[i >> 3] = pri; 3829 f->f_pcmp[i >> 3] = pri_cmp; 3830 } 3831 while (*p == ',' || *p == ' ') 3832 p++; 3833 } 3834 3835 p = q; 3836 } 3837 3838 /* skip to action part */ 3839 while (isblank((unsigned char)*p)) 3840 p++; 3841 3842 /* 3843 * should this be "#ifndef DISABLE_SIGN" or is it a general option? 3844 * '+' before file destination: write with PRI field for later 3845 * verification 3846 */ 3847 if (*p == '+') { 3848 f->f_flags |= FFLAG_FULL; 3849 p++; 3850 } 3851 if (*p == '-') { 3852 syncfile = 0; 3853 p++; 3854 } else 3855 syncfile = 1; 3856 3857 switch (*p) { 3858 case '@': 3859 #ifndef DISABLE_SIGN 3860 if (GlobalSign.sg == 3) 3861 f->f_flags |= FFLAG_SIGN; 3862 #endif /* !DISABLE_SIGN */ 3863 #ifndef DISABLE_TLS 3864 if (*(p+1) == '[') { 3865 /* TLS destination */ 3866 if (!parse_tls_destination(p, f, linenum)) { 3867 logerror("Unable to parse action %s", p); 3868 break; 3869 } 3870 f->f_type = F_TLS; 3871 break; 3872 } 3873 #endif /* !DISABLE_TLS */ 3874 (void)strlcpy(f->f_un.f_forw.f_hname, ++p, 3875 sizeof(f->f_un.f_forw.f_hname)); 3876 memset(&hints, 0, sizeof(hints)); 3877 hints.ai_family = AF_UNSPEC; 3878 hints.ai_socktype = SOCK_DGRAM; 3879 hints.ai_protocol = 0; 3880 error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, 3881 &res); 3882 if (error) { 3883 errno = 0; 3884 logerror("%s", gai_strerror(error)); 3885 break; 3886 } 3887 f->f_un.f_forw.f_addr = res; 3888 f->f_type = F_FORW; 3889 NumForwards++; 3890 break; 3891 3892 case '/': 3893 #ifndef DISABLE_SIGN 3894 if (GlobalSign.sg == 3) 3895 f->f_flags |= FFLAG_SIGN; 3896 #endif /* !DISABLE_SIGN */ 3897 (void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname)); 3898 if ((f->f_file = open(p, O_WRONLY|O_APPEND|O_NONBLOCK, 0)) < 0) 3899 { 3900 f->f_type = F_UNUSED; 3901 logerror("%s", p); 3902 break; 3903 } 3904 if (!fstat(f->f_file, &sb) && S_ISFIFO(sb.st_mode)) { 3905 f->f_file = -1; 3906 f->f_type = F_FIFO; 3907 break; 3908 } 3909 3910 if (isatty(f->f_file)) { 3911 f->f_type = F_TTY; 3912 if (strcmp(p, ctty) == 0) 3913 f->f_type = F_CONSOLE; 3914 } else 3915 f->f_type = F_FILE; 3916 3917 if (syncfile) 3918 f->f_flags |= FFLAG_SYNC; 3919 break; 3920 3921 case '|': 3922 #ifndef DISABLE_SIGN 3923 if (GlobalSign.sg == 3) 3924 f->f_flags |= FFLAG_SIGN; 3925 #endif 3926 f->f_un.f_pipe.f_pid = 0; 3927 (void) strlcpy(f->f_un.f_pipe.f_pname, p + 1, 3928 sizeof(f->f_un.f_pipe.f_pname)); 3929 f->f_type = F_PIPE; 3930 break; 3931 3932 case '*': 3933 f->f_type = F_WALL; 3934 break; 3935 3936 default: 3937 for (i = 0; i < MAXUNAMES && *p; i++) { 3938 for (q = p; *q && *q != ','; ) 3939 q++; 3940 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); 3941 if ((q - p) > UT_NAMESIZE) 3942 f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; 3943 else 3944 f->f_un.f_uname[i][q - p] = '\0'; 3945 while (*q == ',' || *q == ' ') 3946 q++; 3947 p = q; 3948 } 3949 f->f_type = F_USERS; 3950 break; 3951 } 3952 } 3953 3954 3955 /* 3956 * Decode a symbolic name to a numeric value 3957 */ 3958 int 3959 decode(const char *name, CODE *codetab) 3960 { 3961 CODE *c; 3962 char *p, buf[40]; 3963 3964 if (isdigit((unsigned char)*name)) 3965 return atoi(name); 3966 3967 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 3968 if (isupper((unsigned char)*name)) 3969 *p = tolower((unsigned char)*name); 3970 else 3971 *p = *name; 3972 } 3973 *p = '\0'; 3974 for (c = codetab; c->c_name; c++) 3975 if (!strcmp(buf, c->c_name)) 3976 return c->c_val; 3977 3978 return -1; 3979 } 3980 3981 /* 3982 * Retrieve the size of the kernel message buffer, via sysctl. 3983 */ 3984 int 3985 getmsgbufsize(void) 3986 { 3987 #ifdef __NetBSD_Version__ 3988 int msgbufsize, mib[2]; 3989 size_t size; 3990 3991 mib[0] = CTL_KERN; 3992 mib[1] = KERN_MSGBUFSIZE; 3993 size = sizeof msgbufsize; 3994 if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) { 3995 DPRINTF(D_MISC, "Couldn't get kern.msgbufsize\n"); 3996 return 0; 3997 } 3998 return msgbufsize; 3999 #else 4000 return MAXLINE; 4001 #endif /* __NetBSD_Version__ */ 4002 } 4003 4004 /* 4005 * Retrieve the hostname, via sysctl. 4006 */ 4007 char * 4008 getLocalFQDN(void) 4009 { 4010 int mib[2]; 4011 char *hostname; 4012 size_t len; 4013 4014 mib[0] = CTL_KERN; 4015 mib[1] = KERN_HOSTNAME; 4016 sysctl(mib, 2, NULL, &len, NULL, 0); 4017 4018 if (!(hostname = malloc(len))) { 4019 logerror("Unable to allocate memory"); 4020 die(0,0,NULL); 4021 } else if (sysctl(mib, 2, hostname, &len, NULL, 0) == -1) { 4022 DPRINTF(D_MISC, "Couldn't get kern.hostname\n"); 4023 (void)gethostname(hostname, sizeof(len)); 4024 } 4025 return hostname; 4026 } 4027 4028 struct socketEvent * 4029 socksetup(int af, const char *hostname) 4030 { 4031 struct addrinfo hints, *res, *r; 4032 int error, maxs; 4033 int on = 1; 4034 struct socketEvent *s, *socks; 4035 4036 if(SecureMode && !NumForwards) 4037 return NULL; 4038 4039 memset(&hints, 0, sizeof(hints)); 4040 hints.ai_flags = AI_PASSIVE; 4041 hints.ai_family = af; 4042 hints.ai_socktype = SOCK_DGRAM; 4043 error = getaddrinfo(hostname, "syslog", &hints, &res); 4044 if (error) { 4045 errno = 0; 4046 logerror("%s", gai_strerror(error)); 4047 die(0, 0, NULL); 4048 } 4049 4050 /* Count max number of sockets we may open */ 4051 for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 4052 continue; 4053 socks = calloc(maxs+1, sizeof(*socks)); 4054 if (!socks) { 4055 logerror("Couldn't allocate memory for sockets"); 4056 die(0, 0, NULL); 4057 } 4058 4059 socks->fd = 0; /* num of sockets counter at start of array */ 4060 s = socks + 1; 4061 for (r = res; r; r = r->ai_next) { 4062 s->fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 4063 if (s->fd < 0) { 4064 logerror("socket() failed"); 4065 continue; 4066 } 4067 s->af = r->ai_family; 4068 if (r->ai_family == AF_INET6 && setsockopt(s->fd, IPPROTO_IPV6, 4069 IPV6_V6ONLY, &on, sizeof(on)) < 0) { 4070 logerror("setsockopt(IPV6_V6ONLY) failed"); 4071 close(s->fd); 4072 continue; 4073 } 4074 4075 if (!SecureMode) { 4076 if (bind(s->fd, r->ai_addr, r->ai_addrlen) < 0) { 4077 logerror("bind() failed"); 4078 close(s->fd); 4079 continue; 4080 } 4081 s->ev = allocev(); 4082 event_set(s->ev, s->fd, EV_READ | EV_PERSIST, 4083 dispatch_read_finet, s->ev); 4084 if (event_add(s->ev, NULL) == -1) { 4085 DPRINTF((D_EVENT|D_NET), 4086 "Failure in event_add()\n"); 4087 } else { 4088 DPRINTF((D_EVENT|D_NET), 4089 "Listen on UDP port " 4090 "(event@%p)\n", s->ev); 4091 } 4092 } 4093 4094 socks->fd++; /* num counter */ 4095 s++; 4096 } 4097 4098 if (res) 4099 freeaddrinfo(res); 4100 if (socks->fd == 0) { 4101 free (socks); 4102 if(Debug) 4103 return NULL; 4104 else 4105 die(0, 0, NULL); 4106 } 4107 return socks; 4108 } 4109 4110 /* 4111 * Fairly similar to popen(3), but returns an open descriptor, as opposed 4112 * to a FILE *. 4113 */ 4114 int 4115 p_open(char *prog, pid_t *rpid) 4116 { 4117 static char sh[] = "sh", mc[] = "-c"; 4118 int pfd[2], nulldesc, i; 4119 pid_t pid; 4120 char *argv[4]; /* sh -c cmd NULL */ 4121 4122 if (pipe(pfd) == -1) 4123 return -1; 4124 if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) { 4125 /* We are royally screwed anyway. */ 4126 return -1; 4127 } 4128 4129 switch ((pid = fork())) { 4130 case -1: 4131 (void) close(nulldesc); 4132 return -1; 4133 4134 case 0: 4135 argv[0] = sh; 4136 argv[1] = mc; 4137 argv[2] = prog; 4138 argv[3] = NULL; 4139 4140 (void) setsid(); /* avoid catching SIGHUPs. */ 4141 4142 /* 4143 * Reset ignored signals to their default behavior. 4144 */ 4145 (void)signal(SIGTERM, SIG_DFL); 4146 (void)signal(SIGINT, SIG_DFL); 4147 (void)signal(SIGQUIT, SIG_DFL); 4148 (void)signal(SIGPIPE, SIG_DFL); 4149 (void)signal(SIGHUP, SIG_DFL); 4150 4151 dup2(pfd[0], STDIN_FILENO); 4152 dup2(nulldesc, STDOUT_FILENO); 4153 dup2(nulldesc, STDERR_FILENO); 4154 for (i = getdtablesize(); i > 2; i--) 4155 (void) close(i); 4156 4157 (void) execvp(_PATH_BSHELL, argv); 4158 _exit(255); 4159 } 4160 4161 (void) close(nulldesc); 4162 (void) close(pfd[0]); 4163 4164 /* 4165 * Avoid blocking on a hung pipe. With O_NONBLOCK, we are 4166 * supposed to get an EWOULDBLOCK on writev(2), which is 4167 * caught by the logic above anyway, which will in turn 4168 * close the pipe, and fork a new logging subprocess if 4169 * necessary. The stale subprocess will be killed some 4170 * time later unless it terminated itself due to closing 4171 * its input pipe. 4172 */ 4173 if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { 4174 /* This is bad. */ 4175 logerror("Warning: cannot change pipe to pid %d to " 4176 "non-blocking.", (int) pid); 4177 } 4178 *rpid = pid; 4179 return pfd[1]; 4180 } 4181 4182 void 4183 deadq_enter(pid_t pid, const char *name) 4184 { 4185 dq_t p; 4186 int status; 4187 4188 /* 4189 * Be paranoid: if we can't signal the process, don't enter it 4190 * into the dead queue (perhaps it's already dead). If possible, 4191 * we try to fetch and log the child's status. 4192 */ 4193 if (kill(pid, 0) != 0) { 4194 if (waitpid(pid, &status, WNOHANG) > 0) 4195 log_deadchild(pid, status, name); 4196 return; 4197 } 4198 4199 p = malloc(sizeof(*p)); 4200 if (p == NULL) { 4201 logerror("panic: out of memory!"); 4202 exit(1); 4203 } 4204 4205 p->dq_pid = pid; 4206 p->dq_timeout = DQ_TIMO_INIT; 4207 TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); 4208 } 4209 4210 int 4211 deadq_remove(pid_t pid) 4212 { 4213 dq_t q; 4214 4215 for (q = TAILQ_FIRST(&deadq_head); q != NULL; 4216 q = TAILQ_NEXT(q, dq_entries)) { 4217 if (q->dq_pid == pid) { 4218 TAILQ_REMOVE(&deadq_head, q, dq_entries); 4219 free(q); 4220 return 1; 4221 } 4222 } 4223 return 0; 4224 } 4225 4226 void 4227 log_deadchild(pid_t pid, int status, const char *name) 4228 { 4229 int code; 4230 char buf[256]; 4231 const char *reason; 4232 4233 /* Keep strerror() struff out of logerror messages. */ 4234 errno = 0; 4235 if (WIFSIGNALED(status)) { 4236 reason = "due to signal"; 4237 code = WTERMSIG(status); 4238 } else { 4239 reason = "with status"; 4240 code = WEXITSTATUS(status); 4241 if (code == 0) 4242 return; 4243 } 4244 (void) snprintf(buf, sizeof(buf), 4245 "Logging subprocess %d (%s) exited %s %d.", 4246 pid, name, reason, code); 4247 logerror("%s", buf); 4248 } 4249 4250 struct event * 4251 allocev(void) 4252 { 4253 struct event *ev; 4254 4255 if (!(ev = calloc(1, sizeof(*ev)))) 4256 logerror("Unable to allocate memory"); 4257 return ev; 4258 } 4259 4260 /* *ev is allocated if necessary */ 4261 void 4262 schedule_event(struct event **ev, struct timeval *tv, 4263 void (*cb)(int, short, void *), void *arg) 4264 { 4265 if (!*ev && !(*ev = allocev())) { 4266 return; 4267 } 4268 event_set(*ev, 0, 0, cb, arg); 4269 DPRINTF(D_EVENT, "event_add(%s@%p)\n", "schedule_ev", *ev); \ 4270 if (event_add(*ev, tv) == -1) { 4271 DPRINTF(D_EVENT, "Failure in event_add()\n"); 4272 } 4273 } 4274 4275 #ifndef DISABLE_TLS 4276 /* abbreviation for freeing credential lists */ 4277 void 4278 free_cred_SLIST(struct peer_cred_head *head) 4279 { 4280 struct peer_cred *cred; 4281 4282 while (!SLIST_EMPTY(head)) { 4283 cred = SLIST_FIRST(head); 4284 SLIST_REMOVE_HEAD(head, entries); 4285 FREEPTR(cred->data); 4286 free(cred); 4287 } 4288 } 4289 #endif /* !DISABLE_TLS */ 4290 4291 /* 4292 * send message queue after reconnect 4293 */ 4294 /*ARGSUSED*/ 4295 void 4296 send_queue(int fd, short event, void *arg) 4297 { 4298 struct filed *f = (struct filed *) arg; 4299 struct buf_queue *qentry; 4300 #define SQ_CHUNK_SIZE 250 4301 size_t cnt = 0; 4302 4303 #ifndef DISABLE_TLS 4304 if (f->f_type == F_TLS) { 4305 /* use a flag to prevent recursive calls to send_queue() */ 4306 if (f->f_un.f_tls.tls_conn->send_queue) 4307 return; 4308 else 4309 f->f_un.f_tls.tls_conn->send_queue = true; 4310 } 4311 DPRINTF((D_DATA|D_CALL), "send_queue(f@%p with %zu msgs, " 4312 "cnt@%p = %zu)\n", f, f->f_qelements, &cnt, cnt); 4313 #endif /* !DISABLE_TLS */ 4314 4315 while ((qentry = STAILQ_FIRST(&f->f_qhead))) { 4316 #ifndef DISABLE_TLS 4317 /* send_queue() might be called with an unconnected destination 4318 * from init() or die() or one message might take longer, 4319 * leaving the connection in state ST_WAITING and thus not 4320 * ready for the next message. 4321 * this check is a shortcut to skip these unnecessary calls */ 4322 if (f->f_type == F_TLS 4323 && f->f_un.f_tls.tls_conn->state != ST_TLS_EST) { 4324 DPRINTF(D_TLS, "abort send_queue(cnt@%p = %zu) " 4325 "on TLS connection in state %d\n", 4326 &cnt, cnt, f->f_un.f_tls.tls_conn->state); 4327 return; 4328 } 4329 #endif /* !DISABLE_TLS */ 4330 fprintlog(f, qentry->msg, qentry); 4331 4332 /* Sending a long queue can take some time during which 4333 * SIGHUP and SIGALRM are blocked and no events are handled. 4334 * To avoid that we only send SQ_CHUNK_SIZE messages at once 4335 * and then reschedule ourselves to continue. Thus the control 4336 * will return first from all signal-protected functions so a 4337 * possible SIGHUP/SIGALRM is handled and then back to the 4338 * main loop which can handle possible input. 4339 */ 4340 if (++cnt >= SQ_CHUNK_SIZE) { 4341 if (!f->f_sq_event) { /* alloc on demand */ 4342 f->f_sq_event = allocev(); 4343 event_set(f->f_sq_event, 0, 0, send_queue, f); 4344 } 4345 if (event_add(f->f_sq_event, &((struct timeval){0, 1})) == -1) { 4346 DPRINTF(D_EVENT, "Failure in event_add()\n"); 4347 } 4348 break; 4349 } 4350 } 4351 #ifndef DISABLE_TLS 4352 if (f->f_type == F_TLS) 4353 f->f_un.f_tls.tls_conn->send_queue = false; 4354 #endif 4355 4356 } 4357 4358 /* 4359 * finds the next queue element to delete 4360 * 4361 * has stateful behaviour, before using it call once with reset = true 4362 * after that every call will return one next queue elemen to delete, 4363 * depending on strategy either the oldest or the one with the lowest priority 4364 */ 4365 static struct buf_queue * 4366 find_qentry_to_delete(const struct buf_queue_head *head, int strategy, 4367 bool reset) 4368 { 4369 static int pri; 4370 static struct buf_queue *qentry_static; 4371 4372 struct buf_queue *qentry_tmp; 4373 4374 if (reset || STAILQ_EMPTY(head)) { 4375 pri = LOG_DEBUG; 4376 qentry_static = STAILQ_FIRST(head); 4377 return NULL; 4378 } 4379 4380 /* find elements to delete */ 4381 if (strategy == PURGE_BY_PRIORITY) { 4382 qentry_tmp = qentry_static; 4383 if (!qentry_tmp) return NULL; 4384 while ((qentry_tmp = STAILQ_NEXT(qentry_tmp, entries)) != NULL) 4385 { 4386 if (LOG_PRI(qentry_tmp->msg->pri) == pri) { 4387 /* save the successor, because qentry_tmp 4388 * is probably deleted by the caller */ 4389 qentry_static = STAILQ_NEXT(qentry_tmp, entries); 4390 return qentry_tmp; 4391 } 4392 } 4393 /* nothing found in while loop --> next pri */ 4394 if (--pri) 4395 return find_qentry_to_delete(head, strategy, false); 4396 else 4397 return NULL; 4398 } else /* strategy == PURGE_OLDEST or other value */ { 4399 qentry_tmp = qentry_static; 4400 qentry_static = STAILQ_NEXT(qentry_tmp, entries); 4401 return qentry_tmp; /* is NULL on empty queue */ 4402 } 4403 } 4404 4405 /* note on TAILQ: newest message added at TAIL, 4406 * oldest to be removed is FIRST 4407 */ 4408 /* 4409 * checks length of a destination's message queue 4410 * if del_entries == 0 then assert queue length is 4411 * less or equal to configured number of queue elements 4412 * otherwise del_entries tells how many entries to delete 4413 * 4414 * returns the number of removed queue elements 4415 * (which not necessarily means free'd messages) 4416 * 4417 * strategy PURGE_OLDEST to delete oldest entry, e.g. after it was resent 4418 * strategy PURGE_BY_PRIORITY to delete messages with lowest priority first, 4419 * this is much slower but might be desirable when unsent messages have 4420 * to be deleted, e.g. in call from domark() 4421 */ 4422 size_t 4423 message_queue_purge(struct filed *f, size_t del_entries, int strategy) 4424 { 4425 size_t removed = 0; 4426 struct buf_queue *qentry = NULL; 4427 4428 DPRINTF((D_CALL|D_BUFFER), "purge_message_queue(%p, %zu, %d) with " 4429 "f_qelements=%zu and f_qsize=%zu\n", 4430 f, del_entries, strategy, 4431 f->f_qelements, f->f_qsize); 4432 4433 /* reset state */ 4434 (void)find_qentry_to_delete(&f->f_qhead, strategy, true); 4435 4436 while (removed < del_entries 4437 || (TypeInfo[f->f_type].queue_length != -1 4438 && (size_t)TypeInfo[f->f_type].queue_length <= f->f_qelements) 4439 || (TypeInfo[f->f_type].queue_size != -1 4440 && (size_t)TypeInfo[f->f_type].queue_size <= f->f_qsize)) { 4441 qentry = find_qentry_to_delete(&f->f_qhead, strategy, 0); 4442 if (message_queue_remove(f, qentry)) 4443 removed++; 4444 else 4445 break; 4446 } 4447 return removed; 4448 } 4449 4450 /* run message_queue_purge() for all destinations to free memory */ 4451 size_t 4452 message_allqueues_purge(void) 4453 { 4454 size_t sum = 0; 4455 struct filed *f; 4456 4457 for (f = Files; f; f = f->f_next) 4458 sum += message_queue_purge(f, 4459 f->f_qelements/10, PURGE_BY_PRIORITY); 4460 4461 DPRINTF(D_BUFFER, 4462 "message_allqueues_purge(): removed %zu buffer entries\n", sum); 4463 return sum; 4464 } 4465 4466 /* run message_queue_purge() for all destinations to check limits */ 4467 size_t 4468 message_allqueues_check(void) 4469 { 4470 size_t sum = 0; 4471 struct filed *f; 4472 4473 for (f = Files; f; f = f->f_next) 4474 sum += message_queue_purge(f, 0, PURGE_BY_PRIORITY); 4475 DPRINTF(D_BUFFER, 4476 "message_allqueues_check(): removed %zu buffer entries\n", sum); 4477 return sum; 4478 } 4479 4480 struct buf_msg * 4481 buf_msg_new(const size_t len) 4482 { 4483 struct buf_msg *newbuf; 4484 4485 CALLOC(newbuf, sizeof(*newbuf)); 4486 4487 if (len) { /* len = 0 is valid */ 4488 MALLOC(newbuf->msg, len); 4489 newbuf->msgorig = newbuf->msg; 4490 newbuf->msgsize = len; 4491 } 4492 return NEWREF(newbuf); 4493 } 4494 4495 void 4496 buf_msg_free(struct buf_msg *buf) 4497 { 4498 if (!buf) 4499 return; 4500 4501 buf->refcount--; 4502 if (buf->refcount == 0) { 4503 FREEPTR(buf->timestamp); 4504 /* small optimizations: the host/recvhost may point to the 4505 * global HostName/FQDN. of course this must not be free()d 4506 * same goes for appname and include_pid 4507 */ 4508 if (buf->recvhost != buf->host 4509 && buf->recvhost != LocalHostName 4510 && buf->recvhost != LocalFQDN 4511 && buf->recvhost != oldLocalFQDN) 4512 FREEPTR(buf->recvhost); 4513 if (buf->host != LocalHostName 4514 && buf->host != LocalFQDN 4515 && buf->host != oldLocalFQDN) 4516 FREEPTR(buf->host); 4517 if (buf->prog != appname) 4518 FREEPTR(buf->prog); 4519 if (buf->pid != include_pid) 4520 FREEPTR(buf->pid); 4521 FREEPTR(buf->msgid); 4522 FREEPTR(buf->sd); 4523 FREEPTR(buf->msgorig); /* instead of msg */ 4524 FREEPTR(buf); 4525 } 4526 } 4527 4528 size_t 4529 buf_queue_obj_size(struct buf_queue *qentry) 4530 { 4531 size_t sum = 0; 4532 4533 if (!qentry) 4534 return 0; 4535 sum += sizeof(*qentry) 4536 + sizeof(*qentry->msg) 4537 + qentry->msg->msgsize 4538 + SAFEstrlen(qentry->msg->timestamp)+1 4539 + SAFEstrlen(qentry->msg->msgid)+1; 4540 if (qentry->msg->prog 4541 && qentry->msg->prog != include_pid) 4542 sum += strlen(qentry->msg->prog)+1; 4543 if (qentry->msg->pid 4544 && qentry->msg->pid != appname) 4545 sum += strlen(qentry->msg->pid)+1; 4546 if (qentry->msg->recvhost 4547 && qentry->msg->recvhost != LocalHostName 4548 && qentry->msg->recvhost != LocalFQDN 4549 && qentry->msg->recvhost != oldLocalFQDN) 4550 sum += strlen(qentry->msg->recvhost)+1; 4551 if (qentry->msg->host 4552 && qentry->msg->host != LocalHostName 4553 && qentry->msg->host != LocalFQDN 4554 && qentry->msg->host != oldLocalFQDN) 4555 sum += strlen(qentry->msg->host)+1; 4556 4557 return sum; 4558 } 4559 4560 bool 4561 message_queue_remove(struct filed *f, struct buf_queue *qentry) 4562 { 4563 if (!f || !qentry || !qentry->msg) 4564 return false; 4565 4566 assert(!STAILQ_EMPTY(&f->f_qhead)); 4567 STAILQ_REMOVE(&f->f_qhead, qentry, buf_queue, entries); 4568 f->f_qelements--; 4569 f->f_qsize -= buf_queue_obj_size(qentry); 4570 4571 DPRINTF(D_BUFFER, "msg @%p removed from queue @%p, new qlen = %zu\n", 4572 qentry->msg, f, f->f_qelements); 4573 DELREF(qentry->msg); 4574 FREEPTR(qentry); 4575 return true; 4576 } 4577 4578 /* 4579 * returns *qentry on success and NULL on error 4580 */ 4581 struct buf_queue * 4582 message_queue_add(struct filed *f, struct buf_msg *buffer) 4583 { 4584 struct buf_queue *qentry; 4585 4586 /* check on every call or only every n-th time? */ 4587 message_queue_purge(f, 0, PURGE_BY_PRIORITY); 4588 4589 while (!(qentry = malloc(sizeof(*qentry))) 4590 && message_queue_purge(f, 1, PURGE_OLDEST)) 4591 continue; 4592 if (!qentry) { 4593 logerror("Unable to allocate memory"); 4594 DPRINTF(D_BUFFER, "queue empty, no memory, msg dropped\n"); 4595 return NULL; 4596 } else { 4597 qentry->msg = buffer; 4598 f->f_qelements++; 4599 f->f_qsize += buf_queue_obj_size(qentry); 4600 STAILQ_INSERT_TAIL(&f->f_qhead, qentry, entries); 4601 4602 DPRINTF(D_BUFFER, "msg @%p queued @%p, qlen = %zu\n", 4603 buffer, f, f->f_qelements); 4604 return qentry; 4605 } 4606 } 4607 4608 void 4609 message_queue_freeall(struct filed *f) 4610 { 4611 struct buf_queue *qentry; 4612 4613 if (!f) return; 4614 DPRINTF(D_MEM, "message_queue_freeall(f@%p) with f_qhead@%p\n", f, 4615 &f->f_qhead); 4616 4617 while (!STAILQ_EMPTY(&f->f_qhead)) { 4618 qentry = STAILQ_FIRST(&f->f_qhead); 4619 STAILQ_REMOVE(&f->f_qhead, qentry, buf_queue, entries); 4620 DELREF(qentry->msg); 4621 FREEPTR(qentry); 4622 } 4623 4624 f->f_qelements = 0; 4625 f->f_qsize = 0; 4626 } 4627 4628 #ifndef DISABLE_TLS 4629 /* utility function for tls_reconnect() */ 4630 struct filed * 4631 get_f_by_conninfo(struct tls_conn_settings *conn_info) 4632 { 4633 struct filed *f; 4634 4635 for (f = Files; f; f = f->f_next) { 4636 if ((f->f_type == F_TLS) && f->f_un.f_tls.tls_conn == conn_info) 4637 return f; 4638 } 4639 DPRINTF(D_TLS, "get_f_by_conninfo() called on invalid conn_info\n"); 4640 return NULL; 4641 } 4642 4643 /* 4644 * Called on signal. 4645 * Lets the admin reconnect without waiting for the reconnect timer expires. 4646 */ 4647 /*ARGSUSED*/ 4648 void 4649 dispatch_force_tls_reconnect(int fd, short event, void *ev) 4650 { 4651 struct filed *f; 4652 DPRINTF((D_TLS|D_CALL|D_EVENT), "dispatch_force_tls_reconnect()\n"); 4653 for (f = Files; f; f = f->f_next) { 4654 if (f->f_type == F_TLS && 4655 f->f_un.f_tls.tls_conn->state == ST_NONE) 4656 tls_reconnect(fd, event, f->f_un.f_tls.tls_conn); 4657 } 4658 } 4659 #endif /* !DISABLE_TLS */ 4660 4661 /* 4662 * return a timestamp in a static buffer, 4663 * either format the timestamp given by parameter in_now 4664 * or use the current time if in_now is NULL. 4665 */ 4666 char * 4667 make_timestamp(time_t *in_now, bool iso, size_t tlen) 4668 { 4669 int frac_digits = 6; 4670 struct timeval tv; 4671 time_t mytime; 4672 struct tm ltime; 4673 int len = 0; 4674 int tzlen = 0; 4675 /* uses global var: time_t now; */ 4676 4677 if (in_now) { 4678 mytime = *in_now; 4679 } else { 4680 gettimeofday(&tv, NULL); 4681 mytime = now = tv.tv_sec; 4682 } 4683 4684 if (!iso) { 4685 strlcpy(timestamp, ctime(&mytime) + 4, sizeof(timestamp)); 4686 timestamp[BSD_TIMESTAMPLEN] = '\0'; 4687 } else { 4688 localtime_r(&mytime, <ime); 4689 len += strftime(timestamp, sizeof(timestamp), "%FT%T", <ime); 4690 snprintf(×tamp[len], frac_digits + 2, ".%.*jd", 4691 frac_digits, (intmax_t)tv.tv_usec); 4692 len += frac_digits + 1; 4693 tzlen = strftime(×tamp[len], sizeof(timestamp) - len, "%z", 4694 <ime); 4695 len += tzlen; 4696 4697 if (tzlen == 5) { 4698 /* strftime gives "+0200", but we need "+02:00" */ 4699 timestamp[len + 2] = '\0'; 4700 timestamp[len + 1] = timestamp[len]; 4701 timestamp[len] = timestamp[len - 1]; 4702 timestamp[len - 1] = timestamp[len - 2]; 4703 timestamp[len - 2] = ':'; 4704 } 4705 } 4706 4707 switch (tlen) { 4708 case (size_t)-1: 4709 return timestamp; 4710 case 0: 4711 return strdup(timestamp); 4712 default: 4713 return strndup(timestamp, tlen); 4714 } 4715 } 4716 4717 /* auxillary code to allocate memory and copy a string */ 4718 bool 4719 copy_string(char **mem, const char *p, const char *q) 4720 { 4721 const size_t len = 1 + q - p; 4722 if (!(*mem = malloc(len))) { 4723 logerror("Unable to allocate memory for config"); 4724 return false; 4725 } 4726 strlcpy(*mem, p, len); 4727 return true; 4728 } 4729 4730 /* keyword has to end with ", everything until next " is copied */ 4731 bool 4732 copy_config_value_quoted(const char *keyword, char **mem, const char **p) 4733 { 4734 const char *q; 4735 if (strncasecmp(*p, keyword, strlen(keyword))) 4736 return false; 4737 q = *p += strlen(keyword); 4738 if (!(q = strchr(*p, '"'))) { 4739 errno = 0; 4740 logerror("unterminated \"\n"); 4741 return false; 4742 } 4743 if (!(copy_string(mem, *p, q))) 4744 return false; 4745 *p = ++q; 4746 return true; 4747 } 4748 4749 /* for config file: 4750 * following = required but whitespace allowed, quotes optional 4751 * if numeric, then conversion to integer and no memory allocation 4752 */ 4753 bool 4754 copy_config_value(const char *keyword, char **mem, 4755 const char **p, const char *file, int line) 4756 { 4757 if (strncasecmp(*p, keyword, strlen(keyword))) 4758 return false; 4759 *p += strlen(keyword); 4760 4761 while (isspace((unsigned char)**p)) 4762 *p += 1; 4763 if (**p != '=') { 4764 errno = 0; 4765 logerror("expected \"=\" in file %s, line %d", file, line); 4766 return false; 4767 } 4768 *p += 1; 4769 4770 return copy_config_value_word(mem, p); 4771 } 4772 4773 /* copy next parameter from a config line */ 4774 bool 4775 copy_config_value_word(char **mem, const char **p) 4776 { 4777 const char *q; 4778 while (isspace((unsigned char)**p)) 4779 *p += 1; 4780 if (**p == '"') 4781 return copy_config_value_quoted("\"", mem, p); 4782 4783 /* without quotes: find next whitespace or end of line */ 4784 (void)((q = strchr(*p, ' ')) || (q = strchr(*p, '\t')) 4785 || (q = strchr(*p, '\n')) || (q = strchr(*p, '\0'))); 4786 4787 if (q-*p == 0 || !(copy_string(mem, *p, q))) 4788 return false; 4789 4790 *p = ++q; 4791 return true; 4792 } 4793 4794 static int 4795 writev1(int fd, struct iovec *iov, size_t count) 4796 { 4797 ssize_t nw = 0, tot = 0; 4798 size_t ntries = 5; 4799 4800 if (count == 0) 4801 return 0; 4802 while (ntries--) { 4803 switch ((nw = writev(fd, iov, count))) { 4804 case -1: 4805 if (errno == EAGAIN || errno == EWOULDBLOCK) { 4806 struct pollfd pfd; 4807 pfd.fd = fd; 4808 pfd.events = POLLOUT; 4809 pfd.revents = 0; 4810 (void)poll(&pfd, 1, 500); 4811 continue; 4812 } 4813 return -1; 4814 case 0: 4815 return 0; 4816 default: 4817 tot += nw; 4818 while (nw > 0) { 4819 if (iov->iov_len > (size_t)nw) { 4820 iov->iov_len -= nw; 4821 iov->iov_base = 4822 (char *)iov->iov_base + nw; 4823 break; 4824 } else { 4825 if (--count == 0) 4826 return tot; 4827 nw -= iov->iov_len; 4828 iov++; 4829 } 4830 } 4831 } 4832 } 4833 return tot == 0 ? nw : tot; 4834 } 4835 4836 #ifndef NDEBUG 4837 void 4838 dbprintf(const char *fname, const char *funname, 4839 size_t lnum, const char *fmt, ...) 4840 { 4841 va_list ap; 4842 char *ts; 4843 4844 ts = make_timestamp(NULL, true, (size_t)-1); 4845 printf("%s:%s:%s:%.4zu\t", ts, fname, funname, lnum); 4846 4847 va_start(ap, fmt); 4848 vprintf(fmt, ap); 4849 va_end(ap); 4850 } 4851 #endif 4852