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