postinstall: crash early if the username already exists on the system

This commit is contained in:
Alexandre Aubin 2023-06-23 02:30:38 +02:00
parent bcd2550fdd
commit e87ee09b3e

View file

@ -16,6 +16,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pwd
import re
import os
import subprocess
@ -174,6 +175,12 @@ def tools_postinstall(
raw_msg=True,
)
# Crash early if the username is already a system user, which is
# a common confusion. We don't want to crash later and end up in an half-configured state.
all_existing_usernames = {x.pw_name for x in pwd.getpwall()}
if username in all_existing_usernames:
raise YunohostValidationError("system_username_exists")
if username in ADMIN_ALIASES:
raise YunohostValidationError(
f"Unfortunately, {username} cannot be used as a username", raw_msg=True