#!/bin/sh
# DocumentId:	$Id: update-twinspot-users-samba 1074 2001-08-20 08:41:19Z olalu $
# Author:	$Author: olalu $
# Date:		$Date: 2001-08-20 10:41:19 +0200 (mån, 20 aug 2001) $
# Summary:
#	Updates the shell accounts.

# Read the default twinspot config.
. /usr/share/twinspot/defaults.conf
. /usr/share/twinspot/change.func
. /usr/share/twinspot/owner.func

for TEST in / $DHOME /etc ; do
    if mount | grep "on[[:space:]][[:space:]]*$TEST[[:space:]].*type[[:space:]][[:space:]]*nfs[[:space:]]" > /dev/null 2>&1 ; then
	echo "Does not add or remove users when $TEST is nfsmounted."
	exit 0
    fi
done

if [ ! -e "$(which smbpasswd)" ] ; then
    echo "Samba not installed, skipping."
    exit 0
fi

# Users that should have shell access.
FUSERS=$(find $USERSRC -maxdepth 3 -path "$USERSRC/*/*" -type f -name "samba" | sed -e "s|/samba||;" | sed -e "s|.*/||;" | sort -u)

if [ ! -e $SMBF ] ; then
    if [ ! -z "$FUSERS" ] ; then
	echo "# SMB password file, generated by update-twinspot-users-samba." > $SMBF
	chown 600 $SMBF
    else
	echo "No samba users to configure, skipping."
	exit 0
    fi
fi

# Users that exist in smbpassword file.
# NOTE! This line has to be after FUSERS= and the $SMBF checking routine.
EUSERS=$(grep "^[^:]*:[^:]*:[5-9][0-9][0-9]:" $SMBF | sed -e "s|:.*||g;")

# When removing a user do the following:
# smbpasswd -x $USER

for RU in $EUSERS ; do
    UPID=$(grep "^$RU:" $PWDF | sed -e "s|^[^:]*:[^:]*:\([^:]*\):.*|\1|;")
    if [ $UPID -ge 1000 ] ; then
	echo "User $RU is a ordinary user and should not be removed."
	echo "THIS SHOULD NEVER HAPPEN, error in update script."
    elif [ $UPID -lt 500 ] ; then
	echo "User $RU is a system user and should not be removed."
	echo "THIS SHOULD NEVER HAPPEN, error in update script."
    elif ! echo "$FUSERS" | grep "^$RU$" > /dev/null 2>&1 ; then
	echo "Del user $RU from samba."
	smbpasswd -x $RU
	echo "Remove the profile information manually."
    fi
done

# Users that exist in smbpassword file.
EUSERS=$(grep "^[^:]*:[^:]*:[5-9][0-9][0-9]:" $SMBF | sed -e "s|:.*||g;")

# Modify the user information for all users that should and do exist.
for U in $FUSERS ; do
    . $USERSC
    if [ -f $USERSRC/$U ] ; then
	. $USERSRC/$U
    fi
    if [ ! -z "$SMBCRYPT" ] ; then
	SMBCRT=$(echo "$SMBCRYPT" | sed -e "s|:.*||;")
	# User should have samba access.
	if ! grep "^$U:" $SMBF > /dev/null 2>&1 ; then
	    echo "Adding user $U to smbpasswd."
	    UPID=$(grep "^$U:" $PWDF | sed -e "s|^[^:]*:[^:]*:\([^:]*\):.*|\1|;")
	    echo "$U:$UPID:$SMBCRYPT" >> $SMBF
	elif ! grep "^$U:[0-9]*:$SMBCRT" $SMBF > /dev/null 2>&1 ; then
	    echo "Updating password for user $U."
	    SMBCR=$(echo "$SMBCRYPT" | sed -e "s/|/\\|/g;")
	    changefile "s|^\($U:[0-9]*\):.*|\1:$SMBCR|;" $SMBF
	else
	    # User should not have samba access.
	    if ! grep "^$U:" $SMBF > /dev/null 2>&1 ; then
		echo "Samba not enabled for $U, removing user from smbpasswd."
		smbpasswd -x $U
	    fi
	fi
	if [ -e /etc/samba/smb.conf ] ; then
	    if grep "^$U:" $SMBF > /dev/null 2>&1 ; then
		# User should exist and 
		cat /etc/samba/smb.conf | grep -A 20 "^[[:space:]]*\[profiles\]" | grep -B 20 "^[[:space:]]*\[[^p]" | grep "^[[:space:]]*path[[:space:]]*=[[:space:]]*" | sed -e "s|^[[:space:]]*path[[:space:]]*=[[:space:]]*||;" | {
		    read PROFPATH
		    if [ ! -z "$PROFPATH" ] ; then
			changeowner $U $U $PROFPATH/$U
		    fi
		}
	    fi
	fi
    else
	# User should not have samba access.
	if grep "^$U:" $SMBF > /dev/null 2>&1 ; then
	    echo "No SMBCRYPT info for $U, removing user from smbpasswd."
	    smbpasswd -x $U
	fi
    fi
done
