From ecc241229028a5382218aa1402df9efde81353f2 Mon Sep 17 00:00:00 2001
From: Meetakshi Setiya <msetiya@microsoft.com>
Date: Mon, 11 Nov 2024 00:10:53 -0500
Subject: [PATCH 03/14] use enums to check password or password2 in
 set_password, get_password_from_file and minor documentation additions

Signed-off-by: Meetakshi Setiya <msetiya@microsoft.com>
Reviewed-by: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
---
 mount.cifs.c   | 33 ++++++++++++++++++---------------
 mount.cifs.rst | 10 +++++-----
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/mount.cifs.c b/mount.cifs.c
index 71a2032..041e64f 100644
--- a/mount.cifs.c
+++ b/mount.cifs.c
@@ -336,8 +336,10 @@ static int mount_usage(FILE * stream)
  */
 static int
 set_password(struct parsed_mount_info *parsed_info, const char *src,
-			   const int is_pass2)
+			   const int which_pass)
 {
+	int is_pass2 = which_pass == OPT_PASS2 || which_pass == CRED_PASS2;
+
 	char *dst = is_pass2 ?
 				parsed_info->password2 : parsed_info->password;
 	unsigned int pass_length = is_pass2 ?
@@ -638,12 +640,12 @@ static int open_cred_file(char *file_name,
 			parsed_info->got_user = 1;
 			break;
 		case CRED_PASS:
-			i = set_password(parsed_info, temp_val, 0);
+			i = set_password(parsed_info, temp_val, CRED_PASS);
 			if (i)
 				goto return_i;
 			break;
 		case CRED_PASS2:
-			i = set_password(parsed_info, temp_val, 1);
+			i = set_password(parsed_info, temp_val, CRED_PASS2);
 			if (i)
 				goto return_i;
 			break;
@@ -673,9 +675,10 @@ return_i:
 static int
 get_password_from_file(int file_descript, char *filename,
 		       struct parsed_mount_info *parsed_info, const char *program,
-			   const int is_pass2)
+			   const int which_pass)
 {
 	int rc = 0;
+	int is_pass2 = which_pass == OPT_PASS2;
 	unsigned int pass_length = is_pass2 ?
 					  sizeof(parsed_info->password2) : sizeof(parsed_info->password);
 	char buf[pass_length + 1];
@@ -720,7 +723,7 @@ get_password_from_file(int file_descript, char *filename,
 		goto get_pw_exit;
 	}
 
-	rc = set_password(parsed_info, buf, is_pass2);
+	rc = set_password(parsed_info, buf, which_pass);
 
 get_pw_exit:
 	if (filename != NULL)
@@ -928,7 +931,7 @@ parse_options(const char *data, struct parsed_mount_info *parsed_info)
 				parsed_info->got_password = 1;
 				goto nocopy;
 			}
-			rc = set_password(parsed_info, value, 0);
+			rc = set_password(parsed_info, value, OPT_PASS);
 			if (rc)
 				return rc;
 			goto nocopy;
@@ -943,7 +946,7 @@ parse_options(const char *data, struct parsed_mount_info *parsed_info)
 				parsed_info->got_password2 = 1;
 				goto nocopy;
 			}
-			rc = set_password(parsed_info, value, 1);
+			rc = set_password(parsed_info, value, OPT_PASS2);
 			if (rc)
 				return rc;
 			goto nocopy;
@@ -1426,25 +1429,25 @@ static int get_pw_from_env(struct parsed_mount_info *parsed_info, const char *pr
 	int rc = 0;
 
 	if (getenv("PASSWD"))
-		rc = set_password(parsed_info, getenv("PASSWD"), 0);
+		rc = set_password(parsed_info, getenv("PASSWD"), OPT_PASS);
 	else if (getenv("PASSWD_FD"))
 		rc = get_password_from_file(atoi(getenv("PASSWD_FD")), NULL,
-					    parsed_info, program, 0);
+					    parsed_info, program, OPT_PASS);
 	else if (getenv("PASSWD_FILE"))
 		rc = get_password_from_file(0, getenv("PASSWD_FILE"),
-					    parsed_info, program, 0);
+					    parsed_info, program, OPT_PASS);
 
 	if (rc < 0)
 		return rc;
 
 	if (getenv("PASSWD2"))
-		rc = set_password(parsed_info, getenv("PASSWD2"), 1);
+		rc = set_password(parsed_info, getenv("PASSWD2"), OPT_PASS2);
 	else if (getenv("PASSWD2_FD"))
 		rc = get_password_from_file(atoi(getenv("PASSWD2_FD")), NULL,
-					    parsed_info, program, 1);
+					    parsed_info, program, OPT_PASS2);
 	else if (getenv("PASSWD2_FILE"))
 		rc = get_password_from_file(0, getenv("PASSWD2_FILE"),
-					    parsed_info, program, 1);
+					    parsed_info, program, OPT_PASS2);
 
 	return rc;
 }
@@ -1983,7 +1986,7 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info,
 	}
 
 	// no need to prompt for password2
-	if (!parsed_info->got_password) {
+	if (!parsed_info->got_password && !(parsed_info->flags & MS_REMOUNT)) {
 		char tmp_pass[MOUNT_PASSWD_SIZE + 1];
 		char *prompt = NULL;
 
@@ -1991,7 +1994,7 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info,
 			prompt = NULL;
 
 		if (get_password(prompt ? prompt : "Password: ", tmp_pass, MOUNT_PASSWD_SIZE + 1)) {
-			rc = set_password(parsed_info, tmp_pass, 0);
+			rc = set_password(parsed_info, tmp_pass, OPT_PASS);
 		} else {
 			fprintf(stderr, "Error reading password, exiting\n");
 			rc = EX_SYSERR;
diff --git a/mount.cifs.rst b/mount.cifs.rst
index d461d0a..5281896 100644
--- a/mount.cifs.rst
+++ b/mount.cifs.rst
@@ -943,12 +943,12 @@ The variable ``USER`` may contain the username of the person to be used
 to authenticate to the server. The variable can be used to set both
 username and password by using the format ``username%password``.
 
-The variable ``PASSWD`` may contain the password of the person using
-the client.
+The variables ``PASSWD`` and ``PASSWD2`` may contain the password and the
+alternate password of the person using the client, respectively.
 
-The variable ``PASSWD_FILE`` may contain the pathname of a file to read
-the password from. A single line of input is read and used as the
-password.
+The variables ``PASSWD_FILE`` and ``PASSWD2_FILE`` may contain the
+pathname of the file to read password or password2 from, respectively.
+A single line of input is read and used as the password in each case.
 
 *****
 NOTES
-- 
2.43.0

