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