[PATCH 14/19] mount.cifs: move assembly of parsed_mount_info to separate function

Jeff Layton <[email protected]>
Newsgroups gmane.linux.file-systems.cifs
Message-ID <[email protected]>
From: Jeff Layton <[email protected]>

...later, we'll want to introduce privilege separation so make this
a separate function to facilitate that.

Signed-off-by: Jeff Layton <[email protected]>
---
 mount.cifs.c |  204 +++++++++++++++++++++++++++++++---------------------------
 1 files changed, 110 insertions(+), 94 deletions(-)

diff --git a/mount.cifs.c b/mount.cifs.c
index 903518b..36b6673 100644
--- a/mount.cifs.c
+++ b/mount.cifs.c
@@ -129,7 +129,7 @@ const char *thisprogram;
 int verboseflag;
 const char *cifs_fstype = "cifs";
 
-static int parse_unc(char *unc_name, struct parsed_mount_info *parsed_info);
+static int parse_unc(const char *unc_name, struct parsed_mount_info *parsed_info);
 
 #if CIFS_DISABLE_SETUID_CHECK
 static int check_setuid(void)
@@ -153,7 +153,7 @@ static int check_setuid(void)
 #endif /* CIFS_DISABLE_SETUID_CHECK */
 
 static int
-check_fstab(const char *progname, char *mountpoint, char *devname,
+check_fstab(const char *progname, const char *mountpoint, const char *devname,
 	    char **options)
 {
 	FILE *fstab;
@@ -873,10 +873,10 @@ resolve_host_out:
 	return rc;
 }
 
-static int parse_unc(char *unc_name, struct parsed_mount_info *parsed_info)
+static int parse_unc(const char *unc_name, struct parsed_mount_info *parsed_info)
 {
 	int length = strnlen(unc_name, MAX_UNC_LEN);
-	char *host, *share, *prepath;
+	const char *host, *share, *prepath;
 	size_t hostlen, sharelen, prepathlen;
 
 	if (length > (MAX_UNC_LEN - 1)) {
@@ -1113,6 +1113,110 @@ add_mtab_exit:
 	return rc;
 }
 
+static int
+assemble_mountinfo(struct parsed_mount_info *parsed_info,
+		   const char *thisprogram, const char *mountpoint,
+		   const char *orig_dev, char *orgoptions)
+{
+	int rc;
+
+	/* sanity check for unprivileged mounts */
+	if (getuid()) {
+		rc = check_fstab(thisprogram, mountpoint, orig_dev,
+				 &orgoptions);
+		if (rc)
+			goto assemble_exit;
+
+		/* enable any default user mount flags */
+		parsed_info->flags |= CIFS_SETUID_FLAGS;
+	}
+
+	rc = get_pw_from_env(parsed_info);
+	if (rc)
+		goto assemble_exit;
+
+	if (orgoptions) {
+		rc = parse_options(orgoptions, parsed_info);
+		if (rc)
+			goto assemble_exit;
+	}
+
+	if (getuid()) {
+		if (!(parsed_info->flags & (MS_USERS | MS_USER))) {
+			fprintf(stderr, "%s: permission denied\n", thisprogram);
+			rc = EX_USAGE;
+			goto assemble_exit;
+		}
+
+		if (geteuid()) {
+			fprintf(stderr, "%s: not installed setuid - \"user\" "
+				"CIFS mounts not supported.", thisprogram);
+			rc = EX_FAIL;
+			goto assemble_exit;
+		}
+	}
+
+	parsed_info->flags &= ~(MS_USERS | MS_USER);
+
+	rc = parse_unc(orig_dev, parsed_info);
+	if (rc)
+		goto assemble_exit;
+
+	rc = resolve_host(parsed_info);
+	if (rc)
+		goto assemble_exit;
+
+	if (!parsed_info->got_user) {
+		/*
+		 * Note that the password will not be retrieved from the
+		 * USER env variable (ie user%password form) as there is
+		 * already a PASSWD environment varaible
+		 */
+		if (getenv("USER"))
+			strlcpy(parsed_info->username, getenv("USER"),
+				sizeof(parsed_info->username));
+		else
+			strlcpy(parsed_info->username, getusername(),
+				sizeof(parsed_info->username));
+		parsed_info->got_user = 1;
+	}
+
+	if (!parsed_info->got_password) {
+		/* getpass is obsolete, but there's apparently nothing that replaces it */
+		char *tmp_pass = getpass("Password: ");
+		if (!tmp_pass) {
+			fprintf(stderr, "Error reading password, exiting\n");
+			rc = EX_SYSERR;
+			goto assemble_exit;
+		}
+		rc = set_password(parsed_info, tmp_pass);
+		if (rc)
+			goto assemble_exit;
+	}
+
+	/* copy in ver= string. It's not really needed, but what the hell */
+	strlcat(parsed_info->options, ",ver=", sizeof(parsed_info->options));
+	strlcat(parsed_info->options, OPTIONS_VERSION, sizeof(parsed_info->options));
+
+	/* copy in user= string */
+	if (parsed_info->got_user) {
+		strlcat(parsed_info->options, ",user=",
+			sizeof(parsed_info->options));
+		strlcat(parsed_info->options, parsed_info->username,
+			sizeof(parsed_info->options));
+	}
+
+	if (*parsed_info->domain) {
+		strlcat(parsed_info->options, ",domain=",
+			sizeof(parsed_info->options));
+		strlcat(parsed_info->options, parsed_info->domain,
+			sizeof(parsed_info->options));
+	}
+
+assemble_exit:
+	return rc;
+}
+
 int main(int argc, char **argv)
 {
 	int c;
@@ -1218,99 +1322,11 @@ int main(int argc, char **argv)
 		goto mount_exit;
 	}
 
-	/* sanity check for unprivileged mounts */
-	if (getuid()) {
-		rc = check_fstab(thisprogram, mountpoint, orig_dev,
-				 &orgoptions);
-		if (rc)
-			goto mount_exit;
-
-		/* enable any default user mount flags */
-		parsed_info->flags |= CIFS_SETUID_FLAGS;
-	}
-
-	rc = get_pw_from_env(parsed_info);
+	rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
+				orig_dev, orgoptions);
 	if (rc)
 		goto mount_exit;
 
-	if (orgoptions) {
-		rc = parse_options(orgoptions, parsed_info);
-		if (rc)
-			goto mount_exit;
-	}
-
-	if (getuid()) {
-		if (!(parsed_info->flags & (MS_USERS | MS_USER))) {
-			fprintf(stderr, "%s: permission denied\n", thisprogram);
-			rc = EX_USAGE;
-			goto mount_exit;
-		}
-
-		if (geteuid()) {
-			fprintf(stderr, "%s: not installed setuid - \"user\" "
-				"CIFS mounts not supported.", thisprogram);
-			rc = EX_FAIL;
-			goto mount_exit;
-		}
-	}
-
-	parsed_info->flags &= ~(MS_USERS | MS_USER);
-
-	rc = parse_unc(orig_dev, parsed_info);
-	if (rc)
-		goto mount_exit;
-
-	rc = resolve_host(parsed_info);
-	if (rc)
-		goto mount_exit;
-
-	if (!parsed_info->got_user) {
-		/*
-		 * Note that the password will not be retrieved from the
-		 * USER env variable (ie user%password form) as there is
-		 * already a PASSWD environment varaible
-		 */
-		if (getenv("USER"))
-			strlcpy(parsed_info->username, getenv("USER"),
-				sizeof(parsed_info->username));
-		else
-			strlcpy(parsed_info->username, getusername(),
-				sizeof(parsed_info->username));
-		parsed_info->got_user = 1;
-	}
-
-	if (!parsed_info->got_password) {
-		/* getpass is obsolete, but there's apparently nothing that replaces it */
-		char *tmp_pass = getpass("Password: ");
-		if (!tmp_pass) {
-			fprintf(stderr, "Error reading password, exiting\n");
-			rc = EX_SYSERR;
-			goto mount_exit;
-		}
-		rc = set_password(parsed_info, tmp_pass);
-		if (rc)
-			goto mount_exit;
-	}
-
-	/* copy in ver= string. It's not really needed, but what the hell */
-	strlcat(parsed_info->options, ",ver=", sizeof(parsed_info->options));
-	strlcat(parsed_info->options, OPTIONS_VERSION, sizeof(parsed_info->options));
-
-	/* copy in user= string */
-	if (parsed_info->got_user) {
-		strlcat(parsed_info->options, ",user=",
-			sizeof(parsed_info->options));
-		strlcat(parsed_info->options, parsed_info->username,
-			sizeof(parsed_info->options));
-	}
-
-	if (*parsed_info->domain) {
-		strlcat(parsed_info->options, ",domain=",
-			sizeof(parsed_info->options));
-		strlcat(parsed_info->options, parsed_info->domain,
-			sizeof(parsed_info->options));
-	}
-
 	options = calloc(options_size, 1);
 	if (!options) {
 		fprintf(stderr, "Unable to allocate memory.\n");
-- 
1.6.6.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.