summaryrefslogtreecommitdiffstats
path: root/userman.py
blob: 69f5e9532f0e3c6ee079cd0fe10e97fe4bd38cea (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
#!/usr/bin/env python

from subprocess import call
import sys
import subprocess
import dbus
import string
import os
import fcntl
import time
import pexpect
import glib
import gobject
import dbus.service
import dbus.mainloop.glib

DBUS_NAME = 'org.openbmc.UserManager'
INTF_NAME = 'org.openbmc.Enrol'
OBJ_NAME_GROUPS = '/org/openbmc/UserManager/Groups'
OBJ_NAME_GROUP = '/org/openbmc/UserManager/Group'
OBJ_NAME_USERS = '/org/openbmc/UserManager/Users'
OBJ_NAME_USER = '/org/openbmc/UserManager/User'

'''
    Object Path > /org/openbmc/UserManager/Groups
        Interface:Method > org.openbmc.Enrol.GroupAddSys string:"groupname"
        Interface:Method > org.openbmc.Enrol.GroupAddUsr string:"groupname"
        Interface:Method > org.openbmc.Enrol.GroupListUsr
        Interface:Method > org.openbmc.Enrol.GroupListSys
    Object Path > /org/openbmc/UserManager/Group
        Interface:Method > org.openbmc.Enrol.GroupDel string:"groupname"
    Object Path > /org/openbmc/UserManager/Users
       Interface:Method > org.openbmc.Enrol.UserAdd string:"comment" string:"username" string:"groupname" string:"passwd"
        Interface:Method > org.openbmc.Enrol.UserList
    Object Path > /org/openbmc/UserManager/User
        Interface:Method > org.openbmc.Enrol.UserDel string:"username"
        Interface:Method > org.openbmc.Enrol.Passswd string:"username" string:"passwd"
'''

userman_providers = {
    'pam' : {
        'adduser' : 'user add',
    },
    'ldap' : {
        'adduser' : 'ldap command to add user',
    },
}

class UserManGroups (dbus.service.Object):
    def __init__(self, bus, name):
        self.bus = bus
        self.name = name
        dbus.service.Object.__init__(self,bus,name)

    def setUsermanProvider(self, provider):
        self.provider = provider

    @dbus.service.method(INTF_NAME, "", "")
    def test(self):
        print("TEST")

    @dbus.service.method(INTF_NAME, "s", "x")
    def GroupAddUsr (self, groupname):
        if not groupname : raise ValueError("Invalid Groupname")

        groups = self.GroupListAll ()
        if groupname in groups: raise ValueError("Group ", groupname, " Exists")

        r = call (["addgroup", groupname])
        return r

    #@dbus.service.method(INTF_NAME, "s", "x")
    def GroupAddSys (self, groupname):
        if not groupname : raise ValueError("Invalid Groupname")

        groups = self.GroupListAll ()
        if groupname in groups: raise ValueError("Group ", groupname, " Exists")

        r = call (["addgroup", "-S", groupname])
        return r

    @dbus.service.method(INTF_NAME, "", "as")
    def GroupListUsr (self):
        groupList = []
        with open("/etc/group", "r") as f:
            for grent in f:
                groupParams = grent.split (":")
                if (int(groupParams[2]) >= 1000 and int(groupParams[2]) != 65534):
                    groupList.append(groupParams[0])
        return groupList

    @dbus.service.method(INTF_NAME, "", "as")
    def GroupListSys (self):
        groupList = []
        with open("/etc/group", "r") as f:
            for grent in f:
                groupParams = grent.split (":")
                if (int(groupParams[2]) > 100 and int(groupParams[2]) < 1000): groupList.append(groupParams[0])
        return groupList

    def GroupListAll (self):
        groupList = []
        with open("/etc/group", "r") as f:
            for grent in f:
                groupParams = grent.split (":")
                groupList.append(groupParams[0])
        return groupList

class UserManGroup (dbus.service.Object):
    def __init__(self, bus, name):
        self.bus = bus
        self.name = name
        dbus.service.Object.__init__(self,bus,name)

    def setUsermanProvider(self, provider):
        self.provider = provider

    @dbus.service.method(INTF_NAME, "", "")
    def test(self):
        print("TEST")

    @dbus.service.method(INTF_NAME, "", "x")
    def GroupDel (self, groupname):
        if not groupname : raise ValueError("Invalid Groupname")

        groups = Groupsobj.GroupListAll ()
        if groupname not in groups: raise ValueError("No such Group: ", groupname)

        r = call (["delgroup", groupname])
        return r

class UserManUsers (dbus.service.Object):
    def __init__(self, bus, name):
        self.bus = bus
        self.name = name
        dbus.service.Object.__init__(self,bus,name)

    def setUsermanProvider(self, provider):
        self.provider = provider

    @dbus.service.method(INTF_NAME, "", "")
    def test(self):
        print("TEST")

    @dbus.service.method(INTF_NAME, "ssss", "x")
    def UserAdd (self, gecos, username, groupname, passwd):
        if not username : raise ValueError("Invalid Username")

        users = self.UserListAll ()
        if username in users : raise ValueError("User ", username, " Exists")

        if groupname:
            groups = Groupsobj.GroupListAll ()
            if groupname not in groups: raise ValueError("No such Group: ", groupname)

        opts = ""
        if gecos: opts = " -g " + '"' + gecos + '"'

        if groupname:
            cmd = "adduser "  + opts + " " + " -G " + groupname + " " + "-s /bin/sh" + " " + username
        else:
            cmd = "adduser "  + opts + " " + "-s /bin/sh" + " " + username

        prompts = ['New password: ', 'Retype password: ', 'Re-enter new password: ']
        proc = pexpect.spawn (cmd)
        proc.expect (prompts)
        proc.sendline (passwd)
        proc.expect (prompts)
        proc.sendline (passwd)

        if proc.expect(prompts + [pexpect.EOF]) != len(prompts):
            proc.sendline (passwd)

        r = proc.wait()
        return r if r else 0

    @dbus.service.method(INTF_NAME, "", "as")
    def UserList (self):
        userList = []
        with open("/etc/passwd", "r") as f:
            for usent in f:
                userParams = usent.split (":")
                if (int(userParams[2]) >= 1000 and int(userParams[2]) != 65534):
                    userList.append(userParams[0])
        return userList

    def UserListAll (self):
        userList = []
        with open("/etc/passwd", "r") as f:
            for usent in f:
                userParams = usent.split (":")
                userList.append(userParams[0])
        return userList

class UserManUser (dbus.service.Object):
    def __init__(self, bus, name):
        self.bus = bus
        self.name = name
        dbus.service.Object.__init__(self,bus,name)

    @dbus.service.method(INTF_NAME, "", "")
    def test(self):
        print("TEST")

    def setUsermanProvider(self, provider):
        self.provider = provider

    @dbus.service.method(INTF_NAME, "s", "x")
    def UserDel (self, username):
        if not username : raise ValueError("Invalid Username")

        users = Usersobj.UserList ()
        if username not in users : raise ValueError("No such User: ", username)

        r = call (["deluser", username])
        return r

    @dbus.service.method(INTF_NAME, "ss", "x")
    def Passwd (self, username, passwd):
        if not username : raise ValueError("Invalid Username")

        users = Usersobj.UserList ()
        if username not in users : raise ValueError("No such User: ", username)

        cmd = "passwd" + " " + username
        prompts = ['New password: ', 'Retype password: ', 'Re-enter new password: ']
        proc = pexpect.spawn (cmd)
        proc.expect (prompts)
        proc.sendline (passwd)
        proc.expect (prompts)
        proc.sendline (passwd)

        if proc.expect(prompts + [pexpect.EOF]) != len(prompts):
            proc.sendline (passwd)

        r = proc.wait()
        return r if r else 0

def main():
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()
    name = dbus.service.BusName(DBUS_NAME, bus)

    global Groupsobj
    global Groupobj
    global Usersobj
    global Userobj

    Groupsobj   = UserManGroups (bus, OBJ_NAME_GROUPS)
    Groupobj    = UserManGroup  (bus, OBJ_NAME_GROUP)
    Usersobj    = UserManUsers  (bus, OBJ_NAME_USERS)
    Userobj     = UserManUser   (bus, OBJ_NAME_USER)

    Groupsobj.setUsermanProvider ("pam")
    Groupobj.setUsermanProvider ("pam")
    Usersobj.setUsermanProvider ("pam")
    Userobj.setUsermanProvider ("pam")

    mainloop = gobject.MainLoop()
    print("Started")
    mainloop.run()

if __name__ == '__main__':
    sys.exit(main())

OpenPOWER on IntegriCloud