syslogd: Add support for '-b :service' to bind to another port

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-10-30 16:25:37 +01:00
parent dfff48fb04
commit 72bbee7812
2 changed files with 29 additions and 11 deletions

View File

@ -15,6 +15,7 @@
.Nm .Nm
.Op Fl ?46Adhnrv .Op Fl ?46Adhnrv
.Op Fl a Ar SOCK .Op Fl a Ar SOCK
.Op Fl b Ar :SVC
.Op Fl f Ar FILE .Op Fl f Ar FILE
.Op Fl l Ar HOST[:HOST] .Op Fl l Ar HOST[:HOST]
.Op Fl m Ar MINUTES .Op Fl m Ar MINUTES
@ -91,6 +92,11 @@ environment needs even more, you have to increase the symbol
within the within the
.Pa syslogd.c .Pa syslogd.c
source file. source file.
.It Fl b Ar :service
Bind to a specific port. The port can be specified as a service name or
number. The default service is
.Ql syslog
(514/udp).
.It Fl d .It Fl d
Turns on debug mode. This implicitly enables Turns on debug mode. This implicitly enables
.Fl n .Fl n
@ -298,7 +304,7 @@ same time syslogd listens on the standard syslog port for messages
forwarded from other hosts. To have this work correctly the forwarded from other hosts. To have this work correctly the
.Xr services 5 .Xr services 5
files (typically found in files (typically found in
.Pa /etc/servces ) .Pa /etc/services )
must have the following entry: must have the following entry:
.Bd -literal -offset indent .Bd -literal -offset indent
syslog 514/udp syslog 514/udp

View File

@ -277,6 +277,7 @@ static int *finet = NULL; /* Internet datagram sockets */
static int Initialized = 0; /* set when we have initialized ourselves */ static int Initialized = 0; /* set when we have initialized ourselves */
static int MarkInterval = 20 * 60; /* interval between marks in seconds */ static int MarkInterval = 20 * 60; /* interval between marks in seconds */
static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
static char *service = "syslog"; /* Port to bind to, default 514/udp */
static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */ static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */
static int send_to_all; /* send message to all IPv4/IPv6 addresses */ static int send_to_all; /* send message to all IPv4/IPv6 addresses */
static int MarkSeq = 0; /* mark sequence number */ static int MarkSeq = 0; /* mark sequence number */
@ -308,6 +309,7 @@ void wallmsg(struct filed *f, struct iovec *iov, int iovcnt);
void reapchild(); void reapchild();
const char *cvtaddr(struct sockaddr_storage *f, int len); const char *cvtaddr(struct sockaddr_storage *f, int len);
const char *cvthname(struct sockaddr_storage *f, int len); const char *cvthname(struct sockaddr_storage *f, int len);
static void flog(int pri, char *fmt, ...);
void domark(); void domark();
void debug_switch(); void debug_switch();
void logerror(const char *type); void logerror(const char *type);
@ -334,6 +336,7 @@ int main(int argc, char *argv[])
int fd; int fd;
fd_set readfds; fd_set readfds;
char line[MAXLINE + 1]; char line[MAXLINE + 1];
char *ptr;
int num_fds, maxfds; int num_fds, maxfds;
int i, ch; int i, ch;
@ -342,7 +345,7 @@ int main(int argc, char *argv[])
funix[i] = -1; funix[i] = -1;
} }
while ((ch = getopt(argc, argv, "46Aa:dhHf:l:m:nP:p:R:rs:v?")) != EOF) { while ((ch = getopt(argc, argv, "46Aa:b:dhHf:l:m:nP:p:R:rs:v?")) != EOF) {
switch ((char)ch) { switch ((char)ch) {
case '4': case '4':
family = PF_INET; family = PF_INET;
@ -363,6 +366,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "Out of descriptors, ignoring %s\n", optarg); fprintf(stderr, "Out of descriptors, ignoring %s\n", optarg);
break; break;
case 'b':
ptr = strchr(optarg, ':');
if (ptr)
service = ++ptr;
break;
case 'd': /* debug */ case 'd': /* debug */
Debug = 1; Debug = 1;
break; break;
@ -756,11 +765,11 @@ static int *create_inet_sockets(void)
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
hints.ai_family = family; hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(NULL, "syslog", &hints, &res); error = getaddrinfo(NULL, service, &hints, &res);
if (error) { if (error) {
logerror("network logging disabled (syslog/udp service unknown)."); flog(LOG_SYSLOG | LOG_ERR, "network logging disabled (%s/udp "
" service unknown): %s", service, gai_strerror(error));
logerror("see syslogd(8) for details of whether and how to enable it."); logerror("see syslogd(8) for details of whether and how to enable it.");
logerror(gai_strerror(error));
return NULL; return NULL;
} }
@ -1749,12 +1758,16 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
logit(" %s\n", f->f_un.f_forw.f_hname); logit(" %s\n", f->f_un.f_forw.f_hname);
fwd_suspend = time(NULL) - f->f_time; fwd_suspend = time(NULL) - f->f_time;
if (fwd_suspend >= INET_SUSPEND_TIME) { if (fwd_suspend >= INET_SUSPEND_TIME) {
char *host;
logit("Forwarding suspension to unknown over, retrying\n"); logit("Forwarding suspension to unknown over, retrying\n");
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = family; hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
if ((err = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints, &ai))) { host = f->f_un.f_forw.f_hname;
logit("Failure: %s\n", gai_strerror(err)); err = getaddrinfo(host, service, &hints, &ai);
if (err) {
logit("Failure resolving %s:%s: %s\n", host, service, gai_strerror(err));
logit("Retries: %d\n", f->f_prevcount); logit("Retries: %d\n", f->f_prevcount);
if (--f->f_prevcount < 0) { if (--f->f_prevcount < 0) {
logit("Giving up.\n"); logit("Giving up.\n");
@ -1762,15 +1775,14 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
} else } else
logit("Left retries: %d\n", f->f_prevcount); logit("Left retries: %d\n", f->f_prevcount);
} else { } else {
logit("%s found, resuming.\n", f->f_un.f_forw.f_hname); logit("%s found, resuming.\n", host);
f->f_un.f_forw.f_addr = ai; f->f_un.f_forw.f_addr = ai;
f->f_prevcount = 0; f->f_prevcount = 0;
f->f_type = F_FORW; f->f_type = F_FORW;
goto f_forw; goto f_forw;
} }
} else } else
logit("Forwarding suspension not over, time " logit("Forwarding suspension not over, time left: %d\n",
"left: %d\n",
INET_SUSPEND_TIME - fwd_suspend); INET_SUSPEND_TIME - fwd_suspend);
break; break;
@ -2651,7 +2663,7 @@ void cfline(char *line, struct filed *f)
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = family; hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo(p, "syslog", &hints, &ai)) { if (getaddrinfo(p, service, &hints, &ai)) {
/* /*
* The host might be unknown due to an * The host might be unknown due to an
* inaccessible nameserver (perhaps on the * inaccessible nameserver (perhaps on the