logger.c revision 198702
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
40146756Scharnier#if 0
411590Srgrimes#ifndef lint
421590Srgrimesstatic char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
43146756Scharnier#endif /* not lint */
4427604Scharnier#endif
45146756Scharnier
4693525Sdwmalone#include <sys/cdefs.h>
4793525Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/logger/logger.c 198702 2009-10-30 21:54:53Z mckusick $");
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 *);
66169344Sdwmalonestatic void	logmessage(int, const char *, const char *, const 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
89102944Sdwmalonemain(int argc, char *argv[])
901590Srgrimes{
911590Srgrimes	int ch, logflags, pri;
92169344Sdwmalone	char *tag, *host, buf[1024];
93169344Sdwmalone	const char *svcname;
941590Srgrimes
951590Srgrimes	tag = NULL;
9663402Sdwmalone	host = NULL;
97160917Sbms	svcname = "syslog";
9883150Sru	pri = LOG_USER | LOG_NOTICE;
991590Srgrimes	logflags = 0;
10019075Sphk	unsetenv("TZ");
101160917Sbms	while ((ch = getopt(argc, argv, "46Af:h:iP:p: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);
117198702Smckusick			setvbuf(stdin, 0, _IONBF, 0);
1181590Srgrimes			break;
11963402Sdwmalone		case 'h':		/* hostname to deliver to */
12063402Sdwmalone			host = optarg;
12163402Sdwmalone			break;
1221590Srgrimes		case 'i':		/* log process id also */
1231590Srgrimes			logflags |= LOG_PID;
1241590Srgrimes			break;
125160917Sbms		case 'P':		/* service name or port number */
126160917Sbms			svcname = optarg;
127160917Sbms			break;
1281590Srgrimes		case 'p':		/* priority */
1291590Srgrimes			pri = pencode(optarg);
1301590Srgrimes			break;
1311590Srgrimes		case 's':		/* log to standard error */
1321590Srgrimes			logflags |= LOG_PERROR;
1331590Srgrimes			break;
1341590Srgrimes		case 't':		/* tag */
1351590Srgrimes			tag = optarg;
1361590Srgrimes			break;
1371590Srgrimes		case '?':
1381590Srgrimes		default:
1391590Srgrimes			usage();
1401590Srgrimes		}
1411590Srgrimes	argc -= optind;
1421590Srgrimes	argv += optind;
1431590Srgrimes
1441590Srgrimes	/* setup for logging */
1451590Srgrimes	openlog(tag ? tag : getlogin(), logflags, 0);
1461590Srgrimes	(void) fclose(stdout);
1471590Srgrimes
1481590Srgrimes	/* log input line if appropriate */
1491590Srgrimes	if (argc > 0) {
150102944Sdwmalone		char *p, *endp;
15193525Sdwmalone		size_t len;
1521590Srgrimes
1531590Srgrimes		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1541590Srgrimes			len = strlen(*argv);
1551590Srgrimes			if (p + len > endp && p > buf) {
156160917Sbms				logmessage(pri, host, svcname, buf);
1571590Srgrimes				p = buf;
1581590Srgrimes			}
1591590Srgrimes			if (len > sizeof(buf) - 1)
160160917Sbms				logmessage(pri, host, svcname, *argv++);
1611590Srgrimes			else {
1621590Srgrimes				if (p != buf)
1631590Srgrimes					*p++ = ' ';
1641590Srgrimes				bcopy(*argv++, p, len);
1651590Srgrimes				*(p += len) = '\0';
1661590Srgrimes			}
1671590Srgrimes		}
1681590Srgrimes		if (p != buf)
169160917Sbms			logmessage(pri, host, svcname, buf);
1701590Srgrimes	} else
1711590Srgrimes		while (fgets(buf, sizeof(buf), stdin) != NULL)
172160917Sbms			logmessage(pri, host, svcname, buf);
1731590Srgrimes	exit(0);
1741590Srgrimes}
1751590Srgrimes
1761590Srgrimes/*
17763402Sdwmalone *  Send the message to syslog, either on the local host, or on a remote host
17863402Sdwmalone */
179175993Sobrienvoid
180169344Sdwmalonelogmessage(int pri, const char *host, const char *svcname, const char *buf)
18163402Sdwmalone{
18270100Sume	static struct socks *socks;
18370100Sume	static int nsock = 0;
18470100Sume	struct addrinfo hints, *res, *r;
18563402Sdwmalone	char *line;
18670100Sume	int maxs, len, sock, error, i, lsent;
18763402Sdwmalone
18863402Sdwmalone	if (host == NULL) {
18963402Sdwmalone		syslog(pri, "%s", buf);
19063402Sdwmalone		return;
19163402Sdwmalone	}
19263402Sdwmalone
19370100Sume	if (nsock <= 0) {	/* set up socket stuff */
19470100Sume		/* resolve hostname */
19570100Sume		memset(&hints, 0, sizeof(hints));
19670100Sume		hints.ai_family = family;
19770100Sume		hints.ai_socktype = SOCK_DGRAM;
198160917Sbms		error = getaddrinfo(host, svcname, &hints, &res);
19970100Sume		if (error == EAI_SERVICE) {
200160917Sbms			warnx("%s/udp: unknown service", svcname);
20170100Sume			error = getaddrinfo(host, "514", &hints, &res);
20270100Sume		}
20370100Sume		if (error)
20470100Sume			errx(1, "%s: %s", gai_strerror(error), host);
20570100Sume		/* count max number of sockets we may open */
20670100Sume		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
20770100Sume		socks = malloc(maxs * sizeof(struct socks));
20870100Sume		if (!socks)
20970100Sume			errx(1, "couldn't allocate memory for sockets");
21070100Sume		for (r = res; r; r = r->ai_next) {
21170100Sume			sock = socket(r->ai_family, r->ai_socktype,
21270100Sume				      r->ai_protocol);
21370100Sume			if (sock < 0)
21470100Sume				continue;
21570100Sume			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
21670100Sume			socks[nsock].addrlen = r->ai_addrlen;
21770100Sume			socks[nsock++].sock = sock;
21870100Sume		}
21970100Sume		freeaddrinfo(res);
22070100Sume		if (nsock <= 0)
22163402Sdwmalone			errx(1, "socket");
22263402Sdwmalone	}
22363402Sdwmalone
22463402Sdwmalone	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
22563402Sdwmalone		errx(1, "asprintf");
22663402Sdwmalone
227146756Scharnier	lsent = -1;
22870100Sume	for (i = 0; i < nsock; ++i) {
22970100Sume		lsent = sendto(socks[i].sock, line, len, 0,
23070100Sume			       (struct sockaddr *)&socks[i].addr,
23170100Sume			       socks[i].addrlen);
23270100Sume		if (lsent == len && !send_to_all)
23370100Sume			break;
23470100Sume	}
23593525Sdwmalone	if (lsent != len) {
23691433Sfenner		if (lsent == -1)
23791433Sfenner			warn ("sendto");
23891433Sfenner		else
23991433Sfenner			warnx ("sendto: short send - %d bytes", lsent);
24093525Sdwmalone	}
24163402Sdwmalone
24263402Sdwmalone	free(line);
24363402Sdwmalone}
24463402Sdwmalone
24563402Sdwmalone/*
2461590Srgrimes *  Decode a symbolic name to a numeric value
2471590Srgrimes */
2481590Srgrimesint
249102944Sdwmalonepencode(char *s)
2501590Srgrimes{
2511590Srgrimes	char *save;
2521590Srgrimes	int fac, lev;
2531590Srgrimes
2541590Srgrimes	for (save = s; *s && *s != '.'; ++s);
2551590Srgrimes	if (*s) {
2561590Srgrimes		*s = '\0';
2571590Srgrimes		fac = decode(save, facilitynames);
25827604Scharnier		if (fac < 0)
25927604Scharnier			errx(1, "unknown facility name: %s", save);
2601590Srgrimes		*s++ = '.';
2611590Srgrimes	}
2621590Srgrimes	else {
2631590Srgrimes		fac = 0;
2641590Srgrimes		s = save;
2651590Srgrimes	}
2661590Srgrimes	lev = decode(s, prioritynames);
26727604Scharnier	if (lev < 0)
26827604Scharnier		errx(1, "unknown priority name: %s", save);
2691590Srgrimes	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2701590Srgrimes}
2711590Srgrimes
2721590Srgrimesint
273102944Sdwmalonedecode(char *name, CODE *codetab)
2741590Srgrimes{
275102944Sdwmalone	CODE *c;
2761590Srgrimes
2771590Srgrimes	if (isdigit(*name))
2781590Srgrimes		return (atoi(name));
2791590Srgrimes
2801590Srgrimes	for (c = codetab; c->c_name; c++)
2811590Srgrimes		if (!strcasecmp(name, c->c_name))
2821590Srgrimes			return (c->c_val);
2831590Srgrimes
2841590Srgrimes	return (-1);
2851590Srgrimes}
2861590Srgrimes
28727604Scharnierstatic void
288102944Sdwmaloneusage(void)
2891590Srgrimes{
29063402Sdwmalone	(void)fprintf(stderr, "usage: %s\n",
291160917Sbms	    "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
292160917Sbms	    "              [message ...]"
29363402Sdwmalone	    );
2941590Srgrimes	exit(1);
2951590Srgrimes}
296