#!/bin/ksh # @(#)$Id: rftp.sh,v 6.2 1994/03/13 21:01:49 prem Exp $ - STREAMware TCP/IP source # # Copyrighted as an unpublished work. # (c) Copyright 1987-1993 Lachman Technology, Inc. # All rights reserved. # # SCCS IDENTIFICATION # # rftp -- "rapid" FTP # # This software is based on code submitted to the USENET # newsgroup comp.sources.unix by Michael Zraly, which is # in turn based on code by John Granrose. # # Improvements: # - added better usage message. # - added put, mput functionality. # - added site functionality. # - 2004/01/06 added passive option - brian@aljex.com # cmd= dbg=false use=false ver=false bin=false hash=false group=false mac= dir= fil= key= sub= site= sgroup= sgpass= sitepass= sitegroup= shiftback=false pasv=false usage='- [-duvbhP] [-S "var val .."] file[s]' description="\n -g get \n -p put \n -l ls \n -m mget \n -t mput \n\n -d print debug information\n -u use the .netrc file\n -v print session to terminal\n -b transfer using binary mode\n -h print '#' mark during transfer\n\n -P transfer using passive mode\n -S site variable setting\n -f use \"file\" in place of .keysfile for macro search\n -k use macro \"key\" to index through \".keysfile\" file\n -s append subdir to target directory\n" prog=`basename $0` USAGE="usage:\n$prog $usage\n$description" # # Set initial value for fil # setfil() { if test -n "$FTPKEYS" then fil=$FTPKEYS elif test -r .ftpkeys then fil=.ftpkeys elif test -r $HOME/.ftpkeys then fil=$HOME/.ftpkeys fi } # # Set cmd, fil variables appropriately # setcmd() { if test -z "$cmd" then if [ "$site" = "" ]; then cmd=ls fi fi if test -z "$key" -o -z "$fil" then fil=/dev/null fi } # # Now to determine mac, dir variables # detmacdir() { if test -z "$key" then if test "$cmd" = "ls" then min=2 else min=3 fi mac=$1 dir=$2 if test "$#" -ge "$min" then shift shift fi else # # Here we need to read $fil and look for $key to get $mac. $dir # str=`grep "^$key[ ]" $fil` mac=`echo "$str" | awk '{ print $2 }'` dir=`echo "$str" | awk '{ print $3 }'` fi } checkerr() { if test -z "$mac" then echo machine not set 1>&2 echo $prog: ERROR 1>&2 #dbg=true exit 1 fi if test -n "$site" then if test -z "$dir" -a -n "$cmd" then echo directory not set 1>&2 echo $prog: ERROR 1>&2 #dbg=true exit 1 fi fi } # # if file is readable and you're the owner, then return 1 # else return 0. # checkowner() { owner=`ls -al $HOME/.sitefile | awk '{print $3}'` if [ -r $HOME/.sitefile -a "`logname`" != "$owner" ]; then echo "Permission denied!!" exit 1 else return 1 fi } # # format of .sitefile " " # getsitepass() { echo $site | grep group > /dev/null 2>&1 if [ $? -eq 0 ]; then group=true set `echo $site` sgroup=$2 if test -n "$sgroup" then if test -r $HOME/.sitefile then checkowner str=`grep "^$mac" $HOME/.sitefile` sgptmp=`echo "$str" | awk '{ print $2 }'` if [ "$sgptmp" = "$sgroup" ]; then sgpass=`echo "$str" | awk '{ print $3 }'` sitegroup="site group $sgroup" sitepass="site gpass $sgpass" else echo "No such access group!!" exit 1 fi else echo "No $HOME/.sitefile found!!" echo "create one and chmod that file to go-r" exit 1 fi fi fi } # # Take care of possible subdirectory # if test -n "$sub" then dir=$dir/$sub fi # # Now check for debugging option # chkdbg() { if test $dbg = "true" then echo "cmd = \"$cmd\"" echo "dbg = \"$dbg\"" echo "use = \"$use\"" echo "ver = \"$ver\"" echo "bin = \"$bin\"" echo "hash = \"$hash\"" echo "pasv = \"$pasv\"" echo "mac = \"$mac\"" echo "dir = \"$dir\"" echo "fil = \"$fil\"" echo "key = \"$key\"" echo "sub = \"$sub\"" echo "site = \"$site\"" echo "sitepass = \"$sitepass\"" exit 1 fi } setftpargs() { # # Set arguments to FTP command # if test "$use" = "false" then optn=-n fi if test "$ver" = "true" then optv=-v fi } # # Now for the actual FTP'ing # doftp() { ( # # If not using .netrc file, assume anonymous login # if test "$use" = "false" then echo user anonymous `logname`@`hostname` fi # # cd to base directory # if test -n "$dir" then echo cd $dir fi # # take care of binary, hash, passive and site commands if needed # if test "$bin" = "true" then echo binary fi if test "$hash" = "true" then echo hash fi if test "$pasv" = "true" then echo passive fi if [ "$site" != "" ]; then echo $sitegroup if test "$group" = "true" then echo $sitepass fi fi # # execute ftp command # case "$cmd" in ls) if test "$#" -gt 0 then for n do echo ls $n done else echo ls fi ;; get) echo get $1 $2 ;; put) echo put $1 $2 ;; mget) for n do echo mget $n done ;; mput) for n do echo mput $n done ;; *) if [ "$site" = "" ]; then echo $prog: Invalid ftp command '$cmd' 1>&2 fi ;; esac # # all done # echo bye ) | ftp -i $optn $optv $mac # # Exit with status of FTP command # exit $? } ############################## # -- main -- # Parse options ############################## # # set initial value of the ftpkeys file if any # setfil while getopts gptlmduvbhPf:k:s:S: c do case $c in g) if test -n "$cmd" then echo option -g 1>&2 echo $USAGE 1>&2 exit 1 fi cmd=get ;; l) if test -n "$cmd" then echo option -l 1>&2 echo $USAGE 1>&2 exit 1 fi cmd=ls ;; m) if test -n "$cmd" then echo option -m 1>&2 echo $USAGE 1>&2 exit 1 fi cmd=mget ;; p) if test -n "$cmd" then echo option -p 1>&2 echo $USAGE 1>&2 exit 1 fi cmd=put ;; t) if test -n "$cmd" then echo option -t 1>&2 echo $USAGE 1>&2 exit 1 fi cmd=mput ;; d) dbg=true ;; u) use=true ;; v) ver=true ;; b) bin=true ;; h) hash=true ;; P) pasv=true ;; f) fil=$OPTARG ;; k) key=$OPTARG shiftback=true ;; s) sub=$OPTARG ;; S) site=$OPTARG ;; *) echo option -$c 1>&2 echo $USAGE 1>&2 exit 1 ;; esac done shift `expr $OPTIND - 1` # # set cmd and fil variables # setcmd # # determine machine and directory variables # detmacdir $1 $2 # # check error in machine name and directory variables # checkerr # # get "site group" information # getsitepass # # Take care of possible subdirectory # if test -n "$sub" then dir=$dir/$sub fi # # check for debugging option # chkdbg # # set the ftp arguments # setftpargs # # do the wild thing # if test "$shiftback" = "true" then doftp $1 else doftp $3 fi ######### end of script #######