1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#!/usr/bin/icmake -qi
/***************************************************************************
This file shows an example of a shell around ftp. I use this program a
lot in situations where I want to transfer a file from one unix host to
another, and when I know beforehand what file from what directory I want to
transfer. For the installation:
- copy this file to your personal bin directory, under the name
ftpxfer
- make the file executable: chmod +x ftpxfer
The program will prompt for the user name and password to use for the
ftp transfer. If you often access one host with this program, and don't wont
to type the user/password all the time, you can do the follwing for bash:
> set FTPUSER my_login_name_on_that_host
> export FTPUSER
> set FTPASS my_password_on_that_host
> export FTPASS
For tcsh, try:
> setenv FTPUSER my_login_name_on_that_host
> setenv FTPASS my_password_on_that_host
Net result: this program won't prompt you for the strings, but will retrieve
them from the environment table. You can, of course, place these commands in
your .login or .tcshrc file, to be executed automatically during the login
procedure. But then it may be a good idea to make these files read/write
only for you and for nobody else (e.g., by: chmod 644 .login).
OB The actual ftp transfer occurs using an intermediate temporary file,
TMPFILE in the below #define's. The user name and password _are_ stated in
that file, but this should not be a security risk. First, the file is
read/write for the user only, and for no-one else. Second, the file gets
deleted as soon as it's no longer needed. If you consider this feature still
a security hazard, take a valium and don't use this program.
*****************************************************************************/
// here's a couple of defines, no need to change them..
#define VER "1.02"
#define YEARS "1993"
// the following define controls ftp's `verbatim' mode, set it to "-v"
// if you want verbatim, or to "" if you don't
#define VERBATIM "-v"
list
envp; // environment strings
string
tmpfile, // temp file for ftp use
host, // host to transfer from/to
dir, // foreign directory
file, // local file
direction; // "get" or "put" file?
string getenv (string varname) // purpose: returns setting
{ // of environment variable
int // 'varname'
i;
for (i = 0; i < sizeof (envp); i += 2) // loop thru envp...
if (element (i, envp) == varname) // found varname?
return (element (i + 1, envp)); // yes -- return setting
return (""); // no -- return empty string
}
void inittmp () // purpose: initialize temp
{ // file
if (exists (tmpfile)) // remove any old version
exec ("rm", tmpfile); // if it exists
exec ("touch", tmpfile); // make empty file
exec ("chmod", "600", tmpfile); // make it r/w only for user
}
void process () // purpose: do the actual
{ // ftp transfer
string
user, // user name on foreign host
password, // password
foreignfile; // full name of foreign file
inittmp (); // make new temp file
if (dir) // if foreign dir specified:
foreignfile = change_path (file, dir); // use that
else if (get_path (file)) // if file spec has its own
foreignfile = file; // directory: keep it
else // otherwise: use current
foreignfile = change_path (file, // directory as dest dir
chdir ("."));
if (! (user = getenv ("FTPUSER")) ) // get username from envp
{ // or prompt for it
printf ("User name: ");
user = gets ();
}
if (! (password = getenv ("FTPASS")) ) // get passwd from envp
{ // or prompt for it
printf ("Password : ");
password = gets ();
}
fprintf (tmpfile, // write ftp login procedure
"open ", host, "\n", // to tmpfile, followed
"user ", user, " ", password, "\n", // by transfer commands
"binary\n",
direction, " ", file, " ", foreignfile, "\n",
"quit\n");
exec (P_NOCHECK, "ftp", VERBATIM, // do the ftp transfer
"-n -i", "< ", tmpfile);
exec (P_NOCHECK, "rm", tmpfile); // remove temp file
}
void usage () // purpose: print usage info
{ // and die
printf ("\n"
"ICCE Ftp-based File Transfer Shell V", VER, "\n"
"Copyright (c) ICCE ", YEARS, ". All rights reserved.\n"
"\n"
"Usage: ftpxfer -p|-g host file [directory]\n"
"where:\n"
" -p : selects putting of file\n"
" -g : selects getting of file\n"
" host : host to put/get from/to\n"
" file : file to transfer\n"
" directory : optional directory at foreign host, if "
"not given:\n"
" directory in file argument is used, if not "
"present:\n"
" current directory is used for destination\n"
"Ftpxfer will use the environment variables FTPUSER and FTPASS "
"when available,\n"
"or will prompt for the user and password.\n"
"\n");
exit (1);
}
void main (int argc, list argv, list evp) // main function
{
envp = evp; // store environment
echo (OFF); // no re-echoing of commands
tmpfile = "/tmp/ftpxfer." // make temporary filename
+ (string) getpid ();
if (element (1, argv) == "-p") // first argument: must
direction = "put"; // be -p or -g
else if (element (1, argv) == "-g")
direction = "get";
else
usage ();
if (! (host = element (2, argv)) ) // second argument: must be
usage (); // foreign host
if (! (file = element (3, argv)) ) // third argument: must be
usage (); // file to transfer
if (direction == "put" && ! exists (file)) // if putting: file must
{ // exist
printf ("File to put does not exist.\n");
exit (1);
}
dir = element (4, argv); // fourth element: may be
// foreign directory
process (); // hit it!
exit (0); // exitstatus: success
}
|