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