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