git: a20d50245f28 - main - mountd(8): parsecred(): Re-order operations for clarity

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Mon, 16 Dec 2024 14:45:30 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=a20d50245f280be404cb8e3b5c9d570ded9594b9

commit a20d50245f280be404cb8e3b5c9d570ded9594b9
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2024-10-08 10:06:55 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2024-12-16 14:42:31 +0000

    mountd(8): parsecred(): Re-order operations for clarity
    
    No functional change (intended).
    
    Reviewed by:    rmacklem (older version)
    Approved by:    markj (mentor)
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D47015
---
 usr.sbin/mountd/mountd.c | 53 ++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/usr.sbin/mountd/mountd.c b/usr.sbin/mountd/mountd.c
index 653d7135ef79..27d22ba06fa4 100644
--- a/usr.sbin/mountd/mountd.c
+++ b/usr.sbin/mountd/mountd.c
@@ -3614,21 +3614,14 @@ parsecred(char *namelist, struct expcred *cr)
 	char *name;
 	char *names;
 	struct passwd *pw;
-	struct group *gr;
 	gid_t groups[NGROUPS_MAX + 1];
 	int ngroups;
 	unsigned long name_ul;
 	char *end = NULL;
 
 	/*
-	 * Set up the unprivileged user.
-	 */
-	cr->cr_groups = cr->cr_smallgrps;
-	cr->cr_uid = UID_NOBODY;
-	cr->cr_groups[0] = nogroup();
-	cr->cr_ngroups = 1;
-	/*
-	 * Get the user's password table entry.
+	 * Parse the user and if possible get its password table entry.
+	 * 'cr_uid' is filled when exiting this block.
 	 */
 	names = namelist;
 	name = strsep_quote(&names, ":");
@@ -3637,13 +3630,25 @@ parsecred(char *namelist, struct expcred *cr)
 		pw = getpwnam(name);
 	else
 		pw = getpwuid((uid_t)name_ul);
+	if (pw != NULL) {
+		cr->cr_uid = pw->pw_uid;
+	} else if (*end != '\0' || end == name) {
+		syslog(LOG_ERR, "unknown user: %s", name);
+		cr->cr_uid = UID_NOBODY;
+		goto nogroup;
+	} else {
+		cr->cr_uid = name_ul;
+	}
+
 	/*
-	 * Credentials specified as those of a user.
+	 * Credentials specified as those of a user (i.e., use its associated
+	 * groups as specified in the password database).
 	 */
 	if (names == NULL) {
 		if (pw == NULL) {
-			syslog(LOG_ERR, "unknown user: %s", name);
-			return;
+			syslog(LOG_ERR, "no passwd entry for user: %s, "
+			    "can't determine groups", name);
+			goto nogroup;
 		}
 		cr->cr_uid = pw->pw_uid;
 		ngroups = NGROUPS_MAX + 1;
@@ -3658,20 +3663,14 @@ parsecred(char *namelist, struct expcred *cr)
 		memcpy(cr->cr_groups, groups, ngroups * sizeof(gid_t));
 		return;
 	}
+
 	/*
-	 * Explicit credential specified as a colon separated list:
+	 * Explicit credentials specified as a colon separated list:
 	 *	uid:gid:gid:...
 	 */
-	if (pw != NULL) {
-		cr->cr_uid = pw->pw_uid;
-	} else if (*end != '\0' || end == name) {
-		syslog(LOG_ERR, "unknown user: %s", name);
-		return;
-	} else {
-		cr->cr_uid = name_ul;
-	}
 	cr->cr_ngroups = 0;
 	while (names != NULL && *names != '\0') {
+		const struct group *gr;
 		gid_t group;
 
 		name = strsep_quote(&names, ":");
@@ -3691,14 +3690,16 @@ parsecred(char *namelist, struct expcred *cr)
 		}
 		groups[cr->cr_ngroups++] = group;
 	}
-	if (cr->cr_ngroups == 0) {
-		/* cr->cr_groups[0] filled at start with nogroup(). */
-		cr->cr_ngroups = 1;
-		return;
-	}
+	if (cr->cr_ngroups == 0)
+		goto nogroup;
 	if (cr->cr_ngroups > SMALLNGROUPS)
 		cr->cr_groups = malloc(cr->cr_ngroups * sizeof(gid_t));
 	memcpy(cr->cr_groups, groups, cr->cr_ngroups * sizeof(gid_t));
+	return;
+
+nogroup:
+	cr->cr_ngroups = 1;
+	cr->cr_groups[0] = nogroup();
 }
 
 #define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)