138494Sobrien/*
2174313Sobrien * Copyright (c) 1997-2006 Erez Zadok
338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1990 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174313Sobrien * File: am-utils/amd/ops_pcfs.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien/*
4538494Sobrien * PC (MS-DOS) file system
4638494Sobrien */
4738494Sobrien
4838494Sobrien#ifdef HAVE_CONFIG_H
4938494Sobrien# include <config.h>
5038494Sobrien#endif /* HAVE_CONFIG_H */
5138494Sobrien#include <am_defs.h>
5238494Sobrien#include <amd.h>
5338494Sobrien
5438494Sobrien/* forward definitions */
5538494Sobrienstatic char *pcfs_match(am_opts *fo);
56174313Sobrienstatic int pcfs_mount(am_node *am, mntfs *mf);
57174313Sobrienstatic int pcfs_umount(am_node *am, mntfs *mf);
5838494Sobrien
5938494Sobrien/*
6038494Sobrien * Ops structure
6138494Sobrien */
6238494Sobrienam_ops pcfs_ops =
6338494Sobrien{
6438494Sobrien  "pcfs",
6538494Sobrien  pcfs_match,
6638494Sobrien  0,				/* pcfs_init */
67174313Sobrien  pcfs_mount,
68174313Sobrien  pcfs_umount,
69174313Sobrien  amfs_error_lookup_child,
70174313Sobrien  amfs_error_mount_child,
7138494Sobrien  amfs_error_readdir,
7238494Sobrien  0,				/* pcfs_readlink */
7338494Sobrien  0,				/* pcfs_mounted */
7438494Sobrien  0,				/* pcfs_umounted */
75174313Sobrien  amfs_generic_find_srvr,
76174313Sobrien  0,				/* pcfs_get_wchan */
77174313Sobrien  FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
78174313Sobrien#ifdef HAVE_FS_AUTOFS
79174313Sobrien  AUTOFS_PCFS_FS_FLAGS,
80174313Sobrien#endif /* HAVE_FS_AUTOFS */
8138494Sobrien};
8238494Sobrien
8338494Sobrien
8438494Sobrien
8538494Sobrien/*
8638494Sobrien * PCFS needs remote filesystem.
8738494Sobrien */
8838494Sobrienstatic char *
8938494Sobrienpcfs_match(am_opts *fo)
9038494Sobrien{
9138494Sobrien  if (!fo->opt_dev) {
9238494Sobrien    plog(XLOG_USER, "pcfs: no source device specified");
9338494Sobrien    return 0;
9438494Sobrien  }
9538494Sobrien  dlog("PCFS: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
9638494Sobrien
9738494Sobrien  /*
9838494Sobrien   * Determine magic cookie to put in mtab
9938494Sobrien   */
10038494Sobrien  return strdup(fo->opt_dev);
10138494Sobrien}
10238494Sobrien
10338494Sobrien
10438494Sobrienstatic int
105174313Sobrienmount_pcfs(char *mntdir, char *fs_name, char *opts, int on_autofs)
10638494Sobrien{
10738494Sobrien  pcfs_args_t pcfs_args;
10838494Sobrien  mntent_t mnt;
10938494Sobrien  int flags;
110174313Sobrien#if defined(HAVE_PCFS_ARGS_T_MASK) || defined(HAVE_PCFS_ARGS_T_DIRMASK)
111174313Sobrien  int mask;
112174313Sobrien#endif /* defined(HAVE_PCFS_ARGS_T_MASK) || defined(HAVE_PCFS_ARGS_T_DIRMASK) */
113174313Sobrien#if defined(HAVE_PCFS_ARGS_T_UID) || defined(HAVE_PCFS_ARGS_T_UID)
114174313Sobrien  char *str;
115174313Sobrien#endif /* defined(HAVE_PCFS_ARGS_T_UID) || defined(HAVE_PCFS_ARGS_T_UID) */
11638494Sobrien
11738494Sobrien  /*
11838494Sobrien   * Figure out the name of the file system type.
11938494Sobrien   */
12038494Sobrien  MTYPE_TYPE type = MOUNT_TYPE_PCFS;
12138494Sobrien
12238494Sobrien  memset((voidp) &pcfs_args, 0, sizeof(pcfs_args)); /* Paranoid */
12338494Sobrien
12438494Sobrien  /*
12538494Sobrien   * Fill in the mount structure
12638494Sobrien   */
12738494Sobrien  memset((voidp) &mnt, 0, sizeof(mnt));
128174313Sobrien  mnt.mnt_dir = mntdir;
12938494Sobrien  mnt.mnt_fsname = fs_name;
13038494Sobrien  mnt.mnt_type = MNTTAB_TYPE_PCFS;
13138494Sobrien  mnt.mnt_opts = opts;
13238494Sobrien
13338494Sobrien  flags = compute_mount_flags(&mnt);
134174313Sobrien#ifdef HAVE_FS_AUTOFS
135174313Sobrien  if (on_autofs)
136174313Sobrien    flags |= autofs_compute_mount_flags(&mnt);
137174313Sobrien#endif /* HAVE_FS_AUTOFS */
138174313Sobrien  if (amuDebug(D_TRACE))
139174313Sobrien    plog(XLOG_DEBUG, "mount_pcfs: flags=0x%x", (u_int) flags);
14038494Sobrien
141119679Smbr#ifdef HAVE_PCFS_ARGS_T_FSPEC
14238494Sobrien  pcfs_args.fspec = fs_name;
143119679Smbr#endif /* HAVE_PCFS_ARGS_T_FSPEC */
14438494Sobrien
145119679Smbr#ifdef HAVE_PCFS_ARGS_T_MASK
14638494Sobrien  pcfs_args.mask = 0777;	/* this may be the msdos file modes */
147174313Sobrien  if ((mask = hasmntval(&mnt, MNTTAB_OPT_MASK)) > 0)
148174313Sobrien    pcfs_args.mask = mask;
149174313Sobrien  if (amuDebug(D_TRACE))
150174313Sobrien    plog(XLOG_DEBUG, "mount_pcfs: mask=%o (octal)", (u_int) pcfs_args.mask);
151119679Smbr#endif /* HAVE_PCFS_ARGS_T_MASK */
15238494Sobrien
153131709Smbr#ifdef HAVE_PCFS_ARGS_T_DIRMASK
154174313Sobrien  pcfs_args.dirmask = 0777;    /* this may be the msdos dir modes */
155174313Sobrien  if ((mask = hasmntval(&mnt, MNTTAB_OPT_DIRMASK)) > 0)
156174313Sobrien    pcfs_args.dirmask = mask;
157174313Sobrien  if (amuDebug(D_TRACE))
158174313Sobrien    plog(XLOG_DEBUG, "mount_pcfs: dirmask=%o (octal)", (u_int) pcfs_args.dirmask);
159131709Smbr#endif /* HAVE_PCFS_ARGS_T_DIRMASK */
160131709Smbr
161119679Smbr#ifdef HAVE_PCFS_ARGS_T_UID
162174313Sobrien  pcfs_args.uid = 0;		/* default to root */
163174313Sobrien  if ((str = hasmntstr(&mnt, MNTTAB_OPT_USER)) != NULL) {
164174313Sobrien    struct passwd *pw;
165174313Sobrien    if ((pw = getpwnam(str)) != NULL)
166174313Sobrien      pcfs_args.uid = pw->pw_uid;
167174313Sobrien    else		 /* maybe used passed a UID number, not user name */
168174313Sobrien      pcfs_args.uid = atoi(str); /* atoi returns '0' if it failed */
169174313Sobrien    XFREE(str);
170174313Sobrien  }
171174313Sobrien  if (amuDebug(D_TRACE))
172174313Sobrien    plog(XLOG_DEBUG, "mount_pcfs: uid=%d", (int) pcfs_args.uid);
173119679Smbr#endif /* HAVE_PCFS_ARGS_T_UID */
17438494Sobrien
175119679Smbr#ifdef HAVE_PCFS_ARGS_T_GID
176174313Sobrien  pcfs_args.gid = 0;		/* default to wheel/root group */
177174313Sobrien  if ((str = hasmntstr(&mnt, MNTTAB_OPT_GROUP)) != NULL) {
178174313Sobrien    struct group *gr;
179174313Sobrien    if ((gr = getgrnam(str)) != NULL)
180174313Sobrien      pcfs_args.gid = gr->gr_gid;
181174313Sobrien    else		/* maybe used passed a GID number, not group name */
182174313Sobrien      pcfs_args.gid = atoi(str); /* atoi returns '0' if it failed */
183174313Sobrien    XFREE(str);
184174313Sobrien  }
185174313Sobrien  if (amuDebug(D_TRACE))
186174313Sobrien    plog(XLOG_DEBUG, "mount_pcfs: gid=%d", (int) pcfs_args.gid);
187119679Smbr#endif /* HAVE_PCFS_ARGS_T_GID */
18838494Sobrien
189119679Smbr#ifdef HAVE_PCFS_ARGS_T_SECONDSWEST
19038494Sobrien  pcfs_args.secondswest = 0;	/* XXX: fill in correct values */
191119679Smbr#endif /* HAVE_PCFS_ARGS_T_SECONDSWEST */
192119679Smbr#ifdef HAVE_PCFS_ARGS_T_DSTTIME
19338494Sobrien  pcfs_args.dsttime = 0;	/* XXX: fill in correct values */
194119679Smbr#endif /* HAVE_PCFS_ARGS_T_DSTTIME */
19538494Sobrien
19638494Sobrien  /*
19738494Sobrien   * Call generic mount routine
19838494Sobrien   */
199174313Sobrien  return mount_fs(&mnt, flags, (caddr_t) & pcfs_args, 0, type, 0, NULL, mnttab_file_name, on_autofs);
20038494Sobrien}
20138494Sobrien
20238494Sobrien
20338494Sobrienstatic int
204174313Sobrienpcfs_mount(am_node *am, mntfs *mf)
20538494Sobrien{
206174313Sobrien  int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
20738494Sobrien  int error;
20838494Sobrien
209174313Sobrien  error = mount_pcfs(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
21038494Sobrien  if (error) {
21138494Sobrien    errno = error;
21238494Sobrien    plog(XLOG_ERROR, "mount_pcfs: %m");
21338494Sobrien    return error;
21438494Sobrien  }
21538494Sobrien
21638494Sobrien  return 0;
21738494Sobrien}
21838494Sobrien
21938494Sobrien
22038494Sobrienstatic int
221174313Sobrienpcfs_umount(am_node *am, mntfs *mf)
22238494Sobrien{
223174313Sobrien  int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
224174313Sobrien
225174313Sobrien  return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
22638494Sobrien}
227