mount_msdosfs.c revision 152362
133965Sjdp/* $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $ */ 278828Sobrien 3218822Sdim/* 438889Sjdp * Copyright (c) 1994 Christopher G. Demetriou 533965Sjdp * All rights reserved. 633965Sjdp * 7130561Sobrien * Redistribution and use in source and binary forms, with or without 833965Sjdp * modification, are permitted provided that the following conditions 9130561Sobrien * are met: 10130561Sobrien * 1. Redistributions of source code must retain the above copyright 11130561Sobrien * notice, this list of conditions and the following disclaimer. 12130561Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1333965Sjdp * notice, this list of conditions and the following disclaimer in the 14130561Sobrien * documentation and/or other materials provided with the distribution. 15130561Sobrien * 3. All advertising materials mentioning features or use of this software 16130561Sobrien * must display the following acknowledgement: 17130561Sobrien * This product includes software developed by Christopher G. Demetriou. 1833965Sjdp * 4. The name of the author may not be used to endorse or promote products 19130561Sobrien * derived from this software without specific prior written permission 20130561Sobrien * 21218822Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2233965Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2333965Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2433965Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2533965Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2633965Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2733965Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2833965Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2933965Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3033965Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3133965Sjdp */ 32130561Sobrien 3389857Sobrien#ifndef lint 34218822Sdimstatic const char rcsid[] = 35218822Sdim "$FreeBSD: head/sbin/mount_msdosfs/mount_msdosfs.c 152362 2005-11-13 03:24:44Z rodrigc $"; 36218822Sdim#endif /* not lint */ 37218822Sdim 38130561Sobrien#include <sys/param.h> 39130561Sobrien#include <sys/mount.h> 40130561Sobrien#include <sys/stat.h> 41218822Sdim#include <sys/iconv.h> 42218822Sdim#include <sys/linker.h> 43218822Sdim#include <sys/module.h> 44218822Sdim 45218822Sdim#include <ctype.h> 46130561Sobrien#include <err.h> 47130561Sobrien#include <grp.h> 48130561Sobrien#include <locale.h> 49218822Sdim#include <pwd.h> 5089857Sobrien#include <stdio.h> 5133965Sjdp/* must be after stdio to declare fparseln */ 5233965Sjdp#include <libutil.h> 5389857Sobrien#include <stdlib.h> 5489857Sobrien#include <string.h> 5589857Sobrien#include <sysexits.h> 5633965Sjdp#include <unistd.h> 57218822Sdim 5889857Sobrien#include "mntopts.h" 5989857Sobrien 60218822Sdimstatic struct mntopt mopts[] = { 6189857Sobrien MOPT_STDOPTS, 6233965Sjdp MOPT_FORCE, 6389857Sobrien MOPT_SYNC, 6433965Sjdp MOPT_UPDATE, 6589857Sobrien MOPT_END 6633965Sjdp}; 6789857Sobrien 6889857Sobrienstatic gid_t a_gid(char *); 69218822Sdimstatic uid_t a_uid(char *); 7089857Sobrienstatic mode_t a_mask(char *); 7189857Sobrienstatic void usage(void) __dead2; 72218822Sdimstatic int set_charset(struct iovec *iov, int *iovlen, const char *, const char *); 7389857Sobrien 7489857Sobrienint 7589857Sobrienmain(int argc, char **argv) 7689857Sobrien{ 7789857Sobrien struct iovec *iov = NULL; 7889857Sobrien int iovlen = 0; 7933965Sjdp struct stat sb; 80218822Sdim int c, mntflags, set_gid, set_uid, set_mask, set_dirmask; 8189857Sobrien int optflags = 0; 8289857Sobrien char *dev, *dir, mntpath[MAXPATHLEN], *csp; 83218822Sdim char fstype[] = "msdosfs"; 8489857Sobrien char *cs_dos = NULL; 8533965Sjdp char *cs_local = NULL; 8689857Sobrien mode_t mask = 0, dirmask = 0; 8733965Sjdp uid_t uid = 0; 8889857Sobrien gid_t gid = 0; 8933965Sjdp getmnt_silent = 1; 9033965Sjdp 9189857Sobrien mntflags = set_gid = set_uid = set_mask = set_dirmask = 0; 9233965Sjdp 9333965Sjdp while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) { 9433965Sjdp switch (c) { 9533965Sjdp case 's': 9633965Sjdp build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1); 9733965Sjdp break; 9833965Sjdp case 'l': 9933965Sjdp build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1); 10033965Sjdp break; 10133965Sjdp case '9': 10233965Sjdp build_iovec_argf(&iov, &iovlen, "nowin95", NULL, (size_t)-1); 10333965Sjdp break; 10433965Sjdp case 'u': 10533965Sjdp uid = a_uid(optarg); 106130561Sobrien set_uid = 1; 10733965Sjdp break; 10833965Sjdp case 'g': 10933965Sjdp gid = a_gid(optarg); 11033965Sjdp set_gid = 1; 11133965Sjdp break; 11233965Sjdp case 'm': 11333965Sjdp mask = a_mask(optarg); 11433965Sjdp set_mask = 1; 11533965Sjdp break; 11633965Sjdp case 'M': 11733965Sjdp dirmask = a_mask(optarg); 11833965Sjdp set_dirmask = 1; 11933965Sjdp break; 12033965Sjdp case 'L': { 12133965Sjdp const char *quirk = NULL; 12233965Sjdp if (setlocale(LC_CTYPE, optarg) == NULL) 12333965Sjdp err(EX_CONFIG, "%s", optarg); 12433965Sjdp csp = strchr(optarg,'.'); 12533965Sjdp if (!csp) 12633965Sjdp err(EX_CONFIG, "%s", optarg); 12733965Sjdp quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT); 12833965Sjdp build_iovec_argf(&iov, &iovlen, "cs_local", quirk); 129218822Sdim } 13033965Sjdp break; 13133965Sjdp case 'D': 13233965Sjdp cs_dos = strdup(optarg); 13333965Sjdp build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 13433965Sjdp break; 13533965Sjdp case 'o': { 13633965Sjdp char *p = NULL; 13733965Sjdp char *val = strdup(""); 13833965Sjdp getmntopts(optarg, mopts, &mntflags, &optflags); 13933965Sjdp p = strchr(optarg, '='); 14033965Sjdp if (p != NULL) { 14133965Sjdp free(val); 14233965Sjdp *p = '\0'; 14333965Sjdp val = p + 1; 14433965Sjdp } 14533965Sjdp build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 14633965Sjdp } 14733965Sjdp break; 14833965Sjdp case 'W': 14933965Sjdp if (strcmp(optarg, "iso22dos") == 0) { 15033965Sjdp cs_local = strdup("ISO8859-2"); 15133965Sjdp cs_dos = strdup("CP852"); 15233965Sjdp } else if (strcmp(optarg, "iso72dos") == 0) { 15333965Sjdp cs_local = strdup("ISO8859-7"); 15433965Sjdp cs_dos = strdup("CP737"); 15533965Sjdp } else if (strcmp(optarg, "koi2dos") == 0) { 15633965Sjdp cs_local = strdup("KOI8-R"); 15733965Sjdp cs_dos = strdup("CP866"); 15833965Sjdp } else if (strcmp(optarg, "koi8u2dos") == 0) { 15933965Sjdp cs_local = strdup("KOI8-U"); 16033965Sjdp cs_dos = strdup("CP866"); 16133965Sjdp } else { 16233965Sjdp err(EX_NOINPUT, "%s", optarg); 16333965Sjdp } 16433965Sjdp build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1); 16533965Sjdp build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 16633965Sjdp break; 16733965Sjdp case '?': 168218822Sdim default: 16933965Sjdp usage(); 17033965Sjdp break; 17133965Sjdp } 17233965Sjdp } 17333965Sjdp 17433965Sjdp if (optind + 2 != argc) 17533965Sjdp usage(); 17633965Sjdp 177130561Sobrien if (set_mask && !set_dirmask) { 178218822Sdim dirmask = mask; 179218822Sdim set_dirmask = 1; 18033965Sjdp } 18133965Sjdp else if (set_dirmask && !set_mask) { 18233965Sjdp mask = dirmask; 183130561Sobrien set_mask = 1; 184218822Sdim } 185218822Sdim 186218822Sdim dev = argv[optind]; 18733965Sjdp dir = argv[optind + 1]; 18833965Sjdp 189130561Sobrien if (cs_local != NULL) { 190218822Sdim if (set_charset(iov, &iovlen, cs_local, cs_dos) == -1) 19133965Sjdp err(EX_OSERR, "msdosfs_iconv"); 19233965Sjdp build_iovec_argf(&iov, &iovlen, "kiconv", ""); 19333965Sjdp } else if (cs_dos != NULL) { 194130561Sobrien build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1"); 195218822Sdim if (set_charset(iov, &iovlen, "ISO8859-1", cs_dos) == -1) 19633965Sjdp err(EX_OSERR, "msdosfs_iconv"); 19733965Sjdp build_iovec_argf(&iov, &iovlen, "kiconv", ""); 19833965Sjdp } 199130561Sobrien 20033965Sjdp /* 201130561Sobrien * Resolve the mountpoint with realpath(3) and remove unnecessary 202218822Sdim * slashes from the devicename if there are any. 203218822Sdim */ 204218822Sdim (void)checkpath(dir, mntpath); 205218822Sdim (void)rmslashes(dev, dev); 20633965Sjdp 20733965Sjdp if (!set_gid || !set_uid || !set_mask) { 20833965Sjdp if (stat(mntpath, &sb) == -1) 209218822Sdim err(EX_OSERR, "stat %s", mntpath); 21033965Sjdp 21133965Sjdp if (!set_uid) 21289857Sobrien uid = sb.st_uid; 21333965Sjdp if (!set_gid) 21433965Sjdp gid = sb.st_gid; 21533965Sjdp if (!set_mask) 21633965Sjdp mask = dirmask = 21733965Sjdp sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 21833965Sjdp } 21933965Sjdp 22033965Sjdp build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 22133965Sjdp build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 222218822Sdim build_iovec(&iov, &iovlen, "from", dev, (size_t)-1); 223218822Sdim build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); 224218822Sdim build_iovec_argf(&iov, &iovlen, "gid", "%u", gid); 225218822Sdim build_iovec_argf(&iov, &iovlen, "mask", "%u", mask); 226218822Sdim build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask); 227218822Sdim 228218822Sdim if (nmount(iov, iovlen, mntflags) < 0) 229218822Sdim err(1, "%s", dev); 230218822Sdim 231218822Sdim exit (0); 232218822Sdim} 233218822Sdim 234218822Sdimgid_t 235218822Sdima_gid(s) 236218822Sdim char *s; 23733965Sjdp{ 23833965Sjdp struct group *gr; 23989857Sobrien char *gname; 240218822Sdim gid_t gid; 241218822Sdim 242218822Sdim if ((gr = getgrnam(s)) != NULL) 243218822Sdim gid = gr->gr_gid; 24433965Sjdp else { 245218822Sdim for (gname = s; *s && isdigit(*s); ++s); 246218822Sdim if (!*s) 247218822Sdim gid = atoi(gname); 24833965Sjdp else 249218822Sdim errx(EX_NOUSER, "unknown group id: %s", gname); 250218822Sdim } 25133965Sjdp return (gid); 25233965Sjdp} 25333965Sjdp 25433965Sjdpuid_t 255130561Sobriena_uid(s) 256130561Sobrien char *s; 257218822Sdim{ 258218822Sdim struct passwd *pw; 25933965Sjdp char *uname; 260218822Sdim uid_t uid; 261218822Sdim 262218822Sdim if ((pw = getpwnam(s)) != NULL) 263218822Sdim uid = pw->pw_uid; 264218822Sdim else { 265218822Sdim for (uname = s; *s && isdigit(*s); ++s); 266218822Sdim if (!*s) 267218822Sdim uid = atoi(uname); 268218822Sdim else 269218822Sdim errx(EX_NOUSER, "unknown user id: %s", uname); 270218822Sdim } 271218822Sdim return (uid); 272218822Sdim} 273218822Sdim 274218822Sdimmode_t 275218822Sdima_mask(s) 276218822Sdim char *s; 277218822Sdim{ 278218822Sdim int done, rv; 279218822Sdim char *ep; 280218822Sdim 281218822Sdim done = 0; 282218822Sdim rv = -1; 283218822Sdim if (*s >= '0' && *s <= '7') { 284218822Sdim done = 1; 285218822Sdim rv = strtol(optarg, &ep, 8); 286218822Sdim } 287218822Sdim if (!done || rv < 0 || *ep) 288218822Sdim errx(EX_USAGE, "invalid file mode: %s", s); 289218822Sdim return (rv); 290218822Sdim} 29177298Sobrien 292218822Sdimvoid 29333965Sjdpusage() 29433965Sjdp{ 29533965Sjdp fprintf(stderr, "%s\n%s\n%s\n", 29633965Sjdp "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]", 29733965Sjdp " [-M mask] [-m mask] [-o options] [-u uid]", 29833965Sjdp " [-W table] special node"); 29933965Sjdp exit(EX_USAGE); 30033965Sjdp} 30133965Sjdp 30233965Sjdpint 30333965Sjdpset_charset(struct iovec *iov, int *iovlen, const char *cs_local, const char *cs_dos) 30433965Sjdp{ 30533965Sjdp int error; 30633965Sjdp 30733965Sjdp if (modfind("msdosfs_iconv") < 0) 30833965Sjdp if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) { 30933965Sjdp warnx("cannot find or load \"msdosfs_iconv\" kernel module"); 31033965Sjdp return (-1); 31133965Sjdp } 31233965Sjdp 31333965Sjdp build_iovec_argf(&iov, iovlen, "cs_win", ENCODING_UNICODE); 31433965Sjdp error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local); 31533965Sjdp if (error) 31633965Sjdp return (-1); 31733965Sjdp if (cs_dos != NULL) { 31889857Sobrien error = kiconv_add_xlat16_cspairs(cs_dos, cs_local); 31933965Sjdp if (error) 32033965Sjdp return (-1); 32133965Sjdp } else { 32233965Sjdp build_iovec_argf(&iov, iovlen, "cs_dos", cs_local); 32333965Sjdp error = kiconv_add_xlat16_cspair(cs_local, cs_local, 32433965Sjdp KICONV_FROM_UPPER | KICONV_LOWER); 32533965Sjdp if (error) 32633965Sjdp return (-1); 32733965Sjdp } 32833965Sjdp 32933965Sjdp return (0); 33033965Sjdp} 33133965Sjdp