2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/capsicum.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <capsicum_helpers.h>
62 #include <libcasper.h>
63 #include <casper/cap_syslog.h>
68 #define sstosa(ss) ((struct sockaddr *)(void *)ss)
73 struct sockaddr_storage sk_addr;
76 static int decode(char *, const CODE *);
77 static int pencode(char *);
78 static ssize_t socksetup(const char *, const char *, const char *,
80 static void logmessage(int, const char *, const char *, const char *,
81 struct socks *, ssize_t, const char *);
82 static void usage(void);
84 static cap_channel_t *capsyslog;
86 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
88 static int family = PF_INET; /* protocol family (IPv4 only) */
90 static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
93 * logger -- read and log utility
95 * Reads from an input and arranges to write the result on the system
99 main(int argc, char *argv[])
101 cap_channel_t *capcas;
105 int ch, logflags, pri;
106 char *tag, *host, buf[1024], *timestamp, tbuf[26],
107 *hostname, hbuf[MAXHOSTNAMELEN], *pristr;
108 const char *svcname, *src;
116 pri = LOG_USER | LOG_NOTICE;
120 while ((ch = getopt(argc, argv, "46Af:H:h:iP:p:S:st:")) != -1)
133 case 'f': /* file to log */
134 if (freopen(optarg, "r", stdin) == NULL)
135 err(1, "%s", optarg);
136 setvbuf(stdin, 0, _IONBF, 0);
138 case 'H': /* hostname to set in message header */
141 case 'h': /* hostname to deliver to */
144 case 'i': /* log process id also */
147 case 'P': /* service name or port number */
150 case 'p': /* priority */
153 case 's': /* log to standard error */
154 logflags |= LOG_PERROR;
156 case 'S': /* source address */
170 nsock = socksetup(src, host, svcname, &socks);
175 errx(1, "-h option is missing.");
181 err(1, "Unable to contact Casper");
182 caph_cache_catpages();
185 if (caph_enter() < 0)
186 err(1, "Unable to enter capability mode");
188 capsyslog = cap_service_open(capcas, "system.syslog");
189 if (capsyslog == NULL)
190 err(1, "Unable to open system.syslog service");
194 pri = pencode(pristr);
197 /* setup for logging */
199 cap_openlog(capsyslog, tag, logflags, 0);
202 (void )ctime_r(&now, tbuf);
204 timestamp = tbuf + 4;
206 if (hostname == NULL) {
208 (void )gethostname(hbuf, MAXHOSTNAMELEN);
209 *strchrnul(hostname, '.') = '\0';
212 /* log input line if appropriate */
217 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
219 if (p + len > endp && p > buf) {
220 logmessage(pri, timestamp, hostname, tag,
224 if (len > sizeof(buf) - 1)
225 logmessage(pri, timestamp, hostname, tag,
226 socks, nsock, *argv++);
230 bcopy(*argv++, p, len);
235 logmessage(pri, timestamp, hostname, tag, socks, nsock,
238 while (fgets(buf, sizeof(buf), stdin) != NULL)
239 logmessage(pri, timestamp, hostname, tag, socks, nsock,
245 socksetup(const char *src, const char *dst, const char *svcname,
246 struct socks **socks)
248 struct addrinfo hints, *res, *res0;
249 struct sockaddr_storage *ss_src[AF_MAX];
254 memset(&ss_src[0], 0, sizeof(ss_src));
256 char *p, *p0, *hs, *hbuf, *sbuf;
259 p0 = p = strdup(src);
261 err(1, "strdup failed");
262 hs = p0; /* point to search ":" */
264 /* -S option supports IPv6 addr in "[2001:db8::1]:service". */
268 errx(1, "\"]\" not found in src addr");
270 /* hs points just after ']' (':' or '\0'). */
273 * p points just after '[' while it points hs
276 p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
280 /* (p == hs) means ":514" or "[]:514". */
281 hbuf = (p == hs && *p == ':') ? NULL : p;
285 sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
288 hints = (struct addrinfo){
290 .ai_socktype = SOCK_DGRAM,
291 .ai_flags = AI_PASSIVE
293 error = getaddrinfo(hbuf, sbuf, &hints, &res0);
295 errx(1, "%s: %s", gai_strerror(error), src);
296 for (res = res0; res; res = res->ai_next) {
297 switch (res->ai_family) {
302 if (ss_src[res->ai_family] != NULL)
304 ss_src[res->ai_family] =
305 malloc(sizeof(struct sockaddr_storage));
306 if (ss_src[res->ai_family] == NULL)
307 err(1, "malloc failed");
308 memcpy(ss_src[res->ai_family], res->ai_addr,
316 /* resolve hostname */
317 hints = (struct addrinfo){
319 .ai_socktype = SOCK_DGRAM
321 error = getaddrinfo(dst, svcname, &hints, &res0);
322 if (error == EAI_SERVICE) {
323 warnx("%s/udp: unknown service", svcname);
324 error = getaddrinfo(dst, "514", &hints, &res0);
327 errx(1, "%s: %s", gai_strerror(error), dst);
328 /* count max number of sockets we may open */
330 for (res = res0; res; res = res->ai_next)
332 sk = calloc(maxs, sizeof(*sk));
334 errx(1, "couldn't allocate memory for sockets");
335 for (res = res0; res; res = res->ai_next) {
338 s = socket(res->ai_family, res->ai_socktype,
342 if (src && ss_src[res->ai_family] == NULL)
343 errx(1, "address family mismatch");
345 if (ss_src[res->ai_family]) {
346 error = bind(s, sstosa(ss_src[res->ai_family]),
347 ss_src[res->ai_family]->ss_len);
351 sk[nsock] = (struct socks){
352 .sk_addrlen = res->ai_addrlen,
355 memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
365 * Send the message to syslog, either on the local host, or on a remote host
368 logmessage(int pri, const char *timestamp, const char *hostname,
369 const char *tag, struct socks *sk, ssize_t nsock, const char *buf)
375 cap_syslog(capsyslog, pri, "%s", buf);
378 if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp,
379 hostname, tag, buf)) == -1)
383 for (i = 0; i < nsock; i++) {
384 lsent = sendto(sk[i].sk_sock, line, len, 0,
385 sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
386 if (lsent == len && !send_to_all)
393 warnx("sendto: short send - %d bytes", lsent);
400 * Decode a symbolic name to a numeric value
408 for (save = s; *s && *s != '.'; ++s);
411 fac = decode(save, facilitynames);
413 errx(1, "unknown facility name: %s", save);
420 lev = decode(s, prioritynames);
422 errx(1, "unknown priority name: %s", save);
423 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
427 decode(char *name, const CODE *codetab)
434 for (c = codetab; c->c_name; c++)
435 if (!strcasecmp(name, c->c_name))
444 (void)fprintf(stderr, "usage: %s\n",
445 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
446 " [-S addr:port] [message ...]"