xref: /freebsd/usr.bin/logger/logger.c (revision f4ac32def255b5968bdd8150057b9c0d14595689)
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  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
35df071556SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
41df071556SPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
43df071556SPhilippe Charnier #endif
449b50d902SRodney W. Grimes #endif /* not lint */
459bd5ae85SDavid Malone #include <sys/cdefs.h>
469bd5ae85SDavid Malone __FBSDID("$FreeBSD$");
479bd5ae85SDavid Malone 
48b0fe2da8SDavid Malone #include <sys/types.h>
49b0fe2da8SDavid Malone #include <sys/socket.h>
50b0fe2da8SDavid Malone #include <netinet/in.h>
51b0fe2da8SDavid Malone 
529b50d902SRodney W. Grimes #include <ctype.h>
53df071556SPhilippe Charnier #include <err.h>
54b0fe2da8SDavid Malone #include <netdb.h>
55df071556SPhilippe Charnier #include <stdio.h>
56df071556SPhilippe Charnier #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
58df071556SPhilippe Charnier #include <unistd.h>
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes #define	SYSLOG_NAMES
619b50d902SRodney W. Grimes #include <syslog.h>
629b50d902SRodney W. Grimes 
63f1bb2cd2SWarner Losh int	decode(char *, CODE *);
64f1bb2cd2SWarner Losh int	pencode(char *);
65f1bb2cd2SWarner Losh static void	logmessage(int, char *, char *);
66f1bb2cd2SWarner Losh static void	usage(void);
679b50d902SRodney W. Grimes 
680b5f90afSHajimu UMEMOTO struct socks {
690b5f90afSHajimu UMEMOTO     int sock;
700b5f90afSHajimu UMEMOTO     int addrlen;
710b5f90afSHajimu UMEMOTO     struct sockaddr_storage addr;
720b5f90afSHajimu UMEMOTO };
730b5f90afSHajimu UMEMOTO 
740b5f90afSHajimu UMEMOTO #ifdef INET6
750b5f90afSHajimu UMEMOTO int	family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
760b5f90afSHajimu UMEMOTO #else
770b5f90afSHajimu UMEMOTO int	family = PF_INET;	/* protocol family (IPv4 only) */
780b5f90afSHajimu UMEMOTO #endif
790b5f90afSHajimu UMEMOTO int	send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
800b5f90afSHajimu UMEMOTO 
819b50d902SRodney W. Grimes /*
829b50d902SRodney W. Grimes  * logger -- read and log utility
839b50d902SRodney W. Grimes  *
849b50d902SRodney W. Grimes  *	Reads from an input and arranges to write the result on the system
859b50d902SRodney W. Grimes  *	log.
869b50d902SRodney W. Grimes  */
879b50d902SRodney W. Grimes int
88f4ac32deSDavid Malone main(int argc, char *argv[])
899b50d902SRodney W. Grimes {
909b50d902SRodney W. Grimes 	int ch, logflags, pri;
91b0fe2da8SDavid Malone 	char *tag, *host, buf[1024];
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes 	tag = NULL;
94b0fe2da8SDavid Malone 	host = NULL;
95ca122bf7SRuslan Ermilov 	pri = LOG_USER | LOG_NOTICE;
969b50d902SRodney W. Grimes 	logflags = 0;
9740244c28SPoul-Henning Kamp 	unsetenv("TZ");
980b5f90afSHajimu UMEMOTO 	while ((ch = getopt(argc, argv, "46Af:h:ip: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);
1149b50d902SRodney W. Grimes 			break;
115b0fe2da8SDavid Malone 		case 'h':		/* hostname to deliver to */
116b0fe2da8SDavid Malone 			host = optarg;
117b0fe2da8SDavid Malone 			break;
1189b50d902SRodney W. Grimes 		case 'i':		/* log process id also */
1199b50d902SRodney W. Grimes 			logflags |= LOG_PID;
1209b50d902SRodney W. Grimes 			break;
1219b50d902SRodney W. Grimes 		case 'p':		/* priority */
1229b50d902SRodney W. Grimes 			pri = pencode(optarg);
1239b50d902SRodney W. Grimes 			break;
1249b50d902SRodney W. Grimes 		case 's':		/* log to standard error */
1259b50d902SRodney W. Grimes 			logflags |= LOG_PERROR;
1269b50d902SRodney W. Grimes 			break;
1279b50d902SRodney W. Grimes 		case 't':		/* tag */
1289b50d902SRodney W. Grimes 			tag = optarg;
1299b50d902SRodney W. Grimes 			break;
1309b50d902SRodney W. Grimes 		case '?':
1319b50d902SRodney W. Grimes 		default:
1329b50d902SRodney W. Grimes 			usage();
1339b50d902SRodney W. Grimes 		}
1349b50d902SRodney W. Grimes 	argc -= optind;
1359b50d902SRodney W. Grimes 	argv += optind;
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes 	/* setup for logging */
1389b50d902SRodney W. Grimes 	openlog(tag ? tag : getlogin(), logflags, 0);
1399b50d902SRodney W. Grimes 	(void) fclose(stdout);
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes 	/* log input line if appropriate */
1429b50d902SRodney W. Grimes 	if (argc > 0) {
143f4ac32deSDavid Malone 		char *p, *endp;
1449bd5ae85SDavid Malone 		size_t len;
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1479b50d902SRodney W. Grimes 			len = strlen(*argv);
1489b50d902SRodney W. Grimes 			if (p + len > endp && p > buf) {
149b0fe2da8SDavid Malone 				logmessage(pri, host, buf);
1509b50d902SRodney W. Grimes 				p = buf;
1519b50d902SRodney W. Grimes 			}
1529b50d902SRodney W. Grimes 			if (len > sizeof(buf) - 1)
153b0fe2da8SDavid Malone 				logmessage(pri, host, *argv++);
1549b50d902SRodney W. Grimes 			else {
1559b50d902SRodney W. Grimes 				if (p != buf)
1569b50d902SRodney W. Grimes 					*p++ = ' ';
1579b50d902SRodney W. Grimes 				bcopy(*argv++, p, len);
1589b50d902SRodney W. Grimes 				*(p += len) = '\0';
1599b50d902SRodney W. Grimes 			}
1609b50d902SRodney W. Grimes 		}
1619b50d902SRodney W. Grimes 		if (p != buf)
162b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1639b50d902SRodney W. Grimes 	} else
1649b50d902SRodney W. Grimes 		while (fgets(buf, sizeof(buf), stdin) != NULL)
165b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1669b50d902SRodney W. Grimes 	exit(0);
1679b50d902SRodney W. Grimes }
1689b50d902SRodney W. Grimes 
1699b50d902SRodney W. Grimes /*
170b0fe2da8SDavid Malone  *  Send the message to syslog, either on the local host, or on a remote host
171b0fe2da8SDavid Malone  */
172b0fe2da8SDavid Malone void
173b0fe2da8SDavid Malone logmessage(int pri, char *host, char *buf)
174b0fe2da8SDavid Malone {
1750b5f90afSHajimu UMEMOTO 	static struct socks *socks;
1760b5f90afSHajimu UMEMOTO 	static int nsock = 0;
1770b5f90afSHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
178b0fe2da8SDavid Malone 	char *line;
1790b5f90afSHajimu UMEMOTO 	int maxs, len, sock, error, i, lsent;
180b0fe2da8SDavid Malone 
181b0fe2da8SDavid Malone 	if (host == NULL) {
182b0fe2da8SDavid Malone 		syslog(pri, "%s", buf);
183b0fe2da8SDavid Malone 		return;
184b0fe2da8SDavid Malone 	}
185b0fe2da8SDavid Malone 
1860b5f90afSHajimu UMEMOTO 	if (nsock <= 0) {	/* set up socket stuff */
187b0fe2da8SDavid Malone 		/* resolve hostname */
1880b5f90afSHajimu UMEMOTO 		memset(&hints, 0, sizeof(hints));
1890b5f90afSHajimu UMEMOTO 		hints.ai_family = family;
1900b5f90afSHajimu UMEMOTO 		hints.ai_socktype = SOCK_DGRAM;
1910b5f90afSHajimu UMEMOTO 		error = getaddrinfo(host, "syslog", &hints, &res);
1920b5f90afSHajimu UMEMOTO 		if (error == EAI_SERVICE) {
1930b5f90afSHajimu UMEMOTO 			warnx ("syslog/udp: unknown service");	/* not fatal */
1940b5f90afSHajimu UMEMOTO 			error = getaddrinfo(host, "514", &hints, &res);
1950b5f90afSHajimu UMEMOTO 		}
1960b5f90afSHajimu UMEMOTO 		if (error)
1970b5f90afSHajimu UMEMOTO 			errx(1, "%s: %s", gai_strerror(error), host);
1980b5f90afSHajimu UMEMOTO 		/* count max number of sockets we may open */
1990b5f90afSHajimu UMEMOTO 		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
2000b5f90afSHajimu UMEMOTO 		socks = malloc(maxs * sizeof(struct socks));
2010b5f90afSHajimu UMEMOTO 		if (!socks)
2020b5f90afSHajimu UMEMOTO 			errx(1, "couldn't allocate memory for sockets");
2030b5f90afSHajimu UMEMOTO 		for (r = res; r; r = r->ai_next) {
2040b5f90afSHajimu UMEMOTO 			sock = socket(r->ai_family, r->ai_socktype,
2050b5f90afSHajimu UMEMOTO 				      r->ai_protocol);
206b0fe2da8SDavid Malone 			if (sock < 0)
2070b5f90afSHajimu UMEMOTO 				continue;
2080b5f90afSHajimu UMEMOTO 			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
2090b5f90afSHajimu UMEMOTO 			socks[nsock].addrlen = r->ai_addrlen;
2100b5f90afSHajimu UMEMOTO 			socks[nsock++].sock = sock;
2110b5f90afSHajimu UMEMOTO 		}
2120b5f90afSHajimu UMEMOTO 		freeaddrinfo(res);
2130b5f90afSHajimu UMEMOTO 		if (nsock <= 0)
214b0fe2da8SDavid Malone 			errx(1, "socket");
215b0fe2da8SDavid Malone 	}
216b0fe2da8SDavid Malone 
217b0fe2da8SDavid Malone 	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
218b0fe2da8SDavid Malone 		errx(1, "asprintf");
219b0fe2da8SDavid Malone 
2200b5f90afSHajimu UMEMOTO 	for (i = 0; i < nsock; ++i) {
2210b5f90afSHajimu UMEMOTO 		lsent = sendto(socks[i].sock, line, len, 0,
2220b5f90afSHajimu UMEMOTO 			       (struct sockaddr *)&socks[i].addr,
2230b5f90afSHajimu UMEMOTO 			       socks[i].addrlen);
2240b5f90afSHajimu UMEMOTO 		if (lsent == len && !send_to_all)
2250b5f90afSHajimu UMEMOTO 			break;
2260b5f90afSHajimu UMEMOTO 	}
2279bd5ae85SDavid Malone 	if (lsent != len) {
22857c1a0b6SBill Fenner 		if (lsent == -1)
22957c1a0b6SBill Fenner 			warn ("sendto");
23057c1a0b6SBill Fenner 		else
23157c1a0b6SBill Fenner 			warnx ("sendto: short send - %d bytes", lsent);
2329bd5ae85SDavid Malone 	}
233b0fe2da8SDavid Malone 
234b0fe2da8SDavid Malone 	free(line);
235b0fe2da8SDavid Malone }
236b0fe2da8SDavid Malone 
237b0fe2da8SDavid Malone /*
2389b50d902SRodney W. Grimes  *  Decode a symbolic name to a numeric value
2399b50d902SRodney W. Grimes  */
2409b50d902SRodney W. Grimes int
241f4ac32deSDavid Malone pencode(char *s)
2429b50d902SRodney W. Grimes {
2439b50d902SRodney W. Grimes 	char *save;
2449b50d902SRodney W. Grimes 	int fac, lev;
2459b50d902SRodney W. Grimes 
2469b50d902SRodney W. Grimes 	for (save = s; *s && *s != '.'; ++s);
2479b50d902SRodney W. Grimes 	if (*s) {
2489b50d902SRodney W. Grimes 		*s = '\0';
2499b50d902SRodney W. Grimes 		fac = decode(save, facilitynames);
250df071556SPhilippe Charnier 		if (fac < 0)
251df071556SPhilippe Charnier 			errx(1, "unknown facility name: %s", save);
2529b50d902SRodney W. Grimes 		*s++ = '.';
2539b50d902SRodney W. Grimes 	}
2549b50d902SRodney W. Grimes 	else {
2559b50d902SRodney W. Grimes 		fac = 0;
2569b50d902SRodney W. Grimes 		s = save;
2579b50d902SRodney W. Grimes 	}
2589b50d902SRodney W. Grimes 	lev = decode(s, prioritynames);
259df071556SPhilippe Charnier 	if (lev < 0)
260df071556SPhilippe Charnier 		errx(1, "unknown priority name: %s", save);
2619b50d902SRodney W. Grimes 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2629b50d902SRodney W. Grimes }
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes int
265f4ac32deSDavid Malone decode(char *name, CODE *codetab)
2669b50d902SRodney W. Grimes {
267f4ac32deSDavid Malone 	CODE *c;
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes 	if (isdigit(*name))
2709b50d902SRodney W. Grimes 		return (atoi(name));
2719b50d902SRodney W. Grimes 
2729b50d902SRodney W. Grimes 	for (c = codetab; c->c_name; c++)
2739b50d902SRodney W. Grimes 		if (!strcasecmp(name, c->c_name))
2749b50d902SRodney W. Grimes 			return (c->c_val);
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	return (-1);
2779b50d902SRodney W. Grimes }
2789b50d902SRodney W. Grimes 
279df071556SPhilippe Charnier static void
280f4ac32deSDavid Malone usage(void)
2819b50d902SRodney W. Grimes {
282b0fe2da8SDavid Malone 	(void)fprintf(stderr, "usage: %s\n",
2830b5f90afSHajimu UMEMOTO 	    "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
284b0fe2da8SDavid Malone 	    );
2859b50d902SRodney W. Grimes 	exit(1);
2869b50d902SRodney W. Grimes }
287