2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
60 #define sstosa(ss) ((struct sockaddr *)(void *)ss)
65 struct sockaddr_storage sk_addr;
68 static int decode(char *, const CODE *);
69 static int pencode(char *);
70 static ssize_t socksetup(const char *, const char *, const char *,
72 static void logmessage(int, const char *, struct socks *, ssize_t,
74 static void usage(void);
77 static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
79 static int family = PF_INET; /* protocol family (IPv4 only) */
81 static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
84 * logger -- read and log utility
86 * Reads from an input and arranges to write the result on the system
90 main(int argc, char *argv[])
94 int ch, logflags, pri;
95 char *tag, *host, buf[1024];
96 const char *svcname, *src;
103 pri = LOG_USER | LOG_NOTICE;
106 while ((ch = getopt(argc, argv, "46Af:h:iP:p:S:st:")) != -1)
119 case 'f': /* file to log */
120 if (freopen(optarg, "r", stdin) == NULL)
121 err(1, "%s", optarg);
122 setvbuf(stdin, 0, _IONBF, 0);
124 case 'h': /* hostname to deliver to */
127 case 'i': /* log process id also */
130 case 'P': /* service name or port number */
133 case 'p': /* priority */
134 pri = pencode(optarg);
136 case 's': /* log to standard error */
137 logflags |= LOG_PERROR;
139 case 'S': /* source address */
153 nsock = socksetup(src, host, svcname, &socks);
158 errx(1, "-h option is missing.");
164 /* setup for logging */
166 openlog(tag, logflags, 0);
167 (void) fclose(stdout);
169 /* log input line if appropriate */
174 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
176 if (p + len > endp && p > buf) {
177 logmessage(pri, tag, socks, nsock, buf);
180 if (len > sizeof(buf) - 1)
181 logmessage(pri, tag, socks, nsock, *argv++);
185 bcopy(*argv++, p, len);
190 logmessage(pri, tag, socks, nsock, buf);
192 while (fgets(buf, sizeof(buf), stdin) != NULL)
193 logmessage(pri, tag, socks, nsock, buf);
198 socksetup(const char *src, const char *dst, const char *svcname,
199 struct socks **socks)
201 struct addrinfo hints, *res, *res0;
202 struct sockaddr_storage *ss_src[AF_MAX];
207 memset(&ss_src[0], 0, sizeof(ss_src));
209 char *p, *p0, *hs, *hbuf, *sbuf;
212 p0 = p = strdup(src);
214 err(1, "strdup failed");
215 hs = p0; /* point to search ":" */
217 /* -S option supports IPv6 addr in "[2001:db8::1]:service". */
221 errx(1, "\"]\" not found in src addr");
223 /* hs points just after ']' (':' or '\0'). */
226 * p points just after '[' while it points hs
229 p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
233 /* (p == hs) means ":514" or "[]:514". */
234 hbuf = (p == hs && *p == ':') ? NULL : p;
238 sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
241 hints = (struct addrinfo){
243 .ai_socktype = SOCK_DGRAM,
244 .ai_flags = AI_PASSIVE
246 error = getaddrinfo(hbuf, sbuf, &hints, &res0);
248 errx(1, "%s: %s", gai_strerror(error), src);
249 for (res = res0; res; res = res->ai_next) {
250 switch (res->ai_family) {
255 if (ss_src[res->ai_family] != NULL)
257 ss_src[res->ai_family] =
258 malloc(sizeof(struct sockaddr_storage));
259 if (ss_src[res->ai_family] == NULL)
260 err(1, "malloc failed");
261 memcpy(ss_src[res->ai_family], res->ai_addr,
269 /* resolve hostname */
270 hints = (struct addrinfo){
272 .ai_socktype = SOCK_DGRAM
274 error = getaddrinfo(dst, svcname, &hints, &res0);
275 if (error == EAI_SERVICE) {
276 warnx("%s/udp: unknown service", svcname);
277 error = getaddrinfo(dst, "514", &hints, &res);
280 errx(1, "%s: %s", gai_strerror(error), dst);
281 /* count max number of sockets we may open */
283 for (res = res0; res; res = res->ai_next)
285 sk = calloc(maxs, sizeof(*sk));
287 errx(1, "couldn't allocate memory for sockets");
288 for (res = res0; res; res = res->ai_next) {
291 s = socket(res->ai_family, res->ai_socktype,
295 if (src && ss_src[res->ai_family] == NULL)
296 errx(1, "address family mismatch");
298 if (ss_src[res->ai_family]) {
299 error = bind(s, sstosa(ss_src[res->ai_family]),
300 ss_src[res->ai_family]->ss_len);
304 sk[nsock] = (struct socks){
305 .sk_addrlen = res->ai_addrlen,
308 memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
318 * Send the message to syslog, either on the local host, or on a remote host
321 logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
328 syslog(pri, "%s", buf);
331 if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
335 for (i = 0; i < nsock; i++) {
336 lsent = sendto(sk[i].sk_sock, line, len, 0,
337 sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
338 if (lsent == len && !send_to_all)
345 warnx("sendto: short send - %d bytes", lsent);
352 * Decode a symbolic name to a numeric value
360 for (save = s; *s && *s != '.'; ++s);
363 fac = decode(save, facilitynames);
365 errx(1, "unknown facility name: %s", save);
372 lev = decode(s, prioritynames);
374 errx(1, "unknown priority name: %s", save);
375 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
379 decode(char *name, const CODE *codetab)
386 for (c = codetab; c->c_name; c++)
387 if (!strcasecmp(name, c->c_name))
396 (void)fprintf(stderr, "usage: %s\n",
397 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
398 " [-S addr:port] [message ...]"