blob: d99846cc97405bad57b138b77fe1f748aea1f669 [file] [log] [blame]
Hariharasubramanian R302f32c2016-02-10 07:50:45 -06001#!/usr/bin/env python
2
3from subprocess import call
4import sys
5import subprocess
6import dbus
7import string
8import os
9import fcntl
10import time
11import pexpect
12import glib
13import gobject
14import dbus.service
15import dbus.mainloop.glib
16
17DBUS_NAME = 'org.openbmc.UserManager'
18INTF_NAME = 'org.openbmc.Enrol'
19OBJ_NAME_GROUPS = '/org/openbmc/UserManager/Groups'
20OBJ_NAME_GROUP = '/org/openbmc/UserManager/Group'
21OBJ_NAME_USERS = '/org/openbmc/UserManager/Users'
22OBJ_NAME_USER = '/org/openbmc/UserManager/User'
23
24'''
25 Object Path > /org/openbmc/UserManager/Groups
26 Interface:Method > org.openbmc.Enrol.GroupAddSys string:"groupname"
27 Interface:Method > org.openbmc.Enrol.GroupAddUsr string:"groupname"
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060028 Interface:Method > org.openbmc.Enrol.GroupListUsr
29 Interface:Method > org.openbmc.Enrol.GroupListSys
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060030 Object Path > /org/openbmc/UserManager/Group
31 Interface:Method > org.openbmc.Enrol.GroupDel string:"groupname"
32 Object Path > /org/openbmc/UserManager/Users
33 Interface:Method > org.openbmc.Enrol.UserAdd string:"comment" string:"username" string:"groupname" string:"passwd"
34 Interface:Method > org.openbmc.Enrol.UserList
35 Object Path > /org/openbmc/UserManager/User
36 Interface:Method > org.openbmc.Enrol.UserDel string:"username"
37 Interface:Method > org.openbmc.Enrol.Passswd string:"username" string:"passwd"
38'''
39
40userman_providers = {
Adriana Kobylakcf8e1072016-02-12 10:55:01 -060041 'pam' : {
42 'adduser' : 'user add',
43 },
44 'ldap' : {
45 'adduser' : 'ldap command to add user',
46 },
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060047}
48
49class UserManGroups (dbus.service.Object):
50 def __init__(self, bus, name):
51 self.bus = bus
52 self.name = name
53 dbus.service.Object.__init__(self,bus,name)
54
55 def setUsermanProvider(self, provider):
56 self.provider = provider
57
58 @dbus.service.method(INTF_NAME, "", "")
59 def test(self):
60 print("TEST")
61
62 @dbus.service.method(INTF_NAME, "s", "x")
63 def GroupAddUsr (self, groupname):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060064 if not groupname : raise ValueError("Invalid Groupname")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060065
66 groups = self.GroupListAll ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060067 if groupname in groups: raise ValueError("Group ", groupname, " Exists")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060068
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060069 r = call (["addgroup", groupname])
70 return r
71
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060072 #@dbus.service.method(INTF_NAME, "s", "x")
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060073 def GroupAddSys (self, groupname):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060074 if not groupname : raise ValueError("Invalid Groupname")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060075
76 groups = self.GroupListAll ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060077 if groupname in groups: raise ValueError("Group ", groupname, " Exists")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060078
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060079 r = call (["addgroup", "-S", groupname])
Hariharasubramanian R308cffc2016-03-03 09:35:16 -060080 return r
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060081
82 @dbus.service.method(INTF_NAME, "", "as")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060083 def GroupListUsr (self):
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060084 groupList = []
85 with open("/etc/group", "r") as f:
86 for grent in f:
87 groupParams = grent.split (":")
88 if (int(groupParams[2]) >= 1000 and int(groupParams[2]) != 65534):
89 groupList.append(groupParams[0])
90 return groupList
91
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060092 @dbus.service.method(INTF_NAME, "", "as")
93 def GroupListSys (self):
94 groupList = []
95 with open("/etc/group", "r") as f:
96 for grent in f:
97 groupParams = grent.split (":")
98 if (int(groupParams[2]) > 100 and int(groupParams[2]) < 1000): groupList.append(groupParams[0])
99 return groupList
100
101 def GroupListAll (self):
102 groupList = []
103 with open("/etc/group", "r") as f:
104 for grent in f:
105 groupParams = grent.split (":")
106 groupList.append(groupParams[0])
107 return groupList
108
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600109class UserManGroup (dbus.service.Object):
110 def __init__(self, bus, name):
111 self.bus = bus
112 self.name = name
113 dbus.service.Object.__init__(self,bus,name)
114
115 def setUsermanProvider(self, provider):
116 self.provider = provider
117
118 @dbus.service.method(INTF_NAME, "", "")
119 def test(self):
120 print("TEST")
121
122 @dbus.service.method(INTF_NAME, "", "x")
123 def GroupDel (self, groupname):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600124 if not groupname : raise ValueError("Invalid Groupname")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600125
126 groups = Groupsobj.GroupListAll ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600127 if groupname not in groups: raise ValueError("No such Group: ", groupname)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600128
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600129 r = call (["delgroup", groupname])
130 return r
131
132class UserManUsers (dbus.service.Object):
133 def __init__(self, bus, name):
134 self.bus = bus
135 self.name = name
136 dbus.service.Object.__init__(self,bus,name)
137
138 def setUsermanProvider(self, provider):
139 self.provider = provider
140
141 @dbus.service.method(INTF_NAME, "", "")
142 def test(self):
143 print("TEST")
144
145 @dbus.service.method(INTF_NAME, "ssss", "x")
146 def UserAdd (self, gecos, username, groupname, passwd):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600147 if not username : raise ValueError("Invalid Username")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600148
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600149 users = self.UserListAll ()
150 if username in users : raise ValueError("User ", username, " Exists")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600151
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600152 if groupname:
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600153 groups = Groupsobj.GroupListAll ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600154 if groupname not in groups: raise ValueError("No such Group: ", groupname)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600155
156 opts = ""
157 if gecos: opts = " -g " + '"' + gecos + '"'
158
159 if groupname:
160 cmd = "adduser " + opts + " " + " -G " + groupname + " " + username
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600161 else:
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600162 cmd = "adduser " + opts + " " + username
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600163
Brad Bishop15d498e2016-09-07 20:49:52 -0400164 prompts = ['New password: ', 'Retype password: ', 'Re-enter new password: ']
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600165 proc = pexpect.spawn (cmd)
Brad Bishop15d498e2016-09-07 20:49:52 -0400166 proc.expect (prompts)
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600167 proc.sendline (passwd)
Brad Bishop15d498e2016-09-07 20:49:52 -0400168 proc.expect (prompts)
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600169 proc.sendline (passwd)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600170
Brad Bishop15d498e2016-09-07 20:49:52 -0400171 if proc.expect(prompts + [pexpect.EOF]) != len(prompts):
172 proc.sendline (passwd)
173
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600174 r = proc.wait()
Brad Bishop15d498e2016-09-07 20:49:52 -0400175 return r if r else 0
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600176
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600177 @dbus.service.method(INTF_NAME, "", "as")
178 def UserList (self):
179 userList = []
180 with open("/etc/passwd", "r") as f:
181 for usent in f:
182 userParams = usent.split (":")
183 if (int(userParams[2]) >= 1000 and int(userParams[2]) != 65534):
184 userList.append(userParams[0])
185 return userList
186
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600187 def UserListAll (self):
188 userList = []
189 with open("/etc/passwd", "r") as f:
190 for usent in f:
191 userParams = usent.split (":")
192 userList.append(userParams[0])
193 return userList
194
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600195class UserManUser (dbus.service.Object):
196 def __init__(self, bus, name):
197 self.bus = bus
198 self.name = name
199 dbus.service.Object.__init__(self,bus,name)
200
201 @dbus.service.method(INTF_NAME, "", "")
202 def test(self):
203 print("TEST")
204
205 def setUsermanProvider(self, provider):
206 self.provider = provider
207
208 @dbus.service.method(INTF_NAME, "s", "x")
209 def UserDel (self, username):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600210 if not username : raise ValueError("Invalid Username")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600211
212 users = Usersobj.UserList ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600213 if username not in users : raise ValueError("No such User: ", username)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600214
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600215 r = call (["deluser", username])
216 return r
217
218 @dbus.service.method(INTF_NAME, "ss", "x")
219 def Passwd (self, username, passwd):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600220 if not username : raise ValueError("Invalid Username")
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600221
Adriana Kobylakcf8e1072016-02-12 10:55:01 -0600222 users = Usersobj.UserList ()
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600223 if username not in users : raise ValueError("No such User: ", username)
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600224
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600225 cmd = "passwd" + " " + username
226 proc = pexpect.spawn (cmd)
227 proc.expect (['New password: ', 'Retype password: '])
228 proc.sendline (passwd)
229 proc.expect (['New password: ', 'Retype password: '])
230 proc.sendline (passwd)
231
232 proc.wait()
Adriana Kobylakcf8e1072016-02-12 10:55:01 -0600233 return 0
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600234
235def main():
236 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
237 bus = dbus.SystemBus()
238 name = dbus.service.BusName(DBUS_NAME, bus)
239
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600240 global Groupsobj
241 global Groupobj
242 global Usersobj
243 global Userobj
244
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600245 Groupsobj = UserManGroups (bus, OBJ_NAME_GROUPS)
246 Groupobj = UserManGroup (bus, OBJ_NAME_GROUP)
247 Usersobj = UserManUsers (bus, OBJ_NAME_USERS)
248 Userobj = UserManUser (bus, OBJ_NAME_USER)
249
250 Groupsobj.setUsermanProvider ("pam")
251 Groupobj.setUsermanProvider ("pam")
252 Usersobj.setUsermanProvider ("pam")
253 Userobj.setUsermanProvider ("pam")
254
255 mainloop = gobject.MainLoop()
256 print("Started")
257 mainloop.run()
258
259if __name__ == '__main__':
260 sys.exit(main())
Adriana Kobylakcf8e1072016-02-12 10:55:01 -0600261