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