Commit Graph

170 Commits

Author SHA1 Message Date
Eugene Grosbein
01cf0a1724 newsyslog(8): make configuration parser more robust.
Currently newsyslog supports <include> directive that is used
in our default /etc/newsyslog.conf in the following form:

<include> /usr/local/etc/newsyslog.conf.d/*

While this is suitable for ports installing their own rules
for logs rotation, this also makes newsyslog break entire
processing of all files if it encounters single line it cannot parse.
This includes lines referring to nonexistent username/group for log
ownership, so newsyslog stops calling errx() function in the parser.

With this fix, newsyslog uses warnx() instead of errx() in such cases
to print a warning, recover gracefully and continue with execution.

Among other cases, this unbreaks initial creation of log files
having flag "C" at boot time (newsyslog -CN). This is most important
for systems having RAM-based /var file system like nanobsd(8)-based
that rely on newsyslog to bring system log files into existence.

MFC after:	1 month
2020-06-16 17:45:23 +00:00
Mark Johnston
faff7ddb00 newsyslog: Fix stack corruption when initializing a zipwork structure.
This happens when compressing a previously uncompressed already-rotated
file, as happens when handling the 'p' flag in newsyslog.conf.  The file
name is stored in a flexible array member, so these structures cannot be
stack allocated.

Also make sure that we call change_attrs() and do_zipwork() in dry-run
mode; they handle this properly, contrary to the commit log message for
r327451.

CID:		1008168
Github PR:	https://github.com/freebsd/freebsd/pull/427
MFC after:	2 weeks
Submitted by:	Radek Brich (original version)
2020-03-24 18:16:36 +00:00
David Bright
1f641a35fd Fix several Coverity-detected issues in newsyslog.
- CID 1394815, CID 1305673: Dereference before null check - memory was
  allocated and the allocation checked for NULL with a call to errx()
  if it failed. Code below that was guaranteed that the pointer was
  non-NULL, but there was another check for NULL at the exit of the
  function (after the memory had already been referenced). Eliminate
  the useless NULL check.

- CID 1007452: Resource leak - Storage intended to be allocated and
  returned to the caller was never freed. This was the result of a
  regression in the function signature introduced in r208648 (2010)
  (thanks for that find, @cem!). Fixed by altering the function
  signature and passing the allocated memory to the caller as
  intended. This also fixes PR158794.

- CID 1008620: Logically dead code in newsyslog.c - This was a direct
  result of CID 1007452. Since the memory allocated as described there
  was not returned to the caller, a subsequent check for the memory
  having been allocated was dead code. Returning the memory
  re-animates the code that is the subject of this CID.

- CID 1006131: Unused value - in parsing a configuration file, a
  pointer to the end of the last field was saved, but not used after
  that. Rewrite to use the pointer value. This could have been fixed
  by avoiding the assignment altogether, but this solutions more
  closely follows the pattern used in the preceding code.

PR:		158794
Reported by:	Coverity, Ken-ichi EZURA <k.ezura@gmail.com> (PR158794)
Reviewed by:	cem, markj
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D19105
2019-02-22 15:31:50 +00:00
David Bright
c7189f3694 Revert r344389 & r343906 - Fix Coverity errors in newsyslog
It was pointed out that a couple of the "memory leak" CIDs that I
fixed were arguably Coverity errors rather than errors in the
newsyslog code and the cure was worse than the disease. Revert both
changes. The first change, which included fixes for other Coverity
errors, will be re-worked to omit the troublesome changes and then
re-committed with the remaining fixes.

Reported by:	bde
Sponsored by:	Dell EMC Isilon
2019-02-22 14:59:40 +00:00
David Bright
2c3e626a0d Complete fix for CID 1007454, CID 1007453: Resource leak in newsyslog
The result of a strdup() was stored in a global variable and not freed
before program exit. This is a follow-up to r343906. That change
attempted to plug these resource leaks but managed to miss a code path
on which the leak still occurs. Plug the leak on that path, too.

MFC after:	3 days
Sponsored by:	Dell EMC Isilon
2019-02-20 22:05:44 +00:00
David Bright
00c4df53b6 Fix several Coverity-detected issues in newsyslog.
- CID 1394815, CID 1305673: Dereference before null check - memory was
  allocated and the allocation checked for NULL with a call to errx()
  if it failed. Code below that was guaranteed that the pointer was
  non-NULL, but there was another check for NULL at the exit of the
  function (after the memory had already been referenced). Eliminate
  the useless NULL check.

- CID 1007454, CID 1007453: Resource leak - The result of a strdup()
  was stored in a global variable and not freed before program exit.

- CID 1007452: Resource leak - Storage intended to be allocated and
  returned to the caller was never freed. This was the result of a
  regression in the function signature introduced in r208648 (2010)
  (thanks for that find, @cem!). Fixed by altering the function
  signature and passing the allocated memory to the caller as
  intended. This also fixes PR158794.

- CID 1008620: Logically dead code in newsyslog.c - This was a direct
  result of CID 1007452. Since the memory allocated as described there
  was not returned to the caller, a subsequent check for the memory
  having been allocated was dead code. Returning the memory
  re-animates the code that is the subject of this CID.

- CID 1006131: Unused value - in parsing a configuration file, a
  pointer to the end of the last field was saved, but not used after
  that. Rewrite to use the pointer value. This could have been fixed
  by avoiding the assignment altogether, but this solutions more
  closely follows the pattern used in the preceding code.

PR:		158794
Reported by:	Coverity, Ken-ichi EZURA <k.ezura@gmail.com> (PR158794)
Reviewed by:	cem, markj
MFC after:	1 week
Sponsored by:	Dell EMC Isilon
2019-02-08 13:54:16 +00:00
Mark Johnston
c2c3992b39 Fix age_old_log() after r337468.
We can no longer use sizeof() to get the path buffer's size.  Apply
a straightforward fix for now with the aim of MFCing soon.

PR:		233633
Submitted by:	Katsuyuki Miyoshi <katsu@miyoshi.matsuyama.ehime.jp>
MFC after:	3 days
2018-11-29 16:49:56 +00:00
Conrad Meyer
c657f9385b newsyslog(8): Reject configurations that specify setuid or executable logs
Prevent some classes of foot-shooting that may result in permissions
problems.

Reviewed by:	dab, delphij, vangyzen (earlier version)
Relnotes:	yes (behavior change)
Sponsored by:	Dell EMC Isilon
Differential Revision:	D16831
2018-08-21 23:12:46 +00:00
Mark Johnston
28984f2552 Simplify compression code.
- Remove the compression suffix macros and move them directly into the
  compress_type array.
- Remove the hardcoded sizes on the suffix and compression args arrays.
- Simplify the compression args arrays at the expense of a __DECONST
  when calling execv().
- Rewrite do_zipwork.  The COMPRESS_* macros can directly index the
  compress_types array, so the outer loop is not needed. Convert
  fixed-length strings into asprintf or sbuf calls.

Submitted by:	Dan Nelson <dnelson_1901@yahoo.com>
Reviewed by:	gad
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D16518
2018-08-08 17:26:51 +00:00
Mark Johnston
5c1b641b09 Fix a flag collision introduced in r327451.
PR:		230350
MFC after:	3 days
2018-08-04 15:30:56 +00:00
Mark Johnston
c4c9cd8d68 Remove ARGS_NUM, accidentally left over after r337050. 2018-08-01 18:49:40 +00:00
Mark Johnston
322dacf057 Don't hard-code the number of compression utility arguments.
The zstd invocation constructed by newsyslog contains one more parameter
than invocations for the other supported compression utilities.  However,
the maximum number of arguments was hard-coded, leading to an
out-of-bounds array access when using zstd compression.
2018-08-01 18:45:33 +00:00
Baptiste Daroussin
19fe43f796 newsyslog: fix compression arglist construction
Reuse of the index variable in two nested loops resulted in only the first
argument in the list being used (fine for gzip, not fine for zstd). Also
add tests for xz and zstd, and fix the COMPRESS_SUFFIX_MAXLEN macro.

Submitted by:	dnelson_1901_yahoo.com
Differential Revision:	https://reviews.freebsd.org/D16509
2018-07-30 15:46:24 +00:00
Ed Schouten
30dd2da2a3 Use the FQDN in the newsyslog log message when RFC 5424 is enabled.
The RFC 5424 spec mentions that logging FQDNs over short hostnames is
preferred. Alter this code, so that the hostname doesn't get truncated
on startup. Keep track of the length of the short hostname, so that
fprintf() can do the truncation where necessary.

MFC after:	1 month
2018-07-08 10:08:24 +00:00
Eitan Adler
df76ac9951 newsyslog: implement 'p' flag
Implement the 'p' flag for newsyslog from NetBSD. This flag results in
the first log file for a given file to not be compressed.

While here, don't change file attributes during a no-op run

PR:		162798
Submitted by:	heas@shrubbery.net
MFC After:	1 month
2017-12-31 22:01:36 +00:00
Baptiste Daroussin
64874a795e newsyslog: Fix issues after r326616
When building the command to execute for compression, newsyslog was modifying
the generic arguments array instead of its own copy.
Meaning on the second file to compress with the same arguments, the command line
was not the one expected.
Fix it by creating one copy of the arguments per execution and modifying that
copy.

While here, print the command line executed in verbose mode.

Reported by:	many
2017-12-18 09:35:04 +00:00
Baptiste Daroussin
6f80848e09 Really fix typo and improve wording of the comment 2017-12-06 10:47:50 +00:00
Baptiste Daroussin
69f0fc7877 Fix typo
Reported by:	danfe
2017-12-06 10:20:01 +00:00
Baptiste Daroussin
307845dff2 Allow newsyslog to execute compression commands which
have a semantic different than the traditional gzip(1)

This is done to allow to use zstd(1) as a compression tool without
having to patch it to change its default behavior.
2017-12-06 09:44:35 +00:00
David Bright
b326fec486 Add newsyslog capability to write RFC5424 compliant rotation message.
This modification adds the capability to newsyslog to write the
rotation message in a format that is compliant with RFC5424. This
capability is enabled on a per-log file basis through a new value
("T") in the flags field in newsyslog.conf. This is useful on systems
that use the RFC5424 format for log files so that the rotation message
format matches that of the other log messages. There has been recent
mention of adding an RFC5424 compliant mode to syslogd and at least
one alternative system log daemon (rsyslogd) that already has the
capability to use that format.

Reviewed by:	vangyzen, ngie
Approved by:	vangyzen (mentor)
MFC after:	2 months
Relnotes:	yes
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10253
2017-05-26 16:36:30 +00:00
Baptiste Daroussin
1af6dc18b9 Add a new Y flag to newsyslog.conf
This makes newsyslog use zstandard to compress log files.

Given Z is already taken for gzip and zstandard compression level stands in
between gzip and xz (which has the X flag) chosing Y sounds ok :)
2017-04-15 20:37:34 +00:00
Bryan Drewery
28323add09 Fix improper use of "its".
Sponsored by:	Dell EMC Isilon
2016-11-08 23:59:41 +00:00
Ed Schouten
23e591f485 Properly patch up dirname()/basename() calls to not clobber ent->log.
It turns out that we had a couple of more calls to dirname()/basename()
in newsyslog(8) that assume the input isn't clobbered. This is bad,
because it apparently breaks log rotation now that the new dirname()
implementation has been merged.

Fix this by first copying the input and then calling
dirname()/basename(). While there, improve the naming of variables in
this function a bit.

Reported by:	Ryan Steinmetz, gjb
Reviewed by:	bdrewery, allanjude
Differential Revision:	https://reviews.freebsd.org/D7838
2016-09-09 07:10:50 +00:00
Ed Schouten
f0d2919243 Clean up use of basename() and dirname().
Pull copies of the input pathname string before calling basename() and
dirname() to make this comply to POSIX. Free these copies at the end of
this function. While there, remove the duplication of the 's' ->
'logfname' string. There is no need for this.
2016-07-28 16:02:30 +00:00
Eric van Gyzen
c152ba971f newsyslog: Eliminate unnecessary sleep(10) when -R and -s are specified
After going through the signal work list, during which do_sigwork()
is called and essentially does nothing because -s and -R were
specified on the command line, newsyslog will sleep for 10 seconds
as the (verbose) code says: "Pause 10 seconds to allow daemon(s)
to close log file(s)".

However, the man page verbiage for -R (and -s) seems quite clear
that this sleep() is unnecessary because the daemon was expected
to have already closed the log file before calling newsyslog.

PR:		210020
Submitted by:	David A. Bright <david_a_bright@dell.com>
MFC after:	1 week
Sponsored by:	Dell Inc.
Differential Revision:	https://reviews.freebsd.org/D6727
2016-06-06 22:54:08 +00:00
Baptiste Daroussin
caeff1fd44 newsyslog: treat 'c' flag in the config as 'C'
When -C was introduced in r114137 the plan was to have -C and -c being used for
"create" due to a typo in FreeBSD <= 4.8 a temporary compatibility hack has been
added to make -c being like -G aka GLOB and a warning was issued for the user to
be aware of the futur change for -c.

12 years later it is more than time to remove that hack and finish the what was
intent in r114137

Submitted by:	Alexandre Perrin <alex@kaworu.ch>
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D4000
2015-10-31 09:45:11 +00:00
Baptiste Daroussin
c91d99bf1c newsyslog.conf: allow to configure the signal using the signal name.
Submitted by:	Alexandre Perrin <alex@kaworu.ch>
MFC after:	1 week
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D3961
2015-10-24 13:55:12 +00:00
Eitan Adler
463a577b27 Fix a ton of speelling errors
arc lint is helpful

Reviewed By: allanjude, wblock, #manpages, chris@bsdjunk.com
Differential Revision: https://reviews.freebsd.org/D3337
2015-10-21 05:37:09 +00:00
Mark Johnston
9f08174d47 If we fail to send a signal after rotation, print the pidfile from which
the corresponding PID was obtained.

PR:		194143
Submitted by:	Lyndon Nerenberg <lyndon@orthanc.ca>
MFC after:	1 week
2014-10-08 17:44:30 +00:00
Bryan Drewery
e3b7979645 Fix newsyslog(8) to use the size of the file instead of the blocks it
takes on disk, as advertised in newsyslog.conf(5).

This fixes newsyslog(8) on ZFS with compression enabled to not have
large files compared to the expected rotation size.

  # grep remotes/messages /etc/newsyslog.conf
  /var/log/remote/messages       root:info       640  5     500  *     JC
  # ls -alh /var/log/remote/messages
  -rw-r-----  1 root  info    3.2M Jan 31 20:02 /var/log/remote/messages
  # newsyslog -vN|grep remote/messages
  /var/log/remote/messages <5J>: size (Kb): 464 [500] --> skipping
  # stat -f "st_size: %z st_blocks: %b" /var/log/remote/messages
  st_size: 3372627 st_blocks: 928
  # zfs get -H compressratio zroot/syslogs
  zroot/syslogs    compressratio   3.77x   -

With fix:

  # newsyslog -v | grep remote/messages
  /var/log/remote/messages <5J>: size (Kb): 3338 [500] --> trimming log....

Approved by:	bapt (mentor)
PR:		docs/150877
Reported by:	Joshua Isom <jrisom@gmail.com>
MFC after:	2 weeks
2014-02-02 14:11:34 +00:00
Mark Johnston
de6d612a0e Initialize the struct tm before handing it to strptime(3).
PR:		183550
MFC after:	2 weeks
2013-11-04 02:50:43 +00:00
Tim Kientzle
8f95d0d822 Fix -Wunsequenced
Submitted by:	dt71@gmx.com
2013-06-29 15:58:03 +00:00
Mark Johnston
950ff8e4dd We want to stat the archived log file rather than the logfile itself.
PR:		bin/179122
Submitted by:	Oliver Fromme <olli@secnetix.de>
MFC after:	3 days
2013-06-02 01:10:49 +00:00
Mark Johnston
3cc26586e6 Some filesystems (NFS in particular) do not fill out the d_type field when
returning directory entries through readdir(3). In this case we need to
obtain the file type ourselves; otherwise newsyslog -t will not be able to
find archived log files and will fail to both delete old log files and to
do interval-based rotations properly.

Reported by:	jilles
Reviewed by:	jilles
MFC after:	2 weeks
2013-05-12 01:58:04 +00:00
Mark Johnston
91ba008fc5 Fix interval-based rotations when the -t flag is used. In this case, find
the most-recently archived logfile and use its mtime to determine whether
or not to rotate, as in the non-timestamped case.

Previously we would just try to use the mtime of <logfile>.0, which always
results in a rotation since it generally doesn't exist in the -t case.

PR:		bin/166448
Approved by:	emaste (co-mentor)
Tested by:	Marco Steinbach <coco executive-computing.de>
MFC after:	2 weeks
2013-03-26 23:11:30 +00:00
Mark Johnston
6a82e4291b Rename the run_cmd field to sw_runcmd to make it consistent with the
other fields in struct sigwork_entry.

Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-27 06:03:57 +00:00
Mark Johnston
6df0ff43f0 Ensure that newsyslog -n prints the correct message for a rotation rule
that uses the 'R' flag.

Reviewed by:	gad
Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-27 06:02:38 +00:00
Mark Johnston
3b58eef59e When the 'R' flag is used with a newsyslog.conf entry, some fields of
the corresponding struct sigwork_entry were left uninitialized,
potentially causing an early return from do_sigwork(). Ensure that these
fields are initialized, and handle the 'R' flag properly in
do_sigwork().

PR:		bin/175330
Reviewed by:	gad
Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-27 06:01:35 +00:00
Mark Johnston
9df22a6e88 Make sure to update the mtime of a logfile after archiving it. This
ensures that the next rotation happens at the correct time when using
interval-based rotations.

PR:		bin/174438
Reviewed by:	gad
Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-03 16:14:51 +00:00
Mark Johnston
468d206524 Have -n imply -r, since dry-run mode obviously doesn't require root
privileges.

Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-03 16:12:48 +00:00
Mark Johnston
220936938f Fix a typo in an error message.
Approved by:	rstone (co-mentor)
MFC after:	1 week
2013-01-03 16:11:24 +00:00
Ed Schouten
eccad22277 More -Wmissing-variable-declarations fixes.
In addition to adding missing `static' keywords:
- bin/dd: Pull in `extern.h' to guarantee consistency with source file.
- libexec/rpc.rusersd: Move shared globals into an extern.h.
- libexec/talkd: Move `debug' and `hostname' into extern.h.
- usr.bin/cksum: Put counters in extern.h, as they are used by ckdist/mtree.
- usr.bin/m4: Move `end_result' into extern.h.
- usr.sbin/services_mkdb: Move shared globals into an extern.h.
2012-10-20 10:33:15 +00:00
Andrey V. Elsukov
131f6ef034 We don't need to check the result of sending signal when -R option is
specified.

Submitted by:	Ilya A. Arkhipov
MFC after:	1 week
2012-07-09 07:37:10 +00:00
Gleb Kurtsou
0bb2aabf26 Hide DIR definition by making it an opaque struct typedef.
Introduce dirfd() libc exported symbol replacing macro with same name,
preserve _dirfd() macro for internal use.

Replace dirp->dd_fd with dirfd() call. Avoid using dirfd as variable
name to prevent shadowing global symbol.

Sponsored by:	Google Summer Of Code 2011
2012-05-19 12:44:27 +00:00
Gleb Smirnoff
7be124b0a8 Don't run through time checks when entry is definitely oversized. This
leads to newsyslog rotating on (size OR time) if both are specified.

PR:		100018, 160432
2012-03-21 06:51:45 +00:00
Ulrich Spörlein
f7bcc1c411 Further fix a typo and spelling classic correctly in function names,
too.

Submitted by:	Ben Kaduk <minimarmot@gmail.com>
2012-01-05 21:36:36 +00:00
Ed Schouten
b3608ae18f Replace index() and rindex() calls with strchr() and strrchr().
The index() and rindex() functions were marked LEGACY in the 2001
revision of POSIX and were subsequently removed from the 2008 revision.
The strchr() and strrchr() functions are part of the C standard.

This makes the source code a lot more consistent, as most of these C
files also call into other str*() routines. In fact, about a dozen
already perform strchr() calls.
2012-01-03 18:51:58 +00:00
Ulrich Spörlein
3df5ecac8c Spelling fixes for usr.sbin/ 2011-12-30 10:58:14 +00:00
Eitan Adler
520374b5aa - Remove extraneous null ptr deref checks
- Fix memory leak

Submitted by:	Slono Slono <slonoman2011@yandex.ru>
Approved by:	jhb
MFC after:	1 week
2011-12-21 20:27:41 +00:00
Maxim Sobolev
ea640d778b Add new modifier - "R", when it is specified the path to pid file
will be considered as a path to a binary or a shell script to be executed
after rotation has been completed instead of sending signal to the process
id in that file.

Sponsored by:	Sippy Software, Inc.
From the:	FreeBSD hacking lounge at BSDCan
2011-05-14 03:00:55 +00:00