Home | History | Annotate | Line # | Download | only in mount_ntfs
mount_ntfs.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1994 Christopher G. Demetriou
      3  1.1  christos  * Copyright (c) 1999 Semen Ustimenko
      4  1.1  christos  * All rights reserved.
      5  1.1  christos  *
      6  1.1  christos  * Redistribution and use in source and binary forms, with or without
      7  1.1  christos  * modification, are permitted provided that the following conditions
      8  1.1  christos  * are met:
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     15  1.1  christos  *    must display the following acknowledgement:
     16  1.1  christos  *      This product includes software developed by Christopher G. Demetriou.
     17  1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     18  1.1  christos  *    derived from this software without specific prior written permission
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  *
     31  1.1  christos  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
     32  1.1  christos  *
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos #include <sys/param.h>
     37  1.1  christos #define NTFS
     38  1.1  christos #include <sys/mount.h>
     39  1.1  christos #include <sys/stat.h>
     40  1.1  christos #include <ntfs/ntfsmount.h>
     41  1.1  christos #include <ctype.h>
     42  1.1  christos #include <err.h>
     43  1.1  christos #include <grp.h>
     44  1.1  christos #include <pwd.h>
     45  1.1  christos #include <stdio.h>
     46  1.1  christos #include <stdlib.h>
     47  1.1  christos #include <string.h>
     48  1.1  christos #include <sysexits.h>
     49  1.1  christos #include <unistd.h>
     50  1.1  christos 
     51  1.1  christos #include "mntopts.h"
     52  1.1  christos 
     53  1.1  christos static struct mntopt mopts[] = {
     54  1.1  christos 	MOPT_STDOPTS,
     55  1.1  christos 	{ NULL }
     56  1.1  christos };
     57  1.1  christos 
     58  1.1  christos static gid_t	a_gid __P((char *));
     59  1.1  christos static uid_t	a_uid __P((char *));
     60  1.1  christos static mode_t	a_mask __P((char *));
     61  1.1  christos static void	usage __P((void)) __dead2;
     62  1.1  christos 
     63  1.1  christos int
     64  1.1  christos main(argc, argv)
     65  1.1  christos 	int argc;
     66  1.1  christos 	char **argv;
     67  1.1  christos {
     68  1.1  christos 	struct ntfs_args args;
     69  1.1  christos 	struct stat sb;
     70  1.1  christos 	int c, mntflags, set_gid, set_uid, set_mask,error;
     71  1.1  christos 	char *dev, *dir, ndir[MAXPATHLEN+1];
     72  1.1  christos #if __FreeBSD_version >= 300000
     73  1.1  christos 	struct vfsconf vfc;
     74  1.1  christos #else
     75  1.1  christos 	struct vfsconf *vfc;
     76  1.1  christos #endif
     77  1.1  christos 
     78  1.1  christos 	mntflags = set_gid = set_uid = set_mask = 0;
     79  1.1  christos 	(void)memset(&args, '\0', sizeof(args));
     80  1.1  christos 
     81  1.1  christos 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
     82  1.1  christos 		switch (c) {
     83  1.1  christos 		case 'u':
     84  1.1  christos 			args.uid = a_uid(optarg);
     85  1.1  christos 			set_uid = 1;
     86  1.1  christos 			break;
     87  1.1  christos 		case 'g':
     88  1.1  christos 			args.gid = a_gid(optarg);
     89  1.1  christos 			set_gid = 1;
     90  1.1  christos 			break;
     91  1.1  christos 		case 'm':
     92  1.1  christos 			args.mode = a_mask(optarg);
     93  1.1  christos 			set_mask = 1;
     94  1.1  christos 			break;
     95  1.1  christos 		case 'i':
     96  1.1  christos 			args.flag |= NTFS_MFLAG_CASEINS;
     97  1.1  christos 			break;
     98  1.1  christos 		case 'a':
     99  1.1  christos 			args.flag |= NTFS_MFLAG_ALLNAMES;
    100  1.1  christos 			break;
    101  1.1  christos 		case 'o':
    102  1.1  christos 			getmntopts(optarg, mopts, &mntflags, 0);
    103  1.1  christos 			break;
    104  1.1  christos 		case '?':
    105  1.1  christos 		default:
    106  1.1  christos 			usage();
    107  1.1  christos 			break;
    108  1.1  christos 		}
    109  1.1  christos 	}
    110  1.1  christos 
    111  1.1  christos 	if (optind + 2 != argc)
    112  1.1  christos 		usage();
    113  1.1  christos 
    114  1.1  christos 	dev = argv[optind];
    115  1.1  christos 	dir = argv[optind + 1];
    116  1.1  christos 	if (dir[0] != '/') {
    117  1.1  christos 		warnx("\"%s\" is a relative path", dir);
    118  1.1  christos 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    119  1.1  christos 			err(EX_OSERR, "getcwd");
    120  1.1  christos 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    121  1.1  christos 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    122  1.1  christos 		dir = ndir;
    123  1.1  christos 		warnx("using \"%s\" instead", dir);
    124  1.1  christos 	}
    125  1.1  christos 
    126  1.1  christos 	args.fspec = dev;
    127  1.1  christos 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
    128  1.1  christos 	if (mntflags & MNT_RDONLY)
    129  1.1  christos 		args.export.ex_flags = MNT_EXRDONLY;
    130  1.1  christos 	else
    131  1.1  christos 		args.export.ex_flags = 0;
    132  1.1  christos 	if (!set_gid || !set_uid || !set_mask) {
    133  1.1  christos 		if (stat(dir, &sb) == -1)
    134  1.1  christos 			err(EX_OSERR, "stat %s", dir);
    135  1.1  christos 
    136  1.1  christos 		if (!set_uid)
    137  1.1  christos 			args.uid = sb.st_uid;
    138  1.1  christos 		if (!set_gid)
    139  1.1  christos 			args.gid = sb.st_gid;
    140  1.1  christos 		if (!set_mask)
    141  1.1  christos 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    142  1.1  christos 	}
    143  1.1  christos 
    144  1.1  christos #if __FreeBSD_version >= 300000
    145  1.1  christos 	error = getvfsbyname("ntfs", &vfc);
    146  1.1  christos 	if(error && vfsisloadable("ntfs")) {
    147  1.1  christos 		if(vfsload("ntfs"))
    148  1.1  christos #else
    149  1.1  christos 	vfc = getvfsbyname("ntfs");
    150  1.1  christos 	if(!vfc && vfsisloadable("ntfs")) {
    151  1.1  christos 		if(vfsload("ntfs"))
    152  1.1  christos #endif
    153  1.1  christos 			err(EX_OSERR, "vfsload(ntfs)");
    154  1.1  christos 		endvfsent();	/* clear cache */
    155  1.1  christos #if __FreeBSD_version >= 300000
    156  1.1  christos 		error = getvfsbyname("ntfs", &vfc);
    157  1.1  christos #else
    158  1.1  christos 		vfc = getvfsbyname("ntfs");
    159  1.1  christos #endif
    160  1.1  christos 	}
    161  1.1  christos #if __FreeBSD_version >= 300000
    162  1.1  christos 	if (error)
    163  1.1  christos #else
    164  1.1  christos 	if (!vfc)
    165  1.1  christos #endif
    166  1.1  christos 		errx(EX_OSERR, "ntfs filesystem is not available");
    167  1.1  christos 
    168  1.1  christos #if __FreeBSD_version >= 300000
    169  1.1  christos 	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
    170  1.1  christos #else
    171  1.1  christos 	if (mount(vfc->vfc_index, dir, mntflags, &args) < 0)
    172  1.1  christos #endif
    173  1.1  christos 		err(EX_OSERR, "%s", dev);
    174  1.1  christos 
    175  1.1  christos 	exit (0);
    176  1.1  christos }
    177  1.1  christos 
    178  1.1  christos gid_t
    179  1.1  christos a_gid(s)
    180  1.1  christos 	char *s;
    181  1.1  christos {
    182  1.1  christos 	struct group *gr;
    183  1.1  christos 	char *gname;
    184  1.1  christos 	gid_t gid;
    185  1.1  christos 
    186  1.1  christos 	if ((gr = getgrnam(s)) != NULL)
    187  1.1  christos 		gid = gr->gr_gid;
    188  1.1  christos 	else {
    189  1.1  christos 		for (gname = s; *s && isdigit(*s); ++s);
    190  1.1  christos 		if (!*s)
    191  1.1  christos 			gid = atoi(gname);
    192  1.1  christos 		else
    193  1.1  christos 			errx(EX_NOUSER, "unknown group id: %s", gname);
    194  1.1  christos 	}
    195  1.1  christos 	return (gid);
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos uid_t
    199  1.1  christos a_uid(s)
    200  1.1  christos 	char *s;
    201  1.1  christos {
    202  1.1  christos 	struct passwd *pw;
    203  1.1  christos 	char *uname;
    204  1.1  christos 	uid_t uid;
    205  1.1  christos 
    206  1.1  christos 	if ((pw = getpwnam(s)) != NULL)
    207  1.1  christos 		uid = pw->pw_uid;
    208  1.1  christos 	else {
    209  1.1  christos 		for (uname = s; *s && isdigit(*s); ++s);
    210  1.1  christos 		if (!*s)
    211  1.1  christos 			uid = atoi(uname);
    212  1.1  christos 		else
    213  1.1  christos 			errx(EX_NOUSER, "unknown user id: %s", uname);
    214  1.1  christos 	}
    215  1.1  christos 	return (uid);
    216  1.1  christos }
    217  1.1  christos 
    218  1.1  christos mode_t
    219  1.1  christos a_mask(s)
    220  1.1  christos 	char *s;
    221  1.1  christos {
    222  1.1  christos 	int done, rv=0;
    223  1.1  christos 	char *ep;
    224  1.1  christos 
    225  1.1  christos 	done = 0;
    226  1.1  christos 	if (*s >= '0' && *s <= '7') {
    227  1.1  christos 		done = 1;
    228  1.1  christos 		rv = strtol(optarg, &ep, 8);
    229  1.1  christos 	}
    230  1.1  christos 	if (!done || rv < 0 || *ep)
    231  1.1  christos 		errx(EX_USAGE, "invalid file mode: %s", s);
    232  1.1  christos 	return (rv);
    233  1.1  christos }
    234  1.1  christos 
    235  1.1  christos void
    236  1.1  christos usage()
    237  1.1  christos {
    238  1.1  christos 	fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] bdev dir\n");
    239  1.1  christos 	exit(EX_USAGE);
    240  1.1  christos }
    241