19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 309b50d902SRodney W. Grimes #ifndef lint 31df071556SPhilippe Charnier static const char copyright[] = 329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\ 339b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 349b50d902SRodney W. Grimes #endif /* not lint */ 359b50d902SRodney W. Grimes 36df071556SPhilippe Charnier #if 0 37865059c8SPhilippe Charnier #ifndef lint 389b50d902SRodney W. Grimes static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; 399b50d902SRodney W. Grimes #endif /* not lint */ 40865059c8SPhilippe Charnier #endif 41865059c8SPhilippe Charnier 429bd5ae85SDavid Malone #include <sys/cdefs.h> 439bd5ae85SDavid Malone __FBSDID("$FreeBSD$"); 449bd5ae85SDavid Malone 45b0fe2da8SDavid Malone #include <sys/types.h> 46b0fe2da8SDavid Malone #include <sys/socket.h> 47b0fe2da8SDavid Malone #include <netinet/in.h> 48b0fe2da8SDavid Malone 499b50d902SRodney W. Grimes #include <ctype.h> 50df071556SPhilippe Charnier #include <err.h> 51b0fe2da8SDavid Malone #include <netdb.h> 52df071556SPhilippe Charnier #include <stdio.h> 53df071556SPhilippe Charnier #include <stdlib.h> 549b50d902SRodney W. Grimes #include <string.h> 55df071556SPhilippe Charnier #include <unistd.h> 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes #define SYSLOG_NAMES 589b50d902SRodney W. Grimes #include <syslog.h> 599b50d902SRodney W. Grimes 60f1bb2cd2SWarner Losh int decode(char *, CODE *); 61f1bb2cd2SWarner Losh int pencode(char *); 62*81280940SEdwin Groothuis static void logmessage(int, const char *, const char *, const char *, 63*81280940SEdwin Groothuis const char *); 64f1bb2cd2SWarner Losh static void usage(void); 659b50d902SRodney W. Grimes 660b5f90afSHajimu UMEMOTO struct socks { 670b5f90afSHajimu UMEMOTO int sock; 680b5f90afSHajimu UMEMOTO int addrlen; 690b5f90afSHajimu UMEMOTO struct sockaddr_storage addr; 700b5f90afSHajimu UMEMOTO }; 710b5f90afSHajimu UMEMOTO 720b5f90afSHajimu UMEMOTO #ifdef INET6 730b5f90afSHajimu UMEMOTO int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 740b5f90afSHajimu UMEMOTO #else 750b5f90afSHajimu UMEMOTO int family = PF_INET; /* protocol family (IPv4 only) */ 760b5f90afSHajimu UMEMOTO #endif 770b5f90afSHajimu UMEMOTO int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 780b5f90afSHajimu UMEMOTO 799b50d902SRodney W. Grimes /* 809b50d902SRodney W. Grimes * logger -- read and log utility 819b50d902SRodney W. Grimes * 829b50d902SRodney W. Grimes * Reads from an input and arranges to write the result on the system 839b50d902SRodney W. Grimes * log. 849b50d902SRodney W. Grimes */ 859b50d902SRodney W. Grimes int 86f4ac32deSDavid Malone main(int argc, char *argv[]) 879b50d902SRodney W. Grimes { 889b50d902SRodney W. Grimes int ch, logflags, pri; 896f62d863SDavid Malone char *tag, *host, buf[1024]; 906f62d863SDavid Malone const char *svcname; 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes tag = NULL; 93b0fe2da8SDavid Malone host = NULL; 946b04b7f6SBruce M Simpson svcname = "syslog"; 95ca122bf7SRuslan Ermilov pri = LOG_USER | LOG_NOTICE; 969b50d902SRodney W. Grimes logflags = 0; 9740244c28SPoul-Henning Kamp unsetenv("TZ"); 986b04b7f6SBruce M Simpson while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1) 999b50d902SRodney W. Grimes switch((char)ch) { 1000b5f90afSHajimu UMEMOTO case '4': 1010b5f90afSHajimu UMEMOTO family = PF_INET; 1020b5f90afSHajimu UMEMOTO break; 1030b5f90afSHajimu UMEMOTO #ifdef INET6 1040b5f90afSHajimu UMEMOTO case '6': 1050b5f90afSHajimu UMEMOTO family = PF_INET6; 1060b5f90afSHajimu UMEMOTO break; 1070b5f90afSHajimu UMEMOTO #endif 1080b5f90afSHajimu UMEMOTO case 'A': 1090b5f90afSHajimu UMEMOTO send_to_all++; 1100b5f90afSHajimu UMEMOTO break; 1119b50d902SRodney W. Grimes case 'f': /* file to log */ 112df071556SPhilippe Charnier if (freopen(optarg, "r", stdin) == NULL) 113df071556SPhilippe Charnier err(1, "%s", optarg); 1144145bb53SKirk McKusick setvbuf(stdin, 0, _IONBF, 0); 1159b50d902SRodney W. Grimes break; 116b0fe2da8SDavid Malone case 'h': /* hostname to deliver to */ 117b0fe2da8SDavid Malone host = optarg; 118b0fe2da8SDavid Malone break; 1199b50d902SRodney W. Grimes case 'i': /* log process id also */ 1209b50d902SRodney W. Grimes logflags |= LOG_PID; 1219b50d902SRodney W. Grimes break; 1226b04b7f6SBruce M Simpson case 'P': /* service name or port number */ 1236b04b7f6SBruce M Simpson svcname = optarg; 1246b04b7f6SBruce M Simpson break; 1259b50d902SRodney W. Grimes case 'p': /* priority */ 1269b50d902SRodney W. Grimes pri = pencode(optarg); 1279b50d902SRodney W. Grimes break; 1289b50d902SRodney W. Grimes case 's': /* log to standard error */ 1299b50d902SRodney W. Grimes logflags |= LOG_PERROR; 1309b50d902SRodney W. Grimes break; 1319b50d902SRodney W. Grimes case 't': /* tag */ 1329b50d902SRodney W. Grimes tag = optarg; 1339b50d902SRodney W. Grimes break; 1349b50d902SRodney W. Grimes case '?': 1359b50d902SRodney W. Grimes default: 1369b50d902SRodney W. Grimes usage(); 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes argc -= optind; 1399b50d902SRodney W. Grimes argv += optind; 1409b50d902SRodney W. Grimes 141*81280940SEdwin Groothuis if (tag == NULL) 142*81280940SEdwin Groothuis tag = getlogin(); 1439b50d902SRodney W. Grimes /* setup for logging */ 144*81280940SEdwin Groothuis if (host == NULL) 145*81280940SEdwin Groothuis openlog(tag, logflags, 0); 1469b50d902SRodney W. Grimes (void) fclose(stdout); 1479b50d902SRodney W. Grimes 1489b50d902SRodney W. Grimes /* log input line if appropriate */ 1499b50d902SRodney W. Grimes if (argc > 0) { 150f4ac32deSDavid Malone char *p, *endp; 1519bd5ae85SDavid Malone size_t len; 1529b50d902SRodney W. Grimes 1539b50d902SRodney W. Grimes for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { 1549b50d902SRodney W. Grimes len = strlen(*argv); 1559b50d902SRodney W. Grimes if (p + len > endp && p > buf) { 156*81280940SEdwin Groothuis logmessage(pri, tag, host, svcname, buf); 1579b50d902SRodney W. Grimes p = buf; 1589b50d902SRodney W. Grimes } 1599b50d902SRodney W. Grimes if (len > sizeof(buf) - 1) 160*81280940SEdwin Groothuis logmessage(pri, tag, host, svcname, *argv++); 1619b50d902SRodney W. Grimes else { 1629b50d902SRodney W. Grimes if (p != buf) 1639b50d902SRodney W. Grimes *p++ = ' '; 1649b50d902SRodney W. Grimes bcopy(*argv++, p, len); 1659b50d902SRodney W. Grimes *(p += len) = '\0'; 1669b50d902SRodney W. Grimes } 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes if (p != buf) 169*81280940SEdwin Groothuis logmessage(pri, tag, host, svcname, buf); 1709b50d902SRodney W. Grimes } else 1719b50d902SRodney W. Grimes while (fgets(buf, sizeof(buf), stdin) != NULL) 172*81280940SEdwin Groothuis logmessage(pri, tag, host, svcname, buf); 1739b50d902SRodney W. Grimes exit(0); 1749b50d902SRodney W. Grimes } 1759b50d902SRodney W. Grimes 1769b50d902SRodney W. Grimes /* 177b0fe2da8SDavid Malone * Send the message to syslog, either on the local host, or on a remote host 178b0fe2da8SDavid Malone */ 179b0fe2da8SDavid Malone void 180*81280940SEdwin Groothuis logmessage(int pri, const char *tag, const char *host, const char *svcname, 181*81280940SEdwin Groothuis const char *buf) 182b0fe2da8SDavid Malone { 1830b5f90afSHajimu UMEMOTO static struct socks *socks; 1840b5f90afSHajimu UMEMOTO static int nsock = 0; 1850b5f90afSHajimu UMEMOTO struct addrinfo hints, *res, *r; 186b0fe2da8SDavid Malone char *line; 1870b5f90afSHajimu UMEMOTO int maxs, len, sock, error, i, lsent; 188b0fe2da8SDavid Malone 189b0fe2da8SDavid Malone if (host == NULL) { 190b0fe2da8SDavid Malone syslog(pri, "%s", buf); 191b0fe2da8SDavid Malone return; 192b0fe2da8SDavid Malone } 193b0fe2da8SDavid Malone 1940b5f90afSHajimu UMEMOTO if (nsock <= 0) { /* set up socket stuff */ 195b0fe2da8SDavid Malone /* resolve hostname */ 1960b5f90afSHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 1970b5f90afSHajimu UMEMOTO hints.ai_family = family; 1980b5f90afSHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; 1996b04b7f6SBruce M Simpson error = getaddrinfo(host, svcname, &hints, &res); 2000b5f90afSHajimu UMEMOTO if (error == EAI_SERVICE) { 2016b04b7f6SBruce M Simpson warnx("%s/udp: unknown service", svcname); 2020b5f90afSHajimu UMEMOTO error = getaddrinfo(host, "514", &hints, &res); 2030b5f90afSHajimu UMEMOTO } 2040b5f90afSHajimu UMEMOTO if (error) 2050b5f90afSHajimu UMEMOTO errx(1, "%s: %s", gai_strerror(error), host); 2060b5f90afSHajimu UMEMOTO /* count max number of sockets we may open */ 2070b5f90afSHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 2080b5f90afSHajimu UMEMOTO socks = malloc(maxs * sizeof(struct socks)); 2090b5f90afSHajimu UMEMOTO if (!socks) 2100b5f90afSHajimu UMEMOTO errx(1, "couldn't allocate memory for sockets"); 2110b5f90afSHajimu UMEMOTO for (r = res; r; r = r->ai_next) { 2120b5f90afSHajimu UMEMOTO sock = socket(r->ai_family, r->ai_socktype, 2130b5f90afSHajimu UMEMOTO r->ai_protocol); 214b0fe2da8SDavid Malone if (sock < 0) 2150b5f90afSHajimu UMEMOTO continue; 2160b5f90afSHajimu UMEMOTO memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen); 2170b5f90afSHajimu UMEMOTO socks[nsock].addrlen = r->ai_addrlen; 2180b5f90afSHajimu UMEMOTO socks[nsock++].sock = sock; 2190b5f90afSHajimu UMEMOTO } 2200b5f90afSHajimu UMEMOTO freeaddrinfo(res); 2210b5f90afSHajimu UMEMOTO if (nsock <= 0) 222b0fe2da8SDavid Malone errx(1, "socket"); 223b0fe2da8SDavid Malone } 224b0fe2da8SDavid Malone 225*81280940SEdwin Groothuis if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1) 226b0fe2da8SDavid Malone errx(1, "asprintf"); 227b0fe2da8SDavid Malone 228865059c8SPhilippe Charnier lsent = -1; 2290b5f90afSHajimu UMEMOTO for (i = 0; i < nsock; ++i) { 2300b5f90afSHajimu UMEMOTO lsent = sendto(socks[i].sock, line, len, 0, 2310b5f90afSHajimu UMEMOTO (struct sockaddr *)&socks[i].addr, 2320b5f90afSHajimu UMEMOTO socks[i].addrlen); 2330b5f90afSHajimu UMEMOTO if (lsent == len && !send_to_all) 2340b5f90afSHajimu UMEMOTO break; 2350b5f90afSHajimu UMEMOTO } 2369bd5ae85SDavid Malone if (lsent != len) { 23757c1a0b6SBill Fenner if (lsent == -1) 23857c1a0b6SBill Fenner warn ("sendto"); 23957c1a0b6SBill Fenner else 24057c1a0b6SBill Fenner warnx ("sendto: short send - %d bytes", lsent); 2419bd5ae85SDavid Malone } 242b0fe2da8SDavid Malone 243b0fe2da8SDavid Malone free(line); 244b0fe2da8SDavid Malone } 245b0fe2da8SDavid Malone 246b0fe2da8SDavid Malone /* 2479b50d902SRodney W. Grimes * Decode a symbolic name to a numeric value 2489b50d902SRodney W. Grimes */ 2499b50d902SRodney W. Grimes int 250f4ac32deSDavid Malone pencode(char *s) 2519b50d902SRodney W. Grimes { 2529b50d902SRodney W. Grimes char *save; 2539b50d902SRodney W. Grimes int fac, lev; 2549b50d902SRodney W. Grimes 2559b50d902SRodney W. Grimes for (save = s; *s && *s != '.'; ++s); 2569b50d902SRodney W. Grimes if (*s) { 2579b50d902SRodney W. Grimes *s = '\0'; 2589b50d902SRodney W. Grimes fac = decode(save, facilitynames); 259df071556SPhilippe Charnier if (fac < 0) 260df071556SPhilippe Charnier errx(1, "unknown facility name: %s", save); 2619b50d902SRodney W. Grimes *s++ = '.'; 2629b50d902SRodney W. Grimes } 2639b50d902SRodney W. Grimes else { 2649b50d902SRodney W. Grimes fac = 0; 2659b50d902SRodney W. Grimes s = save; 2669b50d902SRodney W. Grimes } 2679b50d902SRodney W. Grimes lev = decode(s, prioritynames); 268df071556SPhilippe Charnier if (lev < 0) 269df071556SPhilippe Charnier errx(1, "unknown priority name: %s", save); 2709b50d902SRodney W. Grimes return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes 2739b50d902SRodney W. Grimes int 274f4ac32deSDavid Malone decode(char *name, CODE *codetab) 2759b50d902SRodney W. Grimes { 276f4ac32deSDavid Malone CODE *c; 2779b50d902SRodney W. Grimes 2789b50d902SRodney W. Grimes if (isdigit(*name)) 2799b50d902SRodney W. Grimes return (atoi(name)); 2809b50d902SRodney W. Grimes 2819b50d902SRodney W. Grimes for (c = codetab; c->c_name; c++) 2829b50d902SRodney W. Grimes if (!strcasecmp(name, c->c_name)) 2839b50d902SRodney W. Grimes return (c->c_val); 2849b50d902SRodney W. Grimes 2859b50d902SRodney W. Grimes return (-1); 2869b50d902SRodney W. Grimes } 2879b50d902SRodney W. Grimes 288df071556SPhilippe Charnier static void 289f4ac32deSDavid Malone usage(void) 2909b50d902SRodney W. Grimes { 291b0fe2da8SDavid Malone (void)fprintf(stderr, "usage: %s\n", 2926b04b7f6SBruce M Simpson "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n" 2936b04b7f6SBruce M Simpson " [message ...]" 294b0fe2da8SDavid Malone ); 2959b50d902SRodney W. Grimes exit(1); 2969b50d902SRodney W. Grimes } 297