scripts/helios/sambasetup.sh

61 lines
1.8 KiB
Bash

# CHECKS
################################
DATADIR=$( sudo -u www-data php /var/www/nextcloud/occ config:system:get datadirectory ) || {
echo -e "Error reading data directory. Is NextCloud running and configured?";
}
[ -d "$DATADIR" ] || { echo -e "data directory $DATADIR not found" ; }
# CONFIG
################################
# remove files from this line to the end
sed -i '/# NextCloudSmb automatically/,/\$/d' /etc/samba/smb.conf
# restore this line
cat >> /etc/samba/smb.conf <<EOF
# NextCloudSmb automatically generated from here. Do not remove this comment
EOF
# create a share per Nextcloud user
USERS=()
while read -r path; do
USERS+=( "$( basename $( dirname "$path" ) )" )
done < <( ls -d "$DATADIR"/*/files )
for user in ${USERS[@]}; do
# Exclude users not matching group filter (if enabled)
if [[ -n "$FILTER_BY_GROUP" ]] \
&& [[ -z "$(ncc user:info "$user" --output=json | jq ".groups[] | select( . == \"${FILTER_BY_GROUP}\" )")" ]]
then
echo "Omitting user $user (not in group ${FILTER_BY_GROUP})...";
continue;
fi
echo "adding SAMBA share for user $user"
DIR="$DATADIR/$user/files"
[ -d "$DIR" ] || { echo -e "INFO: directory $DIR does not exist."; }
cat >> /etc/samba/smb.conf <<EOF
[home-$user]
path = $DIR
writeable = yes
valid users = $user
force user = www-data
force group = www-data
create mask = 0770
directory mask = 0771
force create mode = 0660
force directory mode = 0770
EOF
## create user with no login if it doesn't exist
id "$user" &>/dev/null || adduser --disabled-password --force-badname --gecos "" "$user"
echo -e "$PWD\n$PWD" | smbpasswd -s -a $user
usermod -aG www-data $user
sudo chmod g+w $DIR
done
systemctl enable --now smbd
systemctl enable --now nmbd
echo "SMB enabled"