xref: /netbsd-src/external/bsd/am-utils/dist/amd/ops_udf.c (revision 8bae5d409deb915cf7c8f0539fae22ff2cb8a313)
1*8bae5d40Schristos /*	$NetBSD: ops_udf.c,v 1.1.1.2 2015/01/17 16:34:15 christos Exp $	*/
24bcd344eSchristos 
34bcd344eSchristos /*
4*8bae5d40Schristos  * Copyright (c) 1997-2014 Erez Zadok
54bcd344eSchristos  * Copyright (c) 1990 Jan-Simon Pendry
64bcd344eSchristos  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
74bcd344eSchristos  * Copyright (c) 1990 The Regents of the University of California.
84bcd344eSchristos  * All rights reserved.
94bcd344eSchristos  *
104bcd344eSchristos  * This code is derived from software contributed to Berkeley by
114bcd344eSchristos  * Jan-Simon Pendry at Imperial College, London.
124bcd344eSchristos  *
134bcd344eSchristos  * Redistribution and use in source and binary forms, with or without
144bcd344eSchristos  * modification, are permitted provided that the following conditions
154bcd344eSchristos  * are met:
164bcd344eSchristos  * 1. Redistributions of source code must retain the above copyright
174bcd344eSchristos  *    notice, this list of conditions and the following disclaimer.
184bcd344eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
194bcd344eSchristos  *    notice, this list of conditions and the following disclaimer in the
204bcd344eSchristos  *    documentation and/or other materials provided with the distribution.
21*8bae5d40Schristos  * 3. Neither the name of the University nor the names of its contributors
224bcd344eSchristos  *    may be used to endorse or promote products derived from this software
234bcd344eSchristos  *    without specific prior written permission.
244bcd344eSchristos  *
254bcd344eSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264bcd344eSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274bcd344eSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284bcd344eSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294bcd344eSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304bcd344eSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314bcd344eSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324bcd344eSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334bcd344eSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344bcd344eSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354bcd344eSchristos  * SUCH DAMAGE.
364bcd344eSchristos  *
374bcd344eSchristos  *
384bcd344eSchristos  * File: am-utils/amd/ops_udf.c
394bcd344eSchristos  *
404bcd344eSchristos  */
414bcd344eSchristos 
424bcd344eSchristos /*
434bcd344eSchristos  * UDF file system
444bcd344eSchristos  */
454bcd344eSchristos 
464bcd344eSchristos #ifdef HAVE_CONFIG_H
474bcd344eSchristos # include <config.h>
484bcd344eSchristos #endif /* HAVE_CONFIG_H */
494bcd344eSchristos #include <am_defs.h>
504bcd344eSchristos #include <amd.h>
514bcd344eSchristos 
524bcd344eSchristos /* forward definitions */
534bcd344eSchristos static char *udf_match(am_opts *fo);
544bcd344eSchristos static int udf_mount(am_node *am, mntfs *mf);
554bcd344eSchristos static int udf_umount(am_node *am, mntfs *mf);
564bcd344eSchristos 
574bcd344eSchristos /*
584bcd344eSchristos  * Ops structure
594bcd344eSchristos  */
604bcd344eSchristos am_ops udf_ops =
614bcd344eSchristos {
624bcd344eSchristos 	"udf",
634bcd344eSchristos 	udf_match,
644bcd344eSchristos 	0,				/* udf_init */
654bcd344eSchristos 	udf_mount,
664bcd344eSchristos 	udf_umount,
674bcd344eSchristos 	amfs_error_lookup_child,
684bcd344eSchristos 	amfs_error_mount_child,
694bcd344eSchristos 	amfs_error_readdir,
704bcd344eSchristos 	0,				/* udf_readlink */
714bcd344eSchristos 	0,				/* udf_mounted */
724bcd344eSchristos 	0,				/* udf_umounted */
734bcd344eSchristos 	amfs_generic_find_srvr,
744bcd344eSchristos 	0,				/* udf_get_wchan */
754bcd344eSchristos 	FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
764bcd344eSchristos #ifdef HAVE_FS_AUTOFS
774bcd344eSchristos 	AUTOFS_UDF_FS_FLAGS,
784bcd344eSchristos #endif /* HAVE_FS_AUTOFS */
794bcd344eSchristos };
804bcd344eSchristos 
814bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID)
824bcd344eSchristos static int
a_num(const char * s,const char * id_type)834bcd344eSchristos a_num(const char *s, const char *id_type)
844bcd344eSchristos {
854bcd344eSchristos 	int id;
864bcd344eSchristos 	char *ep;
874bcd344eSchristos 
884bcd344eSchristos 	id = strtol(s, &ep, 0);
894bcd344eSchristos 	if (*ep || s == ep || id < 0) {
904bcd344eSchristos 		plog(XLOG_ERROR, "mount_udf: unknown %s: %s", id_type, s);
914bcd344eSchristos 		return 0;
924bcd344eSchristos 	}
934bcd344eSchristos 	return id;
944bcd344eSchristos }
954bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
964bcd344eSchristos 
974bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_GID)
984bcd344eSchristos static gid_t
a_gid(const char * s,const char * id_type)994bcd344eSchristos a_gid(const char *s, const char *id_type)
1004bcd344eSchristos {
1014bcd344eSchristos 	struct group *gr;
1024bcd344eSchristos 
1034bcd344eSchristos 	if ((gr = getgrnam(s)) != NULL)
1044bcd344eSchristos 		return gr->gr_gid;
1054bcd344eSchristos 	return a_num(s, id_type);
1064bcd344eSchristos }
1074bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) */
1084bcd344eSchristos 
1094bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_UID)
1104bcd344eSchristos static uid_t
a_uid(const char * s,const char * id_type)1114bcd344eSchristos a_uid(const char *s, const char *id_type)
1124bcd344eSchristos {
1134bcd344eSchristos 	struct passwd *pw;
1144bcd344eSchristos 
1154bcd344eSchristos 	if ((pw = getpwnam(s)) != NULL)
1164bcd344eSchristos 		return pw->pw_uid;
1174bcd344eSchristos 	return a_num(s, id_type);
1184bcd344eSchristos }
1194bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
1204bcd344eSchristos 
1214bcd344eSchristos /*
1224bcd344eSchristos  * UDF needs remote filesystem.
1234bcd344eSchristos  */
1244bcd344eSchristos static char *
udf_match(am_opts * fo)1254bcd344eSchristos udf_match(am_opts *fo)
1264bcd344eSchristos {
1274bcd344eSchristos 
1284bcd344eSchristos 	if (!fo->opt_dev) {
1294bcd344eSchristos 		plog(XLOG_USER, "udf: no source device specified");
1304bcd344eSchristos 		return 0;
1314bcd344eSchristos 	}
1324bcd344eSchristos 	dlog("UDF: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
1334bcd344eSchristos 
1344bcd344eSchristos 	/*
1354bcd344eSchristos 	 * Determine magic cookie to put in mtab
1364bcd344eSchristos 	 */
137*8bae5d40Schristos 	return xstrdup(fo->opt_dev);
1384bcd344eSchristos }
1394bcd344eSchristos 
1404bcd344eSchristos static int
mount_udf(char * mntdir,char * fs_name,char * opts,int on_autofs)1414bcd344eSchristos mount_udf(char *mntdir, char *fs_name, char *opts, int on_autofs)
1424bcd344eSchristos {
1434bcd344eSchristos 	udf_args_t udf_args;
1444bcd344eSchristos 	mntent_t mnt;
1454bcd344eSchristos 	int flags;
1464bcd344eSchristos 	char *str;
1474bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
1484bcd344eSchristos 	uid_t uid_nobody;
1494bcd344eSchristos 	gid_t gid_nobody;
1504bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
1514bcd344eSchristos 	/*
1524bcd344eSchristos 	 * Figure out the name of the file system type.
1534bcd344eSchristos 	 */
1544bcd344eSchristos 	MTYPE_TYPE type = MOUNT_TYPE_UDF;
1554bcd344eSchristos 
1564bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
1574bcd344eSchristos 	uid_nobody = a_uid("nobody", "user");
1584bcd344eSchristos 	if (uid_nobody == 0) {
1594bcd344eSchristos 		plog(XLOG_ERROR, "mount_udf: invalid uid for nobody");
1604bcd344eSchristos 		return EPERM;
1614bcd344eSchristos 	}
1624bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
1634bcd344eSchristos 
1644bcd344eSchristos #if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID)
1654bcd344eSchristos 	gid_nobody = a_gid("nobody", "group");
1664bcd344eSchristos 	if (gid_nobody == 0) {
1674bcd344eSchristos 		plog(XLOG_ERROR, "mount_udf: invalid gid for nobody");
1684bcd344eSchristos 		return EPERM;
1694bcd344eSchristos 	}
1704bcd344eSchristos #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID) */
1714bcd344eSchristos 
1724bcd344eSchristos 	str = NULL;
1734bcd344eSchristos 	memset((voidp) &udf_args, 0, sizeof(udf_args)); /* Paranoid */
1744bcd344eSchristos 
1754bcd344eSchristos 	/*
1764bcd344eSchristos 	 * Fill in the mount structure
1774bcd344eSchristos 	 */
1784bcd344eSchristos 	memset((voidp)&mnt, 0, sizeof(mnt));
1794bcd344eSchristos 	mnt.mnt_dir = mntdir;
1804bcd344eSchristos 	mnt.mnt_fsname = fs_name;
1814bcd344eSchristos 	mnt.mnt_type = MNTTAB_TYPE_UDF;
1824bcd344eSchristos 	mnt.mnt_opts = opts;
1834bcd344eSchristos 
1844bcd344eSchristos 	flags = compute_mount_flags(&mnt);
1854bcd344eSchristos 
1864bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_UDFMFLAGS
1874bcd344eSchristos # if defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION)
1884bcd344eSchristos 	if (amu_hasmntopt(&mnt, MNTTAB_OPT_CLOSESESSION))
1894bcd344eSchristos 		udf_args.udfmflags |= MNT2_UDF_OPT_CLOSESESSION;
1904bcd344eSchristos # endif /* defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION) */
1914bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_UDFMFLAGS */
1924bcd344eSchristos 
1934bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_NOBODY_UID
1944bcd344eSchristos 	udf_args.nobody_uid = uid_nobody;
1954bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_NOBODY_UID */
1964bcd344eSchristos 
1974bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_NOBODY_GID
1984bcd344eSchristos 	udf_args.nobody_gid = gid_nobody;
1994bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_NOBODY_GID */
2004bcd344eSchristos 
2014bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_ANON_UID
2024bcd344eSchristos 	udf_args.anon_uid = uid_nobody;	/* default to nobody */
2034bcd344eSchristos 	if ((str = hasmntstr(&mnt, MNTTAB_OPT_USER)) != NULL) {
2044bcd344eSchristos 		udf_args.anon_uid = a_uid(str, MNTTAB_OPT_USER);
2054bcd344eSchristos 		XFREE(str);
2064bcd344eSchristos 	}
2074bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_ANON_UID */
2084bcd344eSchristos 
2094bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_ANON_GID
2104bcd344eSchristos 	udf_args.anon_gid = gid_nobody;	/* default to nobody */
2114bcd344eSchristos 	if ((str = hasmntstr(&mnt, MNTTAB_OPT_GROUP)) != NULL) {
2124bcd344eSchristos 		udf_args.anon_gid = a_gid(str, MNTTAB_OPT_GROUP);
2134bcd344eSchristos 		XFREE(str);
2144bcd344eSchristos 	}
2154bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_ANON_GID */
2164bcd344eSchristos 
2174bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_GMTOFF
2184bcd344eSchristos 	udf_args.gmtoff = 0;
2194bcd344eSchristos 	if ((str = hasmntstr(&mnt, MNTTAB_OPT_GMTOFF)) != NULL) {
2204bcd344eSchristos 		udf_args.gmtoff = a_num(str, MNTTAB_OPT_GMTOFF);
2214bcd344eSchristos 		XFREE(str);
2224bcd344eSchristos 	}
2234bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_GMTOFF */
2244bcd344eSchristos 
2254bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_SESSIONNR
2264bcd344eSchristos 	udf_args.sessionnr = 0;
2274bcd344eSchristos 	if ((str = hasmntstr(&mnt, MNTTAB_OPT_SESSIONNR)) != NULL) {
2284bcd344eSchristos 		udf_args.sessionnr = a_num(str, MNTTAB_OPT_SESSIONNR);
2294bcd344eSchristos 		XFREE(str);
2304bcd344eSchristos 	}
2314bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_SESSIONNR */
2324bcd344eSchristos 
2334bcd344eSchristos #ifdef HAVE_UDF_ARGS_T_VERSION
2344bcd344eSchristos # ifdef UDFMNT_VERSION
2354bcd344eSchristos 	udf_args.version = UDFMNT_VERSION;
2364bcd344eSchristos # endif /* UDFMNT_VERSION */
2374bcd344eSchristos #endif /* HAVE_UDF_ARGS_T_VERSION */
2384bcd344eSchristos 
2394bcd344eSchristos #ifdef HAVE_UFS_ARGS_T_FSPEC
2404bcd344eSchristos 	udf_args.fspec = fs_name;
2414bcd344eSchristos #endif /* HAVE_UFS_ARGS_T_FSPEC */
2424bcd344eSchristos 
2434bcd344eSchristos 	/*
2444bcd344eSchristos 	 * Call generic mount routine
2454bcd344eSchristos 	 */
2464bcd344eSchristos 	return mount_fs(&mnt, flags, (caddr_t)&udf_args, 0, type, 0, NULL,
2474bcd344eSchristos 	    mnttab_file_name, on_autofs);
2484bcd344eSchristos }
2494bcd344eSchristos 
2504bcd344eSchristos static int
udf_mount(am_node * am,mntfs * mf)2514bcd344eSchristos udf_mount(am_node *am, mntfs *mf)
2524bcd344eSchristos {
2534bcd344eSchristos 	int on_autofs;
2544bcd344eSchristos 	int error;
2554bcd344eSchristos 
2564bcd344eSchristos 	on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
2574bcd344eSchristos 	error = mount_udf(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
2584bcd344eSchristos 	if (error) {
2594bcd344eSchristos 		errno = error;
2604bcd344eSchristos 		plog(XLOG_ERROR, "mount_udf: %m");
2614bcd344eSchristos 		return error;
2624bcd344eSchristos 	}
2634bcd344eSchristos 	return 0;
2644bcd344eSchristos }
2654bcd344eSchristos 
2664bcd344eSchristos 
2674bcd344eSchristos static int
udf_umount(am_node * am,mntfs * mf)2684bcd344eSchristos udf_umount(am_node *am, mntfs *mf)
2694bcd344eSchristos {
2704bcd344eSchristos 	int unmount_flags;
2714bcd344eSchristos 
2724bcd344eSchristos 	unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
2734bcd344eSchristos 	return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
2744bcd344eSchristos }
275