ops_pcfs.c revision 174313
1/*
2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgment:
21 *      This product includes software developed by the University of
22 *      California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *
40 * File: am-utils/amd/ops_pcfs.c
41 *
42 */
43
44/*
45 * PC (MS-DOS) file system
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52#include <amd.h>
53
54/* forward definitions */
55static char *pcfs_match(am_opts *fo);
56static int pcfs_mount(am_node *am, mntfs *mf);
57static int pcfs_umount(am_node *am, mntfs *mf);
58
59/*
60 * Ops structure
61 */
62am_ops pcfs_ops =
63{
64  "pcfs",
65  pcfs_match,
66  0,				/* pcfs_init */
67  pcfs_mount,
68  pcfs_umount,
69  amfs_error_lookup_child,
70  amfs_error_mount_child,
71  amfs_error_readdir,
72  0,				/* pcfs_readlink */
73  0,				/* pcfs_mounted */
74  0,				/* pcfs_umounted */
75  amfs_generic_find_srvr,
76  0,				/* pcfs_get_wchan */
77  FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
78#ifdef HAVE_FS_AUTOFS
79  AUTOFS_PCFS_FS_FLAGS,
80#endif /* HAVE_FS_AUTOFS */
81};
82
83
84
85/*
86 * PCFS needs remote filesystem.
87 */
88static char *
89pcfs_match(am_opts *fo)
90{
91  if (!fo->opt_dev) {
92    plog(XLOG_USER, "pcfs: no source device specified");
93    return 0;
94  }
95  dlog("PCFS: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
96
97  /*
98   * Determine magic cookie to put in mtab
99   */
100  return strdup(fo->opt_dev);
101}
102
103
104static int
105mount_pcfs(char *mntdir, char *fs_name, char *opts, int on_autofs)
106{
107  pcfs_args_t pcfs_args;
108  mntent_t mnt;
109  int flags;
110#if defined(HAVE_PCFS_ARGS_T_MASK) || defined(HAVE_PCFS_ARGS_T_DIRMASK)
111  int mask;
112#endif /* defined(HAVE_PCFS_ARGS_T_MASK) || defined(HAVE_PCFS_ARGS_T_DIRMASK) */
113#if defined(HAVE_PCFS_ARGS_T_UID) || defined(HAVE_PCFS_ARGS_T_UID)
114  char *str;
115#endif /* defined(HAVE_PCFS_ARGS_T_UID) || defined(HAVE_PCFS_ARGS_T_UID) */
116
117  /*
118   * Figure out the name of the file system type.
119   */
120  MTYPE_TYPE type = MOUNT_TYPE_PCFS;
121
122  memset((voidp) &pcfs_args, 0, sizeof(pcfs_args)); /* Paranoid */
123
124  /*
125   * Fill in the mount structure
126   */
127  memset((voidp) &mnt, 0, sizeof(mnt));
128  mnt.mnt_dir = mntdir;
129  mnt.mnt_fsname = fs_name;
130  mnt.mnt_type = MNTTAB_TYPE_PCFS;
131  mnt.mnt_opts = opts;
132
133  flags = compute_mount_flags(&mnt);
134#ifdef HAVE_FS_AUTOFS
135  if (on_autofs)
136    flags |= autofs_compute_mount_flags(&mnt);
137#endif /* HAVE_FS_AUTOFS */
138  if (amuDebug(D_TRACE))
139    plog(XLOG_DEBUG, "mount_pcfs: flags=0x%x", (u_int) flags);
140
141#ifdef HAVE_PCFS_ARGS_T_FSPEC
142  pcfs_args.fspec = fs_name;
143#endif /* HAVE_PCFS_ARGS_T_FSPEC */
144
145#ifdef HAVE_PCFS_ARGS_T_MASK
146  pcfs_args.mask = 0777;	/* this may be the msdos file modes */
147  if ((mask = hasmntval(&mnt, MNTTAB_OPT_MASK)) > 0)
148    pcfs_args.mask = mask;
149  if (amuDebug(D_TRACE))
150    plog(XLOG_DEBUG, "mount_pcfs: mask=%o (octal)", (u_int) pcfs_args.mask);
151#endif /* HAVE_PCFS_ARGS_T_MASK */
152
153#ifdef HAVE_PCFS_ARGS_T_DIRMASK
154  pcfs_args.dirmask = 0777;    /* this may be the msdos dir modes */
155  if ((mask = hasmntval(&mnt, MNTTAB_OPT_DIRMASK)) > 0)
156    pcfs_args.dirmask = mask;
157  if (amuDebug(D_TRACE))
158    plog(XLOG_DEBUG, "mount_pcfs: dirmask=%o (octal)", (u_int) pcfs_args.dirmask);
159#endif /* HAVE_PCFS_ARGS_T_DIRMASK */
160
161#ifdef HAVE_PCFS_ARGS_T_UID
162  pcfs_args.uid = 0;		/* default to root */
163  if ((str = hasmntstr(&mnt, MNTTAB_OPT_USER)) != NULL) {
164    struct passwd *pw;
165    if ((pw = getpwnam(str)) != NULL)
166      pcfs_args.uid = pw->pw_uid;
167    else		 /* maybe used passed a UID number, not user name */
168      pcfs_args.uid = atoi(str); /* atoi returns '0' if it failed */
169    XFREE(str);
170  }
171  if (amuDebug(D_TRACE))
172    plog(XLOG_DEBUG, "mount_pcfs: uid=%d", (int) pcfs_args.uid);
173#endif /* HAVE_PCFS_ARGS_T_UID */
174
175#ifdef HAVE_PCFS_ARGS_T_GID
176  pcfs_args.gid = 0;		/* default to wheel/root group */
177  if ((str = hasmntstr(&mnt, MNTTAB_OPT_GROUP)) != NULL) {
178    struct group *gr;
179    if ((gr = getgrnam(str)) != NULL)
180      pcfs_args.gid = gr->gr_gid;
181    else		/* maybe used passed a GID number, not group name */
182      pcfs_args.gid = atoi(str); /* atoi returns '0' if it failed */
183    XFREE(str);
184  }
185  if (amuDebug(D_TRACE))
186    plog(XLOG_DEBUG, "mount_pcfs: gid=%d", (int) pcfs_args.gid);
187#endif /* HAVE_PCFS_ARGS_T_GID */
188
189#ifdef HAVE_PCFS_ARGS_T_SECONDSWEST
190  pcfs_args.secondswest = 0;	/* XXX: fill in correct values */
191#endif /* HAVE_PCFS_ARGS_T_SECONDSWEST */
192#ifdef HAVE_PCFS_ARGS_T_DSTTIME
193  pcfs_args.dsttime = 0;	/* XXX: fill in correct values */
194#endif /* HAVE_PCFS_ARGS_T_DSTTIME */
195
196  /*
197   * Call generic mount routine
198   */
199  return mount_fs(&mnt, flags, (caddr_t) & pcfs_args, 0, type, 0, NULL, mnttab_file_name, on_autofs);
200}
201
202
203static int
204pcfs_mount(am_node *am, mntfs *mf)
205{
206  int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
207  int error;
208
209  error = mount_pcfs(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
210  if (error) {
211    errno = error;
212    plog(XLOG_ERROR, "mount_pcfs: %m");
213    return error;
214  }
215
216  return 0;
217}
218
219
220static int
221pcfs_umount(am_node *am, mntfs *mf)
222{
223  int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
224
225  return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
226}
227