🍽 Fork YunoHost — snapshot mangé par la machine à tsoins
Upstream: https://github.com/YunoHost/yunohost @ 3a5f8bac8301c450897b96cbd43a4c7d3ba750fb But (José) : transformer tout le code en bions + ploxions du xerboxion. La carte de digestion vit au labo : /yunohost-digest.json
This commit is contained in:
126
helpers/helpers.v2.1.d/systemuser
Normal file
126
helpers/helpers.v2.1.d/systemuser
Normal file
@@ -0,0 +1,126 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Copyright (c) 2024 YunoHost Contributors
|
||||
#
|
||||
# This file is part of YunoHost (see https://yunohost.org)
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
# Check if a user exists on the system
|
||||
#
|
||||
# usage: ynh_system_user_exists --username=username
|
||||
# | arg: --username= - the username to check
|
||||
# | ret: 0 if the user exists, 1 otherwise.
|
||||
ynh_system_user_exists() {
|
||||
# ============ Argument parsing =============
|
||||
local -A args_array=([u]=username=)
|
||||
local username
|
||||
ynh_handle_getopts_args "$@"
|
||||
# ===========================================
|
||||
|
||||
getent passwd "$username" &> /dev/null
|
||||
}
|
||||
|
||||
# Check if a group exists on the system
|
||||
#
|
||||
# usage: ynh_system_group_exists --group=group
|
||||
# | arg: --group= - the group to check
|
||||
# | ret: 0 if the group exists, 1 otherwise.
|
||||
ynh_system_group_exists() {
|
||||
# ============ Argument parsing =============
|
||||
local -A args_array=([g]=group=)
|
||||
local group
|
||||
ynh_handle_getopts_args "$@"
|
||||
# ===========================================
|
||||
|
||||
getent group "$group" &> /dev/null
|
||||
}
|
||||
|
||||
# Create a system user
|
||||
#
|
||||
# usage: ynh_system_user_create --username=user_name [--home_dir=home_dir] [--use_shell] [--groups="group1 group2"]
|
||||
# | arg: --username= - Name of the system user that will be create
|
||||
# | arg: --home_dir= - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home
|
||||
# | arg: --use_shell - Create a user using the default login shell if present. If this argument is omitted, the user will be created with /usr/sbin/nologin shell
|
||||
# | arg: --groups - Add the user to system groups. Typically meant to add the user to the ssh.app / sftp.app group (e.g. for borgserver, my_webapp)
|
||||
#
|
||||
# Create a nextcloud user with no home directory and /usr/sbin/nologin login shell (hence no login capability) :
|
||||
#
|
||||
# ```bash
|
||||
# ynh_system_user_create --username=nextcloud
|
||||
# ```
|
||||
#
|
||||
# Create a discourse user using /var/www/discourse as home directory and the default login shell :
|
||||
#
|
||||
# ```bash
|
||||
# ynh_system_user_create --username=discourse --home_dir=/var/www/discourse --use_shell
|
||||
# ```
|
||||
ynh_system_user_create() {
|
||||
# ============ Argument parsing =============
|
||||
local -A args_array=([u]=username= [h]=home_dir= [s]=use_shell [g]=groups=)
|
||||
local username
|
||||
local home_dir
|
||||
local use_shell
|
||||
local groups
|
||||
ynh_handle_getopts_args "$@"
|
||||
use_shell="${use_shell:-0}"
|
||||
home_dir="${home_dir:-}"
|
||||
groups="${groups:-}"
|
||||
# ===========================================
|
||||
|
||||
if ! ynh_system_user_exists --username="$username"; then # Check if the user exists on the system
|
||||
# If the user doesn't exist
|
||||
if [ -n "$home_dir" ]; then # If a home dir is mentioned
|
||||
local user_home_dir=(--home-dir "$home_dir")
|
||||
else
|
||||
local user_home_dir=(--no-create-home)
|
||||
fi
|
||||
if [ "$use_shell" -eq 1 ]; then # If we want a shell for the user
|
||||
local shell=() # Use default shell
|
||||
else
|
||||
local shell=(--shell /usr/sbin/nologin)
|
||||
fi
|
||||
useradd "${user_home_dir[@]}" --system --user-group "$username" "${shell[@]}" || ynh_die "Unable to create $username system account"
|
||||
fi
|
||||
|
||||
local group
|
||||
for group in $groups; do
|
||||
usermod -a -G "$group" "$username"
|
||||
done
|
||||
}
|
||||
|
||||
# Delete a system user
|
||||
#
|
||||
# usage: ynh_system_user_delete --username=user_name
|
||||
# | arg: --username= - Name of the system user that will be create
|
||||
ynh_system_user_delete() {
|
||||
# ============ Argument parsing =============
|
||||
local -A args_array=([u]=username=)
|
||||
local username
|
||||
ynh_handle_getopts_args "$@"
|
||||
# ===========================================
|
||||
|
||||
# Check if the user exists on the system
|
||||
if ynh_system_user_exists --username="$username"; then
|
||||
deluser "$username"
|
||||
else
|
||||
ynh_print_warn "The user $username was not found"
|
||||
fi
|
||||
|
||||
# Check if the group exists on the system
|
||||
if ynh_system_group_exists --group="$username"; then
|
||||
delgroup "$username"
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user