#!/bin/sh # # /etc/dynamic/scripts/vpnkey.script # # Checks if at least one vpnkey is plugged, and starts/stops sshd when needed ssh_command="/etc/rc.d/init.d/sshd" $ssh_command status > /dev/null ssh_status=$? tmp_dir=`mktemp -d /tmp/vpnkey.mount_dir.XXXXXX` tmp_file=`mktemp /tmp/vpnkey.grep_buffer.XXXXXX` # checks if connected keys are VPNKeys # actually tries to mount the first partition of each scsi device present # in vfat, then looks for a file named vpnkey vpnkey_present="No" for usb_storage_info in /proc/scsi/usb-storage/* do if [ ! -e $usb_storage_info ] then continue fi scsi_host=`head -n 1 $usb_storage_info | sed -e 's/.*scsi\([[:digit:]][[:digit:]]*\).*/\1/'` grep scsi$scsi_host /proc/scsi/scsi > $tmp_file while read line do scsi_bus="bus`echo $line | sed -e 's/.*Channel: [0]*\([[:digit:]][[:digit:]]*\).*/\1/'`" scsi_target="target`echo $line | sed -e 's/.*Id: [0]*\([[:digit:]][[:digit:]]*\).*/\1/'`" scsi_lun="lun`echo $line | sed -e 's/.*Lun: [0]*\([[:digit:]][[:digit:]]*\).*/\1/'`" scsi_device="/dev/scsi/host$scsi_host/$scsi_bus/$scsi_target/$scsi_lun" scsi_disc="$scsi_device/disc" scsi_part="$scsi_device/part1" #Re-read the partition table i=0 while [ $i -lt 8 ] do sfdisk -R $scsi_disc >/dev/null 2>&1 && break sleep 1 i=$(($i + 1)) done #try to mount partition if [ ! -e $scsi_part ] then continue fi mount -o ro -t vfat $scsi_part $tmp_dir mnt_status=$? # if device is mounted, check for file, and unmount if [ -e ${tmp_dir}/vpnkey ] then vpnkey_present="Yes" umount $tmp_dir break elif [ $mnt_status -eq 0 ] then umount $tmp_dir fi done < $tmp_file done rmdir $tmp_dir rm -f $tmp_file # manages sshd accordingly if [ "$vpnkey_present" = "Yes" ] then # stops sshd if it is running if [ "$ssh_status" -eq "0" ] then $ssh_command stop fi else # starts sshd if it is not running if [ "$ssh_status" -ne "0" ] then $ssh_command start fi fi