#!/usr/bin/env bash set -Eeuo pipefail : "${SAMBA:="Y"}" # Enable Samba : "${SAMBA_LEVEL:="1"}" # Logging level : "${SAMBA_DEBUG:="N"}" # Disable debug : "${SAMBA_UID:="1000"}" # Samba user ID : "${SAMBA_GID:="1000"}" # Samba group ID tmp="/tmp/smb" rm -rf "$tmp" rm -f /var/run/wsdd.pid rm -f /var/run/samba/nmbd.pid rm -f /var/run/samba/smbd.pid [[ "$SAMBA" == [Nn]* ]] && return 0 [[ "$NETWORK" == [Nn]* ]] && return 0 if [[ "$DHCP" == [Yy1]* ]]; then socket="$IP" hostname="$IP" interfaces="$VM_NET_DEV" else hostname="host.lan" case "${NETWORK,,}" in "passt" | "slirp" ) interfaces="lo" socket="127.0.0.1" ;; *) socket="$VM_NET_IP" interfaces="$VM_NET_BRIDGE" ;; esac if [ -n "${SAMBA_INTERFACE:-}" ]; then interfaces+=",$SAMBA_INTERFACE" fi fi html "Initializing shared folder..." [[ "$DEBUG" == [Yy1]* ]] && echo "Starting Samba daemon..." addShare() { local dir="$1" local ref="$2" local name="$3" local comment="$4" local user="$5" local group="$6" local cfg="$7" mkdir -p "$dir" || return 1 if ! ls -A "$dir" >/dev/null 2>&1; then error "Failed to access directory $dir" && return 1 fi if [ -z "$(ls -A "$dir")" ]; then if ! chmod 777 "$dir"; then error "Failed to set permissions for directory $dir" && return 1 fi if [[ "$user" != "root" || "$group" != "root" ]]; then if ! chown "$user:$group" "$dir" ; then error "Failed to set ownership for directory $dir" && return 1 fi fi fi if [[ "$dir" == "$tmp" ]]; then { echo "--------------------------------------------------------" echo " $APP for $ENGINE v$( "$dir/readme.txt" fi { echo "" echo "[$name]" echo " path = $dir" echo " comment = $comment" echo " writable = yes" echo " guest ok = yes" echo " guest only = yes" echo " force user = $user" echo " force group = $group" } >> "$cfg" return 0 } addUser() { local username="$1" local uid="$2" local groupname="$3" local gid="$4" local password="$1" local cfg="$5" # Check if the group exists, if not, create it if ! getent group "$groupname" &>/dev/null; then if ! groupadd -o -g "$gid" "$groupname" > /dev/null; then error "Failed to create group $groupname" && return 1 fi else # Check if the gid is right, if not, change it local current_gid current_gid=$(getent group "$groupname" | cut -d: -f3) if [[ "$current_gid" != "$gid" ]]; then if ! groupmod -o -g "$gid" "$groupname" > /dev/null; then error "Failed to update GID for group $groupname" && return 1 fi fi fi # Check if the user already exists, if not, create it if ! id "$username" &>/dev/null; then if ! adduser --gid "$gid" --uid "$uid" --comment "$username" --no-create-home --disabled-login "$username"; then error "Failed to create user $username" && return 1 fi else # Check if the uid is right, if not, change it local current_uid current_uid=$(id -u "$username") if [[ "$current_uid" != "$uid" ]]; then if ! usermod -o -u "$uid" "$username" > /dev/null; then error "Failed to update UID for user $username" && return 1 fi fi # Update user's group if ! usermod -g "$groupname" "$username" > /dev/null; then echo "Failed to update group for user $username" && return 1 fi fi # Check if the user is a samba user pdb_output=$(pdbedit -s "$cfg" -L) if echo "$pdb_output" | grep -q "^$username:"; then # skip samba password update if password is * or ! if [[ "$password" != "*" && "$password" != "!" ]]; then # If the user is a samba user, update its password in case it changed if ! echo -e "$password\n$password" | smbpasswd -c "$cfg" -s "$username" > /dev/null; then error "Failed to update Samba password for $username" && return 1 fi fi else # If the user is not a samba user, create it and set a password if ! echo -e "$password\n$password" | smbpasswd -a -c "$cfg" -s "$username" > /dev/null; then error "Failed to add Samba user $username" && return 1 fi fi return 0 } SAMBA_USER="root" SAMBA_GROUP="root" SAMBA_CONFIG="/etc/samba/smb.conf" { echo "[global]" echo " server string = Dockur" echo " netbios name = $hostname" echo " workgroup = WORKGROUP" echo " interfaces = $interfaces" echo " bind interfaces only = yes" echo " security = user" echo " guest account = nobody" echo " map to guest = Bad User" echo " server min protocol = NT1" echo " follow symlinks = yes" echo " wide links = yes" echo " unix extensions = no" echo " socket address = $socket" echo "" echo " # disable printing services" echo " load printers = no" echo " printing = bsd" echo " printcap name = /dev/null" echo " disable spoolss = yes" } > "$SAMBA_CONFIG" # Setup user and group if [[ "$SAMBA_UID" != "1000" || "$SAMBA_GID" != "1000" ]]; then SAMBA_USER="samba" SAMBA_GROUP="samba" ! addUser "$SAMBA_USER" "$SAMBA_UID" "$SAMBA_GROUP" "$SAMBA_GID" "$SAMBA_CONFIG" && return 0 fi # Add shared folders share="/shared" [ ! -d "$share" ] && [ -d "$STORAGE/shared" ] && share="$STORAGE/shared" [ ! -d "$share" ] && [ -d "/data" ] && share="/data" [ ! -d "$share" ] && [ -d "$STORAGE/data" ] && share="$STORAGE/data" [ ! -d "$share" ] && share="$tmp" m1="Failed to add shared folder" m2="Please check its permissions." if ! addShare "$share" "/shared" "Data" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG"; then error "$m1 '$share'. $m2" && return 0 fi if [ -d "/shared2" ]; then addShare "/shared2" "/shared2" "Data2" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/shared2'. $m2" else if [ -d "/data2" ]; then addShare "/data2" "/shared2" "Data2" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/data2'. $m2." fi fi if [ -d "/shared3" ]; then addShare "/shared3" "/shared3" "Data3" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/shared3'. $m2" else if [ -d "/data3" ]; then addShare "/data3" "/shared3" "Data3" "Shared" "$SAMBA_USER" "$SAMBA_GROUP" "$SAMBA_CONFIG" || error "$m1 '/data3'. $m2" fi fi IFS=',' read -r -a dirs <<< "${SHARES:-}" for dir in "${dirs[@]}"; do [ ! -d "$dir" ] && continue dir_name=$(basename "$dir") addShare "$dir" "/shared" "$dir_name" "Shared $dir_name" || error "Failed to create shared folder for $dir!" done # Create directories if missing mkdir -p /var/lib/samba/sysvol mkdir -p /var/lib/samba/private mkdir -p /var/lib/samba/bind-dns # Try to repair Samba permissions [ -d /run/samba/msg.lock ] && chmod -R 0755 /run/samba/msg.lock 2>/dev/null || : [ -d /var/log/samba/cores ] && chmod -R 0700 /var/log/samba/cores 2>/dev/null || : [ -d /var/cache/samba/msg.lock ] && chmod -R 0755 /var/cache/samba/msg.lock 2>/dev/null || : rm -f /var/log/samba/log.smbd if ! smbd -l /var/log/samba; then SAMBA_DEBUG="Y" error "Failed to start Samba daemon!" fi if [[ "$SAMBA_DEBUG" == [Yy1]* ]]; then tail -fn +0 /var/log/samba/log.smbd --pid=$$ & fi case "${NETWORK,,}" in "passt" | "slirp" ) return 0 ;; esac if [[ "${BOOT_MODE:-}" == "windows_legacy" ]]; then # Enable NetBIOS on Windows 7 and lower [[ "$DEBUG" == [Yy1]* ]] && echo "Starting NetBIOS daemon..." rm -f /var/log/samba/log.nmbd if ! nmbd -l /var/log/samba; then SAMBA_DEBUG="Y" error "Failed to start NetBIOS daemon!" fi if [[ "$SAMBA_DEBUG" == [Yy1]* ]]; then tail -fn +0 /var/log/samba/log.nmbd --pid=$$ & fi else # Enable Web Service Discovery on Vista and up [[ "$DEBUG" == [Yy1]* ]] && echo "Starting wsddn daemon..." rm -f /var/log/wsddn.log if ! wsddn -i "${interfaces%%,*}" -H "$hostname" --unixd --log-file=/var/log/wsddn.log --pid-file=/var/run/wsdd.pid; then SAMBA_DEBUG="Y" error "Failed to start wsddn daemon!" fi if [[ "$SAMBA_DEBUG" == [Yy1]* ]]; then tail -fn +0 /var/log/wsddn.log --pid=$$ & fi fi return 0