xref: /netbsd-src/sbin/mount_msdos/mount_msdos.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /* $NetBSD: mount_msdos.c,v 1.37 2005/09/23 12:10:35 jmmv 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 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  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: mount_msdos.c,v 1.37 2005/09/23 12:10:35 jmmv Exp $");
40 #endif /* not lint */
41 
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/stat.h>
46 #include <msdosfs/msdosfsmount.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <grp.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56 #include <util.h>
57 
58 #include <mntopts.h>
59 #include <fattr.h>
60 
61 static const struct mntopt mopts[] = {
62 	MOPT_STDOPTS,
63 	MOPT_ASYNC,
64 	MOPT_SYNC,
65 	MOPT_UPDATE,
66 	MOPT_GETARGS,
67 	{ NULL }
68 };
69 
70 int	mount_msdos(int argc, char **argv);
71 static void	usage(void) __attribute__((__noreturn__));
72 
73 #ifndef MOUNT_NOMAIN
74 int
75 main(int argc, char **argv)
76 {
77 	return mount_msdos(argc, argv);
78 }
79 #endif
80 
81 int
82 mount_msdos(int argc, char **argv)
83 {
84 	struct msdosfs_args args;
85 	struct stat sb;
86 	int c, mntflags, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
87 	char *dev, *dir, canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
88 	time_t now;
89 	struct tm *tm;
90 
91 	mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
92 	(void)memset(&args, '\0', sizeof(args));
93 
94 	while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
95 		switch (c) {
96 		case 'G':
97 			args.flags |= MSDOSFSMNT_GEMDOSFS;
98 			break;
99 		case 's':
100 			args.flags |= MSDOSFSMNT_SHORTNAME;
101 			break;
102 		case 'l':
103 			args.flags |= MSDOSFSMNT_LONGNAME;
104 			break;
105 		case '9':
106 			args.flags |= MSDOSFSMNT_NOWIN95;
107 			break;
108 		case 'u':
109 			args.uid = a_uid(optarg);
110 			set_uid = 1;
111 			break;
112 		case 'g':
113 			args.gid = a_gid(optarg);
114 			set_gid = 1;
115 			break;
116 		case 'm':
117 			args.mask = a_mask(optarg);
118 			set_mask = 1;
119 			break;
120 		case 'M':
121 			args.dirmask = a_mask(optarg);
122 			set_dirmask = 1;
123 			break;
124 		case 'o':
125 			getmntopts(optarg, mopts, &mntflags, 0);
126 			break;
127 		case 't':
128 			args.gmtoff = atoi(optarg);
129 			set_gmtoff = 1;
130 			break;
131 		case '?':
132 		default:
133 			usage();
134 			break;
135 		}
136 	}
137 
138 	if (optind + 2 != argc)
139 		usage();
140 
141 	if (set_mask && !set_dirmask) {
142 		args.dirmask = args.mask;
143 		set_dirmask = 1;
144 	} else if (set_dirmask && !set_mask) {
145 		args.mask = args.dirmask;
146 		set_mask = 1;
147 	}
148 
149 	dev = argv[optind];
150 	dir = argv[optind + 1];
151 
152 	if (realpath(dev, canon_dev) == NULL)        /* Check device path */
153 		err(1, "realpath %s", dev);
154 	if (strncmp(dev, canon_dev, MAXPATHLEN)) {
155 		warnx("\"%s\" is a relative path.", dev);
156 		dev = canon_dev;
157 		warnx("using \"%s\" instead.", dev);
158 	}
159 
160 	if (realpath(dir, canon_dir) == NULL)        /* Check mounton path */
161 		err(1, "realpath %s", dir);
162 	if (strncmp(dir, canon_dir, MAXPATHLEN)) {
163 		warnx("\"%s\" is a relative path.", dir);
164 		dir = canon_dir;
165 		warnx("using \"%s\" instead.", dir);
166 	}
167 
168 	args.fspec = dev;
169 	if (!set_gid || !set_uid || !set_mask) {
170 		if (stat(dir, &sb) == -1)
171 			err(1, "stat %s", dir);
172 
173 		if (!set_uid)
174 			args.uid = sb.st_uid;
175 		if (!set_gid)
176 			args.gid = sb.st_gid;
177 		if (!set_mask) {
178 			args.mask = args.dirmask =
179 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
180 		}
181 	}
182 
183 	if (!set_gmtoff) {
184 		/* use user's time zone as default */
185 		time(&now);
186 		tm = localtime(&now);
187 		args.gmtoff = tm->tm_gmtoff;
188 
189 	}
190 	args.flags |= MSDOSFSMNT_VERSIONED;
191 	args.version = MSDOSFSMNT_VERSION;
192 
193 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
194 		err(1, "%s on %s", dev, dir);
195 
196 	if (mntflags & MNT_GETARGS) {
197 		char buf[1024];
198 		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
199 		printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
200 		    args.uid, args.gid, args.mask, args.dirmask, buf);
201 	}
202 
203 	exit (0);
204 }
205 
206 static void
207 usage(void)
208 {
209 
210 	fprintf(stderr, "usage: mount_msdos [-9Gls] [-g gid] [-M mask] [-m mask] [-o options]\n"
211 			"\t[-t gmtoff] [-u uid] special node\n");
212 	exit(1);
213 }
214