1/*	$NetBSD$	*/
2
3/*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgment:
23 *      This product includes software developed by the University of
24 *      California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *
42 * File: am-utils/amd/ops_udf.c
43 *
44 */
45
46/*
47 * UDF file system
48 */
49
50#ifdef HAVE_CONFIG_H
51# include <config.h>
52#endif /* HAVE_CONFIG_H */
53#include <am_defs.h>
54#include <amd.h>
55
56/* forward definitions */
57static char *udf_match(am_opts *fo);
58static int udf_mount(am_node *am, mntfs *mf);
59static int udf_umount(am_node *am, mntfs *mf);
60
61/*
62 * Ops structure
63 */
64am_ops udf_ops =
65{
66	"udf",
67	udf_match,
68	0,				/* udf_init */
69	udf_mount,
70	udf_umount,
71	amfs_error_lookup_child,
72	amfs_error_mount_child,
73	amfs_error_readdir,
74	0,				/* udf_readlink */
75	0,				/* udf_mounted */
76	0,				/* udf_umounted */
77	amfs_generic_find_srvr,
78	0,				/* udf_get_wchan */
79	FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
80#ifdef HAVE_FS_AUTOFS
81	AUTOFS_UDF_FS_FLAGS,
82#endif /* HAVE_FS_AUTOFS */
83};
84
85#if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID)
86static int
87a_num(const char *s, const char *id_type)
88{
89	int id;
90	char *ep;
91
92	id = strtol(s, &ep, 0);
93	if (*ep || s == ep || id < 0) {
94		plog(XLOG_ERROR, "mount_udf: unknown %s: %s", id_type, s);
95		return 0;
96	}
97	return id;
98}
99#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
100
101#if defined(HAVE_UDF_ARGS_T_NOBODY_GID)
102static gid_t
103a_gid(const char *s, const char *id_type)
104{
105	struct group *gr;
106
107	if ((gr = getgrnam(s)) != NULL)
108		return gr->gr_gid;
109	return a_num(s, id_type);
110}
111#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) */
112
113#if defined(HAVE_UDF_ARGS_T_NOBODY_UID)
114static uid_t
115a_uid(const char *s, const char *id_type)
116{
117	struct passwd *pw;
118
119	if ((pw = getpwnam(s)) != NULL)
120		return pw->pw_uid;
121	return a_num(s, id_type);
122}
123#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
124
125/*
126 * UDF needs remote filesystem.
127 */
128static char *
129udf_match(am_opts *fo)
130{
131
132	if (!fo->opt_dev) {
133		plog(XLOG_USER, "udf: no source device specified");
134		return 0;
135	}
136	dlog("UDF: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
137
138	/*
139	 * Determine magic cookie to put in mtab
140	 */
141	return strdup(fo->opt_dev);
142}
143
144static int
145mount_udf(char *mntdir, char *fs_name, char *opts, int on_autofs)
146{
147	udf_args_t udf_args;
148	mntent_t mnt;
149	int flags;
150	char *str;
151#if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
152	uid_t uid_nobody;
153	gid_t gid_nobody;
154#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
155	/*
156	 * Figure out the name of the file system type.
157	 */
158	MTYPE_TYPE type = MOUNT_TYPE_UDF;
159
160#if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
161	uid_nobody = a_uid("nobody", "user");
162	if (uid_nobody == 0) {
163		plog(XLOG_ERROR, "mount_udf: invalid uid for nobody");
164		return EPERM;
165	}
166#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
167
168#if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID)
169	gid_nobody = a_gid("nobody", "group");
170	if (gid_nobody == 0) {
171		plog(XLOG_ERROR, "mount_udf: invalid gid for nobody");
172		return EPERM;
173	}
174#endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID) */
175
176	str = NULL;
177	memset((voidp) &udf_args, 0, sizeof(udf_args)); /* Paranoid */
178
179	/*
180	 * Fill in the mount structure
181	 */
182	memset((voidp)&mnt, 0, sizeof(mnt));
183	mnt.mnt_dir = mntdir;
184	mnt.mnt_fsname = fs_name;
185	mnt.mnt_type = MNTTAB_TYPE_UDF;
186	mnt.mnt_opts = opts;
187
188	flags = compute_mount_flags(&mnt);
189
190#ifdef HAVE_UDF_ARGS_T_UDFMFLAGS
191# if defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION)
192	if (amu_hasmntopt(&mnt, MNTTAB_OPT_CLOSESESSION))
193		udf_args.udfmflags |= MNT2_UDF_OPT_CLOSESESSION;
194# endif /* defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION) */
195#endif /* HAVE_UDF_ARGS_T_UDFMFLAGS */
196
197#ifdef HAVE_UDF_ARGS_T_NOBODY_UID
198	udf_args.nobody_uid = uid_nobody;
199#endif /* HAVE_UDF_ARGS_T_NOBODY_UID */
200
201#ifdef HAVE_UDF_ARGS_T_NOBODY_GID
202	udf_args.nobody_gid = gid_nobody;
203#endif /* HAVE_UDF_ARGS_T_NOBODY_GID */
204
205#ifdef HAVE_UDF_ARGS_T_ANON_UID
206	udf_args.anon_uid = uid_nobody;	/* default to nobody */
207	if ((str = hasmntstr(&mnt, MNTTAB_OPT_USER)) != NULL) {
208		udf_args.anon_uid = a_uid(str, MNTTAB_OPT_USER);
209		XFREE(str);
210	}
211#endif /* HAVE_UDF_ARGS_T_ANON_UID */
212
213#ifdef HAVE_UDF_ARGS_T_ANON_GID
214	udf_args.anon_gid = gid_nobody;	/* default to nobody */
215	if ((str = hasmntstr(&mnt, MNTTAB_OPT_GROUP)) != NULL) {
216		udf_args.anon_gid = a_gid(str, MNTTAB_OPT_GROUP);
217		XFREE(str);
218	}
219#endif /* HAVE_UDF_ARGS_T_ANON_GID */
220
221#ifdef HAVE_UDF_ARGS_T_GMTOFF
222	udf_args.gmtoff = 0;
223	if ((str = hasmntstr(&mnt, MNTTAB_OPT_GMTOFF)) != NULL) {
224		udf_args.gmtoff = a_num(str, MNTTAB_OPT_GMTOFF);
225		XFREE(str);
226	}
227#endif /* HAVE_UDF_ARGS_T_GMTOFF */
228
229#ifdef HAVE_UDF_ARGS_T_SESSIONNR
230	udf_args.sessionnr = 0;
231	if ((str = hasmntstr(&mnt, MNTTAB_OPT_SESSIONNR)) != NULL) {
232		udf_args.sessionnr = a_num(str, MNTTAB_OPT_SESSIONNR);
233		XFREE(str);
234	}
235#endif /* HAVE_UDF_ARGS_T_SESSIONNR */
236
237#ifdef HAVE_UDF_ARGS_T_VERSION
238# ifdef UDFMNT_VERSION
239	udf_args.version = UDFMNT_VERSION;
240# endif /* UDFMNT_VERSION */
241#endif /* HAVE_UDF_ARGS_T_VERSION */
242
243#ifdef HAVE_UFS_ARGS_T_FSPEC
244	udf_args.fspec = fs_name;
245#endif /* HAVE_UFS_ARGS_T_FSPEC */
246
247	/*
248	 * Call generic mount routine
249	 */
250	return mount_fs(&mnt, flags, (caddr_t)&udf_args, 0, type, 0, NULL,
251	    mnttab_file_name, on_autofs);
252}
253
254static int
255udf_mount(am_node *am, mntfs *mf)
256{
257	int on_autofs;
258	int error;
259
260	on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
261	error = mount_udf(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
262	if (error) {
263		errno = error;
264		plog(XLOG_ERROR, "mount_udf: %m");
265		return error;
266	}
267	return 0;
268}
269
270
271static int
272udf_umount(am_node *am, mntfs *mf)
273{
274	int unmount_flags;
275
276	unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
277	return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
278}
279