From owner-freebsd-bugs Mon Jul 10 6:10:12 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id A14D337B95C for ; Mon, 10 Jul 2000 06:10:02 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id GAA61478; Mon, 10 Jul 2000 06:10:01 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from pancake.earlsfort.iol.ie (pancake.earlsfort.iol.ie [194.125.21.11]) by hub.freebsd.org (Postfix) with ESMTP id 1F0A737B91A for ; Mon, 10 Jul 2000 06:01:27 -0700 (PDT) (envelope-from nick@pancake.earlsfort.iol.ie) Received: (from nick@localhost) by pancake.earlsfort.iol.ie (8.9.1/8.9.1) id OAA24927; Mon, 10 Jul 2000 14:01:24 +0100 (IST) Message-Id: <200007101301.OAA24927@pancake.earlsfort.iol.ie> Date: Mon, 10 Jul 2000 14:01:24 +0100 (IST) From: nick@iol.ie Reply-To: nick@iol.ie To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/19821: extra "-h" option for logger(1) to log to a remote host Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 19821 >Category: bin >Synopsis: logger(1) does not log messages to a remote host. >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Mon Jul 10 06:10:01 PDT 2000 >Closed-Date: >Last-Modified: >Originator: Nick Hilliard >Release: FreeBSD 2.2.6-RELEASE i386 and onwards >Organization: Ireland On-Line >Environment: (not relevant) >Description: There are a bunch of circumstances where the ability to send syslog messages to a remote host is convenience. This patch adds a new option to logger(1), "-h", which specifies the host to send the syslog message to. >How-To-Repeat: (not relevant) >Fix: Apply the following patch. --- logger.c.old Wed Jun 30 12:04:32 1999 +++ logger.c Wed Jun 30 12:37:40 1999 @@ -42,9 +42,15 @@ static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id: logger.c,v 1.2.2.3 1997/09/15 08:32:18 jkh Exp $"; + "$Id: logger.c,v 1.2 1998/01/29 17:28:54 nick Exp $"; #endif /* not lint */ +#include +#include +#include +#include +#include + #include #include #include @@ -57,8 +63,11 @@ int decode __P((char *, CODE *)); int pencode __P((char *)); +void logmessage __P((int, char *, char *)); static void usage __P((void)); +#define MAXBUF 1024 + /* * logger -- read and log utility * @@ -71,13 +80,13 @@ char *argv[]; { int ch, logflags, pri; - char *tag, buf[1024]; + char *tag, *host, buf[MAXBUF]; - tag = NULL; + host = tag = NULL; pri = LOG_NOTICE; logflags = 0; unsetenv("TZ"); - while ((ch = getopt(argc, argv, "f:ip:st:")) != -1) + while ((ch = getopt(argc, argv, "f:h:ip:st:")) != -1) switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) @@ -95,6 +104,9 @@ case 't': /* tag */ tag = optarg; break; + case 'h': + host = optarg; /* hostname to deliver to */ + break; case '?': default: usage(); @@ -114,11 +126,11 @@ for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { - syslog(pri, "%s", buf); + logmessage(pri, host, buf); p = buf; } if (len > sizeof(buf) - 1) - syslog(pri, "%s", *argv++); + logmessage(pri, host, *argv++); else { if (p != buf) *p++ = ' '; @@ -127,14 +139,76 @@ } } if (p != buf) - syslog(pri, "%s", buf); + logmessage(pri, host, buf); } else while (fgets(buf, sizeof(buf), stdin) != NULL) - syslog(pri, "%s", buf); + logmessage(pri, host, buf); exit(0); } /* + * Send the message to syslog, either on the local host, or on a remote host + */ +void +logmessage(int pri, char *host, char *buf) +{ + static int sock = -1; + static struct sockaddr_in sin; + + struct msghdr msg; + struct iovec iov[1]; + ssize_t size; + struct in_addr in; + struct servent *sp; + struct hostent *hp = NULL; + char line[MAXBUF + sizeof("")]; /* int is always < 11 chars */ + + if (host == NULL) { + syslog(pri, "%s", buf); + return; + } + + if (sock == -1) { /* set up socket stuff */ + if ((sp = getservbyname("syslog", "udp")) == NULL) + warnx ("syslog/udp: unknown service"); /* not fatal */ + + /* resolve hostname */ + if (!(inet_aton (host, &in)) && !(hp = gethostbyname(host))) { + errx (1, "unknown host: %s", host); + } + if (hp != NULL) + memcpy ((void *)&in.s_addr, hp->h_addr, + sizeof(struct in_addr)); + + /* set up struct sockaddr_in */ + sin.sin_family = AF_INET; + sin.sin_port = (sp == NULL ? 514 : sp->s_port); + memcpy ((void *)&sin.sin_addr, (void *)&in.s_addr, + sizeof(struct in_addr)); + + sock = socket (PF_INET, SOCK_DGRAM, 0); + if (sock < 0) + errx(1, "socket"); + } + + msg.msg_name = (void *)&sin; + msg.msg_namelen = sizeof sin; + msg.msg_iov = iov; + msg.msg_iovlen = 0; + msg.msg_control = 0; + msg.msg_controllen = 0; + msg.msg_flags = 0; + + snprintf (line, sizeof (line) - 1, "<%d>%s", pri, buf); + + iov[msg.msg_iovlen].iov_base = line; + iov[msg.msg_iovlen++].iov_len = strlen(line); + + if (sendmsg (sock, &msg, 0) < strlen(line)) + warnx ("sendmsg"); +} + +/* * Decode a symbolic name to a numeric value */ int @@ -183,6 +257,6 @@ usage() { (void)fprintf(stderr, - "usage: logger [-is] [-f file] [-p pri] [-t tag] [message ...]\n"); + "usage: logger [-is] [-f file] [-p pri] [-t tag] [-h host] [message ...]\n"); exit(1); } --- logger.1.old Wed Jun 30 12:38:27 1999 +++ logger.1 Wed Jun 30 12:42:48 1999 @@ -43,6 +43,7 @@ .Op Fl f Ar file .Op Fl p Ar pri .Op Fl t Ar tag +.Op Fl h Ar host .Op Ar message ... .Sh DESCRIPTION .Nm Logger @@ -73,6 +74,10 @@ .It Fl t Ar tag Mark every line in the log with the specified .Ar tag . +.It Fl h Ar host +Send the message to the remote system +.Ar host +instead of logging it locally. .It Ar message Write the message to log; if not specified, and the .Fl f >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message