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 * $DragonFly: src/sbin/mount_ntfs/mount_ntfs.c,v 1.2 2003/06/17 04:27:33 dillon Exp $ 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 #include <sys/param.h> 38 #define NTFS 39 #include <sys/mount.h> 40 #include <sys/stat.h> 41 #include <ntfs/ntfsmount.h> 42 #include <ctype.h> 43 #include <err.h> 44 #include <grp.h> 45 #include <pwd.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <sysexits.h> 50 #include <unistd.h> 51 #include <libutil.h> 52 53 #include "mntopts.h" 54 55 static struct mntopt mopts[] = { 56 MOPT_STDOPTS, 57 { NULL } 58 }; 59 60 static gid_t a_gid __P((char *)); 61 static uid_t a_uid __P((char *)); 62 static mode_t a_mask __P((char *)); 63 static void usage __P((void)) __dead2; 64 65 static void load_u2wtable __P((struct ntfs_args *, char *)); 66 67 int 68 main(argc, argv) 69 int argc; 70 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 77 struct vfsconf vfc; 78 #else 79 struct vfsconf *vfc; 80 #endif 81 82 mntflags = set_gid = set_uid = set_mask = 0; 83 (void)memset(&args, '\0', sizeof(args)); 84 85 while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) != -1) { 86 switch (c) { 87 case 'u': 88 args.uid = a_uid(optarg); 89 set_uid = 1; 90 break; 91 case 'g': 92 args.gid = a_gid(optarg); 93 set_gid = 1; 94 break; 95 case 'm': 96 args.mode = a_mask(optarg); 97 set_mask = 1; 98 break; 99 case 'i': 100 args.flag |= NTFS_MFLAG_CASEINS; 101 break; 102 case 'a': 103 args.flag |= NTFS_MFLAG_ALLNAMES; 104 break; 105 case 'o': 106 getmntopts(optarg, mopts, &mntflags, 0); 107 break; 108 case 'W': 109 load_u2wtable(&args, optarg); 110 args.flag |= NTFSMNT_U2WTABLE; 111 break; 112 case '?': 113 default: 114 usage(); 115 break; 116 } 117 } 118 119 if (optind + 2 != argc) 120 usage(); 121 122 dev = argv[optind]; 123 dir = argv[optind + 1]; 124 125 /* 126 * Resolve the mountpoint with realpath(3) and remove unnecessary 127 * slashes from the devicename if there are any. 128 */ 129 (void)checkpath(dir, mntpath); 130 (void)rmslashes(dev, dev); 131 132 args.fspec = dev; 133 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */ 134 if (mntflags & MNT_RDONLY) 135 args.export.ex_flags = MNT_EXRDONLY; 136 else 137 args.export.ex_flags = 0; 138 if (!set_gid || !set_uid || !set_mask) { 139 if (stat(mntpath, &sb) == -1) 140 err(EX_OSERR, "stat %s", mntpath); 141 142 if (!set_uid) 143 args.uid = sb.st_uid; 144 if (!set_gid) 145 args.gid = sb.st_gid; 146 if (!set_mask) 147 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 148 } 149 150 #if __FreeBSD_version >= 300000 151 error = getvfsbyname("ntfs", &vfc); 152 if(error && vfsisloadable("ntfs")) { 153 if(vfsload("ntfs")) 154 #else 155 vfc = getvfsbyname("ntfs"); 156 if(!vfc && vfsisloadable("ntfs")) { 157 if(vfsload("ntfs")) 158 #endif 159 err(EX_OSERR, "vfsload(ntfs)"); 160 endvfsent(); /* clear cache */ 161 #if __FreeBSD_version >= 300000 162 error = getvfsbyname("ntfs", &vfc); 163 #else 164 vfc = getvfsbyname("ntfs"); 165 #endif 166 } 167 #if __FreeBSD_version >= 300000 168 if (error) 169 #else 170 if (!vfc) 171 #endif 172 errx(EX_OSERR, "ntfs filesystem is not available"); 173 174 #if __FreeBSD_version >= 300000 175 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0) 176 #else 177 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0) 178 #endif 179 err(EX_OSERR, "%s", dev); 180 181 exit (0); 182 } 183 184 gid_t 185 a_gid(s) 186 char *s; 187 { 188 struct group *gr; 189 char *gname; 190 gid_t gid; 191 192 if ((gr = getgrnam(s)) != NULL) 193 gid = gr->gr_gid; 194 else { 195 for (gname = s; *s && isdigit(*s); ++s); 196 if (!*s) 197 gid = atoi(gname); 198 else 199 errx(EX_NOUSER, "unknown group id: %s", gname); 200 } 201 return (gid); 202 } 203 204 uid_t 205 a_uid(s) 206 char *s; 207 { 208 struct passwd *pw; 209 char *uname; 210 uid_t uid; 211 212 if ((pw = getpwnam(s)) != NULL) 213 uid = pw->pw_uid; 214 else { 215 for (uname = s; *s && isdigit(*s); ++s); 216 if (!*s) 217 uid = atoi(uname); 218 else 219 errx(EX_NOUSER, "unknown user id: %s", uname); 220 } 221 return (uid); 222 } 223 224 mode_t 225 a_mask(s) 226 char *s; 227 { 228 int done, rv=0; 229 char *ep; 230 231 done = 0; 232 if (*s >= '0' && *s <= '7') { 233 done = 1; 234 rv = strtol(optarg, &ep, 8); 235 } 236 if (!done || rv < 0 || *ep) 237 errx(EX_USAGE, "invalid file mode: %s", s); 238 return (rv); 239 } 240 241 void 242 usage() 243 { 244 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n"); 245 exit(EX_USAGE); 246 } 247 248 void 249 load_u2wtable (pargs, name) 250 struct ntfs_args *pargs; 251 char *name; 252 { 253 FILE *f; 254 int i, j, code[8]; 255 size_t line = 0; 256 char buf[128]; 257 char *fn, *s, *p; 258 259 if (*name == '/') 260 fn = name; 261 else { 262 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name); 263 buf[127] = '\0'; 264 fn = buf; 265 } 266 if ((f = fopen(fn, "r")) == NULL) 267 err(EX_NOINPUT, "%s", fn); 268 p = NULL; 269 for (i = 0; i < 16; i++) { 270 do { 271 if (p != NULL) free(p); 272 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 273 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line); 274 while (isspace((unsigned char)*s)) 275 s++; 276 } while (*s == '\0'); 277 if (sscanf(s, "%i%i%i%i%i%i%i%i", 278 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 279 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line); 280 for (j = 0; j < 8; j++) 281 pargs->u2w[i * 8 + j] = code[j]; 282 } 283 for (i = 0; i < 16; i++) { 284 do { 285 free(p); 286 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 287 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line); 288 while (isspace((unsigned char)*s)) 289 s++; 290 } while (*s == '\0'); 291 if (sscanf(s, "%i%i%i%i%i%i%i%i", 292 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 293 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line); 294 for (j = 0; j < 8; j++) 295 /* pargs->d2u[i * 8 + j] = code[j] */; 296 } 297 for (i = 0; i < 16; i++) { 298 do { 299 free(p); 300 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 301 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line); 302 while (isspace((unsigned char)*s)) 303 s++; 304 } while (*s == '\0'); 305 if (sscanf(s, "%i%i%i%i%i%i%i%i", 306 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 307 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line); 308 for (j = 0; j < 8; j++) 309 /* pargs->u2d[i * 8 + j] = code[j] */; 310 } 311 free(p); 312 fclose(f); 313 } 314 315