logger.c revision 93525
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527604Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1983, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127604Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
4327604Scharnier#endif
441590Srgrimes#endif /* not lint */
451590Srgrimes
4693525Sdwmalone#include <sys/cdefs.h>
4793525Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/logger/logger.c 93525 2002-04-01 11:06:00Z dwmalone $");
4893525Sdwmalone
4963402Sdwmalone#include <sys/types.h>
5063402Sdwmalone#include <sys/socket.h>
5163402Sdwmalone#include <netinet/in.h>
5263402Sdwmalone
5327604Scharnier#include <ctype.h>
5427604Scharnier#include <err.h>
5563402Sdwmalone#include <netdb.h>
5627604Scharnier#include <stdio.h>
571590Srgrimes#include <stdlib.h>
581590Srgrimes#include <string.h>
5927604Scharnier#include <unistd.h>
601590Srgrimes
611590Srgrimes#define	SYSLOG_NAMES
621590Srgrimes#include <syslog.h>
631590Srgrimes
6492920Simpint	decode(char *, CODE *);
6592920Simpint	pencode(char *);
6692920Simpstatic void	logmessage(int, char *, char *);
6792920Simpstatic void	usage(void);
681590Srgrimes
6970100Sumestruct socks {
7070100Sume    int sock;
7170100Sume    int addrlen;
7270100Sume    struct sockaddr_storage addr;
7370100Sume};
7470100Sume
7570100Sume#ifdef INET6
7670100Sumeint	family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
7770100Sume#else
7870100Sumeint	family = PF_INET;	/* protocol family (IPv4 only) */
7970100Sume#endif
8070100Sumeint	send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
8170100Sume
821590Srgrimes/*
831590Srgrimes * logger -- read and log utility
841590Srgrimes *
851590Srgrimes *	Reads from an input and arranges to write the result on the system
861590Srgrimes *	log.
871590Srgrimes */
881590Srgrimesint
891590Srgrimesmain(argc, argv)
901590Srgrimes	int argc;
911590Srgrimes	char *argv[];
921590Srgrimes{
931590Srgrimes	int ch, logflags, pri;
9463402Sdwmalone	char *tag, *host, buf[1024];
951590Srgrimes
961590Srgrimes	tag = NULL;
9763402Sdwmalone	host = NULL;
9883150Sru	pri = LOG_USER | LOG_NOTICE;
991590Srgrimes	logflags = 0;
10019075Sphk	unsetenv("TZ");
10170100Sume	while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
1021590Srgrimes		switch((char)ch) {
10370100Sume		case '4':
10470100Sume			family = PF_INET;
10570100Sume			break;
10670100Sume#ifdef INET6
10770100Sume		case '6':
10870100Sume			family = PF_INET6;
10970100Sume			break;
11070100Sume#endif
11170100Sume		case 'A':
11270100Sume			send_to_all++;
11370100Sume			break;
1141590Srgrimes		case 'f':		/* file to log */
11527604Scharnier			if (freopen(optarg, "r", stdin) == NULL)
11627604Scharnier				err(1, "%s", optarg);
1171590Srgrimes			break;
11863402Sdwmalone		case 'h':		/* hostname to deliver to */
11963402Sdwmalone			host = optarg;
12063402Sdwmalone			break;
1211590Srgrimes		case 'i':		/* log process id also */
1221590Srgrimes			logflags |= LOG_PID;
1231590Srgrimes			break;
1241590Srgrimes		case 'p':		/* priority */
1251590Srgrimes			pri = pencode(optarg);
1261590Srgrimes			break;
1271590Srgrimes		case 's':		/* log to standard error */
1281590Srgrimes			logflags |= LOG_PERROR;
1291590Srgrimes			break;
1301590Srgrimes		case 't':		/* tag */
1311590Srgrimes			tag = optarg;
1321590Srgrimes			break;
1331590Srgrimes		case '?':
1341590Srgrimes		default:
1351590Srgrimes			usage();
1361590Srgrimes		}
1371590Srgrimes	argc -= optind;
1381590Srgrimes	argv += optind;
1391590Srgrimes
1401590Srgrimes	/* setup for logging */
1411590Srgrimes	openlog(tag ? tag : getlogin(), logflags, 0);
1421590Srgrimes	(void) fclose(stdout);
1431590Srgrimes
1441590Srgrimes	/* log input line if appropriate */
1451590Srgrimes	if (argc > 0) {
1461590Srgrimes		register char *p, *endp;
14793525Sdwmalone		size_t len;
1481590Srgrimes
1491590Srgrimes		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1501590Srgrimes			len = strlen(*argv);
1511590Srgrimes			if (p + len > endp && p > buf) {
15263402Sdwmalone				logmessage(pri, host, buf);
1531590Srgrimes				p = buf;
1541590Srgrimes			}
1551590Srgrimes			if (len > sizeof(buf) - 1)
15663402Sdwmalone				logmessage(pri, host, *argv++);
1571590Srgrimes			else {
1581590Srgrimes				if (p != buf)
1591590Srgrimes					*p++ = ' ';
1601590Srgrimes				bcopy(*argv++, p, len);
1611590Srgrimes				*(p += len) = '\0';
1621590Srgrimes			}
1631590Srgrimes		}
1641590Srgrimes		if (p != buf)
16563402Sdwmalone			logmessage(pri, host, buf);
1661590Srgrimes	} else
1671590Srgrimes		while (fgets(buf, sizeof(buf), stdin) != NULL)
16863402Sdwmalone			logmessage(pri, host, buf);
1691590Srgrimes	exit(0);
1701590Srgrimes}
1711590Srgrimes
1721590Srgrimes/*
17363402Sdwmalone *  Send the message to syslog, either on the local host, or on a remote host
17463402Sdwmalone */
17563402Sdwmalonevoid
17663402Sdwmalonelogmessage(int pri, char *host, char *buf)
17763402Sdwmalone{
17870100Sume	static struct socks *socks;
17970100Sume	static int nsock = 0;
18070100Sume	struct addrinfo hints, *res, *r;
18163402Sdwmalone	char *line;
18270100Sume	int maxs, len, sock, error, i, lsent;
18363402Sdwmalone
18463402Sdwmalone	if (host == NULL) {
18563402Sdwmalone		syslog(pri, "%s", buf);
18663402Sdwmalone		return;
18763402Sdwmalone	}
18863402Sdwmalone
18970100Sume	if (nsock <= 0) {	/* set up socket stuff */
19070100Sume		/* resolve hostname */
19170100Sume		memset(&hints, 0, sizeof(hints));
19270100Sume		hints.ai_family = family;
19370100Sume		hints.ai_socktype = SOCK_DGRAM;
19470100Sume		error = getaddrinfo(host, "syslog", &hints, &res);
19570100Sume		if (error == EAI_SERVICE) {
19663402Sdwmalone			warnx ("syslog/udp: unknown service");	/* not fatal */
19770100Sume			error = getaddrinfo(host, "514", &hints, &res);
19870100Sume		}
19970100Sume		if (error)
20070100Sume			errx(1, "%s: %s", gai_strerror(error), host);
20170100Sume		/* count max number of sockets we may open */
20270100Sume		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
20370100Sume		socks = malloc(maxs * sizeof(struct socks));
20470100Sume		if (!socks)
20570100Sume			errx(1, "couldn't allocate memory for sockets");
20670100Sume		for (r = res; r; r = r->ai_next) {
20770100Sume			sock = socket(r->ai_family, r->ai_socktype,
20870100Sume				      r->ai_protocol);
20970100Sume			if (sock < 0)
21070100Sume				continue;
21170100Sume			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
21270100Sume			socks[nsock].addrlen = r->ai_addrlen;
21370100Sume			socks[nsock++].sock = sock;
21470100Sume		}
21570100Sume		freeaddrinfo(res);
21670100Sume		if (nsock <= 0)
21763402Sdwmalone			errx(1, "socket");
21863402Sdwmalone	}
21963402Sdwmalone
22063402Sdwmalone	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
22163402Sdwmalone		errx(1, "asprintf");
22263402Sdwmalone
22370100Sume	for (i = 0; i < nsock; ++i) {
22470100Sume		lsent = sendto(socks[i].sock, line, len, 0,
22570100Sume			       (struct sockaddr *)&socks[i].addr,
22670100Sume			       socks[i].addrlen);
22770100Sume		if (lsent == len && !send_to_all)
22870100Sume			break;
22970100Sume	}
23093525Sdwmalone	if (lsent != len) {
23191433Sfenner		if (lsent == -1)
23291433Sfenner			warn ("sendto");
23391433Sfenner		else
23491433Sfenner			warnx ("sendto: short send - %d bytes", lsent);
23593525Sdwmalone	}
23663402Sdwmalone
23763402Sdwmalone	free(line);
23863402Sdwmalone}
23963402Sdwmalone
24063402Sdwmalone/*
2411590Srgrimes *  Decode a symbolic name to a numeric value
2421590Srgrimes */
2431590Srgrimesint
2441590Srgrimespencode(s)
2451590Srgrimes	register char *s;
2461590Srgrimes{
2471590Srgrimes	char *save;
2481590Srgrimes	int fac, lev;
2491590Srgrimes
2501590Srgrimes	for (save = s; *s && *s != '.'; ++s);
2511590Srgrimes	if (*s) {
2521590Srgrimes		*s = '\0';
2531590Srgrimes		fac = decode(save, facilitynames);
25427604Scharnier		if (fac < 0)
25527604Scharnier			errx(1, "unknown facility name: %s", save);
2561590Srgrimes		*s++ = '.';
2571590Srgrimes	}
2581590Srgrimes	else {
2591590Srgrimes		fac = 0;
2601590Srgrimes		s = save;
2611590Srgrimes	}
2621590Srgrimes	lev = decode(s, prioritynames);
26327604Scharnier	if (lev < 0)
26427604Scharnier		errx(1, "unknown priority name: %s", save);
2651590Srgrimes	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2661590Srgrimes}
2671590Srgrimes
2681590Srgrimesint
2691590Srgrimesdecode(name, codetab)
2701590Srgrimes	char *name;
2711590Srgrimes	CODE *codetab;
2721590Srgrimes{
2731590Srgrimes	register CODE *c;
2741590Srgrimes
2751590Srgrimes	if (isdigit(*name))
2761590Srgrimes		return (atoi(name));
2771590Srgrimes
2781590Srgrimes	for (c = codetab; c->c_name; c++)
2791590Srgrimes		if (!strcasecmp(name, c->c_name))
2801590Srgrimes			return (c->c_val);
2811590Srgrimes
2821590Srgrimes	return (-1);
2831590Srgrimes}
2841590Srgrimes
28527604Scharnierstatic void
2861590Srgrimesusage()
2871590Srgrimes{
28863402Sdwmalone	(void)fprintf(stderr, "usage: %s\n",
28970100Sume	    "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
29063402Sdwmalone	    );
2911590Srgrimes	exit(1);
2921590Srgrimes}
293