Author: amit Date: 2005-08-04 04:22:51 +0000 (Thu, 04 Aug 2005) New Revision: 392
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=lorikeet&rev=392 Log: new class added to manage the group. creation , deletion ang group mapping if possible Added: trunk/sangria/src/LatestDesign/classes/GroupManager/ trunk/sangria/src/LatestDesign/classes/GroupManager/GroupManagerClass.py Changeset: Added: trunk/sangria/src/LatestDesign/classes/GroupManager/GroupManagerClass.py =================================================================== --- trunk/sangria/src/LatestDesign/classes/GroupManager/GroupManagerClass.py 2005-08-02 19:34:41 UTC (rev 391) +++ trunk/sangria/src/LatestDesign/classes/GroupManager/GroupManagerClass.py 2005-08-04 04:22:51 UTC (rev 392) @@ -0,0 +1,207 @@ + +import sys,os,commands +import string + + +class GroupManager: + + def __init__(self, GroupFile): + self.GroupFile = GroupFile + + ### method for UNIX group Handling ... + + def AddUnixGroup(self, GroupName): + ''' add a user group ''' + + cmd = "groupadd %s" %(GroupName) + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + return 1 + + def ListUnixGroups(self): + + list = [] + groupFile = file(self.GroupFile , 'r+') + line = groupFile.readline() + while line: + split = string.split(line , ':') + list.append(split[0]) + line = groupFile.readline() + groupFile.close() + return list + + + def CheckIfUnixGroupExist(self, GroupName): + for name in self.ListUnixGroups(): + if name == GroupName: + return 1 + return 0 + + + def RemoveUnixGroup(self, GroupName): + + cmd = "groupdel %s" %(GroupName) + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + return 1 + + + def AddUserToUnixGroup(self, UserName , GroupName): + cmd = "usermod -G %s %s"%( GroupName , UserName) + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + return 1 + + def RemoveUserFromUnixGroup(self, UserName , GroupName): + ''' delete the userName from the line corresponding to the GroupName in /etc/group + or + know all users in that group and then delete the group and add the group for all other users ... + ''' + + ret = self.CheckIfUnixGroupExist(GroupName) + if not ret: + return 0 + + users_in_group = self.ListUsersInUnixGroup(GroupName) + if not users_in_group: + return 0 + ret = self.RemoveUnixGroup(GroupName) + if ret: + ret = self.AddUnixGroup(GroupName) + + for user in users_in_group: + if not user == UserName: + self.AddUserToUnixGroup(user, GroupName) + + def ListUsersInUnixGroup(self , GroupName): + ''' the slash n at the end is giving problems ''' + ret = self.CheckIfUnixGroupExist(GroupName) + if not ret: + return 0 + list = [] + groupFile = file(self.GroupFile , 'r+') + line = groupFile.readline() + while line: + split = string.split(line , ':') + if split[0] == GroupName: + resplit = string.split(split[3] , ',') + for i in range(0 , len(resplit)): + list.append(string.strip(resplit[i])) + return list + line = groupFile.readline() + groupFile.close() + return 0 + + + + ### methods for SAMBA group Handling ... + + def GetMaxRID(self): + ''' returns the maximum RID value ''' + + cmd = "net maxrid" + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + split = string.split(cmd_output , ":") + rid = string.atoi(split[1]) + return rid + + + def AddSambaGroup(self, sambagroupnameORntgroup , unixgroup , type = None ): + '''{rid=int|sid=string} unixgroup=string [type={domain|local}] [ntgroup=string] [comment=string]''' + + rid = self.GetMaxRID() + 1 + cmd = "net groupmap add ntgroup=\"%s\" unixgroup=\"%s\" rid=%d"%(sambagroupnameORntgroup,unixgroup , rid) + if type: + cmd = cmd+" type=%s"%type + + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + return 1 + + + def ModifySambaGroup(self, sambagroupnameORntgroup , unixgroup=None , type=None): + ''' possible things you can do with this ... + for a samba group name .. change the Unix group ... and the type ... + {ntgroup=string|sid=SID} [unixgroup=string] [comment=string] [type={domain|local}] ''' + + cmd = "net groupmap modify ntgroup=%s"%(sambagroupnameORntgroup) + if type: + cmd = cmd+" type=%s"%type + if unixgroup: + cmd = cmd+" unixgroup=%s"%unixgroup + + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + return 1 + + def RemoveSambaGroup(self, sambagroupnameORntgroup ): + ''' {ntgroup=string|sid=SID}''' + + cmd = "net groupmap delete ntgroup=\"%s\""%sambagroupnameORntgroup + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + return 1 + + def ListSambaGroups(self, ntgroup = None , SID = None): + '''[verbose] [ntgroup=string] [sid=SID]''' + + cmd = "net groupmap list" + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + + print cmd_output + return 1 + + def ModifyUserSID(self, UserName , SID): + + cmd = "pdbedit -U %s -u %s"%( SID , UserName) + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + return 1 + + + def ModifyGroupSID(self, UserName , SID): + cmd = "pdbedit -G %s -u %s"%( SID , UserName) + ( status, cmd_output ) = commands.getstatusoutput( cmd ) + if status: + print cmd_output + return 0 + return 1 + + def AddGroupMap_WINgroupToUnixGroup(self, WinGroup , UnixGroup , Type = None): + return self.AddSambaGroup(WinGroup , UnixGroup , Type) + + def ModifyGroupMap_WINgroupToUnixGroup(self, WinGroup , UnixGroup , Type = None): + self.ModifySambaGroup(WinGroup , UnixGroup , Type) + + def RemoveGroupMap_WINgroupToUnixGroup(self, WinGroup): + self.RemoveSambaGroup(WinGroup) + + + +