summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/automount.py
blob: b4b696d10dba606494fb703f090392027ca67374 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# Authors:
#   Rob Crittenden <rcritten@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

"""
Frontend plugins for 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 ldap import explode_dn
from ipalib import crud, errors2
from ipalib import api, Str, Flag, Object, Command

map_attributes = ['automountMapName', 'description', ]
key_attributes = ['description', 'automountKey', 'automountInformation']

def display_entry(textui, entry):
    # FIXME: for now delete dn here. In the future pass in the kw to
    # output_for_cli()
    attr = sorted(entry.keys())

    for a in attr:
        if a != 'dn':
            textui.print_plain("%s: %s" % (a, entry[a]))

def make_automount_dn(mapname):
    """
    Construct automount dn from map name.
    """
    # FIXME, should this be in b_ldap?
    # Experimenting to see what a plugin looks like for a 3rd party who can't
    # modify the backend.
    import ldap
    return 'automountmapname=%s,%s,%s' % (
        ldap.dn.escape_dn_chars(mapname),
        api.env.container_automount,
        api.env.basedn,
    )


def make_ldap_map(ldapuri, mapname):
    """
    Convert a map name into an LDAP name.

    Note: This is unused currently. This would return map names as a
    quasi ldap URL which will work with older autofs clients. We are
    not currently supporting them.
    """
    if not ldapuri:
        return mapname
    if mapname.find('ldap:') >= 0:
        return mapname

    return 'ldap:%s:%s' % (api.env.host, make_automount_dn(mapname))


class automount(Object):
    """
    Automount object.
    """
    takes_params = (
        Str('automountmapname',
            cli_name='mapname',
            primary_key=True,
            doc='A group of related automount objects',
        ),
    )
api.register(automount)


class automount_addmap(crud.Add):
    'Add a new automount map.'

    takes_options = (
        Str('description?',
            doc='A description of the automount map'),
    )

    def execute(self, mapname, **kw):
        """
        Execute the automount-addmap operation.

        Returns the entry as it will be created in LDAP.

        :param mapname: The map name being added.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'automountmapname' not in kw
        assert 'dn' not in kw
        ldap = self.api.Backend.ldap
        kw['automountmapname'] = mapname
        kw['dn'] = make_automount_dn(mapname)

        kw['objectclass'] = ['automountMap']

        return ldap.create(**kw)

    def output_for_cli(self, textui, result, map, **options):
        """
        Output result of this command to command line interface.
        """
        textui.print_plain("Automount map %s added" % map)

api.register(automount_addmap)


class automount_addkey(crud.Add):
    'Add a new automount key.'
    takes_options = (
        Str('automountkey',
            cli_name='key',
            doc='An entry in an automount map'),
        Str('automountinformation',
            cli_name='info',
            doc='Mount information for this key'),
        Str('description?',
            doc='A description of the mount'),
    )

    def execute(self, mapname, **kw):
        """
        Execute the automount-addkey operation.

        Returns the entry as it will be created in LDAP.

        :param mapname: The map name being added to.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'automountmapname' not in kw
        assert 'dn' not in kw
        ldap = self.api.Backend.ldap
        config = ldap.get_ipa_config()
        # use find_entry_dn instead of make_automap_dn so we can confirm that
        # the map exists
        map_dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        kw['dn'] = "automountkey=%s,%s" % (kw['automountkey'], map_dn)

        kw['automountinformation'] = make_ldap_map(config.get('automountldapuri', False), kw['automountinformation'])

        kw['objectclass'] = ['automount']

        return ldap.create(**kw)

    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        textui.print_plain("Automount key added")

api.register(automount_addkey)


class automount_delmap(crud.Del):
    'Delete an automount map.'
    def execute(self, mapname, **kw):
        """Delete an automount map. This will also remove all of the keys
           associated with this map.

           mapname is the automount map to remove

           :param mapname: The map to be removed
           :param kw: Not used.
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)

        # First remove all the keys for this map so we don't leave orphans
        keys = api.Command['automount_getkeys'](mapname)
        for k in keys:
                ldap.delete(k.get('dn'))

        # Now remove the parental connection
        try:
            infodn = ldap.find_entry_dn("automountinformation", mapname, "automount", api.env.container_automount)
            ldap.delete(infodn)
        except errors2.NotFound:
            # direct maps may not have this
            pass

        return ldap.delete(dn)
    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        print "Automount map and associated keys deleted"

api.register(automount_delmap)


class automount_delkey(crud.Del):
    'Delete an automount key.'
    takes_options = (
        Str('automountkey',
            cli_name='key',
            doc='The automount key to remove'),
    )
    def execute(self, mapname, **kw):
        """Delete an automount key.

           key is the automount key to remove

           :param mapname: The automount map containing the key to be removed
           :param kw: "key" the key to be removed
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        keys = api.Command['automount_getkeys'](mapname)
        keydn = None
        keyname = kw.get('automountkey').lower()
        if keys:
            for k in keys:
                if k.get('automountkey').lower() == keyname:
                    keydn = k.get('dn')
                    break
        if not keydn:
            raise errors2.NotFound(msg='Entry not found')
        return ldap.delete(keydn)
    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        print "Automount key deleted"

api.register(automount_delkey)

class automount_modmap(crud.Mod):
    'Edit an existing automount map.'
    takes_options = (
        Str('description?',
            doc='A description of the automount map'),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-modmap operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry

        :param mapname: The map name to update.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'automountmapname' not in kw
        assert 'dn' not in kw
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        return ldap.update(dn, **kw)

    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        print "Automount map updated"

api.register(automount_modmap)


class automount_modkey(crud.Mod):
    'Edit an existing automount key.'
    takes_options = (
        Str('automountkey',
            cli_name='key',
            doc='An entry in an automount map'),
        Str('automountinformation?',
            cli_name='info',
            doc='Mount information for this key'),
        Str('description?',
            doc='A description of the automount map'),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-modkey operation.

        Returns the entry

        :param mapname: The map name to update.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'automountmapname' not in kw
        assert 'dn' not in kw
        keyname = kw.get('automountkey').lower()
        del kw['automountkey']
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        keys = api.Command['automount_getkeys'](mapname)
        keydn = None
        if keys:
            for k in keys:
                if k.get('automountkey').lower() == keyname:
                    keydn = k.get('dn')
                    break
        if not keydn:
            raise errors2.NotFound(msg='Entry not found')
        return ldap.update(keydn, **kw)

    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        print "Automount key updated"

api.register(automount_modkey)


class automount_findmap(crud.Find):
    'Search automount maps.'
    takes_options = (
        Flag('all', doc='Retrieve all attributes'),
    )
    def execute(self, term, **kw):
        ldap = self.api.Backend.ldap

        search_fields = map_attributes

        for s in search_fields:
            kw[s] = term

        kw['objectclass'] = 'automountMap'
        kw['base'] = api.env.container_automount
        if kw.get('all', False):
            kw['attributes'] = ['*']
        else:
            kw['attributes'] = map_attributes
        return ldap.search(**kw)

    def output_for_cli(self, textui, result, *args, **options):
        counter = result[0]
        entries = result[1:]
        if counter == 0:
            textui.print_plain("No entries found")
            return
        elif counter == -1:
            textui.print_plain("These results are truncated.")
            textui.print_plain("Please refine your search and try again.")

        for e in entries:
            display_entry(textui, e)
            textui.print_plain("")

api.register(automount_findmap)


class automount_findkey(crud.Find):
    'Search automount keys.'
    takes_options = (
        Flag('all?', doc='Retrieve all attributes'),
    )
    def get_args(self):
        return (Str('automountkey',
                   cli_name='key',
                   doc='An entry in an automount map'),)
    def execute(self, term, **kw):
        ldap = self.api.Backend.ldap

        search_fields = key_attributes

        for s in search_fields:
            kw[s] = term

        kw['objectclass'] = 'automount'
        kw['base'] = api.env.container_automount
        if kw.get('all', False):
            kw['attributes'] = ['*']
        else:
            kw['attributes'] = key_attributes
        return ldap.search(**kw)
    def output_for_cli(self, textui, result, *args, **options):
        counter = result[0]
        entries = result[1:]
        if counter == 0:
            textui.print_plain("No entries found")
            return
        elif counter == -1:
            textui.print_plain("These results are truncated.")
            textui.print_plain("Please refine your search and try again.")

        for e in entries:
            display_entry(textui, e)
            textui.print_plain("")

api.register(automount_findkey)


class automount_showmap(crud.Get):
    'Examine an existing automount map.'
    takes_options = (
        Flag('all?', doc='Retrieve all attributes'),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-showmap operation.

        Returns the entry

        :param mapname: The automount map to retrieve
        :param kw: "all" set to True = return all attributes
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        # FIXME: should kw contain the list of attributes to display?
        if kw.get('all', False):
            return ldap.retrieve(dn)
        else:
            return ldap.retrieve(dn, map_attributes)
    def output_for_cli(self, textui, result, *args, **options):
        if result:
            display_entry(textui, result)

api.register(automount_showmap)


class automount_showkey(crud.Get):
    'Examine an existing automount key.'
    takes_options = (
        Str('automountkey',
            cli_name='key',
            doc='The automount key to display'),
        Flag('all?', doc='Retrieve all attributes'),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-showkey operation.

        Returns the entry

        :param mapname: The mapname to examine
        :param kw: "automountkey" the key to retrieve
        :param kw: "all" set to True = return all attributes
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        keys = api.Command['automount_getkeys'](mapname)
        keyname = kw.get('automountkey').lower()
        keydn = None
        if keys:
            for k in keys:
                if k.get('automountkey').lower() == keyname:
                    keydn = k.get('dn')
                    break
        if not keydn:
            raise errors2.NotFound(msg='Entry not found')
        # FIXME: should kw contain the list of attributes to display?
        if kw.get('all', False):
            return ldap.retrieve(keydn)
        else:
            return ldap.retrieve(keydn, key_attributes)
    def output_for_cli(self, textui, result, *args, **options):
        # The automount map name associated with this key is available only
        # in the dn. Add it as an attribute to display instead.
        if result and not result.get('automountmapname'):
            elements = explode_dn(result.get('dn').lower())
            for e in elements:
                (attr, value) = e.split('=',1)
                if attr == 'automountmapname':
                    result['automountmapname'] = value
            display_entry(textui, result)

api.register(automount_showkey)


class automount_getkeys(Command):
    'Retrieve all keys for an automount map.'
    takes_args = (
        Str('automountmapname',
            cli_name='mapname',
            primary_key=True,
            doc='A group of related automount objects',
        ),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-getkeys operation.

        Return a list of all automount keys for this mapname

        :param mapname: Retrieve all keys for this mapname
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
        try:
            keys = ldap.get_one_entry(dn, 'objectclass=*', ['*'])
        except errors2.NotFound:
            keys = []

        return keys
    def output_for_cli(self, textui, result, *args, **options):
        for k in result:
            textui.print_plain('%s' % k.get('automountkey'))

api.register(automount_getkeys)


class automount_getmaps(Command):
    'Retrieve all automount maps'
    takes_args = (
        Str('automountmapname?',
            cli_name='mapname',
            primary_key=True,
            doc='A group of related automount objects',
            default=u'auto.master',
        ),
    )
    def execute(self, mapname, **kw):
        """
        Execute the automount-getmaps operation.

        Return a list of all automount maps.
        """

        ldap = self.api.Backend.ldap
        base = api.env.container_automount + "," + api.env.basedn

        search_base = "automountmapname=%s,%s" % (mapname, base)
        maps = ldap.get_one_entry(search_base, "objectClass=*", ["*"])

        return maps
    def output_for_cli(self, textui, result, *args, **options):
        for k in result:
            textui.print_plain('%s: %s' % (k.get('automountinformation'), k.get('automountkey')))

api.register(automount_getmaps)

class automount_addindirectmap(crud.Add):
    """
    Add a new automap indirect mount point.
    """

    takes_options = (
        Str('parentmap?',
            cli_name='parentmap',
            default=u'auto.master',
            autofill=True,
            doc='The parent map to connect this to.',
        ),
        Str('automountkey',
            cli_name='key',
            doc='An entry in an automount map',
        ),
        Str('description?',
            doc='A description of the automount map',
        ),
    )

    def execute(self, mapname, **kw):
        """
        Execute the automount-addindirectmap operation.

        Returns the key entry as it will be created in LDAP.

        This function creates 2 LDAP entries. It creates an
        automountmapname entry and an automountkey entry.

        :param mapname: The map name being added.
        :param kw['parentmap'] is the top-level map to add this to.
           defaulting to auto.master
        :param kw['automountkey'] is the mount point
        :param kw['description'] is a textual description of this map
        """
        mapkw = {}
        if kw.get('description'):
            mapkw['description'] = kw.get('description')
        newmap = api.Command['automount_addmap'](mapname, **mapkw)

        keykw = {'automountkey': kw['automountkey'], 'automountinformation': mapname}
        if kw.get('description'):
            keykw['description'] = kw.get('description')
        newkey = api.Command['automount_addkey'](kw['parentmap'], **keykw)

        return newkey
    def output_for_cli(self, textui, result, map, **options):
        """
        Output result of this command to command line interface.
        """
        textui.print_plain("Indirect automount map %s added" % map)

api.register(automount_addindirectmap)


class automount_tofiles(Command):
    'Generate the automount maps as they would be in the filesystem'
    def execute(self, **kw):
        """
        Execute the automount-getmaps operation.

        Return a list of all automount maps.
        """

        ldap = self.api.Backend.ldap
        base = api.env.container_automount + "," + api.env.basedn

        search_base = "automountmapname=auto.master,%s" % base
        maps = ldap.get_one_entry(search_base, "objectClass=autoMount", ["*"])

        mapkeys = {}
        for m in maps:
            keys = api.Command['automount_getkeys'](m.get('automountinformation').decode('UTF-8'))
            mapkeys[m.get('automountinformation')] = keys

        return maps, mapkeys
    def output_for_cli(self, textui, result, **options):
        maps = result[0]
        keys = result[1]
        textui.print_plain("/etc/auto.master:")
        for m in maps:
            textui.print_plain('%s\t/etc/%s' % (m.get('automountkey'), m.get('automountinformation')))
        for m in maps:
            textui.print_plain('---------------------------')
            textui.print_plain('/etc/%s:' % m.get('automountinformation'))
            mapkeys = keys.get(m.get('automountinformation'))
            for k in mapkeys:
                textui.print_plain('%s\t%s' % (k.get('automountkey'), k.get('automountinformation')))

api.register(automount_tofiles)