Exemplo n.º 1
0
    def add_user(self, username, name, password, require_password_reset,
                 primary_group_name, location, work_phone, home_phone):
        """Add C{username} to the computer.

        @raises UserManagementError: Raised when C{adduser} fails.
        @raises UserManagementError: Raised when C{passwd} fails.
        """
        logging.info("Adding user %s.", username)
        gecos = "%s,%s,%s,%s" % (name, location or "", work_phone
                                 or "", home_phone or "")
        command = [
            "adduser", username, "--disabled-password", "--gecos", gecos
        ]
        if primary_group_name:
            command.extend(
                ["--gid",
                 str(self._provider.get_gid(primary_group_name))])
        result, output = self.call_popen(command)
        if result != 0:
            raise UserManagementError("Error adding user %s.\n%s" %
                                      (username, output))

        self._set_password(username, password)
        if require_password_reset:
            result, new_output = self.call_popen(["passwd", username, "-e"])
            if result != 0:
                raise UserManagementError("Error resetting password for user "
                                          "%s.\n%s" % (username, new_output))
            else:
                output += new_output
        return output
Exemplo n.º 2
0
    def set_user_details(self,
                         username,
                         password=None,
                         name=None,
                         location=None,
                         work_number=None,
                         home_number=None,
                         primary_group_name=None):
        """Update details for the account matching C{uid}."""
        uid = self._provider.get_uid(username)
        logging.info("Updating data for user %s (UID %d).", username, uid)
        if password:
            self._set_password(username, password)

        if primary_group_name:
            self._set_primary_group(username, primary_group_name)

        command = ["chfn"]
        for option, value in [("-r", location), ("-f", name),
                              ("-w", work_number), ("-h", home_number)]:
            if value is not None:
                command += [option, value]

        if len(command) > 1:
            result, output = self.call_popen(command + [username])
            if result != 0:
                raise UserManagementError("Error setting details for user "
                                          "%s.\n%s" % (username, output))
            return output
Exemplo n.º 3
0
 def _set_password(self, username, password):
     password = password.encode("utf-8")
     crypted = md5_crypt.encrypt(password)
     result, output = self.call_popen(["usermod", "-p", crypted, username])
     if result != 0:
         raise UserManagementError("Error setting password for user "
                                   "%s.\n%s" % (username, output))
     return output
Exemplo n.º 4
0
 def add_group(self, groupname):
     """Add C{group} with the C{addgroup} system command."""
     logging.info("Adding group %s.", groupname)
     result, output = self.call_popen(["addgroup", groupname])
     if result != 0:
         raise UserManagementError("Error adding group %s.\n%s" %
                                   (groupname, output))
     return output
Exemplo n.º 5
0
 def _set_primary_group(self, username, groupname):
     primary_gid = self._provider.get_gid(groupname)
     command = ["usermod", "-g", str(primary_gid), username]
     result, output = self.call_popen(command)
     if result != 0:
         raise UserManagementError("Error setting primary group to %d for"
                                   "%s.\n%s" %
                                   (primary_gid, username, output))
     return output
Exemplo n.º 6
0
 def remove_group(self, groupname):
     """Remove the account matching C{groupname} from the computer."""
     gid = self._provider.get_gid(groupname)
     logging.info("Removing group %s (GID %d).", groupname, gid)
     result, output = self.call_popen(["groupdel", groupname])
     if result != 0:
         raise UserManagementError("Error removing group %s (GID %d).\n%s" %
                                   (groupname, gid, output))
     return output
Exemplo n.º 7
0
 def lock_user(self, username):
     """
     Lock the account matching C{username} to prevent them from logging in.
     """
     uid = self._provider.get_uid(username)
     logging.info("Locking out user %s (UID %d).", username, uid)
     result, output = self.call_popen(["usermod", "-L", username])
     if result != 0:
         raise UserManagementError("Error locking user %s.\n%s" %
                                   (username, output))
Exemplo n.º 8
0
    def unlock_user(self, username):
        """Unlock the account matching C{username}."""
        uid = self._provider.get_uid(username)
        logging.info("Unlocking user %s (UID %d).", username, uid)

        result, output = self.call_popen(["usermod", "-U", username])
        if result != 0:
            raise UserManagementError("Error unlocking user %s.\n%s" %
                                      (username, output))
        return output
Exemplo n.º 9
0
 def set_group_details(self, groupname, new_name):
     """Update details for the group matching C{gid}."""
     gid = self._provider.get_gid(groupname)
     logging.info("Renaming group %s (GID %d) to %s.", groupname, gid,
                  new_name)
     command = ["groupmod", "-n", new_name, groupname]
     result, output = self.call_popen(command)
     if result != 0:
         raise UserManagementError("Error renaming group %s (GID %d) to "
                                   "%s.\n%s" %
                                   (groupname, gid, new_name, output))
     return output
Exemplo n.º 10
0
 def _set_password(self, username, password):
     chpasswd_input = "{}:{}".format(username, password).encode("utf-8")
     chpasswd = self._provider.popen(["chpasswd"],
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
     output, stderr = chpasswd.communicate(chpasswd_input)
     result = chpasswd.returncode
     if result != 0:
         username = username.encode("utf-8")
         raise UserManagementError(
             "Error setting password for user {}.\n{} {}".format(
                 username, output, stderr))
     return output
Exemplo n.º 11
0
 def remove_group_member(self, username, groupname):
     """
     Remove the user matching C{username} from the group matching
     C{groupname} with the C{gpasswd} system command.
     """
     uid = self._provider.get_uid(username)
     gid = self._provider.get_gid(groupname)
     logging.info("Removing user %s (UID %d) from group %s (GID %d).",
                  username, uid, groupname, gid)
     result, output = self.call_popen(
         ["gpasswd", "-d", username, groupname])
     if result != 0:
         raise UserManagementError("Error removing user %s (UID %d) "
                                   "from group %s (GID (%d).\n%s" %
                                   (username, uid, groupname, gid, output))
     return output
Exemplo n.º 12
0
 def add_group_member(self, username, groupname):
     """
     Add the user matching C{username} to the group matching C{groupname}
     with the C{gpasswd} system command.
     """
     uid = self._provider.get_uid(username)
     gid = self._provider.get_gid(groupname)
     logging.info("Adding user %s (UID %d) to group %s (GID %d).", username,
                  uid, groupname, gid)
     result, output = self.call_popen(
         ["gpasswd", "-a", username, groupname])
     if result != 0:
         raise UserManagementError("Error adding user %s (UID %d) to "
                                   "group %s (GID %d).\n%s" %
                                   (username, uid, groupname, gid, output))
     return output
Exemplo n.º 13
0
    def remove_user(self, username, delete_home=False):
        """Remove the account matching C{username} from the computer."""
        uid = self._provider.get_uid(username)
        command = ["deluser", username]
        if delete_home:
            logging.info(
                "Removing user %s (UID %d) and deleting their home "
                "directory.", username, uid)
            command.append("--remove-home")
        else:
            logging.info(
                "Removing user %s (UID %d) without deleting their "
                "home directory.", username, uid)

        result, output = self.call_popen(command)
        if result != 0:
            raise UserManagementError("Error removing user %s (UID %d).\n%s" %
                                      (username, uid, output))
        return output