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 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 #include <netinet/in.h> 51 52 #include <ctype.h> 53 #include <err.h> 54 #include <netdb.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #define SYSLOG_NAMES 61 #include <syslog.h> 62 63 int decode(char *, CODE *); 64 int pencode(char *); 65 static void logmessage(int, char *, char *); 66 static void usage(void); 67 68 struct socks { 69 int sock; 70 int addrlen; 71 struct sockaddr_storage addr; 72 }; 73 74 #ifdef INET6 75 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 76 #else 77 int family = PF_INET; /* protocol family (IPv4 only) */ 78 #endif 79 int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 80 81 /* 82 * logger -- read and log utility 83 * 84 * Reads from an input and arranges to write the result on the system 85 * log. 86 */ 87 int 88 main(argc, argv) 89 int argc; 90 char *argv[]; 91 { 92 int ch, logflags, pri; 93 char *tag, *host, buf[1024]; 94 95 tag = NULL; 96 host = NULL; 97 pri = LOG_USER | LOG_NOTICE; 98 logflags = 0; 99 unsetenv("TZ"); 100 while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1) 101 switch((char)ch) { 102 case '4': 103 family = PF_INET; 104 break; 105 #ifdef INET6 106 case '6': 107 family = PF_INET6; 108 break; 109 #endif 110 case 'A': 111 send_to_all++; 112 break; 113 case 'f': /* file to log */ 114 if (freopen(optarg, "r", stdin) == NULL) 115 err(1, "%s", optarg); 116 break; 117 case 'h': /* hostname to deliver to */ 118 host = optarg; 119 break; 120 case 'i': /* log process id also */ 121 logflags |= LOG_PID; 122 break; 123 case 'p': /* priority */ 124 pri = pencode(optarg); 125 break; 126 case 's': /* log to standard error */ 127 logflags |= LOG_PERROR; 128 break; 129 case 't': /* tag */ 130 tag = optarg; 131 break; 132 case '?': 133 default: 134 usage(); 135 } 136 argc -= optind; 137 argv += optind; 138 139 /* setup for logging */ 140 openlog(tag ? tag : getlogin(), logflags, 0); 141 (void) fclose(stdout); 142 143 /* log input line if appropriate */ 144 if (argc > 0) { 145 register char *p, *endp; 146 int len; 147 148 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { 149 len = strlen(*argv); 150 if (p + len > endp && p > buf) { 151 logmessage(pri, host, buf); 152 p = buf; 153 } 154 if (len > sizeof(buf) - 1) 155 logmessage(pri, host, *argv++); 156 else { 157 if (p != buf) 158 *p++ = ' '; 159 bcopy(*argv++, p, len); 160 *(p += len) = '\0'; 161 } 162 } 163 if (p != buf) 164 logmessage(pri, host, buf); 165 } else 166 while (fgets(buf, sizeof(buf), stdin) != NULL) 167 logmessage(pri, host, buf); 168 exit(0); 169 } 170 171 /* 172 * Send the message to syslog, either on the local host, or on a remote host 173 */ 174 void 175 logmessage(int pri, char *host, char *buf) 176 { 177 static struct socks *socks; 178 static int nsock = 0; 179 struct addrinfo hints, *res, *r; 180 char *line; 181 int maxs, len, sock, error, i, lsent; 182 183 if (host == NULL) { 184 syslog(pri, "%s", buf); 185 return; 186 } 187 188 if (nsock <= 0) { /* set up socket stuff */ 189 /* resolve hostname */ 190 memset(&hints, 0, sizeof(hints)); 191 hints.ai_family = family; 192 hints.ai_socktype = SOCK_DGRAM; 193 error = getaddrinfo(host, "syslog", &hints, &res); 194 if (error == EAI_SERVICE) { 195 warnx ("syslog/udp: unknown service"); /* not fatal */ 196 error = getaddrinfo(host, "514", &hints, &res); 197 } 198 if (error) 199 errx(1, "%s: %s", gai_strerror(error), host); 200 /* count max number of sockets we may open */ 201 for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 202 socks = malloc(maxs * sizeof(struct socks)); 203 if (!socks) 204 errx(1, "couldn't allocate memory for sockets"); 205 for (r = res; r; r = r->ai_next) { 206 sock = socket(r->ai_family, r->ai_socktype, 207 r->ai_protocol); 208 if (sock < 0) 209 continue; 210 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen); 211 socks[nsock].addrlen = r->ai_addrlen; 212 socks[nsock++].sock = sock; 213 } 214 freeaddrinfo(res); 215 if (nsock <= 0) 216 errx(1, "socket"); 217 } 218 219 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1) 220 errx(1, "asprintf"); 221 222 for (i = 0; i < nsock; ++i) { 223 lsent = sendto(socks[i].sock, line, len, 0, 224 (struct sockaddr *)&socks[i].addr, 225 socks[i].addrlen); 226 if (lsent == len && !send_to_all) 227 break; 228 } 229 if (lsent != len) 230 if (lsent == -1) 231 warn ("sendto"); 232 else 233 warnx ("sendto: short send - %d bytes", lsent); 234 235 free(line); 236 } 237 238 /* 239 * Decode a symbolic name to a numeric value 240 */ 241 int 242 pencode(s) 243 register char *s; 244 { 245 char *save; 246 int fac, lev; 247 248 for (save = s; *s && *s != '.'; ++s); 249 if (*s) { 250 *s = '\0'; 251 fac = decode(save, facilitynames); 252 if (fac < 0) 253 errx(1, "unknown facility name: %s", save); 254 *s++ = '.'; 255 } 256 else { 257 fac = 0; 258 s = save; 259 } 260 lev = decode(s, prioritynames); 261 if (lev < 0) 262 errx(1, "unknown priority name: %s", save); 263 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 264 } 265 266 int 267 decode(name, codetab) 268 char *name; 269 CODE *codetab; 270 { 271 register CODE *c; 272 273 if (isdigit(*name)) 274 return (atoi(name)); 275 276 for (c = codetab; c->c_name; c++) 277 if (!strcasecmp(name, c->c_name)) 278 return (c->c_val); 279 280 return (-1); 281 } 282 283 static void 284 usage() 285 { 286 (void)fprintf(stderr, "usage: %s\n", 287 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]" 288 ); 289 exit(1); 290 } 291