xref: /freebsd/usr.bin/logger/logger.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
328838296eSMariusz Zaborski #include <sys/capsicum.h>
3365547fb3SGleb Smirnoff #include <sys/param.h>
34b0fe2da8SDavid Malone #include <sys/socket.h>
35b0fe2da8SDavid Malone #include <netinet/in.h>
36b0fe2da8SDavid Malone 
378838296eSMariusz Zaborski #include <capsicum_helpers.h>
389b50d902SRodney W. Grimes #include <ctype.h>
39df071556SPhilippe Charnier #include <err.h>
40b0fe2da8SDavid Malone #include <netdb.h>
41df071556SPhilippe Charnier #include <stdio.h>
42df071556SPhilippe Charnier #include <stdlib.h>
439b50d902SRodney W. Grimes #include <string.h>
4465547fb3SGleb Smirnoff #include <time.h>
45df071556SPhilippe Charnier #include <unistd.h>
469b50d902SRodney W. Grimes 
478838296eSMariusz Zaborski #include <libcasper.h>
488838296eSMariusz Zaborski #include <casper/cap_syslog.h>
498838296eSMariusz Zaborski 
509b50d902SRodney W. Grimes #define	SYSLOG_NAMES
519b50d902SRodney W. Grimes #include <syslog.h>
529b50d902SRodney W. Grimes 
53a04667abSHiroki Sato #define	sstosa(ss)	((struct sockaddr *)(void *)ss)
549b50d902SRodney W. Grimes 
550b5f90afSHajimu UMEMOTO struct socks {
56a04667abSHiroki Sato 	int sk_sock;
57a04667abSHiroki Sato 	int sk_addrlen;
58a04667abSHiroki Sato 	struct sockaddr_storage sk_addr;
590b5f90afSHajimu UMEMOTO };
600b5f90afSHajimu UMEMOTO 
61a04667abSHiroki Sato static int	decode(char *, const CODE *);
62a04667abSHiroki Sato static int	pencode(char *);
63a04667abSHiroki Sato static ssize_t	socksetup(const char *, const char *, const char *,
64a04667abSHiroki Sato 		    struct socks **);
6565547fb3SGleb Smirnoff static void	logmessage(int, const char *, const char *, const char *,
6665547fb3SGleb Smirnoff 		    struct socks *, ssize_t, const char *);
67a04667abSHiroki Sato static void	usage(void);
68a04667abSHiroki Sato 
698838296eSMariusz Zaborski static cap_channel_t *capsyslog;
700b5f90afSHajimu UMEMOTO #ifdef INET6
71481bce6cSEd Schouten static int family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
720b5f90afSHajimu UMEMOTO #else
73481bce6cSEd Schouten static int family = PF_INET;	/* protocol family (IPv4 only) */
740b5f90afSHajimu UMEMOTO #endif
75481bce6cSEd Schouten static int send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
760b5f90afSHajimu UMEMOTO 
779b50d902SRodney W. Grimes /*
789b50d902SRodney W. Grimes  * logger -- read and log utility
799b50d902SRodney W. Grimes  *
809b50d902SRodney W. Grimes  *	Reads from an input and arranges to write the result on the system
819b50d902SRodney W. Grimes  *	log.
829b50d902SRodney W. Grimes  */
839b50d902SRodney W. Grimes int
main(int argc,char * argv[])84f4ac32deSDavid Malone main(int argc, char *argv[])
859b50d902SRodney W. Grimes {
868838296eSMariusz Zaborski 	cap_channel_t *capcas;
87a04667abSHiroki Sato 	struct socks *socks;
88a04667abSHiroki Sato 	ssize_t nsock;
8965547fb3SGleb Smirnoff 	time_t now;
909b50d902SRodney W. Grimes 	int ch, logflags, pri;
9165547fb3SGleb Smirnoff 	char *tag, *host, buf[1024], *timestamp, tbuf[26],
928838296eSMariusz Zaborski 	    *hostname, hbuf[MAXHOSTNAMELEN], *pristr;
93a04667abSHiroki Sato 	const char *svcname, *src;
949b50d902SRodney W. Grimes 
959b50d902SRodney W. Grimes 	tag = NULL;
96b0fe2da8SDavid Malone 	host = NULL;
9765547fb3SGleb Smirnoff 	hostname = NULL;
986b04b7f6SBruce M Simpson 	svcname = "syslog";
99a04667abSHiroki Sato 	src = NULL;
1008f14a1afSHiroki Sato 	socks = NULL;
101ca122bf7SRuslan Ermilov 	pri = LOG_USER | LOG_NOTICE;
1028838296eSMariusz Zaborski 	pristr = NULL;
1039b50d902SRodney W. Grimes 	logflags = 0;
10440244c28SPoul-Henning Kamp 	unsetenv("TZ");
10565547fb3SGleb Smirnoff 	while ((ch = getopt(argc, argv, "46Af:H:h:iP:p:S:st:")) != -1)
1069b50d902SRodney W. Grimes 		switch((char)ch) {
1070b5f90afSHajimu UMEMOTO 		case '4':
1080b5f90afSHajimu UMEMOTO 			family = PF_INET;
1090b5f90afSHajimu UMEMOTO 			break;
1100b5f90afSHajimu UMEMOTO #ifdef INET6
1110b5f90afSHajimu UMEMOTO 		case '6':
1120b5f90afSHajimu UMEMOTO 			family = PF_INET6;
1130b5f90afSHajimu UMEMOTO 			break;
1140b5f90afSHajimu UMEMOTO #endif
1150b5f90afSHajimu UMEMOTO 		case 'A':
1160b5f90afSHajimu UMEMOTO 			send_to_all++;
1170b5f90afSHajimu UMEMOTO 			break;
1189b50d902SRodney W. Grimes 		case 'f':		/* file to log */
119df071556SPhilippe Charnier 			if (freopen(optarg, "r", stdin) == NULL)
120df071556SPhilippe Charnier 				err(1, "%s", optarg);
1214145bb53SKirk McKusick 			setvbuf(stdin, 0, _IONBF, 0);
1229b50d902SRodney W. Grimes 			break;
12365547fb3SGleb Smirnoff 		case 'H':		/* hostname to set in message header */
12465547fb3SGleb Smirnoff 			hostname = optarg;
12565547fb3SGleb Smirnoff 			break;
126b0fe2da8SDavid Malone 		case 'h':		/* hostname to deliver to */
127b0fe2da8SDavid Malone 			host = optarg;
128b0fe2da8SDavid Malone 			break;
1299b50d902SRodney W. Grimes 		case 'i':		/* log process id also */
1309b50d902SRodney W. Grimes 			logflags |= LOG_PID;
1319b50d902SRodney W. Grimes 			break;
1326b04b7f6SBruce M Simpson 		case 'P':		/* service name or port number */
1336b04b7f6SBruce M Simpson 			svcname = optarg;
1346b04b7f6SBruce M Simpson 			break;
1359b50d902SRodney W. Grimes 		case 'p':		/* priority */
1368838296eSMariusz Zaborski 			pristr = optarg;
1379b50d902SRodney W. Grimes 			break;
1389b50d902SRodney W. Grimes 		case 's':		/* log to standard error */
1399b50d902SRodney W. Grimes 			logflags |= LOG_PERROR;
1409b50d902SRodney W. Grimes 			break;
141a04667abSHiroki Sato 		case 'S':		/* source address */
142a04667abSHiroki Sato 			src = optarg;
143a04667abSHiroki Sato 			break;
1449b50d902SRodney W. Grimes 		case 't':		/* tag */
1459b50d902SRodney W. Grimes 			tag = optarg;
1469b50d902SRodney W. Grimes 			break;
1479b50d902SRodney W. Grimes 		case '?':
1489b50d902SRodney W. Grimes 		default:
1499b50d902SRodney W. Grimes 			usage();
1509b50d902SRodney W. Grimes 		}
1519b50d902SRodney W. Grimes 	argc -= optind;
1529b50d902SRodney W. Grimes 	argv += optind;
1539b50d902SRodney W. Grimes 
154a04667abSHiroki Sato 	if (host) {
155a04667abSHiroki Sato 		nsock = socksetup(src, host, svcname, &socks);
156a04667abSHiroki Sato 		if (nsock <= 0)
157a04667abSHiroki Sato 			errx(1, "socket");
158a04667abSHiroki Sato 	} else {
159a04667abSHiroki Sato 		if (src)
160a04667abSHiroki Sato 			errx(1, "-h option is missing.");
161a04667abSHiroki Sato 		nsock = 0;
162a04667abSHiroki Sato 	}
163a04667abSHiroki Sato 
1648838296eSMariusz Zaborski 	capcas = cap_init();
1658838296eSMariusz Zaborski 	if (capcas == NULL)
1668838296eSMariusz Zaborski 		err(1, "Unable to contact Casper");
1678838296eSMariusz Zaborski 	caph_cache_catpages();
1688838296eSMariusz Zaborski 	caph_cache_tzdata();
169d86cc385SMariusz Zaborski 	if (nsock == 0) {
170af329789SMariusz Zaborski 		if (caph_enter_casper() < 0)
1718838296eSMariusz Zaborski 			err(1, "Unable to enter capability mode");
172d86cc385SMariusz Zaborski 	}
1738838296eSMariusz Zaborski 	capsyslog = cap_service_open(capcas, "system.syslog");
1748838296eSMariusz Zaborski 	if (capsyslog == NULL)
1758838296eSMariusz Zaborski 		err(1, "Unable to open system.syslog service");
1768838296eSMariusz Zaborski 	cap_close(capcas);
1778838296eSMariusz Zaborski 
1788838296eSMariusz Zaborski 	if (pristr != NULL)
1798838296eSMariusz Zaborski 		pri = pencode(pristr);
18081280940SEdwin Groothuis 	if (tag == NULL)
18181280940SEdwin Groothuis 		tag = getlogin();
1829b50d902SRodney W. Grimes 	/* setup for logging */
18381280940SEdwin Groothuis 	if (host == NULL)
1848838296eSMariusz Zaborski 		cap_openlog(capsyslog, tag, logflags, 0);
1859b50d902SRodney W. Grimes 
18665547fb3SGleb Smirnoff 	if (hostname == NULL) {
18765547fb3SGleb Smirnoff 		hostname = hbuf;
18865547fb3SGleb Smirnoff 		(void )gethostname(hbuf, MAXHOSTNAMELEN);
1892ff3551aSGleb Smirnoff 		*strchrnul(hostname, '.') = '\0';
19065547fb3SGleb Smirnoff 	}
19165547fb3SGleb Smirnoff 
192*83fd35b3SEugene Grosbein 	timestamp = tbuf + 4;
193*83fd35b3SEugene Grosbein 
1949b50d902SRodney W. Grimes 	/* log input line if appropriate */
1959b50d902SRodney W. Grimes 	if (argc > 0) {
196f4ac32deSDavid Malone 		char *p, *endp;
1979bd5ae85SDavid Malone 		size_t len;
1989b50d902SRodney W. Grimes 
199*83fd35b3SEugene Grosbein 		(void )time(&now);
200*83fd35b3SEugene Grosbein 		(void )ctime_r(&now, tbuf);
201*83fd35b3SEugene Grosbein 		tbuf[19] = '\0';
202*83fd35b3SEugene Grosbein 
2039b50d902SRodney W. Grimes 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
2049b50d902SRodney W. Grimes 			len = strlen(*argv);
2059b50d902SRodney W. Grimes 			if (p + len > endp && p > buf) {
20665547fb3SGleb Smirnoff 				logmessage(pri, timestamp, hostname, tag,
20765547fb3SGleb Smirnoff 				    socks, nsock, buf);
2089b50d902SRodney W. Grimes 				p = buf;
2099b50d902SRodney W. Grimes 			}
2109b50d902SRodney W. Grimes 			if (len > sizeof(buf) - 1)
21165547fb3SGleb Smirnoff 				logmessage(pri, timestamp, hostname, tag,
21265547fb3SGleb Smirnoff 				    socks, nsock, *argv++);
2139b50d902SRodney W. Grimes 			else {
2149b50d902SRodney W. Grimes 				if (p != buf)
2159b50d902SRodney W. Grimes 					*p++ = ' ';
2169b50d902SRodney W. Grimes 				bcopy(*argv++, p, len);
2179b50d902SRodney W. Grimes 				*(p += len) = '\0';
2189b50d902SRodney W. Grimes 			}
2199b50d902SRodney W. Grimes 		}
2209b50d902SRodney W. Grimes 		if (p != buf)
22165547fb3SGleb Smirnoff 			logmessage(pri, timestamp, hostname, tag, socks, nsock,
22265547fb3SGleb Smirnoff 			    buf);
2239b50d902SRodney W. Grimes 	} else
224*83fd35b3SEugene Grosbein 		while (fgets(buf, sizeof(buf), stdin) != NULL) {
225*83fd35b3SEugene Grosbein 			(void )time(&now);
226*83fd35b3SEugene Grosbein 			(void )ctime_r(&now, tbuf);
227*83fd35b3SEugene Grosbein 			tbuf[19] = '\0';
228*83fd35b3SEugene Grosbein 
22965547fb3SGleb Smirnoff 			logmessage(pri, timestamp, hostname, tag, socks, nsock,
23065547fb3SGleb Smirnoff 			    buf);
231*83fd35b3SEugene Grosbein 		}
2329b50d902SRodney W. Grimes 	exit(0);
2339b50d902SRodney W. Grimes }
2349b50d902SRodney W. Grimes 
235a04667abSHiroki Sato static ssize_t
socksetup(const char * src,const char * dst,const char * svcname,struct socks ** socks)236a04667abSHiroki Sato socksetup(const char *src, const char *dst, const char *svcname,
237a04667abSHiroki Sato 	struct socks **socks)
238a04667abSHiroki Sato {
239a04667abSHiroki Sato 	struct addrinfo hints, *res, *res0;
240a04667abSHiroki Sato 	struct sockaddr_storage *ss_src[AF_MAX];
241a04667abSHiroki Sato 	struct socks *sk;
242a04667abSHiroki Sato 	ssize_t nsock = 0;
243a04667abSHiroki Sato 	int error, maxs;
244a04667abSHiroki Sato 
245a04667abSHiroki Sato 	memset(&ss_src[0], 0, sizeof(ss_src));
246a04667abSHiroki Sato 	if (src) {
247a04667abSHiroki Sato 		char *p, *p0, *hs, *hbuf, *sbuf;
248a04667abSHiroki Sato 
249a04667abSHiroki Sato 		hbuf = sbuf = NULL;
250a04667abSHiroki Sato 		p0 = p = strdup(src);
251a04667abSHiroki Sato 		if (p0 == NULL)
252a04667abSHiroki Sato 			err(1, "strdup failed");
253a04667abSHiroki Sato 		hs = p0;	/* point to search ":" */
254a04667abSHiroki Sato #ifdef INET6
255a04667abSHiroki Sato 		/* -S option supports IPv6 addr in "[2001:db8::1]:service". */
256a04667abSHiroki Sato 		if (*p0 == '[') {
257a04667abSHiroki Sato 			p = strchr(p0, ']');
258a04667abSHiroki Sato 			if (p == NULL)
259a04667abSHiroki Sato 				errx(1, "\"]\" not found in src addr");
260a04667abSHiroki Sato 			*p = '\0';
261a04667abSHiroki Sato 			/* hs points just after ']' (':' or '\0'). */
262a04667abSHiroki Sato 			hs = p + 1;
263a04667abSHiroki Sato 			/*
264a04667abSHiroki Sato 			 * p points just after '[' while it points hs
265a04667abSHiroki Sato 			 * in the case of [].
266a04667abSHiroki Sato 			 */
267a04667abSHiroki Sato 			p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
268a04667abSHiroki Sato 		}
269a04667abSHiroki Sato #endif
270a04667abSHiroki Sato 		if (*p != '\0') {
271a04667abSHiroki Sato 			/* (p == hs) means ":514" or "[]:514". */
272a04667abSHiroki Sato 			hbuf = (p == hs && *p == ':') ? NULL : p;
273a04667abSHiroki Sato 			p = strchr(hs, ':');
274a04667abSHiroki Sato 			if (p != NULL) {
275a04667abSHiroki Sato 				*p = '\0';
276a04667abSHiroki Sato 				sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
277a04667abSHiroki Sato 			}
278a04667abSHiroki Sato 		}
279a04667abSHiroki Sato 		hints = (struct addrinfo){
280a04667abSHiroki Sato 			.ai_family = family,
281a04667abSHiroki Sato 			.ai_socktype = SOCK_DGRAM,
282a04667abSHiroki Sato 			.ai_flags = AI_PASSIVE
283a04667abSHiroki Sato 		};
284a04667abSHiroki Sato 		error = getaddrinfo(hbuf, sbuf, &hints, &res0);
285a04667abSHiroki Sato 		if (error)
286a04667abSHiroki Sato 			errx(1, "%s: %s", gai_strerror(error), src);
287a04667abSHiroki Sato 		for (res = res0; res; res = res->ai_next) {
288a04667abSHiroki Sato 			switch (res->ai_family) {
289a04667abSHiroki Sato 			case AF_INET:
290a04667abSHiroki Sato #ifdef INET6
291a04667abSHiroki Sato 			case AF_INET6:
292a04667abSHiroki Sato #endif
293a04667abSHiroki Sato 				if (ss_src[res->ai_family] != NULL)
294a04667abSHiroki Sato 					continue;
295a04667abSHiroki Sato 				ss_src[res->ai_family] =
296a04667abSHiroki Sato 				    malloc(sizeof(struct sockaddr_storage));
297a04667abSHiroki Sato 				if (ss_src[res->ai_family] == NULL)
298a04667abSHiroki Sato 					err(1, "malloc failed");
299a04667abSHiroki Sato 				memcpy(ss_src[res->ai_family], res->ai_addr,
300a04667abSHiroki Sato 				    res->ai_addrlen);
301a04667abSHiroki Sato 			}
302a04667abSHiroki Sato 		}
303a04667abSHiroki Sato 		freeaddrinfo(res0);
304a04667abSHiroki Sato 		free(p0);
305a04667abSHiroki Sato 	}
306a04667abSHiroki Sato 
307a04667abSHiroki Sato 	/* resolve hostname */
308a04667abSHiroki Sato 	hints = (struct addrinfo){
309a04667abSHiroki Sato 		.ai_family = family,
310a04667abSHiroki Sato 		.ai_socktype = SOCK_DGRAM
311a04667abSHiroki Sato 	};
312a04667abSHiroki Sato 	error = getaddrinfo(dst, svcname, &hints, &res0);
313a04667abSHiroki Sato 	if (error == EAI_SERVICE) {
314a04667abSHiroki Sato 		warnx("%s/udp: unknown service", svcname);
315488ee96bSEric van Gyzen 		error = getaddrinfo(dst, "514", &hints, &res0);
316a04667abSHiroki Sato 	}
317a04667abSHiroki Sato 	if (error)
318a04667abSHiroki Sato 		errx(1, "%s: %s", gai_strerror(error), dst);
319a04667abSHiroki Sato 	/* count max number of sockets we may open */
320a04667abSHiroki Sato 	maxs = 0;
321a04667abSHiroki Sato 	for (res = res0; res; res = res->ai_next)
322a04667abSHiroki Sato 		maxs++;
323a04667abSHiroki Sato 	sk = calloc(maxs, sizeof(*sk));
324a04667abSHiroki Sato 	if (sk == NULL)
325a04667abSHiroki Sato 		errx(1, "couldn't allocate memory for sockets");
326a04667abSHiroki Sato 	for (res = res0; res; res = res->ai_next) {
327a04667abSHiroki Sato 		int s;
328a04667abSHiroki Sato 
329a04667abSHiroki Sato 		s = socket(res->ai_family, res->ai_socktype,
330a04667abSHiroki Sato 		    res->ai_protocol);
331a04667abSHiroki Sato 		if (s < 0)
332a04667abSHiroki Sato 			continue;
333a04667abSHiroki Sato 		if (src && ss_src[res->ai_family] == NULL)
334a04667abSHiroki Sato 			errx(1, "address family mismatch");
335a04667abSHiroki Sato 
336a04667abSHiroki Sato 		if (ss_src[res->ai_family]) {
337a04667abSHiroki Sato 			error = bind(s, sstosa(ss_src[res->ai_family]),
338a04667abSHiroki Sato 				    ss_src[res->ai_family]->ss_len);
339a04667abSHiroki Sato 			if (error < 0)
340a04667abSHiroki Sato 				err(1, "bind");
341a04667abSHiroki Sato 		}
342a04667abSHiroki Sato 		sk[nsock] = (struct socks){
343a04667abSHiroki Sato 			.sk_addrlen = res->ai_addrlen,
344a04667abSHiroki Sato 			.sk_sock = s
345a04667abSHiroki Sato 		};
346a04667abSHiroki Sato 		memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
347a04667abSHiroki Sato 		nsock++;
348a04667abSHiroki Sato 	}
349a04667abSHiroki Sato 	freeaddrinfo(res0);
350a04667abSHiroki Sato 
351a04667abSHiroki Sato 	*socks = sk;
352a04667abSHiroki Sato 	return (nsock);
353a04667abSHiroki Sato }
354a04667abSHiroki Sato 
3559b50d902SRodney W. Grimes /*
356b0fe2da8SDavid Malone  *  Send the message to syslog, either on the local host, or on a remote host
357b0fe2da8SDavid Malone  */
358481bce6cSEd Schouten static void
logmessage(int pri,const char * timestamp,const char * hostname,const char * tag,struct socks * sk,ssize_t nsock,const char * buf)35965547fb3SGleb Smirnoff logmessage(int pri, const char *timestamp, const char *hostname,
36065547fb3SGleb Smirnoff     const char *tag, struct socks *sk, ssize_t nsock, const char *buf)
361b0fe2da8SDavid Malone {
362b0fe2da8SDavid Malone 	char *line;
363a04667abSHiroki Sato 	int len, i, lsent;
364b0fe2da8SDavid Malone 
365a04667abSHiroki Sato 	if (nsock == 0) {
3668838296eSMariusz Zaborski 		cap_syslog(capsyslog, pri, "%s", buf);
367b0fe2da8SDavid Malone 		return;
368b0fe2da8SDavid Malone 	}
36965547fb3SGleb Smirnoff 	if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp,
37065547fb3SGleb Smirnoff 	    hostname, tag, buf)) == -1)
371b0fe2da8SDavid Malone 		errx(1, "asprintf");
372b0fe2da8SDavid Malone 
373865059c8SPhilippe Charnier 	lsent = -1;
374a04667abSHiroki Sato 	for (i = 0; i < nsock; i++) {
375a04667abSHiroki Sato 		lsent = sendto(sk[i].sk_sock, line, len, 0,
376a04667abSHiroki Sato 			       sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
3770b5f90afSHajimu UMEMOTO 		if (lsent == len && !send_to_all)
3780b5f90afSHajimu UMEMOTO 			break;
3790b5f90afSHajimu UMEMOTO 	}
3809bd5ae85SDavid Malone 	if (lsent != len) {
38157c1a0b6SBill Fenner 		if (lsent == -1)
38257c1a0b6SBill Fenner 			warn("sendto");
38357c1a0b6SBill Fenner 		else
38457c1a0b6SBill Fenner 			warnx("sendto: short send - %d bytes", lsent);
3859bd5ae85SDavid Malone 	}
386b0fe2da8SDavid Malone 
387b0fe2da8SDavid Malone 	free(line);
388b0fe2da8SDavid Malone }
389b0fe2da8SDavid Malone 
390b0fe2da8SDavid Malone /*
3919b50d902SRodney W. Grimes  *  Decode a symbolic name to a numeric value
3929b50d902SRodney W. Grimes  */
393481bce6cSEd Schouten static int
pencode(char * s)394f4ac32deSDavid Malone pencode(char *s)
3959b50d902SRodney W. Grimes {
3969b50d902SRodney W. Grimes 	char *save;
3979b50d902SRodney W. Grimes 	int fac, lev;
3989b50d902SRodney W. Grimes 
3999b50d902SRodney W. Grimes 	for (save = s; *s && *s != '.'; ++s);
4009b50d902SRodney W. Grimes 	if (*s) {
4019b50d902SRodney W. Grimes 		*s = '\0';
4029b50d902SRodney W. Grimes 		fac = decode(save, facilitynames);
403df071556SPhilippe Charnier 		if (fac < 0)
404df071556SPhilippe Charnier 			errx(1, "unknown facility name: %s", save);
4059b50d902SRodney W. Grimes 		*s++ = '.';
4069b50d902SRodney W. Grimes 	}
4079b50d902SRodney W. Grimes 	else {
4089b50d902SRodney W. Grimes 		fac = 0;
4099b50d902SRodney W. Grimes 		s = save;
4109b50d902SRodney W. Grimes 	}
4119b50d902SRodney W. Grimes 	lev = decode(s, prioritynames);
412df071556SPhilippe Charnier 	if (lev < 0)
413df071556SPhilippe Charnier 		errx(1, "unknown priority name: %s", save);
4149b50d902SRodney W. Grimes 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
4159b50d902SRodney W. Grimes }
4169b50d902SRodney W. Grimes 
417481bce6cSEd Schouten static int
decode(char * name,const CODE * codetab)41839893d56SEd Schouten decode(char *name, const CODE *codetab)
4199b50d902SRodney W. Grimes {
42039893d56SEd Schouten 	const CODE *c;
4219b50d902SRodney W. Grimes 
4229b50d902SRodney W. Grimes 	if (isdigit(*name))
4239b50d902SRodney W. Grimes 		return (atoi(name));
4249b50d902SRodney W. Grimes 
4259b50d902SRodney W. Grimes 	for (c = codetab; c->c_name; c++)
4269b50d902SRodney W. Grimes 		if (!strcasecmp(name, c->c_name))
4279b50d902SRodney W. Grimes 			return (c->c_val);
4289b50d902SRodney W. Grimes 
4299b50d902SRodney W. Grimes 	return (-1);
4309b50d902SRodney W. Grimes }
4319b50d902SRodney W. Grimes 
432df071556SPhilippe Charnier static void
usage(void)433f4ac32deSDavid Malone usage(void)
4349b50d902SRodney W. Grimes {
435b0fe2da8SDavid Malone 	(void)fprintf(stderr, "usage: %s\n",
4366b04b7f6SBruce M Simpson 	    "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
437a04667abSHiroki Sato 	    "              [-S addr:port] [message ...]"
438b0fe2da8SDavid Malone 	    );
4399b50d902SRodney W. Grimes 	exit(1);
4409b50d902SRodney W. Grimes }
441