xref: /freebsd/usr.bin/logger/logger.c (revision 9bd5ae85680e93f200d5b8b19a87232b2173b4ad)
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 */
459b50d902SRodney W. Grimes 
469bd5ae85SDavid Malone #include <sys/cdefs.h>
479bd5ae85SDavid Malone __FBSDID("$FreeBSD$");
489bd5ae85SDavid Malone 
49b0fe2da8SDavid Malone #include <sys/types.h>
50b0fe2da8SDavid Malone #include <sys/socket.h>
51b0fe2da8SDavid Malone #include <netinet/in.h>
52b0fe2da8SDavid Malone 
539b50d902SRodney W. Grimes #include <ctype.h>
54df071556SPhilippe Charnier #include <err.h>
55b0fe2da8SDavid Malone #include <netdb.h>
56df071556SPhilippe Charnier #include <stdio.h>
57df071556SPhilippe Charnier #include <stdlib.h>
589b50d902SRodney W. Grimes #include <string.h>
59df071556SPhilippe Charnier #include <unistd.h>
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes #define	SYSLOG_NAMES
629b50d902SRodney W. Grimes #include <syslog.h>
639b50d902SRodney W. Grimes 
64f1bb2cd2SWarner Losh int	decode(char *, CODE *);
65f1bb2cd2SWarner Losh int	pencode(char *);
66f1bb2cd2SWarner Losh static void	logmessage(int, char *, char *);
67f1bb2cd2SWarner Losh static void	usage(void);
689b50d902SRodney W. Grimes 
690b5f90afSHajimu UMEMOTO struct socks {
700b5f90afSHajimu UMEMOTO     int sock;
710b5f90afSHajimu UMEMOTO     int addrlen;
720b5f90afSHajimu UMEMOTO     struct sockaddr_storage addr;
730b5f90afSHajimu UMEMOTO };
740b5f90afSHajimu UMEMOTO 
750b5f90afSHajimu UMEMOTO #ifdef INET6
760b5f90afSHajimu UMEMOTO int	family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
770b5f90afSHajimu UMEMOTO #else
780b5f90afSHajimu UMEMOTO int	family = PF_INET;	/* protocol family (IPv4 only) */
790b5f90afSHajimu UMEMOTO #endif
800b5f90afSHajimu UMEMOTO int	send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
810b5f90afSHajimu UMEMOTO 
829b50d902SRodney W. Grimes /*
839b50d902SRodney W. Grimes  * logger -- read and log utility
849b50d902SRodney W. Grimes  *
859b50d902SRodney W. Grimes  *	Reads from an input and arranges to write the result on the system
869b50d902SRodney W. Grimes  *	log.
879b50d902SRodney W. Grimes  */
889b50d902SRodney W. Grimes int
899b50d902SRodney W. Grimes main(argc, argv)
909b50d902SRodney W. Grimes 	int argc;
919b50d902SRodney W. Grimes 	char *argv[];
929b50d902SRodney W. Grimes {
939b50d902SRodney W. Grimes 	int ch, logflags, pri;
94b0fe2da8SDavid Malone 	char *tag, *host, buf[1024];
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes 	tag = NULL;
97b0fe2da8SDavid Malone 	host = NULL;
98ca122bf7SRuslan Ermilov 	pri = LOG_USER | LOG_NOTICE;
999b50d902SRodney W. Grimes 	logflags = 0;
10040244c28SPoul-Henning Kamp 	unsetenv("TZ");
1010b5f90afSHajimu UMEMOTO 	while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
1029b50d902SRodney W. Grimes 		switch((char)ch) {
1030b5f90afSHajimu UMEMOTO 		case '4':
1040b5f90afSHajimu UMEMOTO 			family = PF_INET;
1050b5f90afSHajimu UMEMOTO 			break;
1060b5f90afSHajimu UMEMOTO #ifdef INET6
1070b5f90afSHajimu UMEMOTO 		case '6':
1080b5f90afSHajimu UMEMOTO 			family = PF_INET6;
1090b5f90afSHajimu UMEMOTO 			break;
1100b5f90afSHajimu UMEMOTO #endif
1110b5f90afSHajimu UMEMOTO 		case 'A':
1120b5f90afSHajimu UMEMOTO 			send_to_all++;
1130b5f90afSHajimu UMEMOTO 			break;
1149b50d902SRodney W. Grimes 		case 'f':		/* file to log */
115df071556SPhilippe Charnier 			if (freopen(optarg, "r", stdin) == NULL)
116df071556SPhilippe Charnier 				err(1, "%s", optarg);
1179b50d902SRodney W. Grimes 			break;
118b0fe2da8SDavid Malone 		case 'h':		/* hostname to deliver to */
119b0fe2da8SDavid Malone 			host = optarg;
120b0fe2da8SDavid Malone 			break;
1219b50d902SRodney W. Grimes 		case 'i':		/* log process id also */
1229b50d902SRodney W. Grimes 			logflags |= LOG_PID;
1239b50d902SRodney W. Grimes 			break;
1249b50d902SRodney W. Grimes 		case 'p':		/* priority */
1259b50d902SRodney W. Grimes 			pri = pencode(optarg);
1269b50d902SRodney W. Grimes 			break;
1279b50d902SRodney W. Grimes 		case 's':		/* log to standard error */
1289b50d902SRodney W. Grimes 			logflags |= LOG_PERROR;
1299b50d902SRodney W. Grimes 			break;
1309b50d902SRodney W. Grimes 		case 't':		/* tag */
1319b50d902SRodney W. Grimes 			tag = optarg;
1329b50d902SRodney W. Grimes 			break;
1339b50d902SRodney W. Grimes 		case '?':
1349b50d902SRodney W. Grimes 		default:
1359b50d902SRodney W. Grimes 			usage();
1369b50d902SRodney W. Grimes 		}
1379b50d902SRodney W. Grimes 	argc -= optind;
1389b50d902SRodney W. Grimes 	argv += optind;
1399b50d902SRodney W. Grimes 
1409b50d902SRodney W. Grimes 	/* setup for logging */
1419b50d902SRodney W. Grimes 	openlog(tag ? tag : getlogin(), logflags, 0);
1429b50d902SRodney W. Grimes 	(void) fclose(stdout);
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes 	/* log input line if appropriate */
1459b50d902SRodney W. Grimes 	if (argc > 0) {
1469b50d902SRodney W. Grimes 		register char *p, *endp;
1479bd5ae85SDavid Malone 		size_t len;
1489b50d902SRodney W. Grimes 
1499b50d902SRodney W. Grimes 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1509b50d902SRodney W. Grimes 			len = strlen(*argv);
1519b50d902SRodney W. Grimes 			if (p + len > endp && p > buf) {
152b0fe2da8SDavid Malone 				logmessage(pri, host, buf);
1539b50d902SRodney W. Grimes 				p = buf;
1549b50d902SRodney W. Grimes 			}
1559b50d902SRodney W. Grimes 			if (len > sizeof(buf) - 1)
156b0fe2da8SDavid Malone 				logmessage(pri, host, *argv++);
1579b50d902SRodney W. Grimes 			else {
1589b50d902SRodney W. Grimes 				if (p != buf)
1599b50d902SRodney W. Grimes 					*p++ = ' ';
1609b50d902SRodney W. Grimes 				bcopy(*argv++, p, len);
1619b50d902SRodney W. Grimes 				*(p += len) = '\0';
1629b50d902SRodney W. Grimes 			}
1639b50d902SRodney W. Grimes 		}
1649b50d902SRodney W. Grimes 		if (p != buf)
165b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1669b50d902SRodney W. Grimes 	} else
1679b50d902SRodney W. Grimes 		while (fgets(buf, sizeof(buf), stdin) != NULL)
168b0fe2da8SDavid Malone 			logmessage(pri, host, buf);
1699b50d902SRodney W. Grimes 	exit(0);
1709b50d902SRodney W. Grimes }
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes /*
173b0fe2da8SDavid Malone  *  Send the message to syslog, either on the local host, or on a remote host
174b0fe2da8SDavid Malone  */
175b0fe2da8SDavid Malone void
176b0fe2da8SDavid Malone logmessage(int pri, char *host, char *buf)
177b0fe2da8SDavid Malone {
1780b5f90afSHajimu UMEMOTO 	static struct socks *socks;
1790b5f90afSHajimu UMEMOTO 	static int nsock = 0;
1800b5f90afSHajimu UMEMOTO 	struct addrinfo hints, *res, *r;
181b0fe2da8SDavid Malone 	char *line;
1820b5f90afSHajimu UMEMOTO 	int maxs, len, sock, error, i, lsent;
183b0fe2da8SDavid Malone 
184b0fe2da8SDavid Malone 	if (host == NULL) {
185b0fe2da8SDavid Malone 		syslog(pri, "%s", buf);
186b0fe2da8SDavid Malone 		return;
187b0fe2da8SDavid Malone 	}
188b0fe2da8SDavid Malone 
1890b5f90afSHajimu UMEMOTO 	if (nsock <= 0) {	/* set up socket stuff */
190b0fe2da8SDavid Malone 		/* resolve hostname */
1910b5f90afSHajimu UMEMOTO 		memset(&hints, 0, sizeof(hints));
1920b5f90afSHajimu UMEMOTO 		hints.ai_family = family;
1930b5f90afSHajimu UMEMOTO 		hints.ai_socktype = SOCK_DGRAM;
1940b5f90afSHajimu UMEMOTO 		error = getaddrinfo(host, "syslog", &hints, &res);
1950b5f90afSHajimu UMEMOTO 		if (error == EAI_SERVICE) {
1960b5f90afSHajimu UMEMOTO 			warnx ("syslog/udp: unknown service");	/* not fatal */
1970b5f90afSHajimu UMEMOTO 			error = getaddrinfo(host, "514", &hints, &res);
1980b5f90afSHajimu UMEMOTO 		}
1990b5f90afSHajimu UMEMOTO 		if (error)
2000b5f90afSHajimu UMEMOTO 			errx(1, "%s: %s", gai_strerror(error), host);
2010b5f90afSHajimu UMEMOTO 		/* count max number of sockets we may open */
2020b5f90afSHajimu UMEMOTO 		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
2030b5f90afSHajimu UMEMOTO 		socks = malloc(maxs * sizeof(struct socks));
2040b5f90afSHajimu UMEMOTO 		if (!socks)
2050b5f90afSHajimu UMEMOTO 			errx(1, "couldn't allocate memory for sockets");
2060b5f90afSHajimu UMEMOTO 		for (r = res; r; r = r->ai_next) {
2070b5f90afSHajimu UMEMOTO 			sock = socket(r->ai_family, r->ai_socktype,
2080b5f90afSHajimu UMEMOTO 				      r->ai_protocol);
209b0fe2da8SDavid Malone 			if (sock < 0)
2100b5f90afSHajimu UMEMOTO 				continue;
2110b5f90afSHajimu UMEMOTO 			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
2120b5f90afSHajimu UMEMOTO 			socks[nsock].addrlen = r->ai_addrlen;
2130b5f90afSHajimu UMEMOTO 			socks[nsock++].sock = sock;
2140b5f90afSHajimu UMEMOTO 		}
2150b5f90afSHajimu UMEMOTO 		freeaddrinfo(res);
2160b5f90afSHajimu UMEMOTO 		if (nsock <= 0)
217b0fe2da8SDavid Malone 			errx(1, "socket");
218b0fe2da8SDavid Malone 	}
219b0fe2da8SDavid Malone 
220b0fe2da8SDavid Malone 	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
221b0fe2da8SDavid Malone 		errx(1, "asprintf");
222b0fe2da8SDavid Malone 
2230b5f90afSHajimu UMEMOTO 	for (i = 0; i < nsock; ++i) {
2240b5f90afSHajimu UMEMOTO 		lsent = sendto(socks[i].sock, line, len, 0,
2250b5f90afSHajimu UMEMOTO 			       (struct sockaddr *)&socks[i].addr,
2260b5f90afSHajimu UMEMOTO 			       socks[i].addrlen);
2270b5f90afSHajimu UMEMOTO 		if (lsent == len && !send_to_all)
2280b5f90afSHajimu UMEMOTO 			break;
2290b5f90afSHajimu UMEMOTO 	}
2309bd5ae85SDavid Malone 	if (lsent != len) {
23157c1a0b6SBill Fenner 		if (lsent == -1)
23257c1a0b6SBill Fenner 			warn ("sendto");
23357c1a0b6SBill Fenner 		else
23457c1a0b6SBill Fenner 			warnx ("sendto: short send - %d bytes", lsent);
2359bd5ae85SDavid Malone 	}
236b0fe2da8SDavid Malone 
237b0fe2da8SDavid Malone 	free(line);
238b0fe2da8SDavid Malone }
239b0fe2da8SDavid Malone 
240b0fe2da8SDavid Malone /*
2419b50d902SRodney W. Grimes  *  Decode a symbolic name to a numeric value
2429b50d902SRodney W. Grimes  */
2439b50d902SRodney W. Grimes int
2449b50d902SRodney W. Grimes pencode(s)
2459b50d902SRodney W. Grimes 	register char *s;
2469b50d902SRodney W. Grimes {
2479b50d902SRodney W. Grimes 	char *save;
2489b50d902SRodney W. Grimes 	int fac, lev;
2499b50d902SRodney W. Grimes 
2509b50d902SRodney W. Grimes 	for (save = s; *s && *s != '.'; ++s);
2519b50d902SRodney W. Grimes 	if (*s) {
2529b50d902SRodney W. Grimes 		*s = '\0';
2539b50d902SRodney W. Grimes 		fac = decode(save, facilitynames);
254df071556SPhilippe Charnier 		if (fac < 0)
255df071556SPhilippe Charnier 			errx(1, "unknown facility name: %s", save);
2569b50d902SRodney W. Grimes 		*s++ = '.';
2579b50d902SRodney W. Grimes 	}
2589b50d902SRodney W. Grimes 	else {
2599b50d902SRodney W. Grimes 		fac = 0;
2609b50d902SRodney W. Grimes 		s = save;
2619b50d902SRodney W. Grimes 	}
2629b50d902SRodney W. Grimes 	lev = decode(s, prioritynames);
263df071556SPhilippe Charnier 	if (lev < 0)
264df071556SPhilippe Charnier 		errx(1, "unknown priority name: %s", save);
2659b50d902SRodney W. Grimes 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2669b50d902SRodney W. Grimes }
2679b50d902SRodney W. Grimes 
2689b50d902SRodney W. Grimes int
2699b50d902SRodney W. Grimes decode(name, codetab)
2709b50d902SRodney W. Grimes 	char *name;
2719b50d902SRodney W. Grimes 	CODE *codetab;
2729b50d902SRodney W. Grimes {
2739b50d902SRodney W. Grimes 	register CODE *c;
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes 	if (isdigit(*name))
2769b50d902SRodney W. Grimes 		return (atoi(name));
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	for (c = codetab; c->c_name; c++)
2799b50d902SRodney W. Grimes 		if (!strcasecmp(name, c->c_name))
2809b50d902SRodney W. Grimes 			return (c->c_val);
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	return (-1);
2839b50d902SRodney W. Grimes }
2849b50d902SRodney W. Grimes 
285df071556SPhilippe Charnier static void
2869b50d902SRodney W. Grimes usage()
2879b50d902SRodney W. Grimes {
288b0fe2da8SDavid Malone 	(void)fprintf(stderr, "usage: %s\n",
2890b5f90afSHajimu UMEMOTO 	    "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
290b0fe2da8SDavid Malone 	    );
2919b50d902SRodney W. Grimes 	exit(1);
2929b50d902SRodney W. Grimes }
293