#!/bin/sh
# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# Author: Daniel Robbins <drobbins@gentoo.org>
# $Header: /home/cvsroot/gentoo-src/keychain/keychain,v 1.21 2002/08/17 22:35:34 drobbins Exp $
#
#
# Tue Sep  3 12:27:04 CEST 2002
# Modified from keychain by Patrice Dumas dumas@centre-cired.fr
# renamed it fake-agent. Removed a lot of things. Kept agent pid detection
# setting of variables and detection of files. Added support for pam-ssh
# and detection of SSH_AUTH_SOCK

PATH="/sbin:/usr/sbin:${PATH}:/usr/ucb"; export PATH;

shelltype=sh

while [ -n "$1" ]
do
	case $1 in 
		-c) shelltype=csh ;;
		-s) ;;
		*) if [ -f $1 ]
			then
				pidfiles="$pidfiles $1"
			fi
		;;
	esac
shift
done

# Query local host for operating system.
cygwin="no"
hpux="no"
myuname=`uname -s`
if [ -n "`echo $myuname | grep CYGWIN`" ]
then
	cygwin="yes"
elif [ -n "`echo $myuname | grep HP-UX`" ]
then
	hpux="yes"
fi

# Find ssh-agent pid

if [ "$cygwin" = "yes" ]
then
	#for cygwin
	psopts="-e -u `whoami` -f"
elif [ "$hpux" = "yes" ]
then	
	#for hp-ux
	psopts="-u `whoami` -f"
else
	#This should work under Linux, BSD, others
	psopts="FAIL"
	ps -u `whoami` -f  >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
		psopts="-u `whoami` -f"
	else
		ps -uxw >/dev/null 2>&1
		if [ $? -eq 0 ]
		then
			psopts="-uxw"
		fi
	fi
	if [ "$psopts" = "FAIL" ]
	then
#		echo "$0: unable to use \"ps\" to scan for ssh-agent processes.  Report keychain version and"
#		echo "system configuration to drobbins@gentoo.org."
		exit 1
	fi
fi

mypids=`ps $psopts 2>/dev/null | grep "[s]sh-agent"` > /dev/null 2>&1
#extract the second item from mypids:
if [ -n "$mypids" ]
then
	set $mypids
	mypids=$2
else
	exit 1
fi

# pidfiles are the files with environment information for ssh-agent.
# we try files generated by pam_ssh or keychain

hostname=`uname -n`

keychaindir="${HOME}/.keychain"
keychainshfile="${keychaindir}/${hostname}-sh"
keychaincshfile="${keychaindir}/${hostname}-csh"

if [ "$shelltype" = "sh" ]
then
	keychainfile="${keydir}/${hostname}-sh"
elif [ "$shelltype" = "csh" ]
then
	keychainfile="${keydir}/${hostname}-csh"
fi

pamsshdir="${HOME}/.ssh"
pamsshfile="${pamsshdir}/agent-${hostname}"

pidfiles="$pidfiles $pamsshfile $keychainshfile"

#set the name of the variables containing auth sock and pid information
SSH_AUTH_SOCK_NAME="SSH_AUTH_SOCK"
SSH_AGENT_PID_NAME="SSH_AGENT_PID"
if [ "$openssh" = "no" ]
then
	SSH_AUTH_SOCK_NAME="SSH2_AUTH_SOCK"
	SSH_AGENT_PID_NAME="SSH2_AGENT_PID"
fi

SSH_AGENT_PID="NULL"
for pidf in $pidfiles
do
	if [ -f $pidf ]
	then
		. $pidf	> /dev/null 2>&1
	fi

# Copy application-specific environment variables into generic local variables.
	if [ "$openssh" = "no" ]
	then
		SSH_AUTH_SOCK=${SSH2_AUTH_SOCK}
		SSH_AGENT_PID=${SSH2_AGENT_PID}
	fi

	for x in $mypids
	do
		if [ "$x" = "$SSH_AGENT_PID" ]
		then
			if [ "$pidf" = "$keychainshfile" ]
			then
				if [ "$shelltype" = "csh" ]
				then
					pidf="$keychaincshfile"
					if [ ! -f $pidf ]
					then
						echo "setenv $SSH_AUTH_SOCK_NAME $SSH_AUTH_SOCK;"
						echo "setenv $SSH_AGENT_PID_NAME $SSH_AGENT_PID;"
						exit 0
					fi
				fi
			fi
			cat $pidf
			exit 0;
		fi
	done
done

SSH_AUTH_SOCK="NULL"
#We don't want an alias, we use /bin/ls
agent_files=`/bin/ls /tmp/ssh-XX*/agent.*`
for file in $agent_files
do
	if [ ! -S $file ]
	then
			continue
	fi
	SSH_AUTH_SOCK="$file"
	break
done

if [ "$SSH_AUTH_SOCK" = "NULL" ]
then
	exit 1
fi

SSH_AGENT_PID=$mypids

if [ "$shelltype" = "sh" ]
then
	echo "$SSH_AUTH_SOCK_NAME=$SSH_AUTH_SOCK; export $SSH_AUTH_SOCK_NAME;"
	echo "$SSH_AGENT_PID_NAME=$SSH_AGENT_PID; export $SSH_AGENT_PID_NAME;"
elif [ "$shelltype" = "csh" ]
then
	echo "setenv $SSH_AUTH_SOCK_NAME $SSH_AUTH_SOCK;"
	echo "setenv $SSH_AGENT_PID_NAME $SSH_AGENT_PID;"
fi
