#!/bin/sh
# DocumentId:	$Id: update-dpsyco-users-sshaccess,v 1.21 2002/07/05 13:00:45 ola Exp $
# Author:	$Author: ola $
# Date:		$Date: 2002/07/05 13:00:45 $
# Summary:
#	Updates the user access (via ssh).

# Read the default dpsyco config.
. /etc/dpsyco/defaults.conf

TESTNFS="/ $DHOME"
. /usr/share/dpsyco/checknfs.test

# Users that exist in password file.

IFS=:
cat $PWDF | while read user junk uid gid gecos home shell ; do
    if [ "$user" != "" ] \
	&& [ "$user" != "+" ] \
	&& [ "$uid" != "" ] \
	&& [ $uid -le $LAST_UID ] \
	&& sudo -u $user test -w $home \
	&& find "$USERSRC"/*/"$user" -printf "" 2>/dev/null
    then
	ISSH="$home/.ssh"
	RIDENT="$ISSH/identity"
	RIDENTP="$RIDENT.pub"
	DIDENT="$ISSH/id_dsa"
	DIDENTP="$DIDENT.pub"
	A1=authorized_keys
	AUTHK="$ISSH/$A1"
	A2=authorized_keys2
	AUTHK2="$ISSH/$A2"
	if [ ! -e $RIDENTP -o ! -e $DIDENTP ] ; then
	    echo "Gen authorized_keys for $user."
	    if [ ! -e $RIDENTP ] ; then
		sudo -H -u $user ssh-keygen -t rsa1 -N "" -f "$RIDENT"
	    fi
	    if [ ! -e $DIDENTP ] ; then
		sudo -H -u $user ssh-keygen -t dsa -N "" -f "$DIDENT"
	    fi
	fi
	if [ -e $ISSH ] ; then
	    > "$AUTHK"
	    find "$ISSH" "$USERSRC"/*/"$user" -name "*.pub" -type f \
	    | while read i ; do
		cat "$i" >> "$AUTHK"
	    done
	fi
	if [ ! -e "$AUTHK2" ] ; then
	    ln -s $A1 $AUTHK2
	fi
	chown $uid.$gid $AUTHK
	chown $uid.$gid $AUTHK2
    fi
done
