Commit Graph

112 Commits

Author SHA1 Message Date
Alex Bligh b6b67955ef logger: Add support to logger for RFC6587 octet counting
This patch adds support to logger for RFC6587 octet counting.
RFC6587 provides support for two sorts of framing:

1. Octet counting (at RFC6587 s3.4.1)

   In essence each frame is preceded by a decimal length and a
   space.

2. Non-transparent framing (at RFC6587 s3.4.2), also called
   'octet stuffing'

   In essence each frame is terminated by a `\n`

Prior to this patch, logger used option 2 (non-transparent framing)
on TCP, and used no framing on UDP. After this patch, the default
behaviour is unchanged, but if the '--octet-count' option is supplied,
option 1 is used for both TCP and UDP. Arguably octet count framing
makes little sense on UDP, but some servers provide it and this
allows testing of those servers.

Signed-off-by: Alex Bligh <alex@alex.org.uk>
2015-07-29 10:33:25 +02:00
Sami Kerola 9b83e7a48d logger: do not exit when socket errors are not enforced
The libc openlog(3) does not have error detection whether unix socket
could be opened.  As a side effect that made it possible to use logger
even if syslogd was not running.  Of course user message in these cases
were lost.  This change makes the logger do behave similar way again, so
that sysvinit scripts can successfully pipe messages to logger when ever.

Addresses: https://bugs.debian.org/787864
Addresses: https://bugs.debian.org/790875
Reported-by: Andreas Beckmann <anbe@debian.org>
Reported-by: Andreas Henriksson <andreas@fatal.se>
Tested-by: Robie Basak <robie.basak@ubuntu.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-07-20 10:54:44 +02:00
Ruediger Meier bcf7e14939 logger: --stderr and --no-act turn "auto-errors" on
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2015-04-09 17:55:17 +02:00
Sami Kerola c3dd2ecd5f logger: fix memory leaks
Reported-by: Patrick Plagwitz <patrick.plagwitz@fau.de>
Signed-off-by: Sami Kerola <sami.kerola@lastminute.com>
2015-04-07 08:58:19 +01:00
Patrick Plagwitz 4a8919a4e5 logger: generate header when reading message from stdin
This change fixes crashing error, that ought not to be simply avoided.

$ echo foo | logger -n localhost
Segmentation fault (core dumped)

If the ctl->hdr is just checked not to be NULL syslog message will not
have valid header, so generating such is not optional when reading
message from stdin and writing it to remote destination.

Reviewed-by: Bernhard Voelker <mail@bernhard-voelker.de>
Signed-off-by: Patrick Plagwitz <patrick.plagwitz@fau.de>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-04-07 08:51:05 +01:00
Karel Zak 98e90a4901 logger: fix LOGGER_TEST_TIMEOFDAY check
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-23 11:40:45 +01:00
Karel Zak ef5fb28001 logger: add -DTEST_LOGGER
"make test_logger" now compiles logger(1) test program
to overwrite system datetime stuff, hostname and PID, for example:

   export TZ=GMT
   export LOGGER_TEST_TIMEOFDAY=1234567890.123456
   export LOGGER_TEST_HOSTNAME=foo
   export LOGGER_TEST_GETPID=123

   ./test_logger --rfc5424 --no-act --stderr -i --tag MyTag mesg
   <13>1 2009-02-13T23:31:30.123456+00:00 foo MyTag 123 - [timeQuality tzKnown="1" isSynced="0"] mesg

if the LOGGER_TEST_* variables are not specified then default to
standard logger(1) behavior.

Note that it would be possible to use for example "unshare --utc" to
make hostname stable and portable, but LOGGER_TEST_* variables allow
to keep the tests less complex.

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-16 14:01:53 +01:00
Karel Zak fd343a0572 logger: add --no-act for testing
* force --journal mode to also output to stderr when the option
  --stderr specified on command line

* add --no-act to avoid all write() operations to make it possible to
  write tests without "spam" system logs

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-16 12:38:46 +01:00
Karel Zak 773df0fa2f logger: fix rfc5424 format crash
$ logger --rfc5424=notq message
Segmentation fault (core dumped)

Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-16 11:51:05 +01:00
Sami Kerola 8fce3924e5 logger: use errx() when checking user input
Additionally inform in usage() the --msgid requires an argument.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-03-16 11:45:38 +01:00
Sami Kerola d5f930614b logger: check xgethostname() return value
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-03-16 11:45:38 +01:00
Sami Kerola 9a13f968e6 logger: tidy few indentation issues
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-03-16 11:45:38 +01:00
Stef Walter 8d341322db logger: Fix use of errno after strtol() without zeroing first
References: https://bugzilla.redhat.com/show_bug.cgi?id=1202104
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-16 11:25:37 +01:00
Karel Zak 7ff6948e59 logger: use xstrdup()
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-13 15:36:39 +01:00
Karel Zak 2cb4046532 logger: small change in usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-11 10:51:13 +01:00
Rainer Gerhards 55f5bc662e logger: add --msgid option, permits to set RFC5424 MSGID field 2015-03-10 17:47:30 +01:00
Rainer Gerhards 4826184bce logger: fix multiple format bugs in rfc5424 formatter
This is more or less a complete rewrite of the formatter. It had
multiple issue, e.g. a missing field (MSGID?) and invalid handling
of nil values.
2015-03-10 17:26:14 +01:00
Karel Zak 7d3a07d87f logger: check for ntp_gettime() rather than for timex.h
It seems that musl libc and uClibc without UCLIBC_NTP_LEGACY
does not provide ntp_gettime and compile will fail.

References: https://github.com/karelzak/util-linux/issues/174
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-10 11:37:45 +01:00
Rainer Gerhards ae6846b842 logger: add --skip-empty-lines to prevent logging empty lines
Empty log messages are generally considered useless. This option
enables to turn them off when processing files (including stdin).

[kzak@redhat.com: - rename --skip-empty-lines to --skip-empty,
                  - add the option to getopt_long(),
                  - add the option to bash-completion]

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-10 11:19:33 +01:00
Rainer Gerhards b9ef27f59b logger: bugfix: missing sanity checks with --prio-prefix option
There were no apparent sanity checks other than applying the logmask
when reading PRI values from files. As such, invalid PRIs (tested with
values 192, 210, and 2100) are accepted. This in turn can trigger
problems in various receivers, especially older versions. See here
for details:

http://www.rsyslog.com/remote-syslog-pri-vulnerability-cve-2014-3683/

Note that 2100 was changed to 52 as described in above link.

This patch refactors PRI processing. Invalid PRIs are detected and in
this case the message is sent with the default priority, with the
invalid pri being part of the message to be sent. This is along the
line of what 2.26 did when it detected the PRI was invalid.

The refactoring now also enables pricese tracking of syslog header
length in all cases, so --size is now strictly obeyed.

[kzak@redhat.com: - fix compiler warning [-Wunused-variable]]

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-10 11:19:33 +01:00
Rainer Gerhards 2b3f40c597 logger: refactor message generation
Previously, the message format was generated in one big step. Now
this is refactored to generate the header independently. This not
only provides a better isolation of functionality, but enables
to calculate the size of the header *before* generating the user
part of the message. That in turn is needed in order to precisely
enforce the message size limit. This is especially important while
processing files, as here parts of the message may be lost if the
receiver truncates the message. The file reader itself tries to
guard against this by reading only the permitted number of bytes,
but without knowing the header size, it would mis-guess here.

Note that when --prio-prefix is given, we still do not know exactly
the header length, because the PRI value is between 1 and 3 bytes.
Unfortunately, we do not know the actual size before reading. With
the current (simple) approach, we need to read the full line before
getting the PRI, so this is a hen-egg problem. To solve this, a
more complex reader would be required. It is questionable if this
is necessary for a tool like logger. So currently, we still have a
2-byte window of uncertainty if --prio-prefix is given.

[kzak@redhat.com: - fix compiler warnings [-Wunused-but-set-variable]]

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-10 11:19:33 +01:00
Rainer Gerhards f68b8aa7f5 logger: permit to send messages larger than 1024 characters
This is an important capability that has been specified in RFC5424.
However, messages larger than 1024 chars are being accepted for years
now by at least rsyslog and syslog-ng.

This patch adds the option --size to permit setting a new max
size, with 1024 being the default.

Note that the size limit is only approximative, as we do not take the
header size in account (RFC talks about total message length).

[[kzak@redhat.com: - add 'S' to getopt_long(),
                   - rename --message-size to --size
                   - add the option to bash-completion]

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-10 11:19:16 +01:00
Karel Zak 0d1f3a707d Merge branch 'logger-fix-tcp-framing' of https://github.com/rgerhards/util-linux
* 'logger-fix-tcp-framing' of https://github.com/rgerhards/util-linux:
  logger: bugfix: tcp syslog framing is broken, -T unusable
  logger: refactor the way output is written
2015-03-06 12:31:48 +01:00
Karel Zak 17d66ccc75 logger: use username as the default tag
Reported-by: Rainer Gerhards <rgerhards@adiscon.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-06 12:15:23 +01:00
Rainer Gerhards 940a14a351 logger: bugfix: tcp syslog framing is broken, -T unusable
Logger can send via plain tcp syslog if -n -T options are given.
However, the framing is broken so that a syslog receiver can not
know where the first message ends and the next one starts. It
actually looks like no framing at all is used. Plain TCP syslog
framing is described in RFC6587.

This patch adds RFC6587 octet-stuffed framing to TCP syslog. For
local logging, this is always fine, for remote logging this is
NOT recommended by the IETF (the RFC is historic). However, a
full blown RFC5425 TLS sender seems to be out of scope for a tool
like logger IMO.

This patch also refactors the way output is written, seperating
the message format generators from the output writer.
2015-03-06 12:12:15 +01:00
Rainer Gerhards 4be843064c logger: refactor the way output is written
Previously, output was written in exactly the same way in three
different places. This is now combined into a single function. This
hopefully makes it easier to adapt to changing output needs.
2015-03-06 11:51:31 +01:00
Karel Zak 59d6ed3f01 Merge branch 'logger-kernel-regression' of https://github.com/rgerhards/util-linux
* 'logger-kernel-regression' of https://github.com/rgerhards/util-linux:
  logger: fix -p kern.* priority is accepted regression
  logger: messages are logged with kern.* priority by default
2015-03-06 11:32:17 +01:00
Karel Zak 0f1c825b75 logger: fix whitespace and compiler warning [-Wunused-variable]
misc-utils/logger.c: In function ‘syslog_rfc3164’:
misc-utils/logger.c:336:9: warning: unused variable ‘now’ [-Wunused-variable]

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-06 11:27:16 +01:00
Karel Zak 603358d588 Merge branch 'logger-fix-local-timestamp' of https://github.com/rgerhards/util-linux
* 'logger-fix-local-timestamp' of https://github.com/rgerhards/util-linux:
  logger: fix invalid timestamp regression in local format
2015-03-06 11:25:56 +01:00
Karel Zak c95d320909 logger: add comment
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-03-06 11:05:30 +01:00
Rainer Gerhards d4c814b18a logger: fix -p kern.* priority is accepted regression
Pre 2.26, syslog(3) was used for local logging, and it did not
accept kern.* priorities. This is re-enabled by the patch.
2015-03-05 16:54:34 +01:00
Rainer Gerhards d0b6c4bf8d logger: messages are logged with kern.* priority by default
The default should be user.notice and kern.* should never be used
(syslog(3) forbids this).

This is a severe regression, as messages are now logged to the wrong
bin or not at all. So they get lost and may confuse readers of the
kernel bin.

regression from 2.25.2 to 2.26
2015-03-05 16:44:03 +01:00
Rainer Gerhards 3070ca77ac logger: fix invalid timestamp regression in local format
Since 1d57503378 logger no longer uses
the syslog(3) call. The way the local timestamp is generated did not
match the syslog(3) format. Most importantly, the month name is
formatted based on the user's local. For example:

$ ./logger --stderr  test with logger 2.26.39-eb651-dirty
<5>Mär  5 14:17:47 logger: test with logger 2.26.39-eb651-dirty

"Mär" like in German "März" for "March".

previously:
$ logger --stderr  test with logger 2.25.2
rger: test with logger 2.25.2

In the system log file, this results to the following:

Mar  5 14:17:47 host Mär  5 14:17:47 logger: test with logger 2.26.39-eb651-dirty
Mar  5 14:18:01 host rger: test with logger 2.25.2

This local naming is invalid as of RFC3164. One may argue that
the local log socket traditionally does not have RFC3164 format,
but the timestamp always was as defined in RFC3164 (and along
the lines of the ctime() call). Anything else would also be impractical,
as a syslog parser would otherwise need to know about all
potential locale-specific representations of month names.

This patch corrects the problem and also refactors the timestamp
handling a bit. The same timestamp is needed in local and rfc3164
processing, so there now is a new function to create that stamp.
2015-03-05 15:20:50 +01:00
Rainer Gerhards 7dc2080433 logger: fix inconsistent format regression when logging locally
The message format when writing to local sockets is inconsistent. Example:

$ ./logger --stderr test
<5>Mär  4 11:03:30 logger: test
$ ./logger -u /dev/log --stderr test
<5>1 2015-03-04T11:03:31.699841+0100 ubuntu1404esp rger - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="29000"] test

The regression was introduced with 4de2e8a038
As far as the commit comments and man page indicates, this was meant to affect
remote system logging only, but it also affects local logging when the -u
option is given.

This causes problems with receivers who do not expect full-blown RFC format
on the log socket, like rsyslog. In consequence, this can also affect
log analysis programs and invalidate some of their results.

The patch corrects the behaviour so that the same old-style format is used for
any type of local logging. New-style can always be selected by command line-options.
RFC5424 is still the default for remote logging, as intended in the orignal
commit.

Result with the patch:
$ ./logger --stderr test
<5>Mär  4 11:15:35 logger: test
$ ./logger -u /dev/log --stderr test
<5>Mär  4 11:15:40 logger: test
$ ./logger -u /dev/log --rfc5424 --stderr test
<5>1 2015-03-04T11:21:28.796170+0100 ubuntu1404esp rger - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="27500"] test
2015-03-05 14:04:54 +01:00
Karel Zak ec27f47048 Merge branch 'logger-fix-rfc5424-timestamp' of https://github.com/rgerhards/util-linux
* 'logger-fix-rfc5424-timestamp' of https://github.com/rgerhards/util-linux:
  logger: fix invalid timestamp in rfc5425 format
2015-03-05 11:52:00 +01:00
Sami Kerola 3f51c10b39 logger: fix -i argument parsing regression
With earlier logger it's possible to combine the option -i with other
options, such as -s.  But currently:

$:~> logger -is
logger: failed to parse id: 's'

The changed behaviour breaks existing scripts like dhcpcd-run-hooks from
dhcpcd.

Broken-since: aab5b44405
Reference: http://comments.gmane.org/gmane.linux.utilities.util-linux-ng/9683
Reported-by: Juergen Daubert <jue@jue.li>
Reviewed-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-03-05 10:31:18 +01:00
Rainer Gerhards 2f267611f0 logger: fix invalid timestamp in rfc5425 format
The timestamp is written as

2015-03-04T15:02:02.566782+0100

unfortunately, this is not an RFC3339 timestamp as demanded by rfc5424.
The colon in the time offset field is missing. The correct timestamp is

2015-03-04T15:02:02.566782+01:00

(Note "+0100" vs. "+01:00")
2015-03-04 18:54:58 +01:00
Sami Kerola 52a49e9add logger: move /dev/log to pathnames.h
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-02-24 13:18:49 +01:00
Sami Kerola d77dc29e6e logger: add --socket-errors compatibility option
Hello,

Depending viewpoint this change is either regression fix, or
re-regression in context of none-systemd init.  I ack the change is sent
very late to be part of v2.26, but then again the excess noise was found
only because of -rc1 was tested in sysvinit environment.  IMHO it would
contradict purpose of having rc's if faults will not lead to fixes.

I also want to point out the sysvinit scripts are broken, not the
logger(1), but getting them corrected is practically impossible.
Assuming sysvinit script are further developed by various teams and
distributions who maintain them they should use --socket-error=on in
future, and write scripts that pass without noise.  Meanwhile trying to
be clever when to silence errors seems like a reasonable thing to do.

--->8----
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 14 Feb 2015 19:05:55 +0000
Subject: [PATCH] logger: add --socket-errors compatibility option

Now when logger(1) has stopped using openlog() for Unix sockets, in
commit mentioned in reference, the lack of /dev/log detected will report
error accordingly.  According to Gabriele Balducci this makes sysvinit
style boot scripts to print a lot of errors.  So make the logger to
detect whether it should be in compatibility mode, and not report errors
if logging device is missing.  That imitates behavior of glibc openlog().

To allow full control to users the /dev/log error messages can be forced
to on or off.  The automatic error messaging is explained in manual page.

Reference: 1d57503378
Reported-by: Gabriele Balducci <balducci@units.it>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-02-16 11:08:24 +01:00
Karel Zak c580b554c9 Merge branch 'timex' of https://github.com/kerolasa/lelux-utiliteetit
* 'timex' of https://github.com/kerolasa/lelux-utiliteetit:
  logger: check availability of ntp_gettime()
2015-01-26 11:59:10 +01:00
Karel Zak d0e875ffd4 logger: improve usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-01-26 11:26:40 +01:00
Benno Schulenberg d45867162c textual: put option and its description into a single translatable string
Also improve the description: notime, notq, and nohost are literals,
not things to be replaced by something else -- so no angular brackets.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-26 11:22:13 +01:00
Benno Schulenberg 54fefa078e textual: fix spellos and inconsistencies in several program messages
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-26 11:17:55 +01:00
Samuel Thibault 87ee265835 logger: check availability of ntp_gettime()
Make compilation to work in systems which don't have sys/timex.h and its
ntp_gettime().

Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Samuel Thibault <sthibault@debian.org>
2015-01-23 19:57:22 +00:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Andreas Henriksson 5ec85227ad logger: gettimeofday needs <sys/time.h>
Either works on linux, but kfreebsd build fails
if we don't use the <sys/time.h> include.

According to man gettimeofday the correct include is <sys/time.h>

Signed-off-by: Andreas Henriksson <andreas@fatal.se>
2014-09-22 12:54:11 +02:00
Karel Zak 59c6ac0b92 logger: use generic --id=<id>
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-08-20 12:24:40 +02:00
Sami Kerola 46ee14df66 logger: set function arguments read-only when possible
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-08-15 21:52:12 +01:00
Sami Kerola 77c3bd5bf6 logger: optimize string initializations
Setting whole array to be completely full of nulls cannot be as quick as
making the only significant member of the array when needed.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-08-15 21:52:12 +01:00
Sami Kerola 35d3619793 logger: remove openlog(3) options
One variable less, and more importantly bit operations become unnecessary
in if statements.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-08-15 21:52:12 +01:00