dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0006-lslogins-return-1-on-lslogins-nonexisting.patch

069435
From 0aa9097f9ecee3688b8659d7f7414f5fb26e147a Mon Sep 17 00:00:00 2001
069435
From: Karel Zak <kzak@redhat.com>
069435
Date: Mon, 13 Aug 2018 14:16:28 +0200
069435
Subject: [PATCH 6/6] lslogins: return 1 on "lslogins nonexisting"
069435
069435
The default behavior for -l and -g is to silently ignore unknown login
069435
names, but this is very confusing when you explicitly specify just one
069435
login name.
069435
069435
Note that the current implementation also prints empty "Last log" for
069435
nonexisting user. It seems ugly.
069435
069435
 # lslogins nonexisting
069435
069435
 Last logs:
069435
069435
new version:
069435
069435
 # lslogins nonexisting
069435
 lt-lslogins: cannot found 'nonexisting'
069435
 # echo $?
069435
 1
069435
069435
The -l and -g behaviour has not been changed.
069435
069435
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1614967
069435
Signed-off-by: Karel Zak <kzak@redhat.com>
069435
---
069435
 login-utils/lslogins.1 |  3 ++-
069435
 login-utils/lslogins.c | 16 +++++++++++++---
069435
 2 files changed, 15 insertions(+), 4 deletions(-)
069435
069435
diff --git a/login-utils/lslogins.1 b/login-utils/lslogins.1
069435
index effd42790..c831739d9 100644
069435
--- a/login-utils/lslogins.1
069435
+++ b/login-utils/lslogins.1
069435
@@ -18,7 +18,8 @@ and output the desired data.
069435
 The optional argument \fIusername\fR forces
069435
 .BR lslogins
069435
 to print all available details about the specified user only. In this case the
069435
-output format is different than in case of \fB\-l\fR or \fB\-g\fR.
069435
+output format is different than in case of \fB\-l\fR or \fB\-g\fR and unknown
069435
+is \fIusername\fR reported as an error.
069435
 
069435
 .PP
069435
 The default action is to list info about all the users in the system.
069435
diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c
069435
index 501778e54..6f804aa35 100644
069435
--- a/login-utils/lslogins.c
069435
+++ b/login-utils/lslogins.c
069435
@@ -266,6 +266,7 @@ struct lslogins_control {
069435
 	const char *journal_path;
069435
 
069435
 	unsigned int selinux_enabled : 1,
069435
+		     fail_on_unknown : 1,		/* fail if user does not exist */
069435
 		     ulist_on : 1,
069435
 		     noheadings : 1,
069435
 		     notrunc : 1;
069435
@@ -653,6 +654,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
069435
 	uid_t uid;
069435
 	errno = 0;
069435
 
069435
+	errno = 0;
069435
 	pwd = username ? getpwnam(username) : getpwent();
069435
 	if (!pwd)
069435
 		return NULL;
069435
@@ -675,6 +677,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
069435
 		return NULL;
069435
 	}
069435
 
069435
+	errno = 0;
069435
 	grp = getgrgid(pwd->pw_gid);
069435
 	if (!grp)
069435
 		return NULL;
069435
@@ -965,10 +968,16 @@ static int create_usertree(struct lslogins_control *ctl)
069435
 
069435
 	if (ctl->ulist_on) {
069435
 		for (n = 0; n < ctl->ulsiz; n++) {
069435
-			if (get_user(ctl, &user, ctl->ulist[n]))
069435
+			int rc = get_user(ctl, &user, ctl->ulist[n]);
069435
+
069435
+			if (ctl->fail_on_unknown && !user) {
069435
+				warnx(_("cannot found '%s'"), ctl->ulist[n]);
069435
+				return -1;
069435
+			}
069435
+			if (rc || !user)
069435
 				continue;
069435
-			if (user) /* otherwise an invalid user name has probably been given */
069435
-				tsearch(user, &ctl->usertree, cmp_uid);
069435
+
069435
+			tsearch(user, &ctl->usertree, cmp_uid);
069435
 		}
069435
 	} else {
069435
 		while ((user = get_next_user(ctl)))
069435
@@ -1518,6 +1527,7 @@ int main(int argc, char *argv[])
069435
 			errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
069435
 		logins = argv[optind];
069435
 		outmode = OUT_PRETTY;
069435
+		ctl->fail_on_unknown = 1;
069435
 	} else if (argc != optind)
069435
 		errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
069435
 
069435
-- 
069435
2.14.4
069435