Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Dec 2015 22:01:37 +0000 (UTC)
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r291657 - in head/usr.sbin/pw: . tests
Message-ID:  <201512022201.tB2M1bPI039447@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: bapt
Date: Wed Dec  2 22:01:37 2015
New Revision: 291657
URL: https://svnweb.freebsd.org/changeset/base/291657

Log:
  Fix handling of numeric-only names with pw lock
  Add a regression test about it
  
  PR:		204968
  MFC after:	1 week

Modified:
  head/usr.sbin/pw/pw_user.c
  head/usr.sbin/pw/tests/pw_lock.sh

Modified: head/usr.sbin/pw/pw_user.c
==============================================================================
--- head/usr.sbin/pw/pw_user.c	Wed Dec  2 21:56:01 2015	(r291656)
+++ head/usr.sbin/pw/pw_user.c	Wed Dec  2 22:01:37 2015	(r291657)
@@ -274,7 +274,7 @@ pw_userlock(char *arg1, int mode)
 	char *passtmp = NULL;
 	char *name;
 	bool locked = false;
-	uid_t id;
+	uid_t id = (uid_t)-1;
 
 	if (geteuid() != 0)
 		errx(EX_NOPERM, "you must be root");
@@ -282,16 +282,19 @@ pw_userlock(char *arg1, int mode)
 	if (arg1 == NULL)
 		errx(EX_DATAERR, "username or id required");
 
-	if (arg1[strspn(arg1, "0123456789")] == '\0') {
-		id = pw_checkid(arg1, UID_MAX);
-		name = NULL;
-	} else
-		name = arg1;
-
-	pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
+	name = arg1;
+	if (arg1[strspn(name, "0123456789")] == '\0')
+		id = pw_checkid(name, UID_MAX);
+
+	pwd = GETPWNAM(pw_checkname(name, 0));
+	if (pwd == NULL && id != (uid_t)-1) {
+		pwd = GETPWUID(id);
+		if (pwd != NULL)
+			name = pwd->pw_name;
+	}
 	if (pwd == NULL) {
-		if (name == NULL)
-			errx(EX_NOUSER, "no such uid `%ju'", (uintmax_t) id);
+		if (id == (uid_t)-1)
+			errx(EX_NOUSER, "no such name or uid `%ju'", (uintmax_t) id);
 		errx(EX_NOUSER, "no such user `%s'", name);
 	}
 

Modified: head/usr.sbin/pw/tests/pw_lock.sh
==============================================================================
--- head/usr.sbin/pw/tests/pw_lock.sh	Wed Dec  2 21:56:01 2015	(r291656)
+++ head/usr.sbin/pw/tests/pw_lock.sh	Wed Dec  2 22:01:37 2015	(r291657)
@@ -16,7 +16,27 @@ user_locking_body() {
 		grep "^test:\*:1001:" $HOME/master.passwd
 }
 
+atf_test_case numeric_locking cleanup
+numeric_locking_body() {
+	populate_etc_skel
+	${PW} useradd test || atf_fail "Creating test user"
+	${PW} lock 1001 || atf_fail "Locking the user"
+	atf_check -s exit:0 -o match:"^test:\*LOCKED\*\*:1001:" \
+		grep "^test:\*LOCKED\*\*:1001:" $HOME/master.passwd
+	${PW} unlock 1001 || atf_fail "Unlocking the user"
+	atf_check -s exit:0 -o match:"^test:\*:1001:" \
+		grep "^test:\*:1001:" $HOME/master.passwd
+	# Now numeric names
+	${PW} useradd -n 1001 || atf_fail "Creating test user"
+	${PW} lock 1001 || atf_fail "Locking the user"
+	atf_check -s exit:0 -o match:"^1001:\*LOCKED\*\*:1002:" \
+		grep "^1001:\*LOCKED\*\*:1002:" $HOME/master.passwd
+	${PW} unlock 1001 || atf_fail "Unlocking the user"
+	atf_check -s exit:0 -o match:"^1001:\*:1002:" \
+		grep "^1001:\*:1002:" $HOME/master.passwd
+}
 
 atf_init_test_cases() {
 	atf_add_test_case user_locking
+	atf_add_test_case numeric_locking
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201512022201.tB2M1bPI039447>