mount_msdosfs.c revision 152362
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 34static const char rcsid[] = 35 "$FreeBSD: head/sbin/mount_msdosfs/mount_msdosfs.c 152362 2005-11-13 03:24:44Z rodrigc $"; 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 60static struct mntopt mopts[] = { 61 MOPT_STDOPTS, 62 MOPT_FORCE, 63 MOPT_SYNC, 64 MOPT_UPDATE, 65 MOPT_END 66}; 67 68static gid_t a_gid(char *); 69static uid_t a_uid(char *); 70static mode_t a_mask(char *); 71static void usage(void) __dead2; 72static int set_charset(struct iovec *iov, int *iovlen, const char *, const char *); 73 74int 75main(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:")) != -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", NULL, (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 } 130 break; 131 case 'D': 132 cs_dos = strdup(optarg); 133 build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 134 break; 135 case 'o': { 136 char *p = NULL; 137 char *val = strdup(""); 138 getmntopts(optarg, mopts, &mntflags, &optflags); 139 p = strchr(optarg, '='); 140 if (p != NULL) { 141 free(val); 142 *p = '\0'; 143 val = p + 1; 144 } 145 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 146 } 147 break; 148 case 'W': 149 if (strcmp(optarg, "iso22dos") == 0) { 150 cs_local = strdup("ISO8859-2"); 151 cs_dos = strdup("CP852"); 152 } else if (strcmp(optarg, "iso72dos") == 0) { 153 cs_local = strdup("ISO8859-7"); 154 cs_dos = strdup("CP737"); 155 } else if (strcmp(optarg, "koi2dos") == 0) { 156 cs_local = strdup("KOI8-R"); 157 cs_dos = strdup("CP866"); 158 } else if (strcmp(optarg, "koi8u2dos") == 0) { 159 cs_local = strdup("KOI8-U"); 160 cs_dos = strdup("CP866"); 161 } else { 162 err(EX_NOINPUT, "%s", optarg); 163 } 164 build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1); 165 build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 166 break; 167 case '?': 168 default: 169 usage(); 170 break; 171 } 172 } 173 174 if (optind + 2 != argc) 175 usage(); 176 177 if (set_mask && !set_dirmask) { 178 dirmask = mask; 179 set_dirmask = 1; 180 } 181 else if (set_dirmask && !set_mask) { 182 mask = dirmask; 183 set_mask = 1; 184 } 185 186 dev = argv[optind]; 187 dir = argv[optind + 1]; 188 189 if (cs_local != NULL) { 190 if (set_charset(iov, &iovlen, cs_local, cs_dos) == -1) 191 err(EX_OSERR, "msdosfs_iconv"); 192 build_iovec_argf(&iov, &iovlen, "kiconv", ""); 193 } else if (cs_dos != NULL) { 194 build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1"); 195 if (set_charset(iov, &iovlen, "ISO8859-1", cs_dos) == -1) 196 err(EX_OSERR, "msdosfs_iconv"); 197 build_iovec_argf(&iov, &iovlen, "kiconv", ""); 198 } 199 200 /* 201 * Resolve the mountpoint with realpath(3) and remove unnecessary 202 * slashes from the devicename if there are any. 203 */ 204 (void)checkpath(dir, mntpath); 205 (void)rmslashes(dev, dev); 206 207 if (!set_gid || !set_uid || !set_mask) { 208 if (stat(mntpath, &sb) == -1) 209 err(EX_OSERR, "stat %s", mntpath); 210 211 if (!set_uid) 212 uid = sb.st_uid; 213 if (!set_gid) 214 gid = sb.st_gid; 215 if (!set_mask) 216 mask = dirmask = 217 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 218 } 219 220 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 221 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 222 build_iovec(&iov, &iovlen, "from", dev, (size_t)-1); 223 build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); 224 build_iovec_argf(&iov, &iovlen, "gid", "%u", gid); 225 build_iovec_argf(&iov, &iovlen, "mask", "%u", mask); 226 build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask); 227 228 if (nmount(iov, iovlen, mntflags) < 0) 229 err(1, "%s", dev); 230 231 exit (0); 232} 233 234gid_t 235a_gid(s) 236 char *s; 237{ 238 struct group *gr; 239 char *gname; 240 gid_t gid; 241 242 if ((gr = getgrnam(s)) != NULL) 243 gid = gr->gr_gid; 244 else { 245 for (gname = s; *s && isdigit(*s); ++s); 246 if (!*s) 247 gid = atoi(gname); 248 else 249 errx(EX_NOUSER, "unknown group id: %s", gname); 250 } 251 return (gid); 252} 253 254uid_t 255a_uid(s) 256 char *s; 257{ 258 struct passwd *pw; 259 char *uname; 260 uid_t uid; 261 262 if ((pw = getpwnam(s)) != NULL) 263 uid = pw->pw_uid; 264 else { 265 for (uname = s; *s && isdigit(*s); ++s); 266 if (!*s) 267 uid = atoi(uname); 268 else 269 errx(EX_NOUSER, "unknown user id: %s", uname); 270 } 271 return (uid); 272} 273 274mode_t 275a_mask(s) 276 char *s; 277{ 278 int done, rv; 279 char *ep; 280 281 done = 0; 282 rv = -1; 283 if (*s >= '0' && *s <= '7') { 284 done = 1; 285 rv = strtol(optarg, &ep, 8); 286 } 287 if (!done || rv < 0 || *ep) 288 errx(EX_USAGE, "invalid file mode: %s", s); 289 return (rv); 290} 291 292void 293usage() 294{ 295 fprintf(stderr, "%s\n%s\n%s\n", 296 "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]", 297 " [-M mask] [-m mask] [-o options] [-u uid]", 298 " [-W table] special node"); 299 exit(EX_USAGE); 300} 301 302int 303set_charset(struct iovec *iov, int *iovlen, const char *cs_local, const char *cs_dos) 304{ 305 int error; 306 307 if (modfind("msdosfs_iconv") < 0) 308 if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) { 309 warnx("cannot find or load \"msdosfs_iconv\" kernel module"); 310 return (-1); 311 } 312 313 build_iovec_argf(&iov, iovlen, "cs_win", ENCODING_UNICODE); 314 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local); 315 if (error) 316 return (-1); 317 if (cs_dos != NULL) { 318 error = kiconv_add_xlat16_cspairs(cs_dos, cs_local); 319 if (error) 320 return (-1); 321 } else { 322 build_iovec_argf(&iov, iovlen, "cs_dos", cs_local); 323 error = kiconv_add_xlat16_cspair(cs_local, cs_local, 324 KICONV_FROM_UPPER | KICONV_LOWER); 325 if (error) 326 return (-1); 327 } 328 329 return (0); 330} 331