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