Commit Graph

132 Commits

Author SHA1 Message Date
Sami Kerola f1f5f21ee6 logger: remove trailing spaces when outputing to journal
Issues:
1. Whitespace-ish \r is not stripped, while it should be.
2. In journal \r is considered unprintable.

Lennart: "it is the duty of the client side to drop the trailing whitespace,
which "logger" doesn't do".

Reported-by: Ivan Babrou <ibobrik@gmail.com>
Explained-by: Lennart Poettering <lennart@poettering.net>
Reference: https://github.com/systemd/systemd/issues/3416
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-07-19 11:02:48 +02:00
Karel Zak 1c72275988 logger: be more precise about --port description
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-06-21 13:32:14 +02:00
Sebastian Rasmussen 9e93004171 misc: Fix various typos
Fix various typos in error messages, warnings, debug strings,
comments and names of static functions.

Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
2016-05-31 23:40:21 +02:00
Benno Schulenberg 426cdc0ac0 logger: correct the grammar of an error message
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2016-03-16 10:39:37 +01:00
Sami Kerola 4e5411f623 logger: fix memory leak [ASAN and valgrind]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-03-14 13:15:09 +01:00
Karel Zak 8e2e9144ef misc: remove duplicate includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-10 14:30:32 +01:00
Ruediger Meier f5fceb401e logger: fix compiler warning, const facilitynames
This was the warning on FreeBSD:
misc-utils/logger.c:221:24: warning: passing 'const CODE [25]' to parameter of type 'CODE *' (aka 'struct _code *') discards qualifiers
      [-Wincompatible-pointer-types-discards-qualifiers]
                facility = decode(s, facilitynames);
                                     ^~~~~~~~~~~~~
misc-utils/logger.c:187:43: note: passing argument to parameter 'codetab' here
static int decode(const char *name, CODE *codetab)

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-08 14:55:57 +01:00
Ruediger Meier eaac9f88f9 misc: fix some includes
features.h:  any glibc header includes this already
libgen.h:    was unused there
sys/uio.h:   for writev(3p)
sys/queue.h  seems like it was never used

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-29 13:04:56 +01:00
Ruediger Meier 4199e9891e logger: use SCM_CREDENTIALS on LINUX only
This is a build fix of FreeBSD and OSX. Basically we revert
the following commit for non-linux systems:
27a9eb53 "use --id as local socket credentials"

Note I could also compile it like this:

  #ifdef HAVE_SYS_UCRED_H
  # define _WANT_UCRED
  # include <sys/param.h>
  # include <sys/ucred.h>
  # define SCM_CREDENTIALS SCM_CREDS
  #endif

  [...]
  #ifdef _linux_
  cred->pid = ctl->pid;
  #endif
  [...]

... but don't know how to test whether it does what it
should.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-26 03:29:41 +01:00
Karel Zak b64dc52a53 Merge branch 'ldadd-cflags-warnings' of https://github.com/rudimeier/util-linux
* 'ldadd-cflags-warnings' of https://github.com/rudimeier/util-linux:
  build-sys: disable unused parameter warnings for some test progs
  misc: fix compiler warnungs (unsigned/signed)
  misc: fix warnings "unused parameter" [-Wunused-parameter]
  libfdisk: fix warnings, "redundant redeclaration" [-Wredundant-decls]
  tests: fix compiler warnings [-Wmissing-prototypes]
  libfdisk: fix compiler warnings [-Wmissing-prototypes]
  libfdisk: fix missing symbol
  libblkid: fix compiler warnings [-Wmissing-prototypes]
  libmount: add mnt_fs_set_priority()
  build-sys: always add AM_CFLAGS
  build-sys: always use global LDADD
2016-02-24 13:51:10 +01:00
Karel Zak 7db029e54a logger: always update header when writing stdin line
Addresses: http://bugs.debian.org/798239
Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-02-24 10:40:08 +01:00
Ruediger Meier b9710f1f08 misc: fix compiler warnungs (unsigned/signed)
These ones should be fixed:
libblkid/src/probe.c:393:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/probe.c:907:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/probe.c:1221:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:540:47: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1043:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1056:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1057:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1061:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1199:27: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/partitions/partitions.c:1410:26: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/partitions/partitions.c:1431:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/superblocks/linux_raid.c:151:8: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
libblkid/src/superblocks/linux_raid.c:155:2: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
libblkid/src/superblocks/superblocks.c:375:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/superblocks/xfs.c:141:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libsmartcols/src/table.c:333:24: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libsmartcols/src/table.c:344:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libsmartcols/src/table_print.c:753:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/ask.c:364:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/utils.c:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/context.c:435:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/context.c:730:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/script.c:557:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/dos.c:1791:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/gpt.c:813:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:140:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:551:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:640:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-23 03:18:27 +01:00
Sami Kerola 2ec100a180 logger: shadow declaration
misc-utils/logger.c:448:17: warning: declaration of 'msg' shadows a
				parameter [-Wshadow]
misc-utils/logger.c:429:74: note: shadowed declaration is here

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-11-06 13:19:39 +01:00
Karel Zak 9b28da2d1f logger: remove unnecessary comment
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-29 11:32:50 +01:00
Karel Zak 27a9eb5359 logger: use --id as local socket credentials
If you have really paranoid syslog (or systemd who listens on /dev/log)
then it replaces in the message PID with a real PID from socket header
credentials:

 # echo $PPID
 1550

 # logger -p info --stderr --id=$PPID "This is message baby!"
 <14>Oct 29 11:22:13 kzak[1550]: This is message baby!

 # journald -n 1
 Oct 29 11:22:13 ws kzak[22100]: This is message baby!
                         ^^^^^

This patch forces kernel to accept another *valid* PID if logger(1)
executed with root permissions; improved version:

 # logger -p info --stderr --id=$PPID "This is message baby!"
 <14>Oct 29 11:26:00 kzak[1550]: This is message baby!

 # journald -n 1
 Oct 29 11:26:00 ws kzak[1550]: This is message baby!

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-29 11:18:21 +01:00
Karel Zak 17c8aa1dc0 logger: use iovec for all message
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-27 18:44:00 +01:00
Karel Zak 94a28496aa logger: use iovec and sendmsg() to send message
The iovec based solutions allow to send multiple strings by one
syscall (for example additional \n messages separator). We can also
use it to send additional socket header metadata (e.g.
SCM_CREDENTIALS) later.

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-27 13:19:16 +01:00
Karel Zak fc20393cdb logger: fix messages separation on UNIX socket
The function write_output() add additional \n after each message on
TYPE_TPC. This is required by syslog daemons, otherwise you will see
multiple log messages merged together in your log file, for example:

  Oct  6 09:01:40 ws kzak: AAA<14>Oct  6 09:01:40 kzak: BBB

for

  printf "AAA\nBBB\n" | logger -p info -u <any-socket>

Unfortunately, the connection initialization functions keep the
default ALL_TYPES as connection type and nowhere in the control struct
is info about the final real connection type. The problem is invisible
when you specify --tpc or --udp on logger command line.

Addresses: https://github.com/karelzak/util-linux/issues/225
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-06 09:16:33 +02:00
Karel Zak 4299ed1ce2 logger: add --sd-id and -sd-param
This patch add support for RFC 5424 structured data elements. For
example:

     logger --rfc5424 --sd-id zoo@123                \
                      --sd-param tiger=\"hungry\"    \
                      --sd-param zebra=\"running\"   \
                      --sd-id manager@123            \
                      --sd-param onMeeting=\"yes\"   \
                      "this is message"

produces:

     <13>1 2015-10-01T14:07:59.168662+02:00 ws kzak - - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="218616"][zoo@123 tiger="hungry" zebra="running"][manager@123 onMeeting="yes"] this is message

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-01 14:48:15 +02:00
Karel Zak 28bad822c4 logger: improve readability [smatch scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-08-05 13:23:34 +02:00
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