Example #1
0
 def cs_add_user(self, username, group, password, extragroups=[],
                 name=None, homedir=None, policy={}):
     """
     This function returns a ChangeSet for when a new user account
     needs to be created. It prepares the arguments that need to be
     passed to the useradd utility based on the arguments given and
     the users' group policy.
     
     @param username: The username of the new account
     @param group: The primary group name
     @param password: The password for the account in crypt hash format
     @param extragroups: Extra groups that the new account should belong to
     @param name: Real name of the accont owner
     @param homedir: Custom home directory for this account
     @param policy: Additional parameters that override the group policy
     @return: A ChangeSet
     """
     # create a copy of the group policy so it can be safely edited
     upolicy = copy.deepcopy(self.pt.policy['groups'].get([group]))
     # place the given arguments into a policy dictionary
     args = {'username': username, 'group': group, 'name': name,
             'extragroups': extragroups, 'homedir': homedir, 'password': password}
     
     # overlay the group policy with any values specified by the user
     upolicy = merge_into(upolicy, policy)
     # overlay the policy with the arguments (username, password, ..)
     args = merge_into(upolicy, args)
     
     # construct the final home directory path
     if args['grouphomes'] == True:
         args['basedir'] = args['basedir'] + '/' + args['group']
     if args['name'] is None:
         args['name'] = ''
     if args['homedir'] is None:
         args['homedir'] = args['basedir'] + '/' + args['username']
     
     # prepare the ChangeSet
     cs = ChangeSet(Change(self.name, "add_user", args))
     # notify the PolicyTool with a USER_ADDED event
     self.pt.emit_event(syspolicy.event.USER_ADDED, cs)
     
     return cs
Example #2
0
 def cs_mod_user(self, username, group=None, extragroups=[],
                 password=None, name=None, homedir=None, policy={}):
     """
     This function returns a ChangeSet which performs user modification.
     
     It prepares any given arguments and checks also if changing the 
     primary group was requested. In case the primary group is to be
     changed, the function also includes the differences from the old
     to the new group policy, keeping the account up to date (eg. quota).
     
     @param username: The username of the account to be modified
     @param group: The new primary group name
     @param password: The new password for the account in crypt hash format
     @param extragroups: Extra groups that the new account should belong to
     @param name: Real name of the accont owner
     @param homedir: Custom home directory for this account
     @param policy: Additional parameters that override the group policy
     @return: A ChangeSet
     """
     # retrieve information about the users current group
     oldgid = get_user_by_name(username).pw_gid
     oldgroup = get_group_by_id(oldgid).gr_name
     
     # merge the given arguments into dict args
     args = {'username': username}
     args = merge_into(args, policy)
     
     # if changing the primary group
     if group is not None:
         args['group'] = group
         args['oldgroup'] = oldgroup
         args['extragroups'] = extragroups
         
         # find out the differences between the old and the new policy
         npol = self.pt.policy['groups'].get([group])
         opol = self.pt.policy['groups'].get([oldgroup])
         diff = compare_trees(npol, opol)
         
         # merge in the attributes that have new values
         for attr in diff:
             args[attr] = diff[attr]
     if password is not None:
         args['password'] = password
     if name is not None:
         args['name'] = name
     if homedir is not None:
         args['homedir'] = homedir
     
     # prepare the ChangeSet
     cs = ChangeSet(Change(self.name, "mod_user", args))
     # notify the PolicyTool with a USER_MODIFIED event
     self.pt.emit_event(syspolicy.event.USER_MODIFIED, cs)
     return cs
Example #3
0
 def cs_del_group(self, group):
     """
     This function returns a ChangeSet which removes a group.
     
     @param group: The name of the group to be removed
     @return: A ChangeSet
     """
     gpolicy = copy.deepcopy(self.pt.policy['groups'].get([group]))
     args = merge_into(gpolicy, {'group': group})
     
     cs = ChangeSet(Change(self.name, "del_group", args))
     # notify the PolicyTool with a GROUP_REMOVED event
     self.pt.emit_event(syspolicy.event.GROUP_REMOVED, cs)
     return cs
Example #4
0
 def cs_del_user(self, username):
     """
     This function returns a ChangeSet which performs user removal.
     
     @param username: The user account to be removed.
     @return: A ChangeSet
     """
     # retrieve information about the users current group
     gid = get_user_by_name(username).pw_gid
     group = get_group_by_id(gid).gr_name
     gpolicy = copy.deepcopy(self.pt.policy['groups'].get([group]))
     
     # prepare the arguments
     args = {'username': username, 'group': group}
     args = merge_into(gpolicy, args)
     
     # prepare the ChangeSet
     cs = ChangeSet(Change(self.name, "del_user", args))
     # notify the PolicyTool with a USER_REMOVED event
     self.pt.emit_event(syspolicy.event.USER_REMOVED, cs)
     return cs