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 * 4. 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;
102 pri = LOG_USER | LOG_NOTICE;
105 while ((ch = getopt(argc, argv, "46Af:h:iP:p:S:st:")) != -1)
118 case 'f': /* file to log */
119 if (freopen(optarg, "r", stdin) == NULL)
120 err(1, "%s", optarg);
121 setvbuf(stdin, 0, _IONBF, 0);
123 case 'h': /* hostname to deliver to */
126 case 'i': /* log process id also */
129 case 'P': /* service name or port number */
132 case 'p': /* priority */
133 pri = pencode(optarg);
135 case 's': /* log to standard error */
136 logflags |= LOG_PERROR;
138 case 'S': /* source address */
152 nsock = socksetup(src, host, svcname, &socks);
157 errx(1, "-h option is missing.");
163 /* setup for logging */
165 openlog(tag, logflags, 0);
166 (void) fclose(stdout);
168 /* log input line if appropriate */
173 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
175 if (p + len > endp && p > buf) {
176 logmessage(pri, tag, socks, nsock, buf);
179 if (len > sizeof(buf) - 1)
180 logmessage(pri, tag, socks, nsock, *argv++);
184 bcopy(*argv++, p, len);
189 logmessage(pri, tag, socks, nsock, buf);
191 while (fgets(buf, sizeof(buf), stdin) != NULL)
192 logmessage(pri, tag, socks, nsock, buf);
197 socksetup(const char *src, const char *dst, const char *svcname,
198 struct socks **socks)
200 struct addrinfo hints, *res, *res0;
201 struct sockaddr_storage *ss_src[AF_MAX];
206 memset(&ss_src[0], 0, sizeof(ss_src));
208 char *p, *p0, *hs, *hbuf, *sbuf;
211 p0 = p = strdup(src);
213 err(1, "strdup failed");
214 hs = p0; /* point to search ":" */
216 /* -S option supports IPv6 addr in "[2001:db8::1]:service". */
220 errx(1, "\"]\" not found in src addr");
222 /* hs points just after ']' (':' or '\0'). */
225 * p points just after '[' while it points hs
228 p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
232 /* (p == hs) means ":514" or "[]:514". */
233 hbuf = (p == hs && *p == ':') ? NULL : p;
237 sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
240 hints = (struct addrinfo){
242 .ai_socktype = SOCK_DGRAM,
243 .ai_flags = AI_PASSIVE
245 error = getaddrinfo(hbuf, sbuf, &hints, &res0);
247 errx(1, "%s: %s", gai_strerror(error), src);
248 for (res = res0; res; res = res->ai_next) {
249 switch (res->ai_family) {
254 if (ss_src[res->ai_family] != NULL)
256 ss_src[res->ai_family] =
257 malloc(sizeof(struct sockaddr_storage));
258 if (ss_src[res->ai_family] == NULL)
259 err(1, "malloc failed");
260 memcpy(ss_src[res->ai_family], res->ai_addr,
268 /* resolve hostname */
269 hints = (struct addrinfo){
271 .ai_socktype = SOCK_DGRAM
273 error = getaddrinfo(dst, svcname, &hints, &res0);
274 if (error == EAI_SERVICE) {
275 warnx("%s/udp: unknown service", svcname);
276 error = getaddrinfo(dst, "514", &hints, &res);
279 errx(1, "%s: %s", gai_strerror(error), dst);
280 /* count max number of sockets we may open */
282 for (res = res0; res; res = res->ai_next)
284 sk = calloc(maxs, sizeof(*sk));
286 errx(1, "couldn't allocate memory for sockets");
287 for (res = res0; res; res = res->ai_next) {
290 s = socket(res->ai_family, res->ai_socktype,
294 if (src && ss_src[res->ai_family] == NULL)
295 errx(1, "address family mismatch");
297 if (ss_src[res->ai_family]) {
298 error = bind(s, sstosa(ss_src[res->ai_family]),
299 ss_src[res->ai_family]->ss_len);
303 sk[nsock] = (struct socks){
304 .sk_addrlen = res->ai_addrlen,
307 memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
317 * Send the message to syslog, either on the local host, or on a remote host
320 logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
327 syslog(pri, "%s", buf);
330 if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
334 for (i = 0; i < nsock; i++) {
335 lsent = sendto(sk[i].sk_sock, line, len, 0,
336 sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
337 if (lsent == len && !send_to_all)
344 warnx("sendto: short send - %d bytes", lsent);
351 * Decode a symbolic name to a numeric value
359 for (save = s; *s && *s != '.'; ++s);
362 fac = decode(save, facilitynames);
364 errx(1, "unknown facility name: %s", save);
371 lev = decode(s, prioritynames);
373 errx(1, "unknown priority name: %s", save);
374 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
378 decode(char *name, const CODE *codetab)
385 for (c = codetab; c->c_name; c++)
386 if (!strcasecmp(name, c->c_name))
395 (void)fprintf(stderr, "usage: %s\n",
396 "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
397 " [-S addr:port] [message ...]"