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
|
/*
* (c) 1998-2002 Gerd Knorr
*
* functions to handle ftp uploads using the ftp utility
*
*/
#include "config.h"
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <signal.h>
#include "ftp.h"
/* ---------------------------------------------------------------------- */
/* FTP stuff */
struct ftp_state {
char *name;
int connected;
int debug;
int pty, pid;
char tty_name[32];
};
static
int open_pty(struct ftp_state *s)
{
#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(s->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(s->tty_name,"/dev/tty%c%c",*p1,*p2);
if (-1 == access(s->tty_name,R_OK|W_OK))
continue;
if (-1 != (pty = open(pty_name,O_RDWR)))
return pty;
}
}
return -1;
#endif
}
struct ftp_state* ftp_init(char *name, int autologin, int passive, int debug)
{
static char *doauto[] = { "ftp", NULL }; /* allow autologin via ~/.netrc */
static char *noauto[] = { "ftp", "-n", NULL };
struct ftp_state *s;
s = malloc(sizeof(*s));
memset(s,0,sizeof(*s));
s->name = name;
s->debug = debug;
if (-1 == (s->pty = open_pty(s))) {
fprintf(stderr,"can't grab pty\n");
exit(1);
}
switch (s->pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
/* child */
close(s->pty);
close(0); close(1); close(2);
setsid();
open(s->tty_name,O_RDWR); dup(0); dup(0);
/* need english messages from ftp */
setenv("LC_ALL","C",1);
if (autologin)
execvp(doauto[0],doauto);
else
execvp(noauto[0],noauto);
perror("execvp");
exit(1);
default:
/* parent */
break;
}
ftp_recv(s);
/* initialisation */
if (passive) {
ftp_send(s,1,"pass");
ftp_recv(s);
}
return s;
}
void
ftp_send(struct ftp_state *s, 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 (s->debug)
fprintf(stderr,"[%s]>> %s",s->name,line);
if (length != write(s->pty,line,length)) {
fprintf(stderr,"ftp: write error\n");
exit(1);
}
}
int
ftp_recv(struct ftp_state *s)
{
char line[512],*p,*n;
int length, done, status, ret=0;
fd_set set;
for (done = 0; !done;) {
FD_ZERO(&set);
FD_SET(s->pty,&set);
select(s->pty+1,&set,NULL,NULL,NULL);
switch (length = read(s->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 (s->debug)
fprintf(stderr,"[%s]<< %s\n",s->name,p);
/* prompt? */
if (NULL != strstr(p,"ftp>")) {
done = 1;
}
/* line dropped ? */
if (NULL != strstr(p,"closed connection")) {
fprintf(stderr,"ftp: lost connection\n");
s->connected = 0;
}
if (NULL != strstr(p,"Not connected")) {
if (!ftp_connected(s))
fprintf(stderr,"ftp: lost connection\n");
s->connected = 0;
}
/* status? */
if (1 == sscanf(p,"%d",&status)) {
ret = status;
}
}
}
return ret;
}
void
ftp_connect(struct ftp_state *s, char *host, char *user, char *pass, char *dir)
{
int delay = 0, status;
for (;;) {
/* 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(s,1,"close");
ftp_recv(s);
ftp_send(s,2,"open",host);
status = ftp_recv(s);
if (230 == status) {
fprintf(stderr,"ftp: connected to %s, login ok\n",host);
s->connected = 1;
goto login_ok;
}
if (220 != status && 530 != status)
continue;
fprintf(stderr,"ftp: connected to %s\n",host);
s->connected = 1;
/* login */
ftp_send(s,3,"user",user,pass);
if (230 != ftp_recv(s)) {
if (!ftp_connected(s))
continue;
fprintf(stderr,"ftp: login incorrect\n");
exit(1);
}
login_ok:
/* set directory */
ftp_send(s,2,"cd",dir);
if (250 != ftp_recv(s)) {
if (!s->connected)
continue;
fprintf(stderr,"ftp: cd %s failed\n",dir);
exit(1);
}
/* initialisation */
ftp_send(s,1,"bin");
ftp_recv(s);
ftp_send(s,1,"umask 022");
ftp_recv(s);
/* ok */
break;
}
}
int ftp_connected(struct ftp_state *s)
{
return s->connected;
}
void
ftp_upload(struct ftp_state *s, char *local, char *remote, char *tmp)
{
ftp_send(s,3,"put",local,tmp);
ftp_recv(s);
ftp_send(s,3,"rename",tmp,remote);
ftp_recv(s);
}
void ftp_fini(struct ftp_state *s)
{
ftp_send(s,1,"bye");
ftp_recv(s);
kill(s->pid,SIGTERM);
close(s->pty);
free(s);
}
|