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/param.h> 36 #define NTFS 37 #include <sys/mount.h> 38 #include <sys/module.h> 39 #include <sys/linker.h> 40 #include <sys/iconv.h> 41 #include <sys/stat.h> 42 #include <vfs/ntfs/ntfsmount.h> 43 #include <ctype.h> 44 #include <err.h> 45 #include <grp.h> 46 #include <mntopts.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 <libutil.h> 54 55 static struct mntopt mopts[] = { 56 MOPT_STDOPTS, 57 MOPT_NULL 58 }; 59 60 static gid_t a_gid(char *); 61 static uid_t a_uid(char *); 62 static mode_t a_mask(char *); 63 static void usage(void) __dead2; 64 65 int 66 set_charset(struct ntfs_args *pargs, const char *cs_local); 67 68 int 69 main(int argc, char **argv) 70 { 71 struct ntfs_args args; 72 struct stat sb; 73 int c, mntflags, set_gid, set_uid, set_mask, error; 74 char *dev, *dir, mntpath[MAXPATHLEN]; 75 struct vfsconf vfc; 76 const char *quirk; 77 char *cs_local = NULL; 78 79 mntflags = set_gid = set_uid = set_mask = 0; 80 memset(&args, '\0', sizeof(args)); 81 82 while ((c = getopt(argc, argv, "aiu:g:m:o:C:")) != -1) { 83 switch (c) { 84 case 'u': 85 args.uid = a_uid(optarg); 86 set_uid = 1; 87 break; 88 case 'g': 89 args.gid = a_gid(optarg); 90 set_gid = 1; 91 break; 92 case 'm': 93 args.mode = a_mask(optarg); 94 set_mask = 1; 95 break; 96 case 'i': 97 args.flag |= NTFS_MFLAG_CASEINS; 98 break; 99 case 'a': 100 args.flag |= NTFS_MFLAG_ALLNAMES; 101 break; 102 case 'o': 103 getmntopts(optarg, mopts, &mntflags, 0); 104 break; 105 case 'C': 106 quirk = kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT); 107 cs_local = strdup(quirk); 108 if (set_charset(&args, cs_local) == -1) 109 err(EX_OSERR, "ntfs_iconv"); 110 args.flag |= NTFS_MFLAG_KICONV; 111 mntflags |= MNT_RDONLY; 112 break; 113 case '?': 114 default: 115 usage(); 116 break; 117 } 118 } 119 120 if (optind + 2 != argc) 121 usage(); 122 123 dev = argv[optind]; 124 dir = argv[optind + 1]; 125 126 /* 127 * Resolve the mountpoint with realpath(3) and remove unnecessary 128 * slashes from the devicename if there are any. 129 */ 130 checkpath(dir, mntpath); 131 rmslashes(dev, dev); 132 133 args.fspec = dev; 134 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */ 135 if (mntflags & MNT_RDONLY) 136 args.export.ex_flags = MNT_EXRDONLY; 137 else 138 args.export.ex_flags = 0; 139 if (!set_gid || !set_uid || !set_mask) { 140 if (stat(mntpath, &sb) == -1) 141 err(EX_OSERR, "stat %s", mntpath); 142 143 if (!set_uid) 144 args.uid = sb.st_uid; 145 if (!set_gid) 146 args.gid = sb.st_gid; 147 if (!set_mask) 148 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 149 } 150 151 error = getvfsbyname("ntfs", &vfc); 152 if(error && vfsisloadable("ntfs")) { 153 if(vfsload("ntfs")) 154 err(EX_OSERR, "vfsload(ntfs)"); 155 endvfsent(); /* clear cache */ 156 error = getvfsbyname("ntfs", &vfc); 157 } 158 if (error) 159 errx(EX_OSERR, "ntfs filesystem is not available"); 160 161 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0) 162 err(EX_OSERR, "%s", dev); 163 164 exit (0); 165 } 166 167 gid_t 168 a_gid(char *s) 169 { 170 struct group *gr; 171 char *gname; 172 gid_t gid; 173 174 if ((gr = getgrnam(s)) != NULL) 175 gid = gr->gr_gid; 176 else { 177 for (gname = s; *s && isdigit(*s); ++s); 178 if (!*s) 179 gid = atoi(gname); 180 else 181 errx(EX_NOUSER, "unknown group id: %s", gname); 182 } 183 return (gid); 184 } 185 186 uid_t 187 a_uid(char *s) 188 { 189 struct passwd *pw; 190 char *uname; 191 uid_t uid; 192 193 if ((pw = getpwnam(s)) != NULL) 194 uid = pw->pw_uid; 195 else { 196 for (uname = s; *s && isdigit(*s); ++s); 197 if (!*s) 198 uid = atoi(uname); 199 else 200 errx(EX_NOUSER, "unknown user id: %s", uname); 201 } 202 return (uid); 203 } 204 205 mode_t 206 a_mask(char *s) 207 { 208 int done, rv=0; 209 char *ep; 210 211 done = 0; 212 if (*s >= '0' && *s <= '7') { 213 done = 1; 214 rv = strtol(optarg, &ep, 8); 215 } 216 if (!done || rv < 0 || *ep) 217 errx(EX_USAGE, "invalid file mode: %s", s); 218 return (rv); 219 } 220 221 void 222 usage(void) 223 { 224 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n"); 225 exit(EX_USAGE); 226 } 227 228 int 229 set_charset(struct ntfs_args *pargs, const char *cs_local) 230 { 231 int error; 232 233 if (modfind("ntfs_iconv") < 0) 234 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) { 235 warnx( "cannot find or load \"ntfs_iconv\" kernel module"); 236 return (-1); 237 } 238 strncpy(pargs->cs_ntfs, ENCODING_UNICODE, ICONV_CSNMAXLEN); 239 strncpy(pargs->cs_local, cs_local, ICONV_CSNMAXLEN); 240 error = kiconv_add_xlat16_cspairs(pargs->cs_ntfs, cs_local); 241 if (error) 242 return (-1); 243 244 return (0); 245 } 246