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 <locale.h> 47 #include <mntopts.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 /* must be after stdio to declare fparseln */ 51 #include <libutil.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <sysexits.h> 55 #include <unistd.h> 56 57 /* 58 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd 59 * want for "-u", "-g", "-m", "-M", "-L", and "-D". 60 */ 61 static struct mntopt mopts[] = { 62 MOPT_STDOPTS, 63 MOPT_FORCE, 64 MOPT_SYNC, 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 args.magic = MSDOSFS_ARGSMAGIC; 92 93 while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) { 94 switch (c) { 95 case 's': 96 args.flags |= MSDOSFSMNT_SHORTNAME; 97 break; 98 case 'l': 99 args.flags |= MSDOSFSMNT_LONGNAME; 100 break; 101 case '9': 102 args.flags |= MSDOSFSMNT_NOWIN95; 103 break; 104 case 'u': 105 args.uid = a_uid(optarg); 106 set_uid = 1; 107 break; 108 case 'g': 109 args.gid = a_gid(optarg); 110 set_gid = 1; 111 break; 112 case 'm': 113 args.mask = a_mask(optarg); 114 set_mask = 1; 115 break; 116 case 'M': 117 args.dirmask = a_mask(optarg); 118 set_dirmask = 1; 119 break; 120 case 'L': 121 if (setlocale(LC_CTYPE, optarg) == NULL) 122 err(EX_CONFIG, "%s", optarg); 123 csp = strchr(optarg,'.'); 124 if (!csp) 125 err(EX_CONFIG, "%s", optarg); 126 quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT); 127 cs_local = strdup(quirk); 128 args.flags |= MSDOSFSMNT_KICONV; 129 break; 130 case 'D': 131 csp = optarg; 132 cs_dos = strdup(optarg); 133 args.flags |= MSDOSFSMNT_KICONV; 134 break; 135 case 'o': 136 getmntopts(optarg, mopts, &mntflags, &args.flags); 137 break; 138 case '?': 139 default: 140 usage(); 141 break; 142 } 143 } 144 145 if (optind + 2 != argc) 146 usage(); 147 148 if (set_mask && !set_dirmask) { 149 args.dirmask = args.mask; 150 set_dirmask = 1; 151 } 152 else if (set_dirmask && !set_mask) { 153 args.mask = args.dirmask; 154 set_mask = 1; 155 } 156 157 dev = argv[optind]; 158 dir = argv[optind + 1]; 159 160 /* 161 * Resolve the mountpoint with realpath(3) and remove unnecessary 162 * slashes from the devicename if there are any. 163 */ 164 checkpath(dir, mntpath); 165 rmslashes(dev, dev); 166 167 args.fspec = dev; 168 args.export.ex_root = -2; /* unchecked anyway on DOS fs */ 169 170 if (cs_local != NULL) { 171 if (set_charset(&args, cs_local, cs_dos) == -1) 172 err(EX_OSERR, "msdos_iconv"); 173 } else if (cs_dos != NULL) { 174 if (set_charset(&args, "ISO8859-1", cs_dos) == -1) 175 err(EX_OSERR, "msdos_iconv"); 176 } 177 178 if (mntflags & MNT_RDONLY) 179 args.export.ex_flags = MNT_EXRDONLY; 180 else 181 args.export.ex_flags = 0; 182 if (!set_gid || !set_uid || !set_mask) { 183 if (stat(mntpath, &sb) == -1) 184 err(EX_OSERR, "stat %s", mntpath); 185 186 if (!set_uid) 187 args.uid = sb.st_uid; 188 if (!set_gid) 189 args.gid = sb.st_gid; 190 if (!set_mask) 191 args.mask = args.dirmask = 192 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 193 } 194 195 error = getvfsbyname("msdos", &vfc); 196 if (error && vfsisloadable("msdos")) { 197 if (vfsload("msdos")) 198 err(EX_OSERR, "vfsload(msdos)"); 199 endvfsent(); /* clear cache */ 200 error = getvfsbyname("msdos", &vfc); 201 } 202 if (error) 203 errx(EX_OSERR, "msdos filesystem is not available"); 204 205 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0) 206 err(EX_OSERR, "%s", dev); 207 208 exit (0); 209 } 210 211 static gid_t 212 a_gid(char *s) 213 { 214 struct group *gr; 215 char *gname; 216 gid_t gid; 217 218 if ((gr = getgrnam(s)) != NULL) 219 gid = gr->gr_gid; 220 else { 221 for (gname = s; *s && isdigit(*s); ++s); 222 if (!*s) 223 gid = atoi(gname); 224 else 225 errx(EX_NOUSER, "unknown group id: %s", gname); 226 } 227 return (gid); 228 } 229 230 static uid_t 231 a_uid(char *s) 232 { 233 struct passwd *pw; 234 char *uname; 235 uid_t uid; 236 237 if ((pw = getpwnam(s)) != NULL) 238 uid = pw->pw_uid; 239 else { 240 for (uname = s; *s && isdigit(*s); ++s); 241 if (!*s) 242 uid = atoi(uname); 243 else 244 errx(EX_NOUSER, "unknown user id: %s", uname); 245 } 246 return (uid); 247 } 248 249 static mode_t 250 a_mask(char *s) 251 { 252 int done, rv; 253 char *ep; 254 255 done = 0; 256 rv = -1; 257 if (*s >= '0' && *s <= '7') { 258 done = 1; 259 rv = strtol(optarg, &ep, 8); 260 } 261 if (!done || rv < 0 || *ep) 262 errx(EX_USAGE, "invalid file mode: %s", s); 263 return (rv); 264 } 265 266 static void 267 usage(void) 268 { 269 fprintf(stderr, "%s\n%s\n%s\n", 270 "usage: mount_msdos [-9ls] [-D DOS_codepage] [-g gid] [-L locale]", 271 " [-M mask] [-m mask] [-o options] [-u uid]", 272 " special node"); 273 exit(EX_USAGE); 274 } 275 276 int 277 set_charset(struct msdosfs_args *args, const char *cs_local, const char *cs_dos) 278 { 279 int error; 280 if (modfind("msdos_iconv") < 0) { 281 if (kldload("msdos_iconv") < 0 || modfind("msdos_iconv") < 0) { 282 warnx("cannot find or load \"msdos_iconv\" kernel module"); 283 return (-1); 284 } 285 } 286 snprintf(args->cs_local, ICONV_CSNMAXLEN, "%s", cs_local); 287 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local); 288 if (error) 289 return (-1); 290 if (!cs_dos) 291 cs_dos = strdup("CP437"); 292 snprintf(args->cs_dos, ICONV_CSNMAXLEN, "%s", cs_dos); 293 error = kiconv_add_xlat16_cspairs(cs_dos, cs_local); 294 if (error) 295 return (-1); 296 return (0); 297 } 298