xref: /freebsd/usr.bin/logger/logger.c (revision ca122bf7d80e14d2eec1158f1cfaafbf5280d743)
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
44df071556SPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
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 
639b50d902SRodney W. Grimes int	decode __P((char *, CODE *));
649b50d902SRodney W. Grimes int	pencode __P((char *));
65b0fe2da8SDavid Malone static void	logmessage __P((int, char *, char *));
66df071556SPhilippe Charnier static void	usage __P((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
889b50d902SRodney W. Grimes main(argc, argv)
899b50d902SRodney W. Grimes 	int argc;
909b50d902SRodney W. Grimes 	char *argv[];
919b50d902SRodney W. Grimes {
929b50d902SRodney W. Grimes 	int ch, logflags, pri;
93b0fe2da8SDavid Malone 	char *tag, *host, buf[1024];
949b50d902SRodney W. Grimes 
959b50d902SRodney W. Grimes 	tag = NULL;
96b0fe2da8SDavid Malone 	host = NULL;
97ca122bf7SRuslan Ermilov 	pri = LOG_USER | LOG_NOTICE;
989b50d902SRodney W. Grimes 	logflags = 0;
9940244c28SPoul-Henning Kamp 	unsetenv("TZ");
1000b5f90afSHajimu UMEMOTO 	while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
1019b50d902SRodney W. Grimes 		switch((char)ch) {
1020b5f90afSHajimu UMEMOTO 		case '4':
1030b5f90afSHajimu UMEMOTO 			family = PF_INET;
1040b5f90afSHajimu UMEMOTO 			break;
1050b5f90afSHajimu UMEMOTO #ifdef INET6
1060b5f90afSHajimu UMEMOTO 		case '6':
1070b5f90afSHajimu UMEMOTO 			family = PF_INET6;
1080b5f90afSHajimu UMEMOTO 			break;
1090b5f90afSHajimu UMEMOTO #endif
1100b5f90afSHajimu UMEMOTO 		case 'A':
1110b5f90afSHajimu UMEMOTO 			send_to_all++;
1120b5f90afSHajimu UMEMOTO 			break;
1139b50d902SRodney W. Grimes 		case 'f':		/* file to log */
114df071556SPhilippe Charnier 			if (freopen(optarg, "r", stdin) == NULL)
115df071556SPhilippe Charnier 				err(1, "%s", optarg);
1169b50d902SRodney W. Grimes 			break;
117b0fe2da8SDavid Malone 		case 'h':		/* hostname to deliver to */
118b0fe2da8SDavid Malone 			host = optarg;
119b0fe2da8SDavid Malone 			break;
1209b50d902SRodney W. Grimes 		case 'i':		/* log process id also */
1219b50d902SRodney W. Grimes 			logflags |= LOG_PID;
1229b50d902SRodney W. Grimes 			break;
1239b50d902SRodney W. Grimes 		case 'p':		/* priority */
1249b50d902SRodney W. Grimes 			pri = pencode(optarg);
1259b50d902SRodney W. Grimes 			break;
1269b50d902SRodney W. Grimes 		case 's':		/* log to standard error */
1279b50d902SRodney W. Grimes 			logflags |= LOG_PERROR;
1289b50d902SRodney W. Grimes 			break;
1299b50d902SRodney W. Grimes 		case 't':		/* tag */
1309b50d902SRodney W. Grimes 			tag = optarg;
1319b50d902SRodney W. Grimes 			break;
1329b50d902SRodney W. Grimes 		case '?':
1339b50d902SRodney W. Grimes 		default:
1349b50d902SRodney W. Grimes 			usage();
1359b50d902SRodney W. Grimes 		}
1369b50d902SRodney W. Grimes 	argc -= optind;
1379b50d902SRodney W. Grimes 	argv += optind;
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	/* setup for logging */
1409b50d902SRodney W. Grimes 	openlog(tag ? tag : getlogin(), logflags, 0);
1419b50d902SRodney W. Grimes 	(void) fclose(stdout);
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes 	/* log input line if appropriate */
1449b50d902SRodney W. Grimes 	if (argc > 0) {
1459b50d902SRodney W. Grimes 		register char *p, *endp;
1469b50d902SRodney W. Grimes 		int len;
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1499b50d902SRodney W. Grimes 			len = strlen(*argv);
1509b50d902SRodney W. Grimes 			if (p + len > endp && p > buf) {
151b0fe2da8SDavid Malone 				logmessage(pri, host, buf);
1529b50d902SRodney W. Grimes 				p = buf;
1539b50d902SRodney W. Grimes 			}
1549b50d902SRodney W. Grimes 			if (len > sizeof(buf) - 1)
155b0fe2da8SDavid Malone 				logmessage(pri, host, *argv++);
1569b50d902SRodney W. Grimes 			else {
1579b50d902SRodney W. Grimes 				if (p != buf)
1589b50d902SRodney W. Grimes 					*p++ = ' ';
1599b50d902SRodney W. Grimes 				bcopy(*argv++, p, len);
1609b50d902SRodney W. Grimes 				*(p += len) = '\0';
1619b50d902SRodney W. Grimes 			}
1629b50d902SRodney W. Grimes 		}
1639b50d902SRodney W. Grimes 		if (p != buf)
164b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1659b50d902SRodney W. Grimes 	} else
1669b50d902SRodney W. Grimes 		while (fgets(buf, sizeof(buf), stdin) != NULL)
167b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1689b50d902SRodney W. Grimes 	exit(0);
1699b50d902SRodney W. Grimes }
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes /*
172b0fe2da8SDavid Malone  *  Send the message to syslog, either on the local host, or on a remote host
173b0fe2da8SDavid Malone  */
174b0fe2da8SDavid Malone void
175b0fe2da8SDavid Malone logmessage(int pri, char *host, char *buf)
176b0fe2da8SDavid Malone {
1770b5f90afSHajimu UMEMOTO 	static struct socks *socks;
1780b5f90afSHajimu UMEMOTO 	static int nsock = 0;
1790b5f90afSHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
180b0fe2da8SDavid Malone 	char *line;
1810b5f90afSHajimu UMEMOTO 	int maxs, len, sock, error, i, lsent;
182b0fe2da8SDavid Malone 
183b0fe2da8SDavid Malone 	if (host == NULL) {
184b0fe2da8SDavid Malone 		syslog(pri, "%s", buf);
185b0fe2da8SDavid Malone 		return;
186b0fe2da8SDavid Malone 	}
187b0fe2da8SDavid Malone 
1880b5f90afSHajimu UMEMOTO 	if (nsock <= 0) {	/* set up socket stuff */
189b0fe2da8SDavid Malone 		/* resolve hostname */
1900b5f90afSHajimu UMEMOTO 		memset(&hints, 0, sizeof(hints));
1910b5f90afSHajimu UMEMOTO 		hints.ai_family = family;
1920b5f90afSHajimu UMEMOTO 		hints.ai_socktype = SOCK_DGRAM;
1930b5f90afSHajimu UMEMOTO 		error = getaddrinfo(host, "syslog", &hints, &res);
1940b5f90afSHajimu UMEMOTO 		if (error == EAI_SERVICE) {
1950b5f90afSHajimu UMEMOTO 			warnx ("syslog/udp: unknown service");	/* not fatal */
1960b5f90afSHajimu UMEMOTO 			error = getaddrinfo(host, "514", &hints, &res);
1970b5f90afSHajimu UMEMOTO 		}
1980b5f90afSHajimu UMEMOTO 		if (error)
1990b5f90afSHajimu UMEMOTO 			errx(1, "%s: %s", gai_strerror(error), host);
2000b5f90afSHajimu UMEMOTO 		/* count max number of sockets we may open */
2010b5f90afSHajimu UMEMOTO 		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
2020b5f90afSHajimu UMEMOTO 		socks = malloc(maxs * sizeof(struct socks));
2030b5f90afSHajimu UMEMOTO 		if (!socks)
2040b5f90afSHajimu UMEMOTO 			errx(1, "couldn't allocate memory for sockets");
2050b5f90afSHajimu UMEMOTO 		for (r = res; r; r = r->ai_next) {
2060b5f90afSHajimu UMEMOTO 			sock = socket(r->ai_family, r->ai_socktype,
2070b5f90afSHajimu UMEMOTO 				      r->ai_protocol);
208b0fe2da8SDavid Malone 			if (sock < 0)
2090b5f90afSHajimu UMEMOTO 				continue;
2100b5f90afSHajimu UMEMOTO 			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
2110b5f90afSHajimu UMEMOTO 			socks[nsock].addrlen = r->ai_addrlen;
2120b5f90afSHajimu UMEMOTO 			socks[nsock++].sock = sock;
2130b5f90afSHajimu UMEMOTO 		}
2140b5f90afSHajimu UMEMOTO 		freeaddrinfo(res);
2150b5f90afSHajimu UMEMOTO 		if (nsock <= 0)
216b0fe2da8SDavid Malone 			errx(1, "socket");
217b0fe2da8SDavid Malone 	}
218b0fe2da8SDavid Malone 
219b0fe2da8SDavid Malone 	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
220b0fe2da8SDavid Malone 		errx(1, "asprintf");
221b0fe2da8SDavid Malone 
2220b5f90afSHajimu UMEMOTO 	for (i = 0; i < nsock; ++i) {
2230b5f90afSHajimu UMEMOTO 		lsent = sendto(socks[i].sock, line, len, 0,
2240b5f90afSHajimu UMEMOTO 			       (struct sockaddr *)&socks[i].addr,
2250b5f90afSHajimu UMEMOTO 			       socks[i].addrlen);
2260b5f90afSHajimu UMEMOTO 		if (lsent == len && !send_to_all)
2270b5f90afSHajimu UMEMOTO 			break;
2280b5f90afSHajimu UMEMOTO 	}
2290b5f90afSHajimu UMEMOTO 	if (lsent != len)
230b0fe2da8SDavid Malone 		warnx ("sendmsg");
231b0fe2da8SDavid Malone 
232b0fe2da8SDavid Malone 	free(line);
233b0fe2da8SDavid Malone }
234b0fe2da8SDavid Malone 
235b0fe2da8SDavid Malone /*
2369b50d902SRodney W. Grimes  *  Decode a symbolic name to a numeric value
2379b50d902SRodney W. Grimes  */
2389b50d902SRodney W. Grimes int
2399b50d902SRodney W. Grimes pencode(s)
2409b50d902SRodney W. Grimes 	register char *s;
2419b50d902SRodney W. Grimes {
2429b50d902SRodney W. Grimes 	char *save;
2439b50d902SRodney W. Grimes 	int fac, lev;
2449b50d902SRodney W. Grimes 
2459b50d902SRodney W. Grimes 	for (save = s; *s && *s != '.'; ++s);
2469b50d902SRodney W. Grimes 	if (*s) {
2479b50d902SRodney W. Grimes 		*s = '\0';
2489b50d902SRodney W. Grimes 		fac = decode(save, facilitynames);
249df071556SPhilippe Charnier 		if (fac < 0)
250df071556SPhilippe Charnier 			errx(1, "unknown facility name: %s", save);
2519b50d902SRodney W. Grimes 		*s++ = '.';
2529b50d902SRodney W. Grimes 	}
2539b50d902SRodney W. Grimes 	else {
2549b50d902SRodney W. Grimes 		fac = 0;
2559b50d902SRodney W. Grimes 		s = save;
2569b50d902SRodney W. Grimes 	}
2579b50d902SRodney W. Grimes 	lev = decode(s, prioritynames);
258df071556SPhilippe Charnier 	if (lev < 0)
259df071556SPhilippe Charnier 		errx(1, "unknown priority name: %s", save);
2609b50d902SRodney W. Grimes 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2619b50d902SRodney W. Grimes }
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes int
2649b50d902SRodney W. Grimes decode(name, codetab)
2659b50d902SRodney W. Grimes 	char *name;
2669b50d902SRodney W. Grimes 	CODE *codetab;
2679b50d902SRodney W. Grimes {
2689b50d902SRodney W. Grimes 	register CODE *c;
2699b50d902SRodney W. Grimes 
2709b50d902SRodney W. Grimes 	if (isdigit(*name))
2719b50d902SRodney W. Grimes 		return (atoi(name));
2729b50d902SRodney W. Grimes 
2739b50d902SRodney W. Grimes 	for (c = codetab; c->c_name; c++)
2749b50d902SRodney W. Grimes 		if (!strcasecmp(name, c->c_name))
2759b50d902SRodney W. Grimes 			return (c->c_val);
2769b50d902SRodney W. Grimes 
2779b50d902SRodney W. Grimes 	return (-1);
2789b50d902SRodney W. Grimes }
2799b50d902SRodney W. Grimes 
280df071556SPhilippe Charnier static void
2819b50d902SRodney W. Grimes usage()
2829b50d902SRodney W. Grimes {
283b0fe2da8SDavid Malone 	(void)fprintf(stderr, "usage: %s\n",
2840b5f90afSHajimu UMEMOTO 	    "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
285b0fe2da8SDavid Malone 	    );
2869b50d902SRodney W. Grimes 	exit(1);
2879b50d902SRodney W. Grimes }
288