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 #include <sys/capsicum.h>
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <capsicum_helpers.h>
60 #include <libcasper.h>
61 #include <casper/cap_syslog.h>
66 #define sstosa(ss) ((struct sockaddr *)(void *)ss)
71 struct sockaddr_storage sk_addr;
74 static int decode(char *, const CODE *);
75 static int pencode(char *);
76 static ssize_t socksetup(const char *, const char *, const char *,
78 static void logmessage(int, const char *, const char *, const char *,
79 struct socks *, ssize_t, const char *);
80 static void usage(void);
82 static cap_channel_t *capsyslog;
84 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
86 static int family = PF_INET; /* protocol family (IPv4 only) */
88 static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
91 * logger -- read and log utility
93 * Reads from an input and arranges to write the result on the system
97 main(int argc, char *argv[])
99 cap_channel_t *capcas;
103 int ch, logflags, pri;
104 char *tag, *host, buf[1024], *timestamp, tbuf[26],
105 *hostname, hbuf[MAXHOSTNAMELEN], *pristr;
106 const char *svcname, *src;
114 pri = LOG_USER | LOG_NOTICE;
118 while ((ch = getopt(argc, argv, "46Af:H:h:iP:p:S:st:")) != -1)
131 case 'f': /* file to log */
132 if (freopen(optarg, "r", stdin) == NULL)
133 err(1, "%s", optarg);
134 setvbuf(stdin, 0, _IONBF, 0);
136 case 'H': /* hostname to set in message header */
139 case 'h': /* hostname to deliver to */
142 case 'i': /* log process id also */
145 case 'P': /* service name or port number */
148 case 'p': /* priority */
151 case 's': /* log to standard error */
152 logflags |= LOG_PERROR;
154 case 'S': /* source address */
168 nsock = socksetup(src, host, svcname, &socks);
173 errx(1, "-h option is missing.");
179 err(1, "Unable to contact Casper");
180 caph_cache_catpages();
183 if (caph_enter_casper() < 0)
184 err(1, "Unable to enter capability mode");
186 capsyslog = cap_service_open(capcas, "system.syslog");
187 if (capsyslog == NULL)
188 err(1, "Unable to open system.syslog service");
192 pri = pencode(pristr);
195 /* setup for logging */
197 cap_openlog(capsyslog, tag, logflags, 0);
199 if (hostname == NULL) {
201 (void )gethostname(hbuf, MAXHOSTNAMELEN);
202 *strchrnul(hostname, '.') = '\0';
205 timestamp = tbuf + 4;
207 /* log input line if appropriate */
213 (void )ctime_r(&now, tbuf);
216 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
218 if (p + len > endp && p > buf) {
219 logmessage(pri, timestamp, hostname, tag,
223 if (len > sizeof(buf) - 1)
224 logmessage(pri, timestamp, hostname, tag,
225 socks, nsock, *argv++);
229 bcopy(*argv++, p, len);
234 logmessage(pri, timestamp, hostname, tag, socks, nsock,
237 while (fgets(buf, sizeof(buf), stdin) != NULL) {
239 (void )ctime_r(&now, tbuf);
242 logmessage(pri, timestamp, hostname, tag, socks, nsock,
249 socksetup(const char *src, const char *dst, const char *svcname,
250 struct socks **socks)
252 struct addrinfo hints, *res, *res0;
253 struct sockaddr_storage *ss_src[AF_MAX];
258 memset(&ss_src[0], 0, sizeof(ss_src));
260 char *p, *p0, *hs, *hbuf, *sbuf;
263 p0 = p = strdup(src);
265 err(1, "strdup failed");
266 hs = p0; /* point to search ":" */
268 /* -S option supports IPv6 addr in "[2001:db8::1]:service". */
272 errx(1, "\"]\" not found in src addr");
274 /* hs points just after ']' (':' or '\0'). */
277 * p points just after '[' while it points hs
280 p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
284 /* (p == hs) means ":514" or "[]:514". */
285 hbuf = (p == hs && *p == ':') ? NULL : p;
289 sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
292 hints = (struct addrinfo){
294 .ai_socktype = SOCK_DGRAM,
295 .ai_flags = AI_PASSIVE
297 error = getaddrinfo(hbuf, sbuf, &hints, &res0);
299 errx(1, "%s: %s", gai_strerror(error), src);
300 for (res = res0; res; res = res->ai_next) {
301 switch (res->ai_family) {
306 if (ss_src[res->ai_family] != NULL)
308 ss_src[res->ai_family] =
309 malloc(sizeof(struct sockaddr_storage));
310 if (ss_src[res->ai_family] == NULL)
311 err(1, "malloc failed");
312 memcpy(ss_src[res->ai_family], res->ai_addr,
320 /* resolve hostname */
321 hints = (struct addrinfo){
323 .ai_socktype = SOCK_DGRAM
325 error = getaddrinfo(dst, svcname, &hints, &res0);
326 if (error == EAI_SERVICE) {
327 warnx("%s/udp: unknown service", svcname);
328 error = getaddrinfo(dst, "514", &hints, &res0);
331 errx(1, "%s: %s", gai_strerror(error), dst);
332 /* count max number of sockets we may open */
334 for (res = res0; res; res = res->ai_next)
336 sk = calloc(maxs, sizeof(*sk));
338 errx(1, "couldn't allocate memory for sockets");
339 for (res = res0; res; res = res->ai_next) {
342 s = socket(res->ai_family, res->ai_socktype,
346 if (src && ss_src[res->ai_family] == NULL)
347 errx(1, "address family mismatch");
349 if (ss_src[res->ai_family]) {
350 error = bind(s, sstosa(ss_src[res->ai_family]),
351 ss_src[res->ai_family]->ss_len);
355 sk[nsock] = (struct socks){
356 .sk_addrlen = res->ai_addrlen,
359 memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
369 * Send the message to syslog, either on the local host, or on a remote host
372 logmessage(int pri, const char *timestamp, const char *hostname,
373 const char *tag, struct socks *sk, ssize_t nsock, const char *buf)
379 cap_syslog(capsyslog, pri, "%s", buf);
382 if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp,
383 hostname, tag, buf)) == -1)
387 for (i = 0; i < nsock; i++) {
388 lsent = sendto(sk[i].sk_sock, line, len, 0,
389 sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
390 if (lsent == len && !send_to_all)
397 warnx("sendto: short send - %d bytes", lsent);
404 * Decode a symbolic name to a numeric value
412 for (save = s; *s && *s != '.'; ++s);
415 fac = decode(save, facilitynames);
417 errx(1, "unknown facility name: %s", save);
424 lev = decode(s, prioritynames);
426 errx(1, "unknown priority name: %s", save);
427 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
431 decode(char *name, const CODE *codetab)
438 for (c = codetab; c->c_name; c++)
439 if (!strcasecmp(name, c->c_name))
448 (void)fprintf(stderr, "usage: %s\n",
449 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
450 " [-S addr:port] [message ...]"