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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
* (c) 1998-2000 Gerd Knorr
*
* functions to handle ftp uploads using the ftp utility
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include "config.h"
#include "ftp.h"
/* ---------------------------------------------------------------------- */
/* FTP stuff */
int ftp_connected;
int ftp_debug;
static int ftp_pty, ftp_pid;
static char tty_name[32];
static int userbreak = 0;
static void ftp_send (int argc, ...);
static int ftp_recv (void);
static int
open_pty ()
{
#ifdef HAVE_GETPT
int master;
char *slave;
if (-1 == (master = getpt ()))
return -1;
if (-1 == grantpt (master) ||
-1 == unlockpt (master) || NULL == (slave = ptsname (master)))
{
close (master);
return -1;
}
strcpy (tty_name, slave);
return master;
#else
static char pty_name[32];
static char s1[] = "pqrs";
static char s2[] = "0123456789abcdef";
char *p1, *p2;
int pty;
for (p1 = s1; *p1; p1++)
{
for (p2 = s2; *p2; p2++)
{
sprintf (pty_name, "/dev/pty%c%c", *p1, *p2);
sprintf (tty_name, "/dev/tty%c%c", *p1, *p2);
if (-1 == access (tty_name, R_OK | W_OK))
continue;
if (-1 != (pty = open (pty_name, O_RDWR)))
return pty;
}
}
return -1;
#endif
}
void
ftp_init (int passive)
{
static char *noauto[] = { "ftp", "-n", NULL };
if (-1 == (ftp_pty = open_pty ()))
{
fprintf (stderr, "can't grab pty\n");
exit (1);
}
switch (ftp_pid = fork ())
{
case -1:
perror ("fork");
exit (1);
case 0:
/* child */
close (ftp_pty);
close (0);
close (1);
close (2);
setsid ();
open (tty_name, O_RDWR);
dup (0);
dup (0);
#ifndef HAVE_SGI
unsetenv ("LANG"); /* need english messages from ftp */
#endif
execvp (noauto[0], noauto);
perror ("execvp");
exit (1);
default:
/* parent */
break;
}
ftp_recv ();
/* initialisation */
if (passive)
{
ftp_send (1, "pass");
ftp_recv ();
}
return;
}
void
ftp_send (int argc, ...)
{
va_list ap;
char line[256], *arg;
int length, i;
va_start (ap, argc);
memset (line, 0, 256);
length = 0;
for (i = 0; i < argc; i++)
{
if (i)
line[length++] = ' ';
arg = va_arg (ap, char *);
length += strlen (arg);
strcat (line, arg);
}
line[length++] = '\n';
va_end (ap);
if (ftp_debug)
fprintf (stderr, ">> %s", line);
if (length != write (ftp_pty, line, length))
{
fprintf (stderr, "ftp: write error\n");
exit (1);
}
}
int
ftp_recv ()
{
char line[512], *p, *n;
int length, done, status, ret = 0;
fd_set set;
for (done = 0; !done;)
{
FD_ZERO (&set);
FD_SET (ftp_pty, &set);
select (ftp_pty + 1, &set, NULL, NULL, NULL);
switch (length = read (ftp_pty, line, 511))
{
case -1:
perror ("ftp: read error");
exit (1);
case 0:
fprintf (stderr, "ftp: EOF\n");
exit (1);
}
line[length] = 0;
for (p = line; p && *p; p = n)
{
/* split into lines */
if (NULL != (n = strchr (p, '\n'))
|| NULL != (n = strchr (p, '\r')))
*(n++) = 0;
else
n = NULL;
if (ftp_debug)
fprintf (stderr, "<< %s\n", p);
/* prompt? */
if (NULL != strstr (p, "ftp>"))
{
done = 1;
}
/* line dropped ? */
if (NULL != strstr (p, "closed connection"))
{
fprintf (stderr, "ftp: lost connection\n");
ftp_connected = 0;
}
if (NULL != strstr (p, "Not connected"))
{
if (ftp_connected)
fprintf (stderr, "ftp: lost connection\n");
ftp_connected = 0;
}
/* status? */
if (1 == sscanf (p, "%d", &status))
{
ret = status;
}
}
}
return ret;
}
void
ftp_connect (char *host, char *user, char *pass, char *dir)
{
int delay = 0, status;
// for (;;)
while(userbreak < 1)
{
/* Wiederholungsversuche mit wachsendem Intervall, 10 min max. */
if (delay)
{
fprintf (stderr, "ftp: connect failed, sleeping %d sec\n", delay);
sleep (delay);
delay *= 2;
if (delay > 600)
delay = 600;
}
else
{
delay = 5;
}
/* (re-) connect */
ftp_send (1, "close");
ftp_recv ();
ftp_send (2, "open", host);
status = ftp_recv ();
if (230 == status)
{
fprintf (stderr, "ftp: connected to %s, login ok\n", host);
ftp_connected = 1;
goto login_ok;
}
if (220 != status && 530 != status)
continue;
fprintf (stderr, "ftp: connected to %s\n", host);
ftp_connected = 1;
/* login */
ftp_send (3, "user", user, pass);
if (230 != ftp_recv ())
{
if (!ftp_connected)
continue;
fprintf (stderr, "ftp: login incorrect\n");
exit (1);
}
login_ok:
fprintf(stderr, "ftp: login successful\n");
/* set directory */
ftp_send (2, "cd", dir);
if (250 != ftp_recv ())
{
if (!ftp_connected)
continue;
fprintf(stderr,"ftp: cd %s failed, ignoring directory path\n",dir);
}
/* initialisation */
ftp_send (1, "bin");
ftp_recv ();
ftp_send (1, "umask 022");
ftp_recv ();
/* ok */
break;
}
}
void
ftp_upload (char *local, char *remote, char *tmp)
{
ftp_send (3, "put", local, tmp);
ftp_recv ();
ftp_send (3, "rename", tmp, remote);
ftp_recv ();
}
void
ftp_close ()
{
if(ftp_connected) ftp_send(1,"quit");
close(ftp_pty);
userbreak = 1;
}
|