Home | History | Annotate | Line # | Download | only in mount_udf
mount_udf.c revision 1.1
      1 /* $NetBSD $ */
      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.1 2006/02/02 15:21:29 reinoud Exp $");
     40 #endif /* not lint */
     41 
     42 
     43 /* main includes; strip me! */
     44 #include <sys/cdefs.h>
     45 #include <sys/param.h>
     46 #include <sys/mount.h>
     47 #include <sys/stat.h>
     48 #include <ctype.h>
     49 #include <err.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 #include <assert.h>
     59 #include <pwd.h>
     60 #include <errno.h>
     61 #include <grp.h>
     62 
     63 
     64 /* mount specific options */
     65 #include <fs/udf/udf_mount.h>
     66 #include <mntopts.h>
     67 #include <fattr.h>
     68 
     69 
     70 /* options to pass on to the `mount' call */
     71 static const struct mntopt mopts[] = {
     72 	MOPT_STDOPTS,		/* `normal' options	*/
     73 	MOPT_ASYNC,		/* default		*/
     74 	MOPT_UPDATE,		/* not yet supported	*/
     75 	MOPT_GETARGS,		/* printing		*/
     76 	{ NULL }
     77 };
     78 
     79 
     80 /* prototypes */
     81 int		main(int, char *[]);
     82 int		mount_udf(int argc, char **argv);
     83 static void	usage(void);
     84 
     85 
     86 /* code */
     87 
     88 static void
     89 usage()
     90 {
     91 	fprintf(stderr, "usage: %s [-g gid] [-o options] [-s session] "
     92 			"[-t gmtoff] [-u uid] special node\n", getprogname());
     93 	exit(EXIT_FAILURE);
     94 }
     95 
     96 
     97 /* copied from mount_msdos; is it nessisary still? */
     98 #ifndef MOUNT_NOMAIN
     99 int
    100 main(int argc, char **argv)
    101 {
    102 	return mount_udf(argc, argv);
    103 }
    104 #endif
    105 
    106 
    107 /* main routine */
    108 int
    109 mount_udf(int argc, char **argv)
    110 {
    111 	struct udf_args args;
    112 	struct tm *tm;
    113 	struct passwd *passwd;
    114 	struct group  *group;
    115 	time_t	 now;
    116 	uid_t	 anon_uid, nobody_uid;
    117 	gid_t	 anon_gid, nobody_gid;
    118 	char	*dev, *dir;
    119 	int	 ch, mntflags, set_gmtoff;
    120 	uint32_t sector_size;
    121 
    122 	/* set program name for error messages */
    123 	setprogname(argv[0]);
    124 
    125 	/* initialise */
    126 	memset(&args, 0, sizeof(args));
    127 	args.version = UDFMNT_VERSION;
    128 
    129 	set_gmtoff = mntflags = 0;
    130 	sector_size = 0;
    131 
    132 	/* get nobody */
    133 	anon_uid = anon_gid = (uid_t)0;	/* shutup gcc  */
    134 	passwd = getpwnam("nobody");
    135 	group  = getgrnam("nobody");
    136 
    137 	if (passwd && group) {
    138 		anon_uid = passwd->pw_uid;
    139 		anon_gid = group->gr_gid;
    140 	} else {
    141 		errx(EXIT_FAILURE, "mount_udf: failed to get nobody:nobody\n");
    142 	};
    143 
    144 	nobody_uid = anon_uid;
    145 	nobody_gid = anon_gid;
    146 
    147 	/* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */
    148 	assert(nobody_uid != 0);
    149 	assert(nobody_gid != 0);
    150 
    151 	while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) {
    152 		switch (ch) {
    153 #ifdef notyet
    154 		case 'c' :
    155 			args.udfmflags |= UDFMNT_CLOSESESSION;
    156 			break;
    157 #endif
    158 		case 'g' :
    159 			/* convert groupname or numeric equiv. */
    160 			group = getgrnam(optarg);
    161 			if (group == NULL)
    162 				group  = getgrgid((gid_t) atoi(optarg));
    163 			if (group == NULL)
    164 				usage();
    165 
    166 			anon_gid = group->gr_gid;
    167 			break;
    168 		case 'u' :
    169 			/* convert username or numeric equiv. */
    170 			passwd = getpwnam(optarg);
    171 			if (passwd == NULL)
    172 				passwd = getpwuid((uid_t) atoi(optarg));
    173 			if (passwd == NULL)
    174 				usage();
    175 
    176 			anon_uid = passwd->pw_uid;
    177 			break;
    178 		case 'o' :
    179 			/* process generic mount options */
    180 			getmntopts(optarg, mopts, &mntflags, 0);
    181 			break;
    182 		case 's' :
    183 			args.sessionnr = atoi(optarg);
    184 			break;
    185 		case 't' :
    186 			args.gmtoff = atoi(optarg);
    187 			set_gmtoff  = 1;
    188 			break;
    189 		default  :
    190 			usage();
    191 		};
    192 	};
    193 
    194 	if (optind + 2 != argc)
    195 		usage();
    196 
    197 	if (!set_gmtoff) {
    198 		/* use user's time zone as default */
    199 		time(&now);
    200 		tm = localtime(&now);
    201 		args.gmtoff = tm->tm_gmtoff;
    202 	}
    203 
    204 	/* get device and directory specifier */
    205 	dev = argv[optind];
    206 	dir = argv[optind + 1];
    207 	args.fspec = dev;
    208 	args.anon_uid    = anon_uid;
    209 	args.anon_gid    = anon_gid;
    210 	args.nobody_uid  = nobody_uid;
    211 	args.nobody_gid  = nobody_gid;
    212 	args.sector_size = sector_size;		/* invalid */
    213 
    214 	/* mount it! :) */
    215 	if (mount(MOUNT_UDF, dir, mntflags, &args) < 0)
    216 		err(1, "%s on %s", dev, dir);
    217 
    218 	if (mntflags & MNT_GETARGS) {
    219 		char buf[1024];
    220 
    221 		snprintb(buf, sizeof(buf), UDFMNT_BITS, args.udfmflags);
    222 		printf("gmtoffset=%d, sessionnr=%d, flags=%s\n",
    223 			args.gmtoff, args.sessionnr, buf
    224 		);
    225 	}
    226 
    227 	exit(EXIT_SUCCESS);
    228 }
    229 
    230