1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <netinet/in.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <netdb.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #define SYSLOG_NAMES 58 #include <syslog.h> 59 60 #define sstosa(ss) ((struct sockaddr *)(void *)ss) 61 62 struct socks { 63 int sk_sock; 64 int sk_addrlen; 65 struct sockaddr_storage sk_addr; 66 }; 67 68 static int decode(char *, const CODE *); 69 static int pencode(char *); 70 static ssize_t socksetup(const char *, const char *, const char *, 71 struct socks **); 72 static void logmessage(int, const char *, struct socks *, ssize_t, 73 const char *); 74 static void usage(void); 75 76 #ifdef INET6 77 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 78 #else 79 static int family = PF_INET; /* protocol family (IPv4 only) */ 80 #endif 81 static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 82 83 /* 84 * logger -- read and log utility 85 * 86 * Reads from an input and arranges to write the result on the system 87 * log. 88 */ 89 int 90 main(int argc, char *argv[]) 91 { 92 struct socks *socks; 93 ssize_t nsock; 94 int ch, logflags, pri; 95 char *tag, *host, buf[1024]; 96 const char *svcname, *src; 97 98 tag = NULL; 99 host = NULL; 100 svcname = "syslog"; 101 src = NULL; 102 pri = LOG_USER | LOG_NOTICE; 103 logflags = 0; 104 unsetenv("TZ"); 105 while ((ch = getopt(argc, argv, "46Af:h:iP:p:S:st:")) != -1) 106 switch((char)ch) { 107 case '4': 108 family = PF_INET; 109 break; 110 #ifdef INET6 111 case '6': 112 family = PF_INET6; 113 break; 114 #endif 115 case 'A': 116 send_to_all++; 117 break; 118 case 'f': /* file to log */ 119 if (freopen(optarg, "r", stdin) == NULL) 120 err(1, "%s", optarg); 121 setvbuf(stdin, 0, _IONBF, 0); 122 break; 123 case 'h': /* hostname to deliver to */ 124 host = optarg; 125 break; 126 case 'i': /* log process id also */ 127 logflags |= LOG_PID; 128 break; 129 case 'P': /* service name or port number */ 130 svcname = optarg; 131 break; 132 case 'p': /* priority */ 133 pri = pencode(optarg); 134 break; 135 case 's': /* log to standard error */ 136 logflags |= LOG_PERROR; 137 break; 138 case 'S': /* source address */ 139 src = optarg; 140 break; 141 case 't': /* tag */ 142 tag = optarg; 143 break; 144 case '?': 145 default: 146 usage(); 147 } 148 argc -= optind; 149 argv += optind; 150 151 if (host) { 152 nsock = socksetup(src, host, svcname, &socks); 153 if (nsock <= 0) 154 errx(1, "socket"); 155 } else { 156 if (src) 157 errx(1, "-h option is missing."); 158 nsock = 0; 159 } 160 161 if (tag == NULL) 162 tag = getlogin(); 163 /* setup for logging */ 164 if (host == NULL) 165 openlog(tag, logflags, 0); 166 (void) fclose(stdout); 167 168 /* log input line if appropriate */ 169 if (argc > 0) { 170 char *p, *endp; 171 size_t len; 172 173 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { 174 len = strlen(*argv); 175 if (p + len > endp && p > buf) { 176 logmessage(pri, tag, socks, nsock, buf); 177 p = buf; 178 } 179 if (len > sizeof(buf) - 1) 180 logmessage(pri, tag, socks, nsock, *argv++); 181 else { 182 if (p != buf) 183 *p++ = ' '; 184 bcopy(*argv++, p, len); 185 *(p += len) = '\0'; 186 } 187 } 188 if (p != buf) 189 logmessage(pri, tag, socks, nsock, buf); 190 } else 191 while (fgets(buf, sizeof(buf), stdin) != NULL) 192 logmessage(pri, tag, socks, nsock, buf); 193 exit(0); 194 } 195 196 static ssize_t 197 socksetup(const char *src, const char *dst, const char *svcname, 198 struct socks **socks) 199 { 200 struct addrinfo hints, *res, *res0; 201 struct sockaddr_storage *ss_src[AF_MAX]; 202 struct socks *sk; 203 ssize_t nsock = 0; 204 int error, maxs; 205 206 memset(&ss_src[0], 0, sizeof(ss_src)); 207 if (src) { 208 char *p, *p0, *hs, *hbuf, *sbuf; 209 210 hbuf = sbuf = NULL; 211 p0 = p = strdup(src); 212 if (p0 == NULL) 213 err(1, "strdup failed"); 214 hs = p0; /* point to search ":" */ 215 #ifdef INET6 216 /* -S option supports IPv6 addr in "[2001:db8::1]:service". */ 217 if (*p0 == '[') { 218 p = strchr(p0, ']'); 219 if (p == NULL) 220 errx(1, "\"]\" not found in src addr"); 221 *p = '\0'; 222 /* hs points just after ']' (':' or '\0'). */ 223 hs = p + 1; 224 /* 225 * p points just after '[' while it points hs 226 * in the case of []. 227 */ 228 p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1; 229 } 230 #endif 231 if (*p != '\0') { 232 /* (p == hs) means ":514" or "[]:514". */ 233 hbuf = (p == hs && *p == ':') ? NULL : p; 234 p = strchr(hs, ':'); 235 if (p != NULL) { 236 *p = '\0'; 237 sbuf = (*(p + 1) != '\0') ? p + 1 : NULL; 238 } 239 } 240 hints = (struct addrinfo){ 241 .ai_family = family, 242 .ai_socktype = SOCK_DGRAM, 243 .ai_flags = AI_PASSIVE 244 }; 245 error = getaddrinfo(hbuf, sbuf, &hints, &res0); 246 if (error) 247 errx(1, "%s: %s", gai_strerror(error), src); 248 for (res = res0; res; res = res->ai_next) { 249 switch (res->ai_family) { 250 case AF_INET: 251 #ifdef INET6 252 case AF_INET6: 253 #endif 254 if (ss_src[res->ai_family] != NULL) 255 continue; 256 ss_src[res->ai_family] = 257 malloc(sizeof(struct sockaddr_storage)); 258 if (ss_src[res->ai_family] == NULL) 259 err(1, "malloc failed"); 260 memcpy(ss_src[res->ai_family], res->ai_addr, 261 res->ai_addrlen); 262 } 263 } 264 freeaddrinfo(res0); 265 free(p0); 266 } 267 268 /* resolve hostname */ 269 hints = (struct addrinfo){ 270 .ai_family = family, 271 .ai_socktype = SOCK_DGRAM 272 }; 273 error = getaddrinfo(dst, svcname, &hints, &res0); 274 if (error == EAI_SERVICE) { 275 warnx("%s/udp: unknown service", svcname); 276 error = getaddrinfo(dst, "514", &hints, &res); 277 } 278 if (error) 279 errx(1, "%s: %s", gai_strerror(error), dst); 280 /* count max number of sockets we may open */ 281 maxs = 0; 282 for (res = res0; res; res = res->ai_next) 283 maxs++; 284 sk = calloc(maxs, sizeof(*sk)); 285 if (sk == NULL) 286 errx(1, "couldn't allocate memory for sockets"); 287 for (res = res0; res; res = res->ai_next) { 288 int s; 289 290 s = socket(res->ai_family, res->ai_socktype, 291 res->ai_protocol); 292 if (s < 0) 293 continue; 294 if (src && ss_src[res->ai_family] == NULL) 295 errx(1, "address family mismatch"); 296 297 if (ss_src[res->ai_family]) { 298 error = bind(s, sstosa(ss_src[res->ai_family]), 299 ss_src[res->ai_family]->ss_len); 300 if (error < 0) 301 err(1, "bind"); 302 } 303 sk[nsock] = (struct socks){ 304 .sk_addrlen = res->ai_addrlen, 305 .sk_sock = s 306 }; 307 memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen); 308 nsock++; 309 } 310 freeaddrinfo(res0); 311 312 *socks = sk; 313 return (nsock); 314 } 315 316 /* 317 * Send the message to syslog, either on the local host, or on a remote host 318 */ 319 static void 320 logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock, 321 const char *buf) 322 { 323 char *line; 324 int len, i, lsent; 325 326 if (nsock == 0) { 327 syslog(pri, "%s", buf); 328 return; 329 } 330 if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1) 331 errx(1, "asprintf"); 332 333 lsent = -1; 334 for (i = 0; i < nsock; i++) { 335 lsent = sendto(sk[i].sk_sock, line, len, 0, 336 sstosa(&sk[i].sk_addr), sk[i].sk_addrlen); 337 if (lsent == len && !send_to_all) 338 break; 339 } 340 if (lsent != len) { 341 if (lsent == -1) 342 warn("sendto"); 343 else 344 warnx("sendto: short send - %d bytes", lsent); 345 } 346 347 free(line); 348 } 349 350 /* 351 * Decode a symbolic name to a numeric value 352 */ 353 static int 354 pencode(char *s) 355 { 356 char *save; 357 int fac, lev; 358 359 for (save = s; *s && *s != '.'; ++s); 360 if (*s) { 361 *s = '\0'; 362 fac = decode(save, facilitynames); 363 if (fac < 0) 364 errx(1, "unknown facility name: %s", save); 365 *s++ = '.'; 366 } 367 else { 368 fac = 0; 369 s = save; 370 } 371 lev = decode(s, prioritynames); 372 if (lev < 0) 373 errx(1, "unknown priority name: %s", save); 374 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 375 } 376 377 static int 378 decode(char *name, const CODE *codetab) 379 { 380 const CODE *c; 381 382 if (isdigit(*name)) 383 return (atoi(name)); 384 385 for (c = codetab; c->c_name; c++) 386 if (!strcasecmp(name, c->c_name)) 387 return (c->c_val); 388 389 return (-1); 390 } 391 392 static void 393 usage(void) 394 { 395 (void)fprintf(stderr, "usage: %s\n", 396 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n" 397 " [-S addr:port] [message ...]" 398 ); 399 exit(1); 400 } 401