1 /* $NetBSD: mount_udf.c,v 1.14 2016/02/21 22:51:30 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Reinoud Zandijk 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 for the 18 * NetBSD Project. See http://www.NetBSD.org/ for 19 * information about NetBSD. 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 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: mount_udf.c,v 1.14 2016/02/21 22:51:30 christos Exp $"); 40 #endif /* not lint */ 41 42 43 #include <sys/param.h> 44 #include <sys/mount.h> 45 #include <sys/stat.h> 46 47 #include <assert.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <grp.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 #include <util.h> 58 59 60 /* mount specific options */ 61 #include <fs/udf/udf_mount.h> 62 #include <mntopts.h> 63 64 #include "mountprog.h" 65 #include "mount_udf.h" 66 67 /* options to pass on to the `mount' call */ 68 static const struct mntopt mopts[] = { 69 MOPT_STDOPTS, /* `normal' options */ 70 MOPT_ASYNC, /* default */ 71 MOPT_NOATIME, /* dont update access times */ 72 MOPT_RELATIME, /* update access times on change*/ 73 MOPT_UPDATE, /* not yet supported */ 74 MOPT_GETARGS, /* printing */ 75 MOPT_NULL, 76 }; 77 78 79 /* prototypes */ 80 static void usage(void) __dead; 81 82 83 /* code */ 84 85 static void 86 usage(void) 87 { 88 (void)fprintf(stderr, "Usage: %s [-g gid] [-o options] [-s session] " 89 "[-t gmtoff] [-u uid] special node\n", getprogname()); 90 exit(EXIT_FAILURE); 91 } 92 93 94 #ifndef MOUNT_NOMAIN 95 int 96 main(int argc, char **argv) 97 { 98 99 setprogname(argv[0]); 100 return mount_udf(argc, argv); 101 } 102 #endif 103 104 105 /* main routine */ 106 void 107 mount_udf_parseargs(int argc, char **argv, 108 struct udf_args *args, int *mntflags, 109 char *canon_dev, char *canon_dir) 110 { 111 struct tm *tm; 112 time_t now; 113 uid_t anon_uid, nobody_uid; 114 gid_t anon_gid, nobody_gid; 115 int ch, set_gmtoff; 116 uint32_t sector_size; 117 mntoptparse_t mp; 118 119 /* initialise */ 120 (void)memset(args, 0, sizeof(*args)); 121 122 set_gmtoff = *mntflags = 0; 123 sector_size = 0; 124 125 /* get nobody */ 126 nobody_uid = anon_uid = a_uid("nobody"); 127 nobody_gid = anon_gid = a_gid("nobody"); 128 129 /* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */ 130 assert(nobody_uid != 0); 131 assert(nobody_gid != 0); 132 133 while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) { 134 switch (ch) { 135 case 'c' : 136 args->udfmflags |= UDFMNT_CLOSESESSION; 137 break; 138 case 'g' : 139 /* convert groupname or numeric equiv. */ 140 anon_gid = a_gid(optarg); 141 break; 142 case 'u' : 143 /* convert username or numeric equiv. */ 144 anon_uid = a_uid(optarg); 145 break; 146 case 'o' : 147 /* process generic mount options */ 148 mp = getmntopts(optarg, mopts, mntflags, 0); 149 if (mp == NULL) 150 err(EXIT_FAILURE, "getmntopts"); 151 freemntopts(mp); 152 break; 153 case 's' : 154 args->sessionnr = a_num(optarg, "session number"); 155 break; 156 case 't' : 157 args->gmtoff = a_num(optarg, "gmtoff"); 158 set_gmtoff = 1; 159 break; 160 default : 161 usage(); 162 /* NOTREACHED */ 163 } 164 } 165 166 if (optind + 2 != argc) 167 usage(); 168 169 if (!set_gmtoff) { 170 /* use user's time zone as default */ 171 (void)time(&now); 172 tm = localtime(&now); 173 args->gmtoff = tm->tm_gmtoff; 174 } 175 176 /* get device and directory specifier */ 177 pathadj(argv[optind], canon_dev); 178 pathadj(argv[optind+1], canon_dir); 179 180 args->version = UDFMNT_VERSION; 181 args->fspec = canon_dev; 182 args->anon_uid = anon_uid; 183 args->anon_gid = anon_gid; 184 args->nobody_uid = nobody_uid; 185 args->nobody_gid = nobody_gid; 186 args->sector_size = sector_size; /* invalid */ 187 } 188 189 int 190 mount_udf(int argc, char *argv[]) 191 { 192 struct udf_args args; 193 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 194 int mntflags; 195 196 mount_udf_parseargs(argc, argv, &args, &mntflags, canon_dev, canon_dir); 197 198 /* mount it! :) */ 199 if (mount(MOUNT_UDF, canon_dir, mntflags, &args, sizeof args) == -1) 200 err(EXIT_FAILURE, "Cannot mount %s on %s", canon_dev,canon_dir); 201 202 if (mntflags & MNT_GETARGS) { 203 char buf[1024]; 204 205 (void)snprintb(buf, sizeof(buf), UDFMNT_BITS, 206 (uint64_t)args.udfmflags); 207 (void)printf("gmtoffset=%d, sessionnr=%d, flags=%s\n", 208 args.gmtoff, args.sessionnr, buf); 209 } 210 211 return EXIT_SUCCESS; 212 } 213