Bug 19821

Summary: logger(1) does not log messages to a remote host.
Product: Base System Reporter: nick <nick>
Component: binAssignee: dwmalone
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 2.2.6-RELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description nick 2000-07-10 14:10:01 UTC
	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.

Fix: Apply the following patch.
	
How-To-Repeat: 
	(not relevant)
Comment 1 dwmalone freebsd_committer freebsd_triage 2000-07-16 10:39:19 UTC
Responsible Changed
From-To: freebsd-bugs->dwmalone

I'll have a look at this. 
.
Comment 2 dwmalone 2000-07-17 00:36:49 UTC
I've cleaned up the patch a tiny bit, and being able to log to
remote hosts seems useful. Logger seems relatively untouched,
so I'm not sure who would be interested in OKing this feature.

	David.

Index: logger.1
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/usr.bin/logger/logger.1,v
retrieving revision 1.5
diff -u -r1.5 logger.1
--- logger.1	2000/03/26 14:39:03	1.5
+++ logger.1	2000/07/16 22:12:41
@@ -44,6 +44,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
Index: logger.c
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/usr.bin/logger/logger.c,v
retrieving revision 1.5
diff -u -r1.5 logger.c
--- logger.c	1999/08/28 01:03:08	1.5
+++ logger.c	2000/07/16 23:09:48
@@ -45,8 +45,14 @@
   "$FreeBSD: src/usr.bin/logger/logger.c,v 1.5 1999/08/28 01:03:08 peter Exp $";
 #endif /* not lint */
 
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+
 #include <ctype.h>
 #include <err.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -57,6 +63,7 @@
 
 int	decode __P((char *, CODE *));
 int	pencode __P((char *));
+void	logmessage __P((int, char *, char *));
 static void	usage __P((void));
 
 /*
@@ -71,13 +78,13 @@
 	char *argv[];
 {
 	int ch, logflags, pri;
-	char *tag, buf[1024];
+	char *tag, *host, buf[1024];
 
-	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 +102,9 @@
 		case 't':		/* tag */
 			tag = optarg;
 			break;
+		case 'h':
+			host = optarg;	/* hostname to deliver to */
+			break;
 		case '?':
 		default:
 			usage();
@@ -114,11 +124,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 +137,62 @@
 			}
 		}
 		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;
+	char *line;
+	int len;
+
+	if (host == NULL) {
+		syslog(pri, "%s", buf);
+		return;
+	}
+
+	if (sock == -1) {	/* set up socket stuff */
+		struct servent *sp;
+		struct hostent *hp = NULL;
+
+		sin.sin_family = AF_INET;
+
+		if ((sp = getservbyname("syslog", "udp")) == NULL)
+			warnx ("syslog/udp: unknown service");	/* not fatal */
+		sin.sin_port = (sp == NULL ? htons(514) : sp->s_port);
+
+		/* resolve hostname */
+		if (!(inet_aton(host, &sin.sin_addr)) &&
+		    (hp = gethostbyname(host)) == NULL)
+			errx(1, "unknown host: %s", host);
+		if (hp != NULL)
+			memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
+
+		sock = socket(PF_INET, SOCK_DGRAM, 0);
+		if (sock < 0)
+			errx(1, "socket");
+	}
+
+	if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
+		errx(1, "asprintf");
+
+	if (sendto(sock, line, len, 0, (struct sockaddr *)&sin, sizeof(sin))
+	    < len)
+		warnx ("sendmsg");
+
+	free(line);
+}
+
+/*
  *  Decode a symbolic name to a numeric value
  */
 int
@@ -183,6 +241,7 @@
 usage()
 {
 	(void)fprintf(stderr,
-	    "usage: logger [-is] [-f file] [-p pri] [-t tag] [message ...]\n");
+	    "usage: logger [-is] [-f file] [-h host] [-p pri] [-t tag]"
+	    " [message ...]\n");
 	exit(1);
 }
Comment 3 dwmalone freebsd_committer freebsd_triage 2000-08-17 19:53:37 UTC
State Changed
From-To: open->closed

MFC'ed by Paul Sabb.