logger.c revision 220448
1275988Sngie/*
2240116Smarcel * Copyright (c) 1983, 1993
3240116Smarcel *	The Regents of the University of California.  All rights reserved.
4240116Smarcel *
5240116Smarcel * Redistribution and use in source and binary forms, with or without
6240116Smarcel * modification, are permitted provided that the following conditions
7240116Smarcel * are met:
8240116Smarcel * 1. Redistributions of source code must retain the above copyright
9240116Smarcel *    notice, this list of conditions and the following disclaimer.
10240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer in the
12240116Smarcel *    documentation and/or other materials provided with the distribution.
13240116Smarcel * 4. Neither the name of the University nor the names of its contributors
14240116Smarcel *    may be used to endorse or promote products derived from this software
15240116Smarcel *    without specific prior written permission.
16240116Smarcel *
17240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20240116Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23240116Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24275988Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25240116Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26275988Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27275988Sngie * SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#ifndef lint
31240116Smarcelstatic const char copyright[] =
32240116Smarcel"@(#) Copyright (c) 1983, 1993\n\
33240116Smarcel	The Regents of the University of California.  All rights reserved.\n";
34275988Sngie#endif /* not lint */
35275988Sngie
36275988Sngie#if 0
37240116Smarcel#ifndef lint
38240116Smarcelstatic char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
39240116Smarcel#endif /* not lint */
40240116Smarcel#endif
41240116Smarcel
42240116Smarcel#include <sys/cdefs.h>
43240116Smarcel__FBSDID("$FreeBSD: head/usr.bin/logger/logger.c 220448 2011-04-08 12:33:07Z edwin $");
44240116Smarcel
45240116Smarcel#include <sys/types.h>
46240116Smarcel#include <sys/socket.h>
47240116Smarcel#include <netinet/in.h>
48240116Smarcel
49240116Smarcel#include <ctype.h>
50240116Smarcel#include <err.h>
51240116Smarcel#include <netdb.h>
52240116Smarcel#include <stdio.h>
53240116Smarcel#include <stdlib.h>
54240116Smarcel#include <string.h>
55240116Smarcel#include <unistd.h>
56240116Smarcel
57240116Smarcel#define	SYSLOG_NAMES
58240116Smarcel#include <syslog.h>
59240116Smarcel
60240116Smarcelint	decode(char *, CODE *);
61240116Smarcelint	pencode(char *);
62240116Smarcelstatic void	logmessage(int, const char *, const char *, const char *,
63240116Smarcel			   const char *);
64240116Smarcelstatic void	usage(void);
65240116Smarcel
66240116Smarcelstruct socks {
67240116Smarcel    int sock;
68240116Smarcel    int addrlen;
69240116Smarcel    struct sockaddr_storage addr;
70240116Smarcel};
71240116Smarcel
72240116Smarcel#ifdef INET6
73240116Smarcelint	family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
74240116Smarcel#else
75240116Smarcelint	family = PF_INET;	/* protocol family (IPv4 only) */
76240116Smarcel#endif
77240116Smarcelint	send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
78240116Smarcel
79240116Smarcel/*
80240116Smarcel * logger -- read and log utility
81240116Smarcel *
82240116Smarcel *	Reads from an input and arranges to write the result on the system
83240116Smarcel *	log.
84240116Smarcel */
85240116Smarcelint
86240116Smarcelmain(int argc, char *argv[])
87240116Smarcel{
88240116Smarcel	int ch, logflags, pri;
89240116Smarcel	char *tag, *host, buf[1024];
90240116Smarcel	const char *svcname;
91240116Smarcel
92240116Smarcel	tag = NULL;
93240116Smarcel	host = NULL;
94240116Smarcel	svcname = "syslog";
95240116Smarcel	pri = LOG_USER | LOG_NOTICE;
96240116Smarcel	logflags = 0;
97240116Smarcel	unsetenv("TZ");
98240116Smarcel	while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
99240116Smarcel		switch((char)ch) {
100240116Smarcel		case '4':
101240116Smarcel			family = PF_INET;
102240116Smarcel			break;
103240116Smarcel#ifdef INET6
104240116Smarcel		case '6':
105240116Smarcel			family = PF_INET6;
106240116Smarcel			break;
107240116Smarcel#endif
108240116Smarcel		case 'A':
109240116Smarcel			send_to_all++;
110240116Smarcel			break;
111240116Smarcel		case 'f':		/* file to log */
112240116Smarcel			if (freopen(optarg, "r", stdin) == NULL)
113240116Smarcel				err(1, "%s", optarg);
114240116Smarcel			setvbuf(stdin, 0, _IONBF, 0);
115240116Smarcel			break;
116240116Smarcel		case 'h':		/* hostname to deliver to */
117240116Smarcel			host = optarg;
118240116Smarcel			break;
119240116Smarcel		case 'i':		/* log process id also */
120240116Smarcel			logflags |= LOG_PID;
121240116Smarcel			break;
122240116Smarcel		case 'P':		/* service name or port number */
123240116Smarcel			svcname = optarg;
124240116Smarcel			break;
125240116Smarcel		case 'p':		/* priority */
126240116Smarcel			pri = pencode(optarg);
127240116Smarcel			break;
128240116Smarcel		case 's':		/* log to standard error */
129240116Smarcel			logflags |= LOG_PERROR;
130240116Smarcel			break;
131240116Smarcel		case 't':		/* tag */
132240116Smarcel			tag = optarg;
133240116Smarcel			break;
134240116Smarcel		case '?':
135240116Smarcel		default:
136240116Smarcel			usage();
137240116Smarcel		}
138240116Smarcel	argc -= optind;
139240116Smarcel	argv += optind;
140240116Smarcel
141240116Smarcel	if (tag == NULL)
142240116Smarcel		tag = getlogin();
143240116Smarcel	/* setup for logging */
144240116Smarcel	if (host == NULL)
145240116Smarcel		openlog(tag, logflags, 0);
146240116Smarcel	(void) fclose(stdout);
147240116Smarcel
148240116Smarcel	/* log input line if appropriate */
149240116Smarcel	if (argc > 0) {
150240116Smarcel		char *p, *endp;
151240116Smarcel		size_t len;
152240116Smarcel
153240116Smarcel		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
154240116Smarcel			len = strlen(*argv);
155240116Smarcel			if (p + len > endp && p > buf) {
156240116Smarcel				logmessage(pri, tag, host, svcname, buf);
157240116Smarcel				p = buf;
158240116Smarcel			}
159240116Smarcel			if (len > sizeof(buf) - 1)
160240116Smarcel				logmessage(pri, tag, host, svcname, *argv++);
161240116Smarcel			else {
162240116Smarcel				if (p != buf)
163240116Smarcel					*p++ = ' ';
164240116Smarcel				bcopy(*argv++, p, len);
165240116Smarcel				*(p += len) = '\0';
166240116Smarcel			}
167240116Smarcel		}
168240116Smarcel		if (p != buf)
169240116Smarcel			logmessage(pri, tag, host, svcname, buf);
170240116Smarcel	} else
171240116Smarcel		while (fgets(buf, sizeof(buf), stdin) != NULL)
172240116Smarcel			logmessage(pri, tag, host, svcname, buf);
173240116Smarcel	exit(0);
174240116Smarcel}
175240116Smarcel
176240116Smarcel/*
177240116Smarcel *  Send the message to syslog, either on the local host, or on a remote host
178240116Smarcel */
179240116Smarcelvoid
180240116Smarcellogmessage(int pri, const char *tag, const char *host, const char *svcname,
181240116Smarcel	   const char *buf)
182240116Smarcel{
183240116Smarcel	static struct socks *socks;
184240116Smarcel	static int nsock = 0;
185240116Smarcel	struct addrinfo hints, *res, *r;
186240116Smarcel	char *line;
187240116Smarcel	int maxs, len, sock, error, i, lsent;
188240116Smarcel
189240116Smarcel	if (host == NULL) {
190240116Smarcel		syslog(pri, "%s", buf);
191240116Smarcel		return;
192240116Smarcel	}
193240116Smarcel
194240116Smarcel	if (nsock <= 0) {	/* set up socket stuff */
195240116Smarcel		/* resolve hostname */
196240116Smarcel		memset(&hints, 0, sizeof(hints));
197240116Smarcel		hints.ai_family = family;
198240116Smarcel		hints.ai_socktype = SOCK_DGRAM;
199240116Smarcel		error = getaddrinfo(host, svcname, &hints, &res);
200240116Smarcel		if (error == EAI_SERVICE) {
201240116Smarcel			warnx("%s/udp: unknown service", svcname);
202240116Smarcel			error = getaddrinfo(host, "514", &hints, &res);
203240116Smarcel		}
204240116Smarcel		if (error)
205240116Smarcel			errx(1, "%s: %s", gai_strerror(error), host);
206240116Smarcel		/* count max number of sockets we may open */
207240116Smarcel		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
208240116Smarcel		socks = malloc(maxs * sizeof(struct socks));
209240116Smarcel		if (!socks)
210240116Smarcel			errx(1, "couldn't allocate memory for sockets");
211240116Smarcel		for (r = res; r; r = r->ai_next) {
212240116Smarcel			sock = socket(r->ai_family, r->ai_socktype,
213240116Smarcel				      r->ai_protocol);
214240116Smarcel			if (sock < 0)
215240116Smarcel				continue;
216240116Smarcel			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
217240116Smarcel			socks[nsock].addrlen = r->ai_addrlen;
218240116Smarcel			socks[nsock++].sock = sock;
219240116Smarcel		}
220240116Smarcel		freeaddrinfo(res);
221240116Smarcel		if (nsock <= 0)
222240116Smarcel			errx(1, "socket");
223240116Smarcel	}
224240116Smarcel
225240116Smarcel	if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
226240116Smarcel		errx(1, "asprintf");
227240116Smarcel
228240116Smarcel	lsent = -1;
229240116Smarcel	for (i = 0; i < nsock; ++i) {
230240116Smarcel		lsent = sendto(socks[i].sock, line, len, 0,
231240116Smarcel			       (struct sockaddr *)&socks[i].addr,
232240116Smarcel			       socks[i].addrlen);
233240116Smarcel		if (lsent == len && !send_to_all)
234240116Smarcel			break;
235240116Smarcel	}
236240116Smarcel	if (lsent != len) {
237240116Smarcel		if (lsent == -1)
238240116Smarcel			warn ("sendto");
239240116Smarcel		else
240240116Smarcel			warnx ("sendto: short send - %d bytes", lsent);
241240116Smarcel	}
242240116Smarcel
243240116Smarcel	free(line);
244240116Smarcel}
245240116Smarcel
246240116Smarcel/*
247240116Smarcel *  Decode a symbolic name to a numeric value
248240116Smarcel */
249240116Smarcelint
250pencode(char *s)
251{
252	char *save;
253	int fac, lev;
254
255	for (save = s; *s && *s != '.'; ++s);
256	if (*s) {
257		*s = '\0';
258		fac = decode(save, facilitynames);
259		if (fac < 0)
260			errx(1, "unknown facility name: %s", save);
261		*s++ = '.';
262	}
263	else {
264		fac = 0;
265		s = save;
266	}
267	lev = decode(s, prioritynames);
268	if (lev < 0)
269		errx(1, "unknown priority name: %s", save);
270	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
271}
272
273int
274decode(char *name, CODE *codetab)
275{
276	CODE *c;
277
278	if (isdigit(*name))
279		return (atoi(name));
280
281	for (c = codetab; c->c_name; c++)
282		if (!strcasecmp(name, c->c_name))
283			return (c->c_val);
284
285	return (-1);
286}
287
288static void
289usage(void)
290{
291	(void)fprintf(stderr, "usage: %s\n",
292	    "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
293	    "              [message ...]"
294	    );
295	exit(1);
296}
297