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