Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from subprocess import call |
| 4 | import sys |
| 5 | import subprocess |
| 6 | import dbus |
| 7 | import string |
| 8 | import os |
| 9 | import fcntl |
| 10 | import time |
| 11 | import pexpect |
| 12 | import glib |
| 13 | import gobject |
| 14 | import dbus.service |
| 15 | import dbus.mainloop.glib |
| 16 | |
| 17 | DBUS_NAME = 'org.openbmc.UserManager' |
| 18 | INTF_NAME = 'org.openbmc.Enrol' |
| 19 | OBJ_NAME_GROUPS = '/org/openbmc/UserManager/Groups' |
| 20 | OBJ_NAME_GROUP = '/org/openbmc/UserManager/Group' |
| 21 | OBJ_NAME_USERS = '/org/openbmc/UserManager/Users' |
| 22 | OBJ_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 R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 28 | Interface:Method > org.openbmc.Enrol.GroupListUsr |
| 29 | Interface:Method > org.openbmc.Enrol.GroupListSys |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 30 | 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 | |
| 40 | userman_providers = { |
George Keishing | c8d216a | 2016-09-20 15:38:08 -0500 | [diff] [blame] | 41 | 'pam' : { |
Adriana Kobylak | cf8e107 | 2016-02-12 10:55:01 -0600 | [diff] [blame] | 42 | 'adduser' : 'user add', |
| 43 | }, |
| 44 | 'ldap' : { |
| 45 | 'adduser' : 'ldap command to add user', |
George Keishing | c8d216a | 2016-09-20 15:38:08 -0500 | [diff] [blame] | 46 | }, |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | class 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 R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 64 | if not groupname : raise ValueError("Invalid Groupname") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 65 | |
| 66 | groups = self.GroupListAll () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 67 | if groupname in groups: raise ValueError("Group ", groupname, " Exists") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 68 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 69 | r = call (["addgroup", groupname]) |
| 70 | return r |
| 71 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 72 | #@dbus.service.method(INTF_NAME, "s", "x") |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 73 | def GroupAddSys (self, groupname): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 74 | if not groupname : raise ValueError("Invalid Groupname") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 75 | |
| 76 | groups = self.GroupListAll () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 77 | if groupname in groups: raise ValueError("Group ", groupname, " Exists") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 78 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 79 | r = call (["addgroup", "-S", groupname]) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 80 | return r |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 81 | |
| 82 | @dbus.service.method(INTF_NAME, "", "as") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 83 | def GroupListUsr (self): |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 84 | 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 R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 92 | @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 R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 109 | class 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 R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 124 | if not groupname : raise ValueError("Invalid Groupname") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 125 | |
| 126 | groups = Groupsobj.GroupListAll () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 127 | if groupname not in groups: raise ValueError("No such Group: ", groupname) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 128 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 129 | r = call (["delgroup", groupname]) |
| 130 | return r |
| 131 | |
| 132 | class 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 R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 147 | if not username : raise ValueError("Invalid Username") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 148 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 149 | users = self.UserListAll () |
| 150 | if username in users : raise ValueError("User ", username, " Exists") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 151 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 152 | if groupname: |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 153 | groups = Groupsobj.GroupListAll () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 154 | if groupname not in groups: raise ValueError("No such Group: ", groupname) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 155 | |
| 156 | opts = "" |
| 157 | if gecos: opts = " -g " + '"' + gecos + '"' |
| 158 | |
| 159 | if groupname: |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 160 | cmd = "adduser " + opts + " " + " -G " + groupname + " " + "-s /bin/sh" + " " + username |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 161 | else: |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 162 | cmd = "adduser " + opts + " " + "-s /bin/sh" + " " + username |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 163 | |
Brad Bishop | 15d498e | 2016-09-07 20:49:52 -0400 | [diff] [blame] | 164 | prompts = ['New password: ', 'Retype password: ', 'Re-enter new password: '] |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 165 | proc = pexpect.spawn (cmd) |
Brad Bishop | 15d498e | 2016-09-07 20:49:52 -0400 | [diff] [blame] | 166 | proc.expect (prompts) |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 167 | proc.sendline (passwd) |
Brad Bishop | 15d498e | 2016-09-07 20:49:52 -0400 | [diff] [blame] | 168 | proc.expect (prompts) |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 169 | proc.sendline (passwd) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 170 | |
Brad Bishop | 15d498e | 2016-09-07 20:49:52 -0400 | [diff] [blame] | 171 | if proc.expect(prompts + [pexpect.EOF]) != len(prompts): |
| 172 | proc.sendline (passwd) |
| 173 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 174 | r = proc.wait() |
Brad Bishop | 15d498e | 2016-09-07 20:49:52 -0400 | [diff] [blame] | 175 | return r if r else 0 |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 176 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 177 | @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 R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 187 | 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 R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 195 | class 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 R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 210 | if not username : raise ValueError("Invalid Username") |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 211 | |
| 212 | users = Usersobj.UserList () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 213 | if username not in users : raise ValueError("No such User: ", username) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 214 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 215 | r = call (["deluser", username]) |
| 216 | return r |
| 217 | |
| 218 | @dbus.service.method(INTF_NAME, "ss", "x") |
| 219 | def Passwd (self, username, passwd): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 220 | if not username : raise ValueError("Invalid Username") |
George Keishing | c8d216a | 2016-09-20 15:38:08 -0500 | [diff] [blame] | 221 | |
Adriana Kobylak | cf8e107 | 2016-02-12 10:55:01 -0600 | [diff] [blame] | 222 | users = Usersobj.UserList () |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 223 | if username not in users : raise ValueError("No such User: ", username) |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 224 | |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 225 | cmd = "passwd" + " " + username |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 226 | prompts = ['New password: ', 'Retype password: ', 'Re-enter new password: '] |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 227 | proc = pexpect.spawn (cmd) |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 228 | proc.expect (prompts) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 229 | proc.sendline (passwd) |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 230 | proc.expect (prompts) |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 231 | proc.sendline (passwd) |
| 232 | |
George Keishing | 97b1ae5 | 2016-09-16 23:21:53 -0500 | [diff] [blame] | 233 | if proc.expect(prompts + [pexpect.EOF]) != len(prompts): |
| 234 | proc.sendline (passwd) |
| 235 | |
| 236 | r = proc.wait() |
| 237 | return r if r else 0 |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 238 | |
| 239 | def main(): |
| 240 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 241 | bus = dbus.SystemBus() |
| 242 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 243 | |
Hariharasubramanian R | af89c09 | 2016-02-11 14:44:10 -0600 | [diff] [blame] | 244 | global Groupsobj |
| 245 | global Groupobj |
| 246 | global Usersobj |
| 247 | global Userobj |
| 248 | |
Hariharasubramanian R | 302f32c | 2016-02-10 07:50:45 -0600 | [diff] [blame] | 249 | Groupsobj = UserManGroups (bus, OBJ_NAME_GROUPS) |
| 250 | Groupobj = UserManGroup (bus, OBJ_NAME_GROUP) |
| 251 | Usersobj = UserManUsers (bus, OBJ_NAME_USERS) |
| 252 | Userobj = UserManUser (bus, OBJ_NAME_USER) |
| 253 | |
| 254 | Groupsobj.setUsermanProvider ("pam") |
| 255 | Groupobj.setUsermanProvider ("pam") |
| 256 | Usersobj.setUsermanProvider ("pam") |
| 257 | Userobj.setUsermanProvider ("pam") |
| 258 | |
| 259 | mainloop = gobject.MainLoop() |
| 260 | print("Started") |
| 261 | mainloop.run() |
| 262 | |
| 263 | if __name__ == '__main__': |
| 264 | sys.exit(main()) |
Adriana Kobylak | cf8e107 | 2016-02-12 10:55:01 -0600 | [diff] [blame] | 265 | |