133549Sjkh/* $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $ */ 233549Sjkh 3330449Seadler/*- 4330449Seadler * SPDX-License-Identifier: BSD-4-Clause 5330449Seadler * 62892Sdfr * Copyright (c) 1994 Christopher G. Demetriou 72892Sdfr * All rights reserved. 82892Sdfr * 92892Sdfr * Redistribution and use in source and binary forms, with or without 102892Sdfr * modification, are permitted provided that the following conditions 112892Sdfr * are met: 122892Sdfr * 1. Redistributions of source code must retain the above copyright 132892Sdfr * notice, this list of conditions and the following disclaimer. 142892Sdfr * 2. Redistributions in binary form must reproduce the above copyright 152892Sdfr * notice, this list of conditions and the following disclaimer in the 162892Sdfr * documentation and/or other materials provided with the distribution. 172892Sdfr * 3. All advertising materials mentioning features or use of this software 182892Sdfr * must display the following acknowledgement: 192892Sdfr * This product includes software developed by Christopher G. Demetriou. 202892Sdfr * 4. The name of the author may not be used to endorse or promote products 212892Sdfr * derived from this software without specific prior written permission 222892Sdfr * 232892Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 242892Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 252892Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 262892Sdfr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 272892Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 282892Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 292892Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 302892Sdfr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 312892Sdfr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 322892Sdfr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 332892Sdfr */ 342892Sdfr 352892Sdfr#ifndef lint 3615771Swollmanstatic const char rcsid[] = 3750476Speter "$FreeBSD: stable/11/sbin/mount_msdosfs/mount_msdosfs.c 340953 2018-11-26 11:22:04Z eugen $"; 382892Sdfr#endif /* not lint */ 392892Sdfr 402892Sdfr#include <sys/param.h> 412892Sdfr#include <sys/mount.h> 422892Sdfr#include <sys/stat.h> 43120492Sfjoe#include <sys/iconv.h> 44121363Strhodes#include <sys/linker.h> 45121429Strhodes#include <sys/module.h> 4623335Sbde 472892Sdfr#include <ctype.h> 482892Sdfr#include <err.h> 49340953Seugen#include <errno.h> 502892Sdfr#include <grp.h> 5133761Sache#include <locale.h> 522892Sdfr#include <pwd.h> 532892Sdfr#include <stdio.h> 5455613Sache/* must be after stdio to declare fparseln */ 5555613Sache#include <libutil.h> 562892Sdfr#include <stdlib.h> 572892Sdfr#include <string.h> 5815771Swollman#include <sysexits.h> 592892Sdfr#include <unistd.h> 60121429Strhodes 612892Sdfr#include "mntopts.h" 622892Sdfr 6392882Simpstatic gid_t a_gid(char *); 6492882Simpstatic uid_t a_uid(char *); 6592882Simpstatic mode_t a_mask(char *); 6692882Simpstatic void usage(void) __dead2; 67152809Savatarstatic int set_charset(struct iovec **iov, int *iovlen, const char *, const char *); 682892Sdfr 692892Sdfrint 70121373Strhodesmain(int argc, char **argv) 712892Sdfr{ 72152416Smaxim struct iovec *iov = NULL; 73152416Smaxim int iovlen = 0; 742892Sdfr struct stat sb; 75247856Sjkim int c, set_gid, set_uid, set_mask, set_dirmask; 76120492Sfjoe char *dev, *dir, mntpath[MAXPATHLEN], *csp; 77152362Srodrigc char fstype[] = "msdosfs"; 78166326Srodrigc char errmsg[255] = {0}; 79152362Srodrigc char *cs_dos = NULL; 80152362Srodrigc char *cs_local = NULL; 81152362Srodrigc mode_t mask = 0, dirmask = 0; 82152362Srodrigc uid_t uid = 0; 83152362Srodrigc gid_t gid = 0; 842892Sdfr 85247856Sjkim set_gid = set_uid = set_mask = set_dirmask = 0; 862892Sdfr 87152731Savatar while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) { 882892Sdfr switch (c) { 8933549Sjkh case 's': 90152362Srodrigc build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1); 9133549Sjkh break; 9233549Sjkh case 'l': 93152362Srodrigc build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1); 9433549Sjkh break; 9533549Sjkh case '9': 96152731Savatar build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1); 9733549Sjkh break; 982892Sdfr case 'u': 99152362Srodrigc uid = a_uid(optarg); 1002892Sdfr set_uid = 1; 1012892Sdfr break; 1022892Sdfr case 'g': 103152362Srodrigc gid = a_gid(optarg); 1042892Sdfr set_gid = 1; 1052892Sdfr break; 1062892Sdfr case 'm': 107152362Srodrigc mask = a_mask(optarg); 1082892Sdfr set_mask = 1; 1092892Sdfr break; 110118837Strhodes case 'M': 111152362Srodrigc dirmask = a_mask(optarg); 112118837Strhodes set_dirmask = 1; 113118837Strhodes break; 114152362Srodrigc case 'L': { 115152362Srodrigc const char *quirk = NULL; 116120492Sfjoe if (setlocale(LC_CTYPE, optarg) == NULL) 117120492Sfjoe err(EX_CONFIG, "%s", optarg); 118120492Sfjoe csp = strchr(optarg,'.'); 119120492Sfjoe if (!csp) 120120492Sfjoe err(EX_CONFIG, "%s", optarg); 121152362Srodrigc quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT); 122152362Srodrigc build_iovec_argf(&iov, &iovlen, "cs_local", quirk); 123152974Savatar cs_local = strdup(quirk); 124152362Srodrigc } 12533761Sache break; 126120492Sfjoe case 'D': 127152362Srodrigc cs_dos = strdup(optarg); 128152362Srodrigc build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 12933749Sache break; 130152362Srodrigc case 'o': { 131152362Srodrigc char *p = NULL; 132152362Srodrigc char *val = strdup(""); 133152416Smaxim p = strchr(optarg, '='); 134152416Smaxim if (p != NULL) { 135152362Srodrigc free(val); 136152362Srodrigc *p = '\0'; 137152362Srodrigc val = p + 1; 138152362Srodrigc } 139152362Srodrigc build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 140152362Srodrigc } 1412892Sdfr break; 142120492Sfjoe case 'W': 143120492Sfjoe if (strcmp(optarg, "iso22dos") == 0) { 144152362Srodrigc cs_local = strdup("ISO8859-2"); 145152362Srodrigc cs_dos = strdup("CP852"); 146120492Sfjoe } else if (strcmp(optarg, "iso72dos") == 0) { 147152362Srodrigc cs_local = strdup("ISO8859-7"); 148152362Srodrigc cs_dos = strdup("CP737"); 149120492Sfjoe } else if (strcmp(optarg, "koi2dos") == 0) { 150152362Srodrigc cs_local = strdup("KOI8-R"); 151152362Srodrigc cs_dos = strdup("CP866"); 152120492Sfjoe } else if (strcmp(optarg, "koi8u2dos") == 0) { 153152362Srodrigc cs_local = strdup("KOI8-U"); 154152362Srodrigc cs_dos = strdup("CP866"); 155120492Sfjoe } else { 156120492Sfjoe err(EX_NOINPUT, "%s", optarg); 157120492Sfjoe } 158152362Srodrigc build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1); 159152362Srodrigc build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 160120492Sfjoe break; 1612892Sdfr case '?': 1622892Sdfr default: 1632892Sdfr usage(); 1642892Sdfr break; 1652892Sdfr } 1662892Sdfr } 1672892Sdfr 1682892Sdfr if (optind + 2 != argc) 1692892Sdfr usage(); 1702892Sdfr 171118837Strhodes if (set_mask && !set_dirmask) { 172152362Srodrigc dirmask = mask; 173118837Strhodes set_dirmask = 1; 174118837Strhodes } 175118837Strhodes else if (set_dirmask && !set_mask) { 176152362Srodrigc mask = dirmask; 177118837Strhodes set_mask = 1; 178118837Strhodes } 179118837Strhodes 1802892Sdfr dev = argv[optind]; 1812892Sdfr dir = argv[optind + 1]; 1822892Sdfr 183152362Srodrigc if (cs_local != NULL) { 184152809Savatar if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1) 185120492Sfjoe err(EX_OSERR, "msdosfs_iconv"); 186152362Srodrigc build_iovec_argf(&iov, &iovlen, "kiconv", ""); 187152362Srodrigc } else if (cs_dos != NULL) { 188152362Srodrigc build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1"); 189152809Savatar if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1) 190120492Sfjoe err(EX_OSERR, "msdosfs_iconv"); 191152362Srodrigc build_iovec_argf(&iov, &iovlen, "kiconv", ""); 192120492Sfjoe } 193120492Sfjoe 19452055Sphk /* 19552055Sphk * Resolve the mountpoint with realpath(3) and remove unnecessary 19652055Sphk * slashes from the devicename if there are any. 19752055Sphk */ 198230226Sjh if (checkpath(dir, mntpath) != 0) 199230226Sjh err(EX_USAGE, "%s", mntpath); 20052055Sphk (void)rmslashes(dev, dev); 20152055Sphk 2022892Sdfr if (!set_gid || !set_uid || !set_mask) { 20352055Sphk if (stat(mntpath, &sb) == -1) 20452055Sphk err(EX_OSERR, "stat %s", mntpath); 2052892Sdfr 2062892Sdfr if (!set_uid) 207152362Srodrigc uid = sb.st_uid; 2082892Sdfr if (!set_gid) 209152362Srodrigc gid = sb.st_gid; 2102892Sdfr if (!set_mask) 211152362Srodrigc mask = dirmask = 212118837Strhodes sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 2132892Sdfr } 2142892Sdfr 215152362Srodrigc build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 216152362Srodrigc build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 217152362Srodrigc build_iovec(&iov, &iovlen, "from", dev, (size_t)-1); 218166326Srodrigc build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 219152362Srodrigc build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); 220152362Srodrigc build_iovec_argf(&iov, &iovlen, "gid", "%u", gid); 221152362Srodrigc build_iovec_argf(&iov, &iovlen, "mask", "%u", mask); 222152362Srodrigc build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask); 2232892Sdfr 224247856Sjkim if (nmount(iov, iovlen, 0) < 0) { 225185422Simp if (errmsg[0]) 226185422Simp err(1, "%s: %s", dev, errmsg); 227185422Simp else 228185422Simp err(1, "%s", dev); 229185422Simp } 230152362Srodrigc 2312892Sdfr exit (0); 2322892Sdfr} 2332892Sdfr 2342892Sdfrgid_t 235201227Seda_gid(char *s) 2362892Sdfr{ 2372892Sdfr struct group *gr; 2382892Sdfr char *gname; 2392892Sdfr gid_t gid; 2402892Sdfr 2412892Sdfr if ((gr = getgrnam(s)) != NULL) 2422892Sdfr gid = gr->gr_gid; 2432892Sdfr else { 2442892Sdfr for (gname = s; *s && isdigit(*s); ++s); 2452892Sdfr if (!*s) 2462892Sdfr gid = atoi(gname); 2472892Sdfr else 24815771Swollman errx(EX_NOUSER, "unknown group id: %s", gname); 2492892Sdfr } 2502892Sdfr return (gid); 2512892Sdfr} 2522892Sdfr 2532892Sdfruid_t 254201227Seda_uid(char *s) 2552892Sdfr{ 2562892Sdfr struct passwd *pw; 2572892Sdfr char *uname; 2582892Sdfr uid_t uid; 2592892Sdfr 2602892Sdfr if ((pw = getpwnam(s)) != NULL) 2612892Sdfr uid = pw->pw_uid; 2622892Sdfr else { 2632892Sdfr for (uname = s; *s && isdigit(*s); ++s); 2642892Sdfr if (!*s) 2652892Sdfr uid = atoi(uname); 2662892Sdfr else 26715771Swollman errx(EX_NOUSER, "unknown user id: %s", uname); 2682892Sdfr } 2692892Sdfr return (uid); 2702892Sdfr} 2712892Sdfr 2722892Sdfrmode_t 273201227Seda_mask(char *s) 2742892Sdfr{ 2752892Sdfr int done, rv; 2762892Sdfr char *ep; 2772892Sdfr 2782892Sdfr done = 0; 27933549Sjkh rv = -1; 2802892Sdfr if (*s >= '0' && *s <= '7') { 2812892Sdfr done = 1; 2822892Sdfr rv = strtol(optarg, &ep, 8); 2832892Sdfr } 2842892Sdfr if (!done || rv < 0 || *ep) 28515771Swollman errx(EX_USAGE, "invalid file mode: %s", s); 2862892Sdfr return (rv); 2872892Sdfr} 2882892Sdfr 2892892Sdfrvoid 290201227Sedusage(void) 2912892Sdfr{ 292121007Sfjoe fprintf(stderr, "%s\n%s\n%s\n", 293121429Strhodes "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]", 294121429Strhodes " [-M mask] [-m mask] [-o options] [-u uid]", 295121429Strhodes " [-W table] special node"); 29615771Swollman exit(EX_USAGE); 2972892Sdfr} 29833749Sache 299120492Sfjoeint 300152809Savatarset_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos) 30133749Sache{ 302120492Sfjoe int error; 30333749Sache 304120492Sfjoe if (modfind("msdosfs_iconv") < 0) 305120492Sfjoe if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) { 306134565Strhodes warnx("cannot find or load \"msdosfs_iconv\" kernel module"); 307120492Sfjoe return (-1); 308120492Sfjoe } 309120492Sfjoe 310152809Savatar build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE); 311152362Srodrigc error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local); 312340953Seugen if (error && errno != EEXIST) 313120492Sfjoe return (-1); 314152362Srodrigc if (cs_dos != NULL) { 315152362Srodrigc error = kiconv_add_xlat16_cspairs(cs_dos, cs_local); 316340953Seugen if (error && errno != EEXIST) 317120492Sfjoe return (-1); 318120492Sfjoe } else { 319152809Savatar build_iovec_argf(iov, iovlen, "cs_dos", cs_local); 320152362Srodrigc error = kiconv_add_xlat16_cspair(cs_local, cs_local, 321120492Sfjoe KICONV_FROM_UPPER | KICONV_LOWER); 322340953Seugen if (error && errno != EEXIST) 323120492Sfjoe return (-1); 32433749Sache } 32533761Sache 326120492Sfjoe return (0); 32733761Sache} 328