From e0ba5f137d3835fe18598a4e721e026314fe4f2e Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 17 Apr 2006 20:12:35 +0000 Subject: Add the ability to log to an arbitrary udp port as well as the standard syslog port. This allows syslog to easily export lines of interest to an external logging system. --- usr.sbin/syslogd/syslog.conf.5 | 3 +++ usr.sbin/syslogd/syslogd.c | 46 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/usr.sbin/syslogd/syslog.conf.5 b/usr.sbin/syslogd/syslog.conf.5 index 88e620c..bf94c41 100644 --- a/usr.sbin/syslogd/syslog.conf.5 +++ b/usr.sbin/syslogd/syslog.conf.5 @@ -337,6 +337,9 @@ sign). Selected messages are forwarded to the .Xr syslogd 8 program on the named host. +If a port number is added after a colon +.Pq ':' +then that port will be used as the destination port rather than the usual syslog port. .It A comma separated list of users. Selected messages are written to those users diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 23e30f8..765859f 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -1154,12 +1154,19 @@ fprintlog(struct filed *f, int flags, const char *msg) f->f_time = now; switch (f->f_type) { + int port; case F_UNUSED: dprintf("\n"); break; case F_FORW: - dprintf(" %s\n", f->f_un.f_forw.f_hname); + port = (int)ntohs(((struct sockaddr_in *) + (f->f_un.f_forw.f_addr->ai_addr))->sin_port); + if (port != 514) { + dprintf(" %s:%d\n", f->f_un.f_forw.f_hname, port); + } else { + dprintf(" %s\n", f->f_un.f_forw.f_hname); + } /* check for local vs remote messages */ if (strcasecmp(f->f_prevhost, LocalHostName)) l = snprintf(line, sizeof line - 1, @@ -1658,6 +1665,7 @@ init(int signo) Initialized = 1; if (Debug) { + int port; for (f = Files; f; f = f->f_next) { for (i = 0; i <= LOG_NFACILITIES; i++) if (f->f_pmask[i] == INTERNAL_NOPRI) @@ -1676,7 +1684,14 @@ init(int signo) break; case F_FORW: - printf("%s", f->f_un.f_forw.f_hname); + port = (int)ntohs(((struct sockaddr_in *) + (f->f_un.f_forw.f_addr->ai_addr))->sin_port); + if (port != 514) { + printf("%s:%d", + f->f_un.f_forw.f_hname, port); + } else { + printf("%s", f->f_un.f_forw.f_hname); + } break; case F_PIPE: @@ -1881,13 +1896,32 @@ cfline(const char *line, struct filed *f, const char *prog, const char *host) switch (*p) { case '@': - (void)strlcpy(f->f_un.f_forw.f_hname, ++p, - sizeof(f->f_un.f_forw.f_hname)); + { + char *tp; + /* + * scan forward to see if there is a port defined. + * so we can't use strlcpy.. + */ + i = sizeof(f->f_un.f_forw.f_hname); + tp = f->f_un.f_forw.f_hname; + p++; + + while (*p && (*p != ':') && (i-- > 0)) { + *tp++ = *p++; + } + *tp = '\0'; + } + /* See if we copied a domain and have a port */ + if (*p == ':') + p++; + else + p = NULL; + memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_DGRAM; - error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, - &res); + error = getaddrinfo(f->f_un.f_forw.f_hname, + p ? p: "syslog", &hints, &res); if (error) { logerror(gai_strerror(error)); break; -- cgit v1.1