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
|
# Copyright (C) 2014 Ipsilon Project Contributors
#
# See the file named COPYING for the project license
from ipsilon.info.common import InfoProviderBase
from ipsilon.info.common import InfoProviderInstaller
from ipsilon.info.common import InfoMapping
from ipsilon.util.plugin import PluginObject
import grp
import pwd
import os
posix_map = {
'gecos': 'fullname'
}
class InfoProvider(InfoProviderBase):
def __init__(self, *pargs):
super(InfoProvider, self).__init__(*pargs)
self.mapper = InfoMapping()
self.mapper.set_mapping(posix_map)
self.name = 'nss'
self.new_config(self.name)
def _get_posix_user(self, user):
p = pwd.getpwnam(user)
return {'username': p.pw_name, 'uidNumber': p.pw_uid,
'gidNumber': p.pw_gid, 'gecos': p.pw_gecos,
'homeDirectory': p.pw_dir, 'loginShell': p.pw_shell}
def _get_posix_groups(self, user, group):
groups = set()
getgrouplist = getattr(os, 'getgrouplist', None)
if getgrouplist:
ids = getgrouplist(user, group)
for i in ids:
try:
g = grp.getgrgid(i)
groups.add(g.gr_name)
except KeyError:
pass
else:
g = grp.getgrgid(group)
groups.add(g.gr_name)
allg = grp.getgrall()
for g in allg:
if user in g.gr_mem:
groups.add(g.gr_name)
return list(groups)
def get_user_attrs(self, user):
reply = dict()
try:
posix_user = self._get_posix_user(user)
userattrs, extras = self.mapper.map_attrs(posix_user)
groups = self._get_posix_groups(posix_user['username'],
posix_user['gidNumber'])
reply['userdata'] = userattrs
reply['groups'] = groups
reply['extras'] = {'posix': extras}
except KeyError:
pass
return reply
class Installer(InfoProviderInstaller):
def __init__(self, *pargs):
super(Installer, self).__init__()
self.name = 'nss'
self.pargs = pargs
def install_args(self, group):
group.add_argument('--info-nss', choices=['yes', 'no'], default='no',
help='Use passwd data to populate user attrs')
def configure(self, opts):
if opts['info_nss'] != 'yes':
return
# Add configuration data to database
po = PluginObject(*self.pargs)
po.name = 'nss'
po.wipe_data()
po.wipe_config_values()
# Update global config to add login plugin
po.is_enabled = True
po.save_enabled_state()
|