11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3127604Scharnierstatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 36146756Scharnier#if 0 371590Srgrimes#ifndef lint 381590Srgrimesstatic char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; 39146756Scharnier#endif /* not lint */ 4027604Scharnier#endif 41146756Scharnier 4293525Sdwmalone#include <sys/cdefs.h> 4393525Sdwmalone__FBSDID("$FreeBSD$"); 4493525Sdwmalone 4563402Sdwmalone#include <sys/types.h> 4663402Sdwmalone#include <sys/socket.h> 4763402Sdwmalone#include <netinet/in.h> 4863402Sdwmalone 4927604Scharnier#include <ctype.h> 5027604Scharnier#include <err.h> 5163402Sdwmalone#include <netdb.h> 5227604Scharnier#include <stdio.h> 531590Srgrimes#include <stdlib.h> 541590Srgrimes#include <string.h> 5527604Scharnier#include <unistd.h> 561590Srgrimes 571590Srgrimes#define SYSLOG_NAMES 581590Srgrimes#include <syslog.h> 591590Srgrimes 60241736Sedstatic int decode(char *, const CODE *); 61227170Sedstatic int pencode(char *); 62220448Sedwinstatic void logmessage(int, const char *, const char *, const char *, 63220448Sedwin const char *); 6492920Simpstatic void usage(void); 651590Srgrimes 6670100Sumestruct socks { 6770100Sume int sock; 6870100Sume int addrlen; 6970100Sume struct sockaddr_storage addr; 7070100Sume}; 7170100Sume 7270100Sume#ifdef INET6 73227170Sedstatic int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ 7470100Sume#else 75227170Sedstatic int family = PF_INET; /* protocol family (IPv4 only) */ 7670100Sume#endif 77227170Sedstatic int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ 7870100Sume 791590Srgrimes/* 801590Srgrimes * logger -- read and log utility 811590Srgrimes * 821590Srgrimes * Reads from an input and arranges to write the result on the system 831590Srgrimes * log. 841590Srgrimes */ 851590Srgrimesint 86102944Sdwmalonemain(int argc, char *argv[]) 871590Srgrimes{ 881590Srgrimes int ch, logflags, pri; 89169344Sdwmalone char *tag, *host, buf[1024]; 90169344Sdwmalone const char *svcname; 911590Srgrimes 921590Srgrimes tag = NULL; 9363402Sdwmalone host = NULL; 94160917Sbms svcname = "syslog"; 9583150Sru pri = LOG_USER | LOG_NOTICE; 961590Srgrimes logflags = 0; 9719075Sphk unsetenv("TZ"); 98160917Sbms while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1) 991590Srgrimes switch((char)ch) { 10070100Sume case '4': 10170100Sume family = PF_INET; 10270100Sume break; 10370100Sume#ifdef INET6 10470100Sume case '6': 10570100Sume family = PF_INET6; 10670100Sume break; 10770100Sume#endif 10870100Sume case 'A': 10970100Sume send_to_all++; 11070100Sume break; 1111590Srgrimes case 'f': /* file to log */ 11227604Scharnier if (freopen(optarg, "r", stdin) == NULL) 11327604Scharnier err(1, "%s", optarg); 114198702Smckusick setvbuf(stdin, 0, _IONBF, 0); 1151590Srgrimes break; 11663402Sdwmalone case 'h': /* hostname to deliver to */ 11763402Sdwmalone host = optarg; 11863402Sdwmalone break; 1191590Srgrimes case 'i': /* log process id also */ 1201590Srgrimes logflags |= LOG_PID; 1211590Srgrimes break; 122160917Sbms case 'P': /* service name or port number */ 123160917Sbms svcname = optarg; 124160917Sbms break; 1251590Srgrimes case 'p': /* priority */ 1261590Srgrimes pri = pencode(optarg); 1271590Srgrimes break; 1281590Srgrimes case 's': /* log to standard error */ 1291590Srgrimes logflags |= LOG_PERROR; 1301590Srgrimes break; 1311590Srgrimes case 't': /* tag */ 1321590Srgrimes tag = optarg; 1331590Srgrimes break; 1341590Srgrimes case '?': 1351590Srgrimes default: 1361590Srgrimes usage(); 1371590Srgrimes } 1381590Srgrimes argc -= optind; 1391590Srgrimes argv += optind; 1401590Srgrimes 141220448Sedwin if (tag == NULL) 142220448Sedwin tag = getlogin(); 1431590Srgrimes /* setup for logging */ 144220448Sedwin if (host == NULL) 145220448Sedwin openlog(tag, logflags, 0); 1461590Srgrimes (void) fclose(stdout); 1471590Srgrimes 1481590Srgrimes /* log input line if appropriate */ 1491590Srgrimes if (argc > 0) { 150102944Sdwmalone char *p, *endp; 15193525Sdwmalone size_t len; 1521590Srgrimes 1531590Srgrimes for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { 1541590Srgrimes len = strlen(*argv); 1551590Srgrimes if (p + len > endp && p > buf) { 156220448Sedwin logmessage(pri, tag, host, svcname, buf); 1571590Srgrimes p = buf; 1581590Srgrimes } 1591590Srgrimes if (len > sizeof(buf) - 1) 160220448Sedwin logmessage(pri, tag, host, svcname, *argv++); 1611590Srgrimes else { 1621590Srgrimes if (p != buf) 1631590Srgrimes *p++ = ' '; 1641590Srgrimes bcopy(*argv++, p, len); 1651590Srgrimes *(p += len) = '\0'; 1661590Srgrimes } 1671590Srgrimes } 1681590Srgrimes if (p != buf) 169220448Sedwin logmessage(pri, tag, host, svcname, buf); 1701590Srgrimes } else 1711590Srgrimes while (fgets(buf, sizeof(buf), stdin) != NULL) 172220448Sedwin logmessage(pri, tag, host, svcname, buf); 1731590Srgrimes exit(0); 1741590Srgrimes} 1751590Srgrimes 1761590Srgrimes/* 17763402Sdwmalone * Send the message to syslog, either on the local host, or on a remote host 17863402Sdwmalone */ 179227170Sedstatic void 180220448Sedwinlogmessage(int pri, const char *tag, const char *host, const char *svcname, 181220448Sedwin const char *buf) 18263402Sdwmalone{ 18370100Sume static struct socks *socks; 18470100Sume static int nsock = 0; 18570100Sume struct addrinfo hints, *res, *r; 18663402Sdwmalone char *line; 18770100Sume int maxs, len, sock, error, i, lsent; 18863402Sdwmalone 18963402Sdwmalone if (host == NULL) { 19063402Sdwmalone syslog(pri, "%s", buf); 19163402Sdwmalone return; 19263402Sdwmalone } 19363402Sdwmalone 19470100Sume if (nsock <= 0) { /* set up socket stuff */ 19570100Sume /* resolve hostname */ 19670100Sume memset(&hints, 0, sizeof(hints)); 19770100Sume hints.ai_family = family; 19870100Sume hints.ai_socktype = SOCK_DGRAM; 199160917Sbms error = getaddrinfo(host, svcname, &hints, &res); 20070100Sume if (error == EAI_SERVICE) { 201160917Sbms warnx("%s/udp: unknown service", svcname); 20270100Sume error = getaddrinfo(host, "514", &hints, &res); 20370100Sume } 20470100Sume if (error) 20570100Sume errx(1, "%s: %s", gai_strerror(error), host); 20670100Sume /* count max number of sockets we may open */ 20770100Sume for (maxs = 0, r = res; r; r = r->ai_next, maxs++); 20870100Sume socks = malloc(maxs * sizeof(struct socks)); 20970100Sume if (!socks) 21070100Sume errx(1, "couldn't allocate memory for sockets"); 21170100Sume for (r = res; r; r = r->ai_next) { 21270100Sume sock = socket(r->ai_family, r->ai_socktype, 21370100Sume r->ai_protocol); 21470100Sume if (sock < 0) 21570100Sume continue; 21670100Sume memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen); 21770100Sume socks[nsock].addrlen = r->ai_addrlen; 21870100Sume socks[nsock++].sock = sock; 21970100Sume } 22070100Sume freeaddrinfo(res); 22170100Sume if (nsock <= 0) 22263402Sdwmalone errx(1, "socket"); 22363402Sdwmalone } 22463402Sdwmalone 225220448Sedwin if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1) 22663402Sdwmalone errx(1, "asprintf"); 22763402Sdwmalone 228146756Scharnier lsent = -1; 22970100Sume for (i = 0; i < nsock; ++i) { 23070100Sume lsent = sendto(socks[i].sock, line, len, 0, 23170100Sume (struct sockaddr *)&socks[i].addr, 23270100Sume socks[i].addrlen); 23370100Sume if (lsent == len && !send_to_all) 23470100Sume break; 23570100Sume } 23693525Sdwmalone if (lsent != len) { 23791433Sfenner if (lsent == -1) 23891433Sfenner warn ("sendto"); 23991433Sfenner else 24091433Sfenner warnx ("sendto: short send - %d bytes", lsent); 24193525Sdwmalone } 24263402Sdwmalone 24363402Sdwmalone free(line); 24463402Sdwmalone} 24563402Sdwmalone 24663402Sdwmalone/* 2471590Srgrimes * Decode a symbolic name to a numeric value 2481590Srgrimes */ 249227170Sedstatic int 250102944Sdwmalonepencode(char *s) 2511590Srgrimes{ 2521590Srgrimes char *save; 2531590Srgrimes int fac, lev; 2541590Srgrimes 2551590Srgrimes for (save = s; *s && *s != '.'; ++s); 2561590Srgrimes if (*s) { 2571590Srgrimes *s = '\0'; 2581590Srgrimes fac = decode(save, facilitynames); 25927604Scharnier if (fac < 0) 26027604Scharnier errx(1, "unknown facility name: %s", save); 2611590Srgrimes *s++ = '.'; 2621590Srgrimes } 2631590Srgrimes else { 2641590Srgrimes fac = 0; 2651590Srgrimes s = save; 2661590Srgrimes } 2671590Srgrimes lev = decode(s, prioritynames); 26827604Scharnier if (lev < 0) 26927604Scharnier errx(1, "unknown priority name: %s", save); 2701590Srgrimes return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 2711590Srgrimes} 2721590Srgrimes 273227170Sedstatic int 274241736Seddecode(char *name, const CODE *codetab) 2751590Srgrimes{ 276241736Sed const CODE *c; 2771590Srgrimes 2781590Srgrimes if (isdigit(*name)) 2791590Srgrimes return (atoi(name)); 2801590Srgrimes 2811590Srgrimes for (c = codetab; c->c_name; c++) 2821590Srgrimes if (!strcasecmp(name, c->c_name)) 2831590Srgrimes return (c->c_val); 2841590Srgrimes 2851590Srgrimes return (-1); 2861590Srgrimes} 2871590Srgrimes 28827604Scharnierstatic void 289102944Sdwmaloneusage(void) 2901590Srgrimes{ 29163402Sdwmalone (void)fprintf(stderr, "usage: %s\n", 292160917Sbms "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n" 293160917Sbms " [message ...]" 29463402Sdwmalone ); 2951590Srgrimes exit(1); 2961590Srgrimes} 297