mount_ntfs.c revision 204404
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: head/sbin/mount_ntfs/mount_ntfs.c 204404 2010-02-27 10:16:14Z uqs $ 32 * 33 */ 34 35#include <sys/cdefs.h> 36#include <sys/param.h> 37#define NTFS 38#include <sys/mount.h> 39#include <sys/stat.h> 40#include <sys/module.h> 41#include <sys/iconv.h> 42#include <sys/linker.h> 43#include <fs/ntfs/ntfsmount.h> 44#include <ctype.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 <libutil.h> 54 55#include "mntopts.h" 56 57#define TRANSITION_PERIOD_HACK 58 59static struct mntopt mopts[] = { 60 MOPT_STDOPTS, 61 MOPT_END 62}; 63 64static gid_t a_gid(char *); 65static uid_t a_uid(char *); 66static mode_t a_mask(char *); 67static void usage(void) __dead2; 68 69static int set_charset(struct ntfs_args *); 70 71int 72main(int argc, char *argv[]) 73{ 74 struct ntfs_args args; 75 struct stat sb; 76 int c, mntflags, set_gid, set_uid, set_mask; 77 char *dev, *dir, mntpath[MAXPATHLEN]; 78 79 mntflags = set_gid = set_uid = set_mask = 0; 80 (void)memset(&args, '\0', sizeof(args)); 81 args.cs_ntfs = NULL; 82 args.cs_local = NULL; 83 84#ifdef TRANSITION_PERIOD_HACK 85 while ((c = getopt(argc, argv, "aiu:g:m:o:C:W:")) != -1) { 86#else 87 while ((c = getopt(argc, argv, "aiu:g:m:o:C:")) != -1) { 88#endif 89 switch (c) { 90 case 'u': 91 args.uid = a_uid(optarg); 92 set_uid = 1; 93 break; 94 case 'g': 95 args.gid = a_gid(optarg); 96 set_gid = 1; 97 break; 98 case 'm': 99 args.mode = a_mask(optarg); 100 set_mask = 1; 101 break; 102 case 'i': 103 args.flag |= NTFS_MFLAG_CASEINS; 104 break; 105 case 'a': 106 args.flag |= NTFS_MFLAG_ALLNAMES; 107 break; 108 case 'o': 109 getmntopts(optarg, mopts, &mntflags, 0); 110 break; 111 case 'C': 112 args.cs_local = malloc(ICONV_CSNMAXLEN); 113 if (args.cs_local == NULL) 114 err(EX_OSERR, "malloc()"); 115 strncpy(args.cs_local, 116 kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT), 117 ICONV_CSNMAXLEN); 118 break; 119#ifdef TRANSITION_PERIOD_HACK 120 case 'W': 121 args.cs_local = malloc(ICONV_CSNMAXLEN); 122 if (args.cs_local == NULL) 123 err(EX_OSERR, "malloc()"); 124 if (strcmp(optarg, "iso22dos") == 0) { 125 strcpy(args.cs_local, "ISO8859-2"); 126 } else if (strcmp(optarg, "iso72dos") == 0) { 127 strcpy(args.cs_local, "ISO8859-7"); 128 } else if (strcmp(optarg, "koi2dos") == 0) { 129 strcpy(args.cs_local, "KOI8-R"); 130 } else if (strcmp(optarg, "koi8u2dos") == 0) { 131 strcpy(args.cs_local, "KOI8-U"); 132 } else { 133 err(EX_NOINPUT, "%s", optarg); 134 } 135 break; 136#endif /* TRANSITION_PERIOD_HACK */ 137 case '?': 138 default: 139 usage(); 140 break; 141 } 142 } 143 144 if (optind + 2 != argc) 145 usage(); 146 147 dev = argv[optind]; 148 dir = argv[optind + 1]; 149 150 if (args.cs_local) { 151 if (set_charset(&args) == -1) 152 err(EX_OSERR, "ntfs_iconv"); 153 args.flag |= NTFS_MFLAG_KICONV; 154 /* 155 * XXX 156 * Force to be MNT_RDONLY, 157 * since only reading is supported right now, 158 */ 159 mntflags |= MNT_RDONLY; 160 } 161 162 /* 163 * Resolve the mountpoint with realpath(3) and remove unnecessary 164 * slashes from the devicename if there are any. 165 */ 166 (void)checkpath(dir, mntpath); 167 (void)rmslashes(dev, dev); 168 169 args.fspec = dev; 170 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */ 171 if (mntflags & MNT_RDONLY) 172 args.export.ex_flags = MNT_EXRDONLY; 173 else 174 args.export.ex_flags = 0; 175 if (!set_gid || !set_uid || !set_mask) { 176 if (stat(mntpath, &sb) == -1) 177 err(EX_OSERR, "stat %s", mntpath); 178 179 if (!set_uid) 180 args.uid = sb.st_uid; 181 if (!set_gid) 182 args.gid = sb.st_gid; 183 if (!set_mask) 184 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 185 } 186 187 if (mount("ntfs", mntpath, mntflags, &args) < 0) 188 err(EX_OSERR, "%s", dev); 189 190 exit (0); 191} 192 193gid_t 194a_gid(char *s) 195{ 196 struct group *gr; 197 char *gname; 198 gid_t gid; 199 200 if ((gr = getgrnam(s)) != NULL) 201 gid = gr->gr_gid; 202 else { 203 for (gname = s; *s && isdigit(*s); ++s); 204 if (!*s) 205 gid = atoi(gname); 206 else 207 errx(EX_NOUSER, "unknown group id: %s", gname); 208 } 209 return (gid); 210} 211 212uid_t 213a_uid(char *s) 214{ 215 struct passwd *pw; 216 char *uname; 217 uid_t uid; 218 219 if ((pw = getpwnam(s)) != NULL) 220 uid = pw->pw_uid; 221 else { 222 for (uname = s; *s && isdigit(*s); ++s); 223 if (!*s) 224 uid = atoi(uname); 225 else 226 errx(EX_NOUSER, "unknown user id: %s", uname); 227 } 228 return (uid); 229} 230 231mode_t 232a_mask(char *s) 233{ 234 int done, rv=0; 235 char *ep; 236 237 done = 0; 238 if (*s >= '0' && *s <= '7') { 239 done = 1; 240 rv = strtol(optarg, &ep, 8); 241 } 242 if (!done || rv < 0 || *ep) 243 errx(EX_USAGE, "invalid file mode: %s", s); 244 return (rv); 245} 246 247void 248usage(void) 249{ 250#ifdef TRANSITION_PERIOD_HACK 251 fprintf(stderr, "%s\n%s\n", 252 "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask]", 253 " [-C charset] [-W u2wtable] special node"); 254#else 255 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] special node\n"); 256#endif 257 exit(EX_USAGE); 258} 259 260int 261set_charset(struct ntfs_args *pargs) 262{ 263 int error; 264 265 if (modfind("ntfs_iconv") < 0) 266 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) { 267 warnx( "cannot find or load \"ntfs_iconv\" kernel module"); 268 return (-1); 269 } 270 271 if ((pargs->cs_ntfs = malloc(ICONV_CSNMAXLEN)) == NULL) 272 return (-1); 273 strncpy(pargs->cs_ntfs, ENCODING_UNICODE, ICONV_CSNMAXLEN); 274 error = kiconv_add_xlat16_cspairs(pargs->cs_ntfs, pargs->cs_local); 275 if (error) 276 return (-1); 277 278 return (0); 279} 280