From miki@ma.krakow.pl  Fri Jul 29 12:44:15 2005
Return-Path: <miki@ma.krakow.pl>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id AB59416A41F
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 29 Jul 2005 12:44:15 +0000 (GMT)
	(envelope-from miki@ma.krakow.pl)
Received: from k2.ma.krakow.pl (k2.ma.krakow.pl [62.121.133.130])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 42C5E43D4C
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 29 Jul 2005 12:44:12 +0000 (GMT)
	(envelope-from miki@ma.krakow.pl)
Received: from k2.ma.krakow.pl (localhost [127.0.0.1])
	by k2.ma.krakow.pl (8.13.4/8.13.4) with ESMTP id j6TChxiP078432
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT)
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 29 Jul 2005 14:43:59 +0200 (CEST)
	(envelope-from miki@k2.ma.krakow.pl)
Received: (from miki@localhost)
	by k2.ma.krakow.pl (8.13.4/8.13.4/Submit) id j6TChxb7078431;
	Fri, 29 Jul 2005 14:43:59 +0200 (CEST)
	(envelope-from miki)
Message-Id: <200507291243.j6TChxb7078431@k2.ma.krakow.pl>
Date: Fri, 29 Jul 2005 14:43:59 +0200 (CEST)
From: Mikolaj Rydzewski <miki@ma.krakow.pl>
Reply-To: Mikolaj Rydzewski <miki@ma.krakow.pl>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] allow mount(8) to recognize relative pathnames as mountpoints
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         84298
>Category:       bin
>Synopsis:       [patch] allow mount(8) to recognize relative pathnames as mountpoints
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 29 12:50:19 GMT 2005
>Closed-Date:    
>Last-Modified:  Fri Jul 29 21:40:26 GMT 2005
>Originator:     Mikolaj Rydzewski
>Release:        FreeBSD 5.4-RELEASE-p5 i386
>Organization:
>Environment:
System: FreeBSD k2.ma.krakow.pl 5.4-RELEASE-p5 FreeBSD 5.4-RELEASE-p5 #3: Tue Jul 26 13:11:08 CEST 2005 miki@k2.ma.krakow.pl:/usr/src/sys/i386/compile/K2 i386


>Description:
	Current version of mount(8) requires to specify absolute mountpoint
	pathname. Let's assume one has some mountpoints located in /mnt:
	/mnt/cdrom, /mnt/floppy, /mnt/usb
	It should be possible to call mount(8) like this:
	mount cdrom (assuming the current directory is /mnt). It has
	more practical impact when system is configured to allow non-root
	users to mount (cdroms, usb sticks) to mountpoints in their home
	directories. It's much more flexible to run mount ~/usb than 
	mount /home/<username>/usb.
	And last but not least ;-) mount(8) is supposed to behave in such
	way - there's a correct call to realpath(3), but infortunately
	previous function checks mountpoint validity without using
	realpath(3) call.
	So my patch just polishes existing code.
>How-To-Repeat:
	Try to mount a filesystem using non absolute path name - an error
	will occur.
>Fix:
	Apply attached patch.

--- mount_patch begins here ---
diff -Nru sbin/mount.old/getmntopts.c sbin/mount/getmntopts.c
--- sbin/mount.old/getmntopts.c	Fri Jul 29 13:55:33 2005
+++ sbin/mount/getmntopts.c	Fri Jul 29 14:08:03 2005
@@ -140,3 +140,18 @@
 	} else
 		errx(EX_USAGE, "%s: %s", resolved, strerror(errno));
 }
+
+const char *
+getrealpath(path, resolved)
+	const char *path;
+	char *resolved;
+{
+	struct stat sb;
+
+	if (realpath(path, resolved) != NULL
+		&& stat(resolved, &sb) == 0
+		&& S_ISDIR(sb.st_mode))
+		return resolved;
+
+	return path;
+}
diff -Nru sbin/mount.old/mntopts.h sbin/mount/mntopts.h
--- sbin/mount.old/mntopts.h	Fri Jul 29 13:55:33 2005
+++ sbin/mount/mntopts.h	Fri Jul 29 14:07:15 2005
@@ -91,4 +91,5 @@
 void getmntopts(const char *, const struct mntopt *, int *, int *);
 void rmslashes(char *, char *);
 void checkpath(const char *, char resolved_path[]);
+const char* getrealpath(const char *, char resolved_path[]);
 extern int getmnt_silent;
diff -Nru sbin/mount.old/mount.c sbin/mount/mount.c
--- sbin/mount.old/mount.c	Fri Jul 29 13:55:33 2005
+++ sbin/mount/mount.c	Fri Jul 29 14:12:42 2005
@@ -126,13 +126,14 @@
 	int argc;
 	char * const argv[];
 {
-	const char *mntfromname, **vfslist, *vfstype;
+	const char *mntfromname, **vfslist, *vfstype, *mntpath_ptr;
 	struct fstab *fs;
 	struct statfs *mntbuf;
 	FILE *mountdfp;
 	pid_t pid;
 	int all, ch, i, init_flags, mntsize, rval, have_fstab;
 	char *cp, *ep, *options;
+	char mntpath[PATH_MAX];
 
 	all = init_flags = 0;
 	options = NULL;
@@ -271,8 +272,9 @@
 			    mntbuf->f_mntonname, init_flags, options, 0);
 			break;
 		}
-		if ((fs = getfsfile(*argv)) == NULL &&
-		    (fs = getfsspec(*argv)) == NULL)
+		mntpath_ptr = getrealpath(*argv, mntpath);
+		if ((fs = getfsfile(mntpath_ptr)) == NULL &&
+		    (fs = getfsspec(mntpath_ptr)) == NULL)
 			errx(1, "%s: unknown special file or file system",
 			    *argv);
 		if (BADTYPE(fs->fs_type))
--- mount_patch ends here ---


>Release-Note:
>Audit-Trail:

From: Brooks Davis <brooks@one-eyed-alien.net>
To: Mikolaj Rydzewski <miki@ma.krakow.pl>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/84298: [patch] allow mount(8) to recognize relative pathnames as mountpoints
Date: Fri, 29 Jul 2005 09:06:56 -0700

 > >Description:
 > 	Current version of mount(8) requires to specify absolute mountpoint
 > 	pathname. Let's assume one has some mountpoints located in /mnt:
 > 	/mnt/cdrom, /mnt/floppy, /mnt/usb
 > 	It should be possible to call mount(8) like this:
 > 	mount cdrom (assuming the current directory is /mnt). It has
 > 	more practical impact when system is configured to allow non-root
 > 	users to mount (cdroms, usb sticks) to mountpoints in their home
 > 	directories. It's much more flexible to run mount ~/usb than 
 > 	mount /home/<username>/usb.
 
 Not really an objection, but this last example is bogus.  As the example
 below shows, tcsh, bash, sh, and csh all make this work since ~expansion
 happens before the command is run by the shell:
 
 [7:22pm] brooks@pagefault (/usr/ports): echo ~brooks
 /usr/home/brooks
 [9:04am] brooks@pagefault (/usr/ports): bash
 brooks@pagefault$ echo ~brooks
 /usr/home/brooks
 brooks@pagefault$ exit
 [9:04am] brooks@pagefault (/usr/ports): sh
 $ echo ~brooks
 /usr/home/brooks
 $ [9:04am] brooks@pagefault (/usr/ports): csh
 [9:04am] brooks@pagefault (/usr/ports): echo ~brooks
 /usr/home/brooks
 [9:04am] brooks@pagefault (/usr/ports): exit
 
 -- Brooks

From: Mikolaj Rydzewski <miki@ma.krakow.pl>
To: Brooks Davis <brooks@one-eyed-alien.net>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/84298: [patch] allow mount(8) to recognize relative pathnames
 as mountpoints
Date: Fri, 29 Jul 2005 23:36:33 +0200 (CEST)

 On Fri, 29 Jul 2005, Brooks Davis wrote:
 
 >> 	Current version of mount(8) requires to specify absolute mountpoint
 >> 	pathname. Let's assume one has some mountpoints located in /mnt:
 >> 	/mnt/cdrom, /mnt/floppy, /mnt/usb
 >> 	It should be possible to call mount(8) like this:
 >> 	mount cdrom (assuming the current directory is /mnt). It has
 >> 	more practical impact when system is configured to allow non-root
 >> 	users to mount (cdroms, usb sticks) to mountpoints in their home
 >> 	directories. It's much more flexible to run mount ~/usb than
 >> 	mount /home/<username>/usb.
 >
 > Not really an objection, but this last example is bogus.  As the example
 > below shows, tcsh, bash, sh, and csh all make this work since ~expansion
 > happens before the command is run by the shell:
 
 Yes, you're right, thanks for pointing this.
 
 But the problem with mount ignoring relative mountpoint pathnames still 
 exists.
 
 Regards
 
 -- 
 Mikolaj Rydzewski    <miki@ma.krakow.pl>    PGP KeyID: 8b12ab02
>Unformatted:
