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