Home | History | Annotate | Line # | Download | only in mount_ntfs
      1 /* $NetBSD: mount_ntfs.c,v 1.22 2009/09/14 12:43:46 pooka Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994 Christopher G. Demetriou
      5  * Copyright (c) 1999 Semen Ustimenko
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Christopher G. Demetriou.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: mount_ntfs.c,v 1.22 2009/09/14 12:43:46 pooka Exp $");
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/mount.h>
     43 #include <sys/stat.h>
     44 #include <ntfs/ntfsmount.h>
     45 #include <err.h>
     46 #include <grp.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <sysexits.h>
     52 #include <unistd.h>
     53 #include <util.h>
     54 
     55 #include <mntopts.h>
     56 
     57 #include "mountprog.h"
     58 #include "mount_ntfs.h"
     59 
     60 static const struct mntopt mopts[] = {
     61 	MOPT_STDOPTS,
     62 	MOPT_GETARGS,
     63 	MOPT_NULL,
     64 };
     65 
     66 static void	usage(void) __dead;
     67 
     68 #ifndef MOUNT_NOMAIN
     69 int
     70 main(int argc, char **argv)
     71 {
     72 
     73 	setprogname(argv[0]);
     74 	return mount_ntfs(argc, argv);
     75 }
     76 #endif
     77 
     78 void
     79 mount_ntfs_parseargs(int argc, char **argv,
     80 	struct ntfs_args *args, int *mntflags,
     81 	char *canon_dev, char *canon_dir)
     82 {
     83 	struct stat sb;
     84 	int c, set_gid, set_uid, set_mask;
     85 	char *dev, *dir;
     86 	mntoptparse_t mp;
     87 
     88 	*mntflags = set_gid = set_uid = set_mask = 0;
     89 	(void)memset(args, '\0', sizeof(*args));
     90 
     91 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
     92 		switch (c) {
     93 		case 'u':
     94 			args->uid = a_uid(optarg);
     95 			set_uid = 1;
     96 			break;
     97 		case 'g':
     98 			args->gid = a_gid(optarg);
     99 			set_gid = 1;
    100 			break;
    101 		case 'm':
    102 			args->mode = a_mask(optarg);
    103 			set_mask = 1;
    104 			break;
    105 		case 'i':
    106 			args->flag |= NTFS_MFLAG_CASEINS;
    107 			break;
    108 		case 'a':
    109 			args->flag |= NTFS_MFLAG_ALLNAMES;
    110 			break;
    111 		case 'o':
    112 			mp = getmntopts(optarg, mopts, mntflags, 0);
    113 			if (mp == NULL)
    114 				err(1, "getmntopts");
    115 			freemntopts(mp);
    116 			break;
    117 		case '?':
    118 		default:
    119 			usage();
    120 			break;
    121 		}
    122 	}
    123 
    124 	if (optind + 2 != argc)
    125 		usage();
    126 
    127 	dev = argv[optind];
    128 	dir = argv[optind + 1];
    129 
    130 	pathadj(dev, canon_dev);
    131 	pathadj(dir, canon_dir);
    132 
    133 	args->fspec = canon_dev;
    134 	if (!set_gid || !set_uid || !set_mask) {
    135 		if (stat(dir, &sb) == -1)
    136 			err(EX_OSERR, "stat %s", dir);
    137 
    138 		if (!set_uid)
    139 			args->uid = sb.st_uid;
    140 		if (!set_gid)
    141 			args->gid = sb.st_gid;
    142 		if (!set_mask)
    143 			args->mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    144 	}
    145 }
    146 
    147 int
    148 mount_ntfs(int argc, char *argv[])
    149 {
    150 	struct ntfs_args args;
    151 	int mntflags;
    152 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    153 
    154 	mount_ntfs_parseargs(argc, argv, &args, &mntflags,
    155 	    canon_dev, canon_dir);
    156 
    157 	if (mount(MOUNT_NTFS, canon_dir, mntflags, &args, sizeof args) == -1)
    158 		err(EX_OSERR, "%s on %s", canon_dev, canon_dir);
    159 
    160 	if (mntflags & MNT_GETARGS) {
    161 		char buf[1024];
    162 		(void)snprintb(buf, sizeof(buf), NTFS_MFLAG_BITS, args.flag);
    163 		printf("uid=%d, gid=%d, mode=0%o, flags=%s\n", args.uid,
    164 		    args.gid, args.mode, buf);
    165 	}
    166 	exit (0);
    167 }
    168 
    169 static void
    170 usage(void)
    171 {
    172 	fprintf(stderr, "usage: %s [-a] [-i] [-u user] [-g group] [-m mask] "
    173 	    "bdev dir\n", getprogname());
    174 	exit(EX_USAGE);
    175 }
    176