diff --git a/usr.sbin/syslogd/syslogd.8 b/usr.sbin/syslogd/syslogd.8 --- a/usr.sbin/syslogd/syslogd.8 +++ b/usr.sbin/syslogd/syslogd.8 @@ -46,6 +46,7 @@ .Op Fl O Ar format .Op Fl P Ar pid_file .Op Fl p Ar log_socket +.Op Fl r Ar format .Op Fl S Ar logpriv_socket .Sh DESCRIPTION The @@ -266,8 +267,12 @@ .Fl s . .It Fl n Disable DNS query for every request. -.It Fl O Ar format -Select the output format of generated log messages. +.It Fl O Ar format , Fl r Ar format +Select the output format of generated log messages +.Pq Fl O , +or just the format of log messages sent over the network to a remote +log server +.Pq Fl r . The values .Ar bsd and diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -363,6 +363,9 @@ #define F_WALL 6 /* everyone logged on */ #define F_PIPE 7 /* pipe to program */ +enum log_format { DEFAULT_FORMAT = 0, RFC3164, RFC5424 }; +#define DEFAULT_FORMAT_IS RFC3164 + static const char *TypeNames[] = { "UNUSED", "FILE", "TTY", "CONSOLE", "FORW", "USERS", "WALL", "PIPE" @@ -406,7 +409,14 @@ static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */ static struct pidfh *pfh; static int sigpipe[2]; /* Pipe to catch a signal during select(). */ -static bool RFC3164OutputFormat = true; /* Use legacy format by default. */ + +/* + * Formats to write output in. NetworkFormat controls logs sent to + * remote syslog servers; when unset, it defaults to the same as + * OutputFormat. OutputFormat defaults to RFC3164 format for backwards + * compatibility. + */ +static enum log_format NetworkFormat, OutputFormat; static volatile sig_atomic_t MarkSet, WantDie, WantInitialize, WantReapchild; @@ -553,7 +563,7 @@ if (madvise(NULL, 0, MADV_PROTECT) != 0) dprintf("madvise() failed: %s\n", strerror(errno)); - while ((ch = getopt(argc, argv, "468Aa:b:cCdf:FHkl:M:m:nNoO:p:P:sS:Tuv")) + while ((ch = getopt(argc, argv, "468Aa:b:cCdf:FHkl:M:m:nNoO:p:P:r:sS:Tuv")) != -1) switch (ch) { #ifdef INET @@ -686,10 +696,10 @@ case 'O': if (strcmp(optarg, "bsd") == 0 || strcmp(optarg, "rfc3164") == 0) - RFC3164OutputFormat = true; + OutputFormat = RFC3164; else if (strcmp(optarg, "syslog") == 0 || strcmp(optarg, "rfc5424") == 0) - RFC3164OutputFormat = false; + OutputFormat = RFC5424; else usage(); break; @@ -699,6 +709,16 @@ case 'P': /* path for alt. PID */ PidFile = optarg; break; + case 'r': + if (strcmp(optarg, "bsd") == 0 || + strcmp(optarg, "rfc3164") == 0) + NetworkFormat = RFC3164; + else if (strcmp(optarg, "syslog") == 0 || + strcmp(optarg, "rfc5424") == 0) + NetworkFormat = RFC5424; + else + usage(); + break; case 's': /* no network mode */ SecureMode++; break; @@ -717,7 +737,13 @@ if ((argc -= optind) != 0) usage(); - if (RFC3164OutputFormat && MaxForwardLen > 1024) + /* Explicitly set formats which have been defaulted to the defaults */ + if (OutputFormat == DEFAULT_FORMAT) + OutputFormat = DEFAULT_FORMAT_IS; + if (NetworkFormat == DEFAULT_FORMAT) + NetworkFormat = OutputFormat; + + if (NetworkFormat == RFC3164 && MaxForwardLen > 1024) errx(1, "RFC 3164 messages may not exceed 1024 bytes"); /* Pipe to catch a signal during select(). */ @@ -960,7 +986,7 @@ " [-b bind_address] [-f config_file]\n" " [-l [mode:]path] [-M fwd_length]\n" " [-m mark_interval] [-O format] [-P pid_file]\n" - " [-p log_socket] [-S logpriv_socket]\n"); + " [-p log_socket] [-r format] [-S logpriv_socket]\n"); exit(1); } @@ -2148,6 +2174,7 @@ const char *procid, const char *msgid __unused, const char *structured_data __unused, const char *msg, int flags) { + enum log_format want_format; dprintf("Logging to %s", TypeNames[f->f_type]); f->f_time = now; @@ -2157,7 +2184,8 @@ return; } - if (RFC3164OutputFormat) + want_format = (f->f_type == F_FORW) ? NetworkFormat : OutputFormat; + if (want_format == RFC3164) fprintlog_rfc3164(f, hostname, app_name, procid, msg, flags); else fprintlog_rfc5424(f, hostname, app_name, procid, msgid, @@ -2306,7 +2334,7 @@ if (hl > 0 && hname[hl-1] == '.') hname[--hl] = '\0'; /* RFC 5424 prefers logging FQDNs. */ - if (RFC3164OutputFormat) + if (OutputFormat == RFC3164) trimdomain(hname, hl); return (hname); } @@ -2573,7 +2601,7 @@ err(EX_OSERR, "gethostname() failed"); if ((p = strchr(LocalHostName, '.')) != NULL) { /* RFC 5424 prefers logging FQDNs. */ - if (RFC3164OutputFormat) + if (OutputFormat == RFC3164) *p = '\0'; LocalDomain = p + 1; } else { @@ -2935,7 +2963,7 @@ if (hl > 0 && f->f_host[hl-1] == '.') f->f_host[--hl] = '\0'; /* RFC 5424 prefers logging FQDNs. */ - if (RFC3164OutputFormat) + if (OutputFormat == RFC3164) trimdomain(f->f_host, hl); }