blob: 033d3d1d258a4e6157e4e02d1490f7eccc9247da [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 = {
41 'pam' : {
42 'adduser' : 'user add',
43 },
44 'ldap' : {
45 'adduser' : 'ldap command to add user',
46 },
47}
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 Raf89c092016-02-11 14:44:10 -060064 if not groupname : return 1
65
66 groups = self.GroupListAll ()
67 if groupname in groups: return 1
68
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060069 r = call (["addgroup", groupname])
70 return r
71
72 @dbus.service.method(INTF_NAME, "s", "x")
73 def GroupAddSys (self, groupname):
Hariharasubramanian Raf89c092016-02-11 14:44:10 -060074 if not groupname : return 1
75
76 groups = self.GroupListAll ()
77 if groupname in groups: return 1
78
Hariharasubramanian R302f32c2016-02-10 07:50:45 -060079 r = call (["addgroup", "-S", groupname])
80 return 0
81
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 Raf89c092016-02-11 14:44:10 -0600124 if not groupname : return 1
125
126 groups = Groupsobj.GroupListAll ()
127 if groupname not in groups: return 1
128
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 Raf89c092016-02-11 14:44:10 -0600147 if not username: return 1
148
149 users = self.UserList ()
150 if username in users : return 1
151
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600152 if groupname:
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600153 groups = Groupsobj.GroupListAll ()
154 if groupname not in groups: return 1
155
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
164 proc = pexpect.spawn (cmd)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600165 proc.expect (['New password: ', 'Retype password: '])
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600166 proc.sendline (passwd)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600167 proc.expect (['New password: ', 'Retype password: '])
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600168 proc.sendline (passwd)
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600169
170 proc.wait()
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600171 return 0
172
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600173 @dbus.service.method(INTF_NAME, "", "as")
174 def UserList (self):
175 userList = []
176 with open("/etc/passwd", "r") as f:
177 for usent in f:
178 userParams = usent.split (":")
179 if (int(userParams[2]) >= 1000 and int(userParams[2]) != 65534):
180 userList.append(userParams[0])
181 return userList
182
183class UserManUser (dbus.service.Object):
184 def __init__(self, bus, name):
185 self.bus = bus
186 self.name = name
187 dbus.service.Object.__init__(self,bus,name)
188
189 @dbus.service.method(INTF_NAME, "", "")
190 def test(self):
191 print("TEST")
192
193 def setUsermanProvider(self, provider):
194 self.provider = provider
195
196 @dbus.service.method(INTF_NAME, "s", "x")
197 def UserDel (self, username):
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600198 if not username : return 1
199
200 users = Usersobj.UserList ()
201 if username not in users : return 1
202
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600203 r = call (["deluser", username])
204 return r
205
206 @dbus.service.method(INTF_NAME, "ss", "x")
207 def Passwd (self, username, passwd):
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600208 if not username : return 1
209
210 users = self.UserList ()
211 if username not in users : return 1
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600212
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600213 cmd = "passwd" + " " + username
214 proc = pexpect.spawn (cmd)
215 proc.expect (['New password: ', 'Retype password: '])
216 proc.sendline (passwd)
217 proc.expect (['New password: ', 'Retype password: '])
218 proc.sendline (passwd)
219
220 proc.wait()
221 return r
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600222
223def main():
224 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
225 bus = dbus.SystemBus()
226 name = dbus.service.BusName(DBUS_NAME, bus)
227
Hariharasubramanian Raf89c092016-02-11 14:44:10 -0600228 global Groupsobj
229 global Groupobj
230 global Usersobj
231 global Userobj
232
Hariharasubramanian R302f32c2016-02-10 07:50:45 -0600233 Groupsobj = UserManGroups (bus, OBJ_NAME_GROUPS)
234 Groupobj = UserManGroup (bus, OBJ_NAME_GROUP)
235 Usersobj = UserManUsers (bus, OBJ_NAME_USERS)
236 Userobj = UserManUser (bus, OBJ_NAME_USER)
237
238 Groupsobj.setUsermanProvider ("pam")
239 Groupobj.setUsermanProvider ("pam")
240 Usersobj.setUsermanProvider ("pam")
241 Userobj.setUsermanProvider ("pam")
242
243 mainloop = gobject.MainLoop()
244 print("Started")
245 mainloop.run()
246
247if __name__ == '__main__':
248 sys.exit(main())