logger.c revision 92920
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
351590Srgrimesstatic 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
411590Srgrimes#if 0
421590Srgrimesstatic char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
431590Srgrimes#endif
441590Srgrimesstatic const char rcsid[] =
451590Srgrimes  "$FreeBSD: head/usr.bin/logger/logger.c 92920 2002-03-22 01:22:50Z imp $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes#include <sys/types.h>
491590Srgrimes#include <sys/socket.h>
501590Srgrimes#include <netinet/in.h>
511590Srgrimes
521590Srgrimes#include <ctype.h>
5312909Swollman#include <err.h>
541590Srgrimes#include <netdb.h>
551590Srgrimes#include <stdio.h>
561590Srgrimes#include <stdlib.h>
571590Srgrimes#include <string.h>
581590Srgrimes#include <unistd.h>
591590Srgrimes
601590Srgrimes#define	SYSLOG_NAMES
611590Srgrimes#include <syslog.h>
621590Srgrimes
631590Srgrimesint	decode(char *, CODE *);
641590Srgrimesint	pencode(char *);
651590Srgrimesstatic void	logmessage(int, char *, char *);
661590Srgrimesstatic void	usage(void);
671590Srgrimes
681590Srgrimesstruct socks {
6912909Swollman    int sock;
7012909Swollman    int addrlen;
711590Srgrimes    struct sockaddr_storage addr;
721590Srgrimes};
731590Srgrimes
741590Srgrimes#ifdef INET6
751590Srgrimesint	family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
761590Srgrimes#else
771590Srgrimesint	family = PF_INET;	/* protocol family (IPv4 only) */
781590Srgrimes#endif
791590Srgrimesint	send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
801590Srgrimes
811590Srgrimes/*
821590Srgrimes * logger -- read and log utility
831590Srgrimes *
841590Srgrimes *	Reads from an input and arranges to write the result on the system
851590Srgrimes *	log.
861590Srgrimes */
871590Srgrimesint
881590Srgrimesmain(argc, argv)
891590Srgrimes	int argc;
901590Srgrimes	char *argv[];
911590Srgrimes{
921590Srgrimes	int ch, logflags, pri;
931590Srgrimes	char *tag, *host, buf[1024];
941590Srgrimes
951590Srgrimes	tag = NULL;
961590Srgrimes	host = NULL;
971590Srgrimes	pri = LOG_USER | LOG_NOTICE;
981590Srgrimes	logflags = 0;
991590Srgrimes	unsetenv("TZ");
1001590Srgrimes	while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
1011590Srgrimes		switch((char)ch) {
1021590Srgrimes		case '4':
10312909Swollman			family = PF_INET;
10412909Swollman			break;
10512909Swollman#ifdef INET6
10612909Swollman		case '6':
10712909Swollman			family = PF_INET6;
10812909Swollman			break;
10912909Swollman#endif
11012909Swollman		case 'A':
1111590Srgrimes			send_to_all++;
11212909Swollman			break;
11312909Swollman		case 'f':		/* file to log */
11412909Swollman			if (freopen(optarg, "r", stdin) == NULL)
11512909Swollman				err(1, "%s", optarg);
1161590Srgrimes			break;
11712909Swollman		case 'h':		/* hostname to deliver to */
11812909Swollman			host = optarg;
11912909Swollman			break;
12012909Swollman		case 'i':		/* log process id also */
1211590Srgrimes			logflags |= LOG_PID;
12212909Swollman			break;
12312909Swollman		case 'p':		/* priority */
12412909Swollman			pri = pencode(optarg);
12512909Swollman			break;
12612909Swollman		case 's':		/* log to standard error */
12712909Swollman			logflags |= LOG_PERROR;
1281590Srgrimes			break;
1291590Srgrimes		case 't':		/* tag */
1301590Srgrimes			tag = optarg;
1311590Srgrimes			break;
1321590Srgrimes		case '?':
1331590Srgrimes		default:
1341590Srgrimes			usage();
1351590Srgrimes		}
1361590Srgrimes	argc -= optind;
1371590Srgrimes	argv += optind;
1381590Srgrimes
1398874Srgrimes	/* setup for logging */
14012909Swollman	openlog(tag ? tag : getlogin(), logflags, 0);
14112909Swollman	(void) fclose(stdout);
14212909Swollman
1431590Srgrimes	/* log input line if appropriate */
1441590Srgrimes	if (argc > 0) {
1451590Srgrimes		register char *p, *endp;
1461590Srgrimes		int len;
1471590Srgrimes
1481590Srgrimes		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1491590Srgrimes			len = strlen(*argv);
15014472Sache			if (p + len > endp && p > buf) {
15114472Sache				logmessage(pri, host, buf);
1521590Srgrimes				p = buf;
15314472Sache			}
1541590Srgrimes			if (len > sizeof(buf) - 1)
1551590Srgrimes				logmessage(pri, host, *argv++);
1561590Srgrimes			else {
1571590Srgrimes				if (p != buf)
1581590Srgrimes					*p++ = ' ';
1591590Srgrimes				bcopy(*argv++, p, len);
1601590Srgrimes				*(p += len) = '\0';
1611590Srgrimes			}
1621590Srgrimes		}
16312909Swollman		if (p != buf)
16412909Swollman			logmessage(pri, host, buf);
16512909Swollman	} else
16612909Swollman		while (fgets(buf, sizeof(buf), stdin) != NULL)
16712909Swollman			logmessage(pri, host, buf);
16812909Swollman	exit(0);
16912909Swollman}
17012909Swollman
17112909Swollman/*
17212909Swollman *  Send the message to syslog, either on the local host, or on a remote host
17312909Swollman */
1741590Srgrimesvoid
175logmessage(int pri, char *host, char *buf)
176{
177	static struct socks *socks;
178	static int nsock = 0;
179	struct addrinfo hints, *res, *r;
180	char *line;
181	int maxs, len, sock, error, i, lsent;
182
183	if (host == NULL) {
184		syslog(pri, "%s", buf);
185		return;
186	}
187
188	if (nsock <= 0) {	/* set up socket stuff */
189		/* resolve hostname */
190		memset(&hints, 0, sizeof(hints));
191		hints.ai_family = family;
192		hints.ai_socktype = SOCK_DGRAM;
193		error = getaddrinfo(host, "syslog", &hints, &res);
194		if (error == EAI_SERVICE) {
195			warnx ("syslog/udp: unknown service");	/* not fatal */
196			error = getaddrinfo(host, "514", &hints, &res);
197		}
198		if (error)
199			errx(1, "%s: %s", gai_strerror(error), host);
200		/* count max number of sockets we may open */
201		for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
202		socks = malloc(maxs * sizeof(struct socks));
203		if (!socks)
204			errx(1, "couldn't allocate memory for sockets");
205		for (r = res; r; r = r->ai_next) {
206			sock = socket(r->ai_family, r->ai_socktype,
207				      r->ai_protocol);
208			if (sock < 0)
209				continue;
210			memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
211			socks[nsock].addrlen = r->ai_addrlen;
212			socks[nsock++].sock = sock;
213		}
214		freeaddrinfo(res);
215		if (nsock <= 0)
216			errx(1, "socket");
217	}
218
219	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
220		errx(1, "asprintf");
221
222	for (i = 0; i < nsock; ++i) {
223		lsent = sendto(socks[i].sock, line, len, 0,
224			       (struct sockaddr *)&socks[i].addr,
225			       socks[i].addrlen);
226		if (lsent == len && !send_to_all)
227			break;
228	}
229	if (lsent != len)
230		if (lsent == -1)
231			warn ("sendto");
232		else
233			warnx ("sendto: short send - %d bytes", lsent);
234
235	free(line);
236}
237
238/*
239 *  Decode a symbolic name to a numeric value
240 */
241int
242pencode(s)
243	register char *s;
244{
245	char *save;
246	int fac, lev;
247
248	for (save = s; *s && *s != '.'; ++s);
249	if (*s) {
250		*s = '\0';
251		fac = decode(save, facilitynames);
252		if (fac < 0)
253			errx(1, "unknown facility name: %s", save);
254		*s++ = '.';
255	}
256	else {
257		fac = 0;
258		s = save;
259	}
260	lev = decode(s, prioritynames);
261	if (lev < 0)
262		errx(1, "unknown priority name: %s", save);
263	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
264}
265
266int
267decode(name, codetab)
268	char *name;
269	CODE *codetab;
270{
271	register CODE *c;
272
273	if (isdigit(*name))
274		return (atoi(name));
275
276	for (c = codetab; c->c_name; c++)
277		if (!strcasecmp(name, c->c_name))
278			return (c->c_val);
279
280	return (-1);
281}
282
283static void
284usage()
285{
286	(void)fprintf(stderr, "usage: %s\n",
287	    "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
288	    );
289	exit(1);
290}
291