1/*
2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *
36 * File: am-utils/amd/amfs_auto.c
37 *
38 */
39
40/*
41 * Automount file system
42 */
43
44#ifdef HAVE_CONFIG_H
45# include <config.h>
46#endif /* HAVE_CONFIG_H */
47#include <am_defs.h>
48#include <amd.h>
49
50/****************************************************************************
51 *** MACROS                                                               ***
52 ****************************************************************************/
53
54
55/****************************************************************************
56 *** STRUCTURES                                                           ***
57 ****************************************************************************/
58
59
60/****************************************************************************
61 *** FORWARD DEFINITIONS                                                  ***
62 ****************************************************************************/
63static int amfs_auto_mount(am_node *mp, mntfs *mf);
64
65
66/****************************************************************************
67 *** OPS STRUCTURES                                                       ***
68 ****************************************************************************/
69am_ops amfs_auto_ops =
70{
71  "auto",
72  amfs_generic_match,
73  0,				/* amfs_auto_init */
74  amfs_auto_mount,
75  amfs_generic_umount,
76  amfs_generic_lookup_child,
77  amfs_generic_mount_child,
78  amfs_generic_readdir,
79  0,				/* amfs_auto_readlink */
80  amfs_generic_mounted,
81  0,				/* amfs_auto_umounted */
82  amfs_generic_find_srvr,
83  0,				/* amfs_auto_get_wchan */
84  FS_AMQINFO | FS_DIRECTORY,
85#ifdef HAVE_FS_AUTOFS
86  AUTOFS_AUTO_FS_FLAGS,
87#endif /* HAVE_FS_AUTOFS */
88};
89
90
91/****************************************************************************
92 *** FUNCTIONS                                                             ***
93 ****************************************************************************/
94/*
95 * Mount a sub-mount
96 */
97static int
98amfs_auto_mount(am_node *mp, mntfs *mf)
99{
100  if (mp->am_parent == NULL)
101    return EINVAL;
102  /*
103   * Pseudo-directories are used to provide some structure
104   * to the automounted directories instead
105   * of putting them all in the top-level automount directory.
106   *
107   * Here, just increment the parent's link count.
108   */
109  mp->am_parent->am_fattr.na_nlink++;
110
111  /*
112   * Info field of . means use parent's info field.
113   * Historical - not documented.
114   */
115  if (mf->mf_info[0] == '.' && mf->mf_info[1] == '\0')
116    mf->mf_info = strealloc(mf->mf_info, mp->am_parent->am_al->al_mnt->mf_info);
117
118  /*
119   * Compute prefix:
120   *
121   * If there is an option prefix then use that else
122   * If the parent had a prefix then use that with name
123   *      of this node appended else
124   * Use the name of this node.
125   *
126   * That means if you want no prefix you must say so
127   * in the map.
128   */
129  if (mf->mf_fo->opt_pref) {
130    /* allow pref:=null to set a real null prefix */
131    if (STREQ(mf->mf_fo->opt_pref, "null")) {
132      mp->am_pref = xstrdup("");
133    } else {
134      /*
135       * the prefix specified as an option
136       */
137      mp->am_pref = xstrdup(mf->mf_fo->opt_pref);
138    }
139  } else {
140    /*
141     * else the parent's prefix
142     * followed by the name
143     * followed by /
144     */
145    char *ppref = mp->am_parent->am_pref;
146    if (ppref == 0)
147      ppref = "";
148    mp->am_pref = str3cat((char *) NULL, ppref, mp->am_name, "/");
149  }
150
151#ifdef HAVE_FS_AUTOFS
152  if (mf->mf_flags & MFF_IS_AUTOFS) {
153    char opts[SIZEOF_OPTS];
154    int error;
155
156    autofs_get_opts(opts, sizeof(opts), mp->am_autofs_fh);
157
158    /* now do the mount */
159    error = amfs_mount(mp, mf, opts);
160    if (error) {
161      errno = error;
162      plog(XLOG_FATAL, "amfs_auto_mount: amfs_mount failed: %m");
163      return error;
164    }
165  }
166#endif /* HAVE_FS_AUTOFS */
167
168  /*
169   * Attach a map cache
170   */
171  amfs_mkcacheref(mf);
172
173  return 0;
174}
175