logger.c revision 160917
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 160917 2006-08-02 13:21:44Z bms $");
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 *);
66160917Sbmsstatic void	logmessage(int, char *, 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
89102944Sdwmalonemain(int argc, char *argv[])
901590Srgrimes{
911590Srgrimes	int ch, logflags, pri;
92160917Sbms	char *tag, *host, *svcname, buf[1024];
931590Srgrimes
941590Srgrimes	tag = NULL;
9563402Sdwmalone	host = NULL;
96160917Sbms	svcname = "syslog";
9783150Sru	pri = LOG_USER | LOG_NOTICE;
981590Srgrimes	logflags = 0;
9919075Sphk	unsetenv("TZ");
100160917Sbms	while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
1011590Srgrimes		switch((char)ch) {
10270100Sume		case '4':
10370100Sume			family = PF_INET;
10470100Sume			break;
10570100Sume#ifdef INET6
10670100Sume		case '6':
10770100Sume			family = PF_INET6;
10870100Sume			break;
10970100Sume#endif
11070100Sume		case 'A':
11170100Sume			send_to_all++;
11270100Sume			break;
1131590Srgrimes		case 'f':		/* file to log */
11427604Scharnier			if (freopen(optarg, "r", stdin) == NULL)
11527604Scharnier				err(1, "%s", optarg);
1161590Srgrimes			break;
11763402Sdwmalone		case 'h':		/* hostname to deliver to */
11863402Sdwmalone			host = optarg;
11963402Sdwmalone			break;
1201590Srgrimes		case 'i':		/* log process id also */
1211590Srgrimes			logflags |= LOG_PID;
1221590Srgrimes			break;
123160917Sbms		case 'P':		/* service name or port number */
124160917Sbms			svcname = optarg;
125160917Sbms			break;
1261590Srgrimes		case 'p':		/* priority */
1271590Srgrimes			pri = pencode(optarg);
1281590Srgrimes			break;
1291590Srgrimes		case 's':		/* log to standard error */
1301590Srgrimes			logflags |= LOG_PERROR;
1311590Srgrimes			break;
1321590Srgrimes		case 't':		/* tag */
1331590Srgrimes			tag = optarg;
1341590Srgrimes			break;
1351590Srgrimes		case '?':
1361590Srgrimes		default:
1371590Srgrimes			usage();
1381590Srgrimes		}
1391590Srgrimes	argc -= optind;
1401590Srgrimes	argv += optind;
1411590Srgrimes
1421590Srgrimes	/* setup for logging */
1431590Srgrimes	openlog(tag ? tag : getlogin(), logflags, 0);
1441590Srgrimes	(void) fclose(stdout);
1451590Srgrimes
1461590Srgrimes	/* log input line if appropriate */
1471590Srgrimes	if (argc > 0) {
148102944Sdwmalone		char *p, *endp;
14993525Sdwmalone		size_t len;
1501590Srgrimes
1511590Srgrimes		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1521590Srgrimes			len = strlen(*argv);
1531590Srgrimes			if (p + len > endp && p > buf) {
154160917Sbms				logmessage(pri, host, svcname, buf);
1551590Srgrimes				p = buf;
1561590Srgrimes			}
1571590Srgrimes			if (len > sizeof(buf) - 1)
158160917Sbms				logmessage(pri, host, svcname, *argv++);
1591590Srgrimes			else {
1601590Srgrimes				if (p != buf)
1611590Srgrimes					*p++ = ' ';
1621590Srgrimes				bcopy(*argv++, p, len);
1631590Srgrimes				*(p += len) = '\0';
1641590Srgrimes			}
1651590Srgrimes		}
1661590Srgrimes		if (p != buf)
167160917Sbms			logmessage(pri, host, svcname, buf);
1681590Srgrimes	} else
1691590Srgrimes		while (fgets(buf, sizeof(buf), stdin) != NULL)
170160917Sbms			logmessage(pri, host, svcname, buf);
1711590Srgrimes	exit(0);
1721590Srgrimes}
1731590Srgrimes
1741590Srgrimes/*
17563402Sdwmalone *  Send the message to syslog, either on the local host, or on a remote host
17663402Sdwmalone */
17763402Sdwmalonevoid
178160917Sbmslogmessage(int pri, char *host, char *svcname, char *buf)
17963402Sdwmalone{
18070100Sume	static struct socks *socks;
18170100Sume	static int nsock = 0;
18270100Sume	struct addrinfo hints, *res, *r;
18363402Sdwmalone	char *line;
18470100Sume	int maxs, len, sock, error, i, lsent;
18563402Sdwmalone
18663402Sdwmalone	if (host == NULL) {
18763402Sdwmalone		syslog(pri, "%s", buf);
18863402Sdwmalone		return;
18963402Sdwmalone	}
19063402Sdwmalone
19170100Sume	if (nsock <= 0) {	/* set up socket stuff */
19270100Sume		/* resolve hostname */
19370100Sume		memset(&hints, 0, sizeof(hints));
19470100Sume		hints.ai_family = family;
19570100Sume		hints.ai_socktype = SOCK_DGRAM;
196160917Sbms		error = getaddrinfo(host, svcname, &hints, &res);
19770100Sume		if (error == EAI_SERVICE) {
198160917Sbms			warnx("%s/udp: unknown service", svcname);
19970100Sume			error = getaddrinfo(host, "514", &hints, &res);
20070100Sume		}
20170100Sume		if (error)
20270100Sume			errx(1, "%s: %s", gai_strerror(error), host);
20370100Sume		/* count max number of sockets we may open */
20470100Sume		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
20570100Sume		socks = malloc(maxs * sizeof(struct socks));
20670100Sume		if (!socks)
20770100Sume			errx(1, "couldn't allocate memory for sockets");
20870100Sume		for (r = res; r; r = r->ai_next) {
20970100Sume			sock = socket(r->ai_family, r->ai_socktype,
21070100Sume				      r->ai_protocol);
21170100Sume			if (sock < 0)
21270100Sume				continue;
21370100Sume			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
21470100Sume			socks[nsock].addrlen = r->ai_addrlen;
21570100Sume			socks[nsock++].sock = sock;
21670100Sume		}
21770100Sume		freeaddrinfo(res);
21870100Sume		if (nsock <= 0)
21963402Sdwmalone			errx(1, "socket");
22063402Sdwmalone	}
22163402Sdwmalone
22263402Sdwmalone	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
22363402Sdwmalone		errx(1, "asprintf");
22463402Sdwmalone
225146756Scharnier	lsent = -1;
22670100Sume	for (i = 0; i < nsock; ++i) {
22770100Sume		lsent = sendto(socks[i].sock, line, len, 0,
22870100Sume			       (struct sockaddr *)&socks[i].addr,
22970100Sume			       socks[i].addrlen);
23070100Sume		if (lsent == len && !send_to_all)
23170100Sume			break;
23270100Sume	}
23393525Sdwmalone	if (lsent != len) {
23491433Sfenner		if (lsent == -1)
23591433Sfenner			warn ("sendto");
23691433Sfenner		else
23791433Sfenner			warnx ("sendto: short send - %d bytes", lsent);
23893525Sdwmalone	}
23963402Sdwmalone
24063402Sdwmalone	free(line);
24163402Sdwmalone}
24263402Sdwmalone
24363402Sdwmalone/*
2441590Srgrimes *  Decode a symbolic name to a numeric value
2451590Srgrimes */
2461590Srgrimesint
247102944Sdwmalonepencode(char *s)
2481590Srgrimes{
2491590Srgrimes	char *save;
2501590Srgrimes	int fac, lev;
2511590Srgrimes
2521590Srgrimes	for (save = s; *s && *s != '.'; ++s);
2531590Srgrimes	if (*s) {
2541590Srgrimes		*s = '\0';
2551590Srgrimes		fac = decode(save, facilitynames);
25627604Scharnier		if (fac < 0)
25727604Scharnier			errx(1, "unknown facility name: %s", save);
2581590Srgrimes		*s++ = '.';
2591590Srgrimes	}
2601590Srgrimes	else {
2611590Srgrimes		fac = 0;
2621590Srgrimes		s = save;
2631590Srgrimes	}
2641590Srgrimes	lev = decode(s, prioritynames);
26527604Scharnier	if (lev < 0)
26627604Scharnier		errx(1, "unknown priority name: %s", save);
2671590Srgrimes	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
2681590Srgrimes}
2691590Srgrimes
2701590Srgrimesint
271102944Sdwmalonedecode(char *name, CODE *codetab)
2721590Srgrimes{
273102944Sdwmalone	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
286102944Sdwmaloneusage(void)
2871590Srgrimes{
28863402Sdwmalone	(void)fprintf(stderr, "usage: %s\n",
289160917Sbms	    "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
290160917Sbms	    "              [message ...]"
29163402Sdwmalone	    );
2921590Srgrimes	exit(1);
2931590Srgrimes}
294