Merge from vendor branch TCPDUMP:
[dragonfly.git] / usr.bin / logger / logger.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)logger.c 8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/logger/logger.c,v 1.5.2.3 2001/09/06 17:38:57 ru Exp $
36  * $DragonFly: src/usr.bin/logger/logger.c,v 1.3 2003/10/04 20:36:48 hmp Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #define SYSLOG_NAMES
52 #include <syslog.h>
53
54 int     decode(char *, CODE *);
55 int     pencode(char *);
56 static void     logmessage(int, char *, char *);
57 static void     usage(void);
58
59 struct socks {
60     int sock;
61     int addrlen;
62     struct sockaddr_storage addr;
63 };
64
65 #ifdef INET6
66 int     family = PF_UNSPEC;     /* protocol family (IPv4, IPv6 or both) */
67 #else
68 int     family = PF_INET;       /* protocol family (IPv4 only) */
69 #endif
70 int     send_to_all = 0;        /* send message to all IPv4/IPv6 addresses */
71
72 /*
73  * logger -- read and log utility
74  *
75  *      Reads from an input and arranges to write the result on the system
76  *      log.
77  */
78 int
79 main(int argc, char **argv)
80 {
81         int ch, logflags, pri;
82         char *tag, *host, buf[1024];
83
84         tag = NULL;
85         host = NULL;
86         pri = LOG_USER | LOG_NOTICE;
87         logflags = 0;
88         unsetenv("TZ");
89         while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
90                 switch((char)ch) {
91                 case '4':
92                         family = PF_INET;
93                         break;
94 #ifdef INET6
95                 case '6':
96                         family = PF_INET6;
97                         break;
98 #endif
99                 case 'A':
100                         send_to_all++;
101                         break;
102                 case 'f':               /* file to log */
103                         if (freopen(optarg, "r", stdin) == NULL)
104                                 err(1, "%s", optarg);
105                         break;
106                 case 'h':               /* hostname to deliver to */
107                         host = optarg;
108                         break;
109                 case 'i':               /* log process id also */
110                         logflags |= LOG_PID;
111                         break;
112                 case 'p':               /* priority */
113                         pri = pencode(optarg);
114                         break;
115                 case 's':               /* log to standard error */
116                         logflags |= LOG_PERROR;
117                         break;
118                 case 't':               /* tag */
119                         tag = optarg;
120                         break;
121                 case '?':
122                 default:
123                         usage();
124                 }
125         argc -= optind;
126         argv += optind;
127
128         /* setup for logging */
129         openlog(tag ? tag : getlogin(), logflags, 0);
130         (void) fclose(stdout);
131
132         /* log input line if appropriate */
133         if (argc > 0) {
134                 register char *p, *endp;
135                 int len;
136
137                 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
138                         len = strlen(*argv);
139                         if (p + len > endp && p > buf) {
140                                 logmessage(pri, host, buf);
141                                 p = buf;
142                         }
143                         if (len > sizeof(buf) - 1)
144                                 logmessage(pri, host, *argv++);
145                         else {
146                                 if (p != buf)
147                                         *p++ = ' ';
148                                 bcopy(*argv++, p, len);
149                                 *(p += len) = '\0';
150                         }
151                 }
152                 if (p != buf)
153                         logmessage(pri, host, buf);
154         } else
155                 while (fgets(buf, sizeof(buf), stdin) != NULL)
156                         logmessage(pri, host, buf);
157         exit(0);
158 }
159
160 /*
161  *  Send the message to syslog, either on the local host, or on a remote host
162  */
163 void 
164 logmessage(int pri, char *host, char *buf)
165 {
166         static struct socks *socks;
167         static int nsock = 0;
168         struct addrinfo hints, *res, *r;
169         char *line;
170         int maxs, len, sock, error, i, lsent;
171
172         if (host == NULL) {
173                 syslog(pri, "%s", buf);
174                 return;
175         }
176
177         if (nsock <= 0) {       /* set up socket stuff */
178                 /* resolve hostname */
179                 memset(&hints, 0, sizeof(hints));
180                 hints.ai_family = family;
181                 hints.ai_socktype = SOCK_DGRAM;
182                 error = getaddrinfo(host, "syslog", &hints, &res);
183                 if (error == EAI_SERVICE) {
184                         warnx ("syslog/udp: unknown service");  /* not fatal */
185                         error = getaddrinfo(host, "514", &hints, &res);
186                 }
187                 if (error)
188                         errx(1, "%s: %s", gai_strerror(error), host);
189                 /* count max number of sockets we may open */
190                 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
191                 socks = malloc(maxs * sizeof(struct socks));
192                 if (!socks)
193                         errx(1, "couldn't allocate memory for sockets");
194                 for (r = res; r; r = r->ai_next) {
195                         sock = socket(r->ai_family, r->ai_socktype,
196                                       r->ai_protocol);
197                         if (sock < 0)
198                                 continue;
199                         memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
200                         socks[nsock].addrlen = r->ai_addrlen;
201                         socks[nsock++].sock = sock;
202                 }
203                 freeaddrinfo(res);
204                 if (nsock <= 0)
205                         errx(1, "socket");
206         }
207
208         if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
209                 errx(1, "asprintf");
210
211         for (i = 0; i < nsock; ++i) {
212                 lsent = sendto(socks[i].sock, line, len, 0,
213                                (struct sockaddr *)&socks[i].addr,
214                                socks[i].addrlen);
215                 if (lsent == len && !send_to_all)
216                         break;
217         }
218         if (lsent != len)
219                 warnx ("sendmsg");
220
221         free(line);
222 }
223
224 /*
225  *  Decode a symbolic name to a numeric value
226  */
227 int
228 pencode(register char *s)
229 {
230         char *save;
231         int fac, lev;
232
233         for (save = s; *s && *s != '.'; ++s);
234         if (*s) {
235                 *s = '\0';
236                 fac = decode(save, facilitynames);
237                 if (fac < 0)
238                         errx(1, "unknown facility name: %s", save);
239                 *s++ = '.';
240         }
241         else {
242                 fac = 0;
243                 s = save;
244         }
245         lev = decode(s, prioritynames);
246         if (lev < 0)
247                 errx(1, "unknown priority name: %s", save);
248         return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
249 }
250
251 int
252 decode(char *name, CODE *codetab)
253 {
254         register CODE *c;
255
256         if (isdigit(*name))
257                 return (atoi(name));
258
259         for (c = codetab; c->c_name; c++)
260                 if (!strcasecmp(name, c->c_name))
261                         return (c->c_val);
262
263         return (-1);
264 }
265
266 static void
267 usage(void)
268 {
269         (void)fprintf(stderr, "usage: %s\n",
270             "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
271             );
272         exit(1);
273 }