summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Generators/Account.py
blob: 06554da07d0cc38f88e9ec64b70943ef37c6e7ca (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
'''This handles authentication setup'''
__revision__ = '$Revision$'

from Bcfg2.Server.Generator import Generator, GeneratorInitError, DirectoryBacked

class Account(Generator):
    '''This module generates account config files,
    based on an internal data repo:
    static.(passwd|group|limits.conf) -> static entries
    dyn.(passwd|group) -> dynamic entries (usually acquired from yp or somesuch)
    useraccess -> users to be granted login access on some hosts
    superusers -> users to be granted root privs on all hosts
    rootlike -> users to be granted root privs on some hosts
    '''
    __name__ = 'Account'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'

    def __init__(self, core, datastore):
        Generator.__init__(self, core, datastore)
        self.__provides__ = {'ConfigFile':{'/etc/passwd':self.from_yp_cb,
                                           '/etc/group':self.from_yp_cb,
                                           '/etc/security/limits.conf':self.gen_limits_cb,
                                           '/root/.ssh/authorized_keys':self.gen_root_keys_cb}}
        try:
            self.repository = DirectoryBacked(self.data, self.core.fam)
        except:
            self.LogError("Failed to load repos: %s, %s" % (self.data, "%s/ssh" % (self.data)))
            raise GeneratorInitError

    def from_yp_cb(self, entry, metadata):
        '''Build password file from cached yp data'''
        fname = entry.attrib['name'].split('/')[-1]
        entry.text = self.repository.entries["static.%s" % (fname)].data
        entry.text += self.repository.entries["dyn.%s" % (fname)].data
        entry.attrib.update({'owner':'root', 'group':'root', 'perms':'0644'})

    def gen_limits_cb(self, entry, metadata):
        '''Build limits entries based on current ACLs'''
        entry.text = self.repository.entries["static.limits.conf"].data
        superusers = self.repository.entries["superusers"].data.split()
        useraccess = self.repository.entries["useraccess"].data
        users = [user for (user, host) in useraccess if host == metadata.hostname]
        entry.attrib.upate({'owner':'root', 'group':'root', 'perms':'0600'})
        entry.text += "".join(["%s hard maxlogins 1024\n" % uname for uname in superusers + users])
        if "*" not in users:
            entry.text += "* hard maxlogins 0\n"

    def gen_root_keys_cb(self, entry, metadata):
        '''Build root authorized keys file based on current ACLs'''
        data = ''
        superusers = self.repository.entries['superusers'].data.split()
        rootlike = [line.split(':', 1) for line in self.repository.entries['rootlike'].data.split()]
        superusers += [user for (user, host) in rootlike if host == metadata.hostname]
        data = ''
        for user in superusers:
            if self.repository.entries.has_key("%s.key", user):
                data += self.repository.entries["%s.key" % user].data
            else:
                self.LogError("Unable to locate key for user %s" % user)
        entry.attrib.update({'owner':'root', 'group':'root', 'perms':'0600'})
        entry.text = data