mount_udf.c revision 1.3 1 1.3 xtraeme /* $NetBSD: mount_udf.c,v 1.3 2006/02/02 16:25:46 xtraeme Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 2006 Reinoud Zandijk
5 1.1 reinoud * All rights reserved.
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud * 3. All advertising materials mentioning features or use of this software
16 1.1 reinoud * must display the following acknowledgement:
17 1.1 reinoud * This product includes software developed for the
18 1.1 reinoud * NetBSD Project. See http://www.NetBSD.org/ for
19 1.1 reinoud * information about NetBSD.
20 1.1 reinoud * 4. The name of the author may not be used to endorse or promote products
21 1.1 reinoud * derived from this software without specific prior written permission.
22 1.1 reinoud *
23 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 reinoud *
34 1.1 reinoud */
35 1.1 reinoud
36 1.1 reinoud
37 1.1 reinoud #include <sys/cdefs.h>
38 1.1 reinoud #ifndef lint
39 1.3 xtraeme __RCSID("$NetBSD: mount_udf.c,v 1.3 2006/02/02 16:25:46 xtraeme Exp $");
40 1.1 reinoud #endif /* not lint */
41 1.1 reinoud
42 1.1 reinoud
43 1.1 reinoud /* main includes; strip me! */
44 1.1 reinoud #include <sys/cdefs.h>
45 1.1 reinoud #include <sys/param.h>
46 1.1 reinoud #include <sys/mount.h>
47 1.1 reinoud #include <sys/stat.h>
48 1.1 reinoud #include <ctype.h>
49 1.1 reinoud #include <err.h>
50 1.1 reinoud #include <grp.h>
51 1.1 reinoud #include <pwd.h>
52 1.1 reinoud #include <stdio.h>
53 1.1 reinoud #include <stdlib.h>
54 1.1 reinoud #include <string.h>
55 1.1 reinoud #include <time.h>
56 1.1 reinoud #include <unistd.h>
57 1.1 reinoud #include <util.h>
58 1.1 reinoud #include <assert.h>
59 1.1 reinoud #include <pwd.h>
60 1.1 reinoud #include <errno.h>
61 1.1 reinoud #include <grp.h>
62 1.1 reinoud
63 1.1 reinoud
64 1.1 reinoud /* mount specific options */
65 1.1 reinoud #include <fs/udf/udf_mount.h>
66 1.1 reinoud #include <mntopts.h>
67 1.1 reinoud #include <fattr.h>
68 1.1 reinoud
69 1.1 reinoud
70 1.1 reinoud /* options to pass on to the `mount' call */
71 1.1 reinoud static const struct mntopt mopts[] = {
72 1.1 reinoud MOPT_STDOPTS, /* `normal' options */
73 1.1 reinoud MOPT_ASYNC, /* default */
74 1.1 reinoud MOPT_UPDATE, /* not yet supported */
75 1.1 reinoud MOPT_GETARGS, /* printing */
76 1.1 reinoud { NULL }
77 1.1 reinoud };
78 1.1 reinoud
79 1.1 reinoud
80 1.1 reinoud /* prototypes */
81 1.1 reinoud int mount_udf(int argc, char **argv);
82 1.1 reinoud static void usage(void);
83 1.1 reinoud
84 1.1 reinoud
85 1.1 reinoud /* code */
86 1.1 reinoud
87 1.1 reinoud static void
88 1.1 reinoud usage()
89 1.1 reinoud {
90 1.3 xtraeme (void)fprintf(stderr, "usage: %s [-g gid] [-o options] [-s session] "
91 1.1 reinoud "[-t gmtoff] [-u uid] special node\n", getprogname());
92 1.1 reinoud exit(EXIT_FAILURE);
93 1.1 reinoud }
94 1.1 reinoud
95 1.1 reinoud
96 1.1 reinoud /* copied from mount_msdos; is it nessisary still? */
97 1.1 reinoud #ifndef MOUNT_NOMAIN
98 1.1 reinoud int
99 1.1 reinoud main(int argc, char **argv)
100 1.1 reinoud {
101 1.1 reinoud return mount_udf(argc, argv);
102 1.1 reinoud }
103 1.1 reinoud #endif
104 1.1 reinoud
105 1.1 reinoud
106 1.1 reinoud /* main routine */
107 1.1 reinoud int
108 1.1 reinoud mount_udf(int argc, char **argv)
109 1.1 reinoud {
110 1.1 reinoud struct udf_args args;
111 1.1 reinoud struct tm *tm;
112 1.1 reinoud struct passwd *passwd;
113 1.1 reinoud struct group *group;
114 1.1 reinoud time_t now;
115 1.1 reinoud uid_t anon_uid, nobody_uid;
116 1.1 reinoud gid_t anon_gid, nobody_gid;
117 1.1 reinoud char *dev, *dir;
118 1.1 reinoud int ch, mntflags, set_gmtoff;
119 1.1 reinoud uint32_t sector_size;
120 1.1 reinoud
121 1.1 reinoud /* set program name for error messages */
122 1.1 reinoud setprogname(argv[0]);
123 1.1 reinoud
124 1.1 reinoud /* initialise */
125 1.1 reinoud memset(&args, 0, sizeof(args));
126 1.1 reinoud args.version = UDFMNT_VERSION;
127 1.1 reinoud
128 1.1 reinoud set_gmtoff = mntflags = 0;
129 1.1 reinoud sector_size = 0;
130 1.1 reinoud
131 1.1 reinoud /* get nobody */
132 1.1 reinoud anon_uid = anon_gid = (uid_t)0; /* shutup gcc */
133 1.1 reinoud passwd = getpwnam("nobody");
134 1.1 reinoud group = getgrnam("nobody");
135 1.1 reinoud
136 1.1 reinoud if (passwd && group) {
137 1.1 reinoud anon_uid = passwd->pw_uid;
138 1.1 reinoud anon_gid = group->gr_gid;
139 1.3 xtraeme } else
140 1.1 reinoud errx(EXIT_FAILURE, "mount_udf: failed to get nobody:nobody\n");
141 1.1 reinoud
142 1.1 reinoud nobody_uid = anon_uid;
143 1.1 reinoud nobody_gid = anon_gid;
144 1.1 reinoud
145 1.1 reinoud /* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */
146 1.1 reinoud assert(nobody_uid != 0);
147 1.1 reinoud assert(nobody_gid != 0);
148 1.1 reinoud
149 1.1 reinoud while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) {
150 1.1 reinoud switch (ch) {
151 1.1 reinoud #ifdef notyet
152 1.1 reinoud case 'c' :
153 1.1 reinoud args.udfmflags |= UDFMNT_CLOSESESSION;
154 1.1 reinoud break;
155 1.1 reinoud #endif
156 1.1 reinoud case 'g' :
157 1.1 reinoud /* convert groupname or numeric equiv. */
158 1.1 reinoud group = getgrnam(optarg);
159 1.1 reinoud if (group == NULL)
160 1.1 reinoud group = getgrgid((gid_t) atoi(optarg));
161 1.1 reinoud if (group == NULL)
162 1.1 reinoud usage();
163 1.1 reinoud
164 1.1 reinoud anon_gid = group->gr_gid;
165 1.1 reinoud break;
166 1.1 reinoud case 'u' :
167 1.1 reinoud /* convert username or numeric equiv. */
168 1.1 reinoud passwd = getpwnam(optarg);
169 1.1 reinoud if (passwd == NULL)
170 1.1 reinoud passwd = getpwuid((uid_t) atoi(optarg));
171 1.1 reinoud if (passwd == NULL)
172 1.1 reinoud usage();
173 1.1 reinoud
174 1.1 reinoud anon_uid = passwd->pw_uid;
175 1.1 reinoud break;
176 1.1 reinoud case 'o' :
177 1.1 reinoud /* process generic mount options */
178 1.1 reinoud getmntopts(optarg, mopts, &mntflags, 0);
179 1.1 reinoud break;
180 1.1 reinoud case 's' :
181 1.1 reinoud args.sessionnr = atoi(optarg);
182 1.1 reinoud break;
183 1.1 reinoud case 't' :
184 1.1 reinoud args.gmtoff = atoi(optarg);
185 1.1 reinoud set_gmtoff = 1;
186 1.1 reinoud break;
187 1.1 reinoud default :
188 1.1 reinoud usage();
189 1.3 xtraeme /* NOTREACHED */
190 1.3 xtraeme }
191 1.3 xtraeme }
192 1.1 reinoud
193 1.1 reinoud if (optind + 2 != argc)
194 1.1 reinoud usage();
195 1.1 reinoud
196 1.1 reinoud if (!set_gmtoff) {
197 1.1 reinoud /* use user's time zone as default */
198 1.1 reinoud time(&now);
199 1.1 reinoud tm = localtime(&now);
200 1.1 reinoud args.gmtoff = tm->tm_gmtoff;
201 1.1 reinoud }
202 1.1 reinoud
203 1.1 reinoud /* get device and directory specifier */
204 1.1 reinoud dev = argv[optind];
205 1.1 reinoud dir = argv[optind + 1];
206 1.1 reinoud args.fspec = dev;
207 1.1 reinoud args.anon_uid = anon_uid;
208 1.1 reinoud args.anon_gid = anon_gid;
209 1.1 reinoud args.nobody_uid = nobody_uid;
210 1.1 reinoud args.nobody_gid = nobody_gid;
211 1.1 reinoud args.sector_size = sector_size; /* invalid */
212 1.1 reinoud
213 1.1 reinoud /* mount it! :) */
214 1.1 reinoud if (mount(MOUNT_UDF, dir, mntflags, &args) < 0)
215 1.1 reinoud err(1, "%s on %s", dev, dir);
216 1.1 reinoud
217 1.1 reinoud if (mntflags & MNT_GETARGS) {
218 1.1 reinoud char buf[1024];
219 1.1 reinoud
220 1.1 reinoud snprintb(buf, sizeof(buf), UDFMNT_BITS, args.udfmflags);
221 1.1 reinoud printf("gmtoffset=%d, sessionnr=%d, flags=%s\n",
222 1.3 xtraeme args.gmtoff, args.sessionnr, buf);
223 1.1 reinoud }
224 1.1 reinoud
225 1.1 reinoud exit(EXIT_SUCCESS);
226 1.1 reinoud }
227 1.1 reinoud
228