summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/automount.py
blob: 24671602414b4b029f71ec8ff374ecef79052925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#   Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Automount

RFC 2707bis http://www.padl.com/~lukeh/rfc2307bis.txt

A few notes on automount:
- It was a design decision to not support different maps by location
- The default parent when adding an indirect map is auto.master
- This uses the short format for automount maps instead of the
  URL format. Support for ldap as a map source in nsswitch.conf was added
  in autofs version 4.1.3-197.  Any version prior to that is not expected
  to work.

As an example, the following automount files:

auto.master:
/-	auto.direct
/mnt	auto.mnt

auto.mnt:
stuff -ro,soft,rsize=8192,wsize=8192 nfs.example.com:/vol/archive/stuff

are equivalent to the following LDAP entries:

# auto.master, automount, example.com
dn: automountmapname=auto.master,cn=automount,dc=example,dc=com
objectClass: automountMap
objectClass: top
automountMapName: auto.master

# auto.direct, automount, example.com
dn: automountmapname=auto.direct,cn=automount,dc=example,dc=com
objectClass: automountMap
objectClass: top
automountMapName: auto.direct

# /-, auto.master, automount, example.com
dn: automountkey=/-,automountmapname=auto.master,cn=automount,dc=example,dc=co
 m
objectClass: automount
objectClass: top
automountKey: /-
automountInformation: auto.direct

# auto.mnt, automount, example.com
dn: automountmapname=auto.mnt,cn=automount,dc=example,dc=com
objectClass: automountMap
objectClass: top
automountMapName: auto.mnt

# /mnt, auto.master, automount, example.com
dn: automountkey=/mnt,automountmapname=auto.master,cn=automount,dc=example,dc=
 com
objectClass: automount
objectClass: top
automountKey: /mnt
automountInformation: auto.mnt

# stuff, auto.mnt, automount, example.com
dn: automountkey=stuff,automountmapname=auto.mnt,cn=automount,dc=example,dc=com
objectClass: automount
objectClass: top
automountKey: stuff
automountInformation: -ro,soft,rsize=8192,wsize=8192 nfs.example.com:/vol/arch
 ive/stuff

"""
from ipalib import api, errors
from ipalib import Object, Command
from ipalib import Flag, Str
from ipalib.plugins.baseldap import *

_map_container_dn = api.env.container_automount

_map_default_attributes = ['automountmapname', 'description']
_key_default_attributes = [
    'automountkey', 'automountinformation', 'description'
]


class automountmap(LDAPObject):
    """
    Automount map object.
    """
    container_dn = _map_container_dn
    object_name = 'automount map'
    object_name_plural = 'automount maps'
    object_class = ['automountmap']
    default_attributes = _map_default_attributes

    takes_params = (
        Str('automountmapname',
            cli_name='name',
            primary_key=True,
            doc='automount map name',
        ),
        Str('description?',
            cli_name='desc',
            doc='description',
        ),
    )

api.register(automountmap)


class automountmap_add(LDAPCreate):
    """
    Create new automount map.
    """

api.register(automountmap_add)


class automountmap_del(LDAPDelete):
    """
    Delete automount map.
    """
    def post_callback(self, ldap, dn, *keys, **options):
        # delete optional parental connection (direct maps may not have this)
        try:
            (dn_, entry_attrs) = ldap.find_entry_by_attr(
                'automountinformation', keys[0], 'automount',
                base_dn=self.obj.container_dn
            )
            ldap.delete_entry(dn_)
        except errors.NotFound:
            pass
        return True

api.register(automountmap_del)


class automountmap_mod(LDAPUpdate):
    """
    Modify automount map.
    """

api.register(automountmap_mod)


class automountmap_find(LDAPSearch):
    """
    Search for automount maps.
    """

api.register(automountmap_find)


class automountmap_show(LDAPRetrieve):
    """
    Display automount map.
    """

api.register(automountmap_show)


class automountkey(LDAPObject):
    """
    Automount key object.
    """
    container_dn = _map_container_dn
    object_name = 'automount key'
    object_name_plural = 'automount keys'
    parent_object_name = 'map'
    object_class = ['automount']
    default_attributes = _key_default_attributes

    takes_params = (
        Str('automountmapname',
            cli_name='map',
            doc='name of automount map grouping related keys',
            parent_key=True,
        ),
        Str('automountkey',
            cli_name='key',
            doc='key name',
            primary_key=True,
        ),
        Str('automountinformation',
            cli_name='info',
            doc='mount information',
        ),
        Str('description?',
            cli_name='desc',
            doc='description',
        ),
    )

api.register(automountkey)


class automountkey_add(LDAPCreate):
    """
    Create new automount key.
    """

api.register(automountkey_add)


class automountkey_del(LDAPDelete):
    """
    Delete automount key.
    """

api.register(automountkey_del)


class automountkey_mod(LDAPUpdate):
    """
    Modify automount key.
    """

api.register(automountkey_mod)


class automountkey_find(LDAPSearch):
    """
    Modify automount key.
    """

api.register(automountkey_find)


class automountkey_show(LDAPRetrieve):
    """
    Display automount key.
    """

api.register(automountkey_show)


class automountmap_add_indirect(LDAPCreate):
    """
    Create new indirect mount point.
    """
    takes_options = (
        Str('key',
            cli_name='key',
            doc='key name (mount point)',
        ),
        Str('parentmap?',
            cli_name='parentmap',
            doc='name of parent automount map to connect this map to',
            default=u'auto.master',
            autofill=True,
        ),
    )

    def execute(self, map, **options):
        self.api.Command['automountmap_add'](map, **options)
        options['automountinformation'] = map
        return self.api.Command['automountkey_add'](
            options['parentmap'], options['key'], **options
        )

api.register(automountmap_add_indirect)


class automount_tofiles(Command):
    """
    Generate the automount maps as they would be in the filesystem.
    """
    def execute(self):
        ldap = self.api.Backend.ldap2

        maps = []
        (maps, truncated) = self.api.Command['automountkey-find'](
            automountmapname=u'auto.master'
        )
        # TODO: handle truncated results
        #       it will probably require direct calls to the ldap backend

        keys = {}
        for (dn, m) in maps:
            info = m['automountinformation'][0]
            (keys[info], truncated) = self.api.Command['automountkey_find'](
                automountmapname=info
            )
            # TODO: handle truncated results

        return (maps, keys)

    def output_for_cli(self, textui, result, **options):
        (maps, keys) = result

        textui.print_plain('/etc/auto.master:')
        for (dn, m) in maps:
            textui.print_plain(
                '%s\t/etc/%s' % (
                    m['automountkey'][0], m['automountinformation'][0]
                )
            )
        for (dn, m) in maps:
            info = m['automountinformation'][0]
            textui.print_plain('---------------------------')
            textui.print_plain('/etc/%s:' % info)
            for (dn, k) in keys[info]:
                textui.print_plain(
                    '%s\t%s' % (
                        k['automountkey'][0], k['automountinformation'][0]
                    )
                )

api.register(automount_tofiles)