Commit Graph

213 Commits

Author SHA1 Message Date
Karel Zak a3d73c246f login: add callback for close_range()
Let's make it more robust for old kernels where is no close_range() or
in cases when it's unsuccessful.

Reported-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:59:53 +02:00
Karel Zak 937fe5d9c5 login: fix close_range() use
This new syscall comes with three arguments (see kernel commit
278a5fbaed89dacd04e9d052f4594ffd0e0585de). Not sure why util-linux
assume only two.

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1981729
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:59:53 +02:00
Karel Zak 3aef5dcad5 login: remove obsolete and confusing comment
Reported-by: Jan Pazdziora <jpazdziora@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:16:06 +02:00
Karel Zak 726ffb4a26 login: move generic setting to ttyutils.h
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-02-23 11:46:10 +01:00
Karel Zak 9c7a613c82 login: use full tty path for PAM_TTY
pam_set_item() man page:
 PAM_TTY
   The terminal name: prefixed by /dev/ if it is a device file;
   for graphical, X-based, applications the value for this item
   should be the $DISPLAY variable.

It seems for example pam_timestamp module is not robust enough to
differentiate between /dev/ and pty/0 and it assumes that '/' in the
path always means '/dev/' prefix ...

Fixes: https://github.com/karelzak/util-linux/issues/1242
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-02-03 14:49:09 +01:00
Karel Zak 867df2617c lib: use ul_prefix for close_all_fds() and mkdir_p()
The names are too generic and it makes problems in static libraries.

Addresses: https://gitlab.com/cryptsetup/cryptsetup/-/issues/623
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-01-28 10:27:21 +01:00
Karel Zak 96c8282584 login: fix compiler warning [-Werror=strict-prototypes]
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-12-07 13:26:33 +01:00
Sami Kerola a75700d86e
lib/fileutils: make close_all_fds() to be similar with close_range()
The util-linux close_all_fds() serves the same purpose as close_range()
that will over time obsolete local implementation completely.  For
upcoming few years it is best to have a fallback that uses same input
arguments as the new system call.  That allows surrounding code and
variables not to be affected by version of mass file descriptor closing
function.

Proposed-by: Karel Zak <kzak@redhat.com>
Reference: https://github.com/karelzak/util-linux/pull/1205#discussion_r534080128
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:09:14 +00:00
Sami Kerola f0649c0d83
login: use close_range() system call when possible
This system call was added while back, but does not have glibc support yet.
Lets try to use the new facility early on.  The promise in reference link is
that the new call is faster, and works when /proc is not mounted.

Reference: https://lwn.net/Articles/789000/
Reference: https://lwn.net/Articles/789023/
Reference: https://kernelnewbies.org/LinuxChanges#Linux_5.9.New_close_range.28.29_system_call_for_easier_closing_of_file_descriptors
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:09:14 +00:00
Sami Kerola 931e60982e
login: move message printing out from main()
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:09:14 +00:00
Sami Kerola b04f183578
login: use xalloc memory allocation helpers everywhere
Apart two function calls login(1) already used xalloc functions.  If there
was a time when login tried to gracefully handle allocation errors that has
not been true for long time.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:30 +00:00
Sami Kerola 7679a2c5ca
login: fix coding style issues
Couple function had spaces instead of tabs in indent.  This change uses tabs
everywhere, and does handful of other spacing additions and removals.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:30 +00:00
Sami Kerola d515381901
login: use system definitions to determine maxium login name length
Allocating stack for about 10x more than longest possible user name is
wasteful, even if it happens for reletively short period of time.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:29 +00:00
Sami Kerola 5941a0db06
login: use calloc() when memory needs to be cleared
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:29 +00:00
Sami Kerola 0da0a5ed3b
login: use explicit_bzero() to get rid of confidental memory
Earlier code was most probably correct, but it is best to be safe than sorry
when dealing with confidental data removals.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:29 +00:00
Sami Kerola 5dd0896aa8
login: add initialize() function to have less stack allocated in main()
And move couple variables to the narrow scope they are used.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:07:28 +00:00
Sami Kerola 79a8afeb8f
login: replace function like definitions with inline functions
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:06:51 +00:00
Sami Kerola fb038d27a1
login: move getlogindefs_num() after localization init
There are translations in getlogindefs_num() and they will not take effect
unless the function call is after the setlocale(), bindtextdomain(), and
textdomain().

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:06:51 +00:00
Sami Kerola 0b4d75fae5
login: move timeout from global to local scope
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 20:06:47 +00:00
Sami Kerola bfcba3f586
login: stop keeping timeout message in memory forever
When the timeout is cancelled the message can be removed from memory.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 19:54:29 +00:00
Sami Kerola 42ccc9cfb4
login: use sig_atomic_t type for variable accessed from signal handler
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03 19:54:29 +00:00
Egor Chelak 8fcdbefb7b login: use ul_copy_file
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:15 +02:00
Sami Kerola c60c65c37e login: close() only a file descriptor that is open [coverity scan]
CID: 360819
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-10-16 11:59:16 +02:00
Sami Kerola d2ab69ff61 login: ensure getutxid() does not use uninitialized variable [coverity scan]
Field ut.ut_pid is uninitialized when calling getutxline().  The safest
option is to ensure all struct data is initialized in the function.

CID: 360793
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-10-16 11:30:45 +02:00
Karel Zak b21d741c21 login: move proctitle code to login.c
The functions are used only by login(1), let's keep the code in
login.c only.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-10-06 15:15:15 +02:00
Karel Zak b648917e0b login: use mem2strcpy() rather than rely on printf()
The strings from utmp does not have to be terminated. It's seems
better to explicitly terminate it than rely on "%.*s" printf()
functionality -- printf() man page assumes that "If a precision is
given, no null byte need be present", but static analyzers are pretty
unhappy with it.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-10-01 14:04:21 +02:00
Thayne McCombs df09e21c24 login: add option to not reset username on each attempt
[kzak@redhat.com: - use different message on failed password]

Addresses: https://github.com/karelzak/util-linux/pull/1138
Addresses: https://github.com/karelzak/util-linux/issues/6
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-11 12:20:37 +02:00
leeceeksdee c9c7de7932
her -> their 2020-08-28 19:18:33 +02:00
Karel Zak 29e204d117 docs: reword others "allow to"
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 14:31:29 +02:00
Karel Zak 111b395aed login: cleanup get_hushlogin_status() use
* use 1 bit for context->quiet
* get_hushlogin_status() return -1 on error, make sure we do not
  interpret it as "hush mode enabled"

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-16 14:00:58 +02:00
Karel Zak 31d79197c0 login: use PAM_SILENT to propagate hushlogin to PAM
login(1) follows /etc/hushlogin and ~/.hushlogin to enable silent
mode, but it's not propagated to PAM now. Note that login(1) from
shadow-utils uses PAM_SILENT too.

Addresses: https://github.com/linux-pam/linux-pam/issues/233
Addresses: https://github.com/karelzak/util-linux/issues/1059
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-16 13:54:14 +02:00
Karel Zak 330b09976f build-sys: remove redundard includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-09 10:53:14 +02:00
Karel Zak a437692943 login: cleanup -f in usage() and comments
Let's remove unnecessary comment and "second authentication" from
usage().

Addresses: https://github.com/karelzak/util-linux/issues/1053
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-01 10:33:29 +02:00
Karel Zak 9789d21a96 login: add MOTD_FIRSTONLY=
This login.defs option allow to configure login to be more compatible
with pam_motd.

Addresses: https://github.com/karelzak/util-linux/issues/1034
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-12 15:21:21 +02:00
Karel Zak 5a528e2c6f login: add support for directories in MOTD_FILE=
The current standard is to use directories to make it easy for
distributions to share resources. This patch also add /etc/motd.d
and /run/motd.d to the default MOTD_FILE=.

Addresses: https://github.com/karelzak/util-linux/issues/10341
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-12 14:20:54 +02:00
Rosen Penev ad296391f9
[clang-tidy] fix wrong *cmp usage
Found with bugprone-suspicious-string-compare

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:21:00 -07:00
Sami Kerola 370734a7eb
login: avoid lseek() with pread() and pwrite()
This makes code a little less complicated by avoiding couple system calls.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-03-29 20:19:12 +01:00
Sami Kerola 3160589d86
various: use threadsafe versions of time functions [lgtm scan]
Deprecating calls to not-thread safe asctime(), ctime(), and localtime()
calls is pretty close to pointless change.  Lets do it to reduce lgtm scan
warnings with justification it's nicer to use static analysis tools when
they have very few positives.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-15 15:23:44 +00:00
Karel Zak 70ff760d41 login: reduce file-descriptors cleanup overhead
Addresses: https://github.com/karelzak/util-linux/issues/883
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-17 10:45:16 +02:00
Sami Kerola 218b1dd6f9
misc: fix typos [codespell]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-09-12 19:41:46 +01:00
Sami Kerola 1aaee548e3 login: simplify string handling
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-07-24 11:11:29 +02:00
Karel Zak 9c6167c394 login: retire use of __FUNCTION__ macro
This fixes following warning about deprecatd __FUNCTION__ that is another
name for __func__ since GCC (pre 2.95, released July 1999).

    login-utils/login.c:860:10: warning: ISO C does not support
    ‘__FUNCTION__’ predefined identifier [-Wpedantic]

Co-Author: Sami Kerola <kerolasa@iki.fi>
Reference: https://c0x.coding-guidelines.com/6.4.2.2.html
Reference: https://github.com/karelzak/util-linux/pull/802
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-27 11:55:19 +02:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Karel Zak 1a83c00d88 login: add support for login.defs(5) LASTLOG_UID_MAX
This new variable allows to keep lastlog file small and filter out
things like huge nfsnobody UIDs.

The variable is also supported by shadow-utils (adduser, etc.).

Addresses: https://github.com/shadow-maint/shadow/pull/142/
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-11 12:16:00 +01:00
Karel Zak ac5c12fdc2 login: cleanup code to copy to log strings
man utmp:
  String fields are terminated by a null byte ('\0') if they are shorter
  than the size of the field.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-10-03 17:08:11 +02:00
Karel Zak 088d487612 login: use const qualifier for username from PAM or struct passwd [-Wcast-qual]
It seems more robust to use 'const' qualifier for username if this
variable points to external resources like PAM or struct passwd. The
patch introduces new variable cmd_username for username specified on
login(1) command line.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-23 12:21:33 +02:00
Karel Zak e6b32e7d1a login: add LOGIN_PLAIN_PROMPT to login.defs
We have command line option -H to disable hostname in login prompt.
Unfortunately, in same cases (e.g. telnetd) it's impossible to specify
login(1) command line options due to hardcoded execl()...

This patch introduces LOGIN_PLAIN_PROMPT boolean for /etc/login.defs
to suppress hostname in the prompt.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-04 12:31:29 +01:00
Karel Zak f4b03edb73 login-utils: use free_getlogindefs_data()
It seems better to deallocate logindefs.conf stuff in long time
running (=waiting) processes like login(1) and su(1).

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-11-28 14:49:05 +01:00
Tobias Stoeckmann f17bda66bd login: fix signal race
The functions warnx(3) and gettext(3) are not safe to use within signal
handlers and should be avoided. Preparing the message beforehand and
calling write(2) as well as calling _exit(2) solves the problem.

[kzak@redhat.com: - use program_invocation_short_name rather than argv[0],
                  - use ignore_result() to keep compiler happy]

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-26 12:03:24 +02:00
Karel Zak 4f5f35fc83 login: add xgetpwnam()
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-18 11:48:56 +02:00