1995-05-24 09:00:58 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1995-05-27 06:19:59 +00:00
|
|
|
* $Id: ftp.c,v 1.10 1995/05/26 19:28:01 jkh Exp $
|
1995-05-24 09:00:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1995-05-23 02:41:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
1995-05-24 09:00:58 +00:00
|
|
|
#include <ctype.h>
|
1995-05-23 02:41:18 +00:00
|
|
|
#include "ftp.h"
|
1995-05-24 11:19:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
#ifndef STANDALONE_FTP
|
1995-05-23 02:41:18 +00:00
|
|
|
#include "sysinstall.h"
|
1995-05-24 09:00:58 +00:00
|
|
|
#endif /*STANDALONE_FTP*/
|
1995-05-23 02:41:18 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
debug(FTP_t ftp, const char *fmt, ...)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char p[BUFSIZ];
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
1995-05-25 06:15:38 +00:00
|
|
|
#ifdef STANDALONE_FTP
|
|
|
|
strcpy(p,"LIBFTP: ");
|
|
|
|
#else
|
|
|
|
*p = '\0';
|
|
|
|
#endif
|
|
|
|
(void) vsnprintf(p+strlen(p), sizeof p - strlen(p), fmt, ap);
|
1995-05-24 09:00:58 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
#ifdef STANDALONE_FTP
|
|
|
|
write(ftp->fd_debug,p,strlen(p));
|
1995-05-23 02:41:18 +00:00
|
|
|
#else
|
1995-05-24 09:00:58 +00:00
|
|
|
msgDebug(p);
|
1995-05-23 02:41:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
writes(int fd, char *s)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i = strlen(s);
|
|
|
|
if (i != write(fd,s,i))
|
|
|
|
return errno ? errno : -1;
|
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
get_a_line(FTP_t ftp)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
static char buf[BUFSIZ];
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for(i=0;i<BUFSIZ;) {
|
|
|
|
j = read(ftp->fd_ctrl,buf+i,1);
|
|
|
|
if (j != 1)
|
|
|
|
return 0;
|
|
|
|
if (buf[i] == '\r' || buf[i] == '\n') {
|
|
|
|
if (!i)
|
|
|
|
continue;
|
|
|
|
buf[i] = '\0';
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "received <%s>\n",buf);
|
1995-05-24 09:00:58 +00:00
|
|
|
return buf;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return buf;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
static int
|
|
|
|
get_a_number(FTP_t ftp, char **q)
|
1995-05-23 02:41:18 +00:00
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char *p;
|
|
|
|
int i = -1,j;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
p = get_a_line(ftp);
|
|
|
|
if (!(isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])))
|
|
|
|
continue;
|
|
|
|
if (i == -1 && p[3] == '-') {
|
|
|
|
i = atoi(p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[3] != ' ' && p[3] != '\t')
|
|
|
|
continue;
|
|
|
|
j = atoi(p);
|
|
|
|
if (i == -1) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
return j;
|
|
|
|
} else if (j == i) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
return j;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
botch(FTP_t ftp, char *func, char *state)
|
|
|
|
{
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "Botch: %s called outside state %s\n",func,state);
|
1995-05-24 09:00:58 +00:00
|
|
|
writes(ftp->fd_ctrl,"QUIT\r\n");
|
|
|
|
close(ftp->fd_ctrl); ftp->fd_ctrl = -1;
|
|
|
|
close(ftp->fd_xfer); ftp->fd_xfer = -1;
|
|
|
|
ftp->state = init;
|
|
|
|
return -1;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd(FTP_t ftp, const char *fmt, ...)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char p[BUFSIZ];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void) vsnprintf(p, sizeof p, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "send <%s>\n",p);
|
1995-05-24 09:00:58 +00:00
|
|
|
strcat(p,"\r\n");
|
|
|
|
if (writes(ftp->fd_ctrl,p))
|
|
|
|
return -1;
|
|
|
|
i = get_a_number(ftp,0);
|
|
|
|
return i;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FTP_t
|
|
|
|
FtpInit()
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
FTP_t ftp;
|
|
|
|
|
|
|
|
ftp = malloc(sizeof *ftp);
|
|
|
|
if (!ftp)
|
1995-05-23 02:41:18 +00:00
|
|
|
return ftp;
|
1995-05-24 09:00:58 +00:00
|
|
|
memset(ftp, 0, sizeof *ftp);
|
|
|
|
ftp->fd_ctrl = -1;
|
1995-05-24 19:31:26 +00:00
|
|
|
ftp->fd_xfer = -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->fd_debug = -1;
|
|
|
|
ftp->state = init;
|
|
|
|
return ftp;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
#ifdef STANDALONE_FTP
|
1995-05-23 02:41:18 +00:00
|
|
|
void
|
|
|
|
FtpDebug(FTP_t ftp, int i)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->fd_debug = i;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
1995-05-24 09:00:58 +00:00
|
|
|
|
1995-05-23 02:41:18 +00:00
|
|
|
int
|
|
|
|
FtpOpen(FTP_t ftp, char *host, char *user, char *passwd)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
struct hostent *he = NULL;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int s;
|
|
|
|
unsigned long temp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ftp->state != init)
|
|
|
|
return botch(ftp,"FtpOpen","init");
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
user = "ftp";
|
|
|
|
|
|
|
|
if (!passwd)
|
|
|
|
passwd = "??@??(FreeBSD:libftp)"; /* XXX */
|
|
|
|
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "FtpOpen(ftp, %s, %s, %s)\n", host, user, passwd);
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
temp = inet_addr(host);
|
|
|
|
if (temp != INADDR_NONE)
|
|
|
|
{
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Using dotted IP address `%s'\n", host);
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->addrtype = sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = temp;
|
|
|
|
} else {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Trying to resolve `%s'\n", host);
|
1995-05-24 09:00:58 +00:00
|
|
|
he = gethostbyname(host);
|
|
|
|
if (!he)
|
1995-05-24 01:27:15 +00:00
|
|
|
{
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Lookup of `%s' failed!\n", host);
|
1995-05-24 09:00:58 +00:00
|
|
|
return ENOENT;
|
1995-05-24 01:27:15 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->addrtype = sin.sin_family = he->h_addrtype;
|
|
|
|
bcopy(he->h_addr, (char *)&sin.sin_addr, he->h_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
sin.sin_port = htons(21);
|
|
|
|
|
|
|
|
if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0)
|
|
|
|
{
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Socket open failed: %s (%i)\n", strerror(errno), errno);
|
1995-05-24 09:00:58 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp,"Connection failed: %s (%i)\n", strerror(errno), errno);
|
1995-05-24 09:00:58 +00:00
|
|
|
(void)close(s);
|
1995-05-23 02:41:18 +00:00
|
|
|
return -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ftp->fd_ctrl = s;
|
|
|
|
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "open (%d)\n",get_a_number(ftp,0));
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
i = cmd(ftp,"USER %s",user);
|
1995-05-25 06:15:38 +00:00
|
|
|
if (i >= 300 && i < 400)
|
|
|
|
i = cmd(ftp,"PASS %s",passwd);
|
|
|
|
if (i >= 299)
|
|
|
|
return -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->state = isopen;
|
|
|
|
return 0;
|
|
|
|
|
1995-05-26 19:28:06 +00:00
|
|
|
#if 0
|
1995-05-24 18:21:49 +00:00
|
|
|
fail:
|
1995-05-24 09:00:58 +00:00
|
|
|
close(ftp->fd_ctrl);
|
|
|
|
ftp->fd_ctrl = -1;
|
|
|
|
return -1;
|
1995-05-26 19:28:06 +00:00
|
|
|
#endif
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FtpClose(FTP_t ftp)
|
|
|
|
{
|
|
|
|
if (ftp->state != isopen)
|
|
|
|
botch(ftp,"FtpClose","open");
|
|
|
|
|
1995-05-27 06:19:59 +00:00
|
|
|
debug(ftp, "FtpClose(ftp)\n");
|
1995-05-24 09:00:58 +00:00
|
|
|
writes(ftp->fd_ctrl,"QUIT\r\n");
|
|
|
|
close(ftp->fd_ctrl); ftp->fd_ctrl = -1;
|
|
|
|
close(ftp->fd_xfer); ftp->fd_xfer = -1;
|
|
|
|
ftp->state = init;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpChdir(FTP_t ftp, char *dir)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i;
|
|
|
|
if (ftp->state != isopen)
|
|
|
|
return botch(ftp,"FtpChdir","open");
|
|
|
|
i = cmd(ftp,"CWD %s",dir);
|
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpGet(FTP_t ftp, char *file)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i,s;
|
|
|
|
char *q;
|
|
|
|
unsigned char addr[6];
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
1995-05-27 06:19:59 +00:00
|
|
|
debug(ftp, "FtpGet(ftp,%s)\n",file);
|
1995-05-24 09:00:58 +00:00
|
|
|
if (ftp->state != isopen)
|
|
|
|
return botch(ftp,"FtpGet","open");
|
|
|
|
if(ftp->binary) {
|
|
|
|
i = cmd(ftp,"TYPE I");
|
|
|
|
if (i > 299)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
1995-05-24 11:19:11 +00:00
|
|
|
if (ftp->passive) {
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "send <%s>\n","PASV");
|
1995-05-24 09:00:58 +00:00
|
|
|
if (writes(ftp->fd_ctrl,"PASV\r\n"))
|
1995-05-24 17:49:20 +00:00
|
|
|
return -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
i = get_a_number(ftp,&q);
|
|
|
|
if (i != 227)
|
|
|
|
return -1;
|
|
|
|
while (*q && !isdigit(*q))
|
|
|
|
q++;
|
|
|
|
if (!*q)
|
|
|
|
return -1;
|
|
|
|
q--;
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
q++;
|
|
|
|
addr[i] = strtol(q,&q,10);
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
sin.sin_family = ftp->addrtype;
|
|
|
|
bcopy(addr, (char *)&sin.sin_addr, 4);
|
|
|
|
bcopy(addr+4, (char *)&sin.sin_port, 2);
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Opening active socket to %s : %u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
|
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0)
|
|
|
|
return -1;
|
1995-05-24 11:19:11 +00:00
|
|
|
|
|
|
|
debug(ftp, "Connecting to %s:%u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
|
1995-05-24 09:00:58 +00:00
|
|
|
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
|
|
|
(void)close(s);
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "connect: %s (%d)\n", strerror(errno), errno);
|
1995-05-24 09:00:58 +00:00
|
|
|
return -1;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
i = cmd(ftp,"RETR %s",file);
|
|
|
|
if (i > 299)
|
|
|
|
return -1;
|
|
|
|
ftp->state = xfer;
|
1995-05-24 18:35:10 +00:00
|
|
|
ftp->fd_xfer = s;
|
1995-05-24 09:00:58 +00:00
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpEOF(FTP_t ftp)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
if (ftp->state != xfer)
|
|
|
|
return botch(ftp,"FtpEOF","xfer");
|
1995-05-27 06:19:59 +00:00
|
|
|
debug(ftp, "FtpEOF(ftp)\n");
|
1995-05-25 06:15:38 +00:00
|
|
|
close(ftp->fd_xfer); ftp->fd_xfer = -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->state = isopen;
|
|
|
|
return get_a_number(ftp,0);
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STANDALONE_FTP
|
|
|
|
|
|
|
|
/* main.c */
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
FTP_t ftp;
|
|
|
|
int i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
ftp = FtpInit();
|
|
|
|
if (!ftp) err(1,"FtpInit()");
|
|
|
|
|
|
|
|
FtpDebug(ftp,1);
|
|
|
|
i = FtpOpen(ftp, "ref.tfs.com", "ftp", "phk-libftp@");
|
|
|
|
if (i) err(1,"FtpOpen(%d)",i);
|
|
|
|
FtpBinary(ftp,1);
|
|
|
|
FtpPassive(ftp,1);
|
1995-05-24 11:19:11 +00:00
|
|
|
FtpChdir(ftp,"/");
|
1995-05-24 09:00:58 +00:00
|
|
|
FtpChdir(ftp,"CTM");
|
1995-05-24 11:19:11 +00:00
|
|
|
i = FtpGet(ftp,"README");
|
1995-05-24 09:00:58 +00:00
|
|
|
while(1 == read(i,&c,1))
|
|
|
|
putchar(c);
|
|
|
|
FtpEOF(ftp);
|
|
|
|
FtpClose(ftp);
|
|
|
|
i = FtpOpen(ftp, "freefall.cdrom.com", "ftp", "phk-libftp@");
|
|
|
|
FtpBinary(ftp,1);
|
|
|
|
FtpPassive(ftp,1);
|
|
|
|
FtpChdir(ftp,"/pub");
|
|
|
|
FtpChdir(ftp,"FreeBSD");
|
|
|
|
i = FtpGet(ftp,"README");
|
|
|
|
while(1 == read(i,&c,1))
|
|
|
|
putchar(c);
|
|
|
|
FtpEOF(ftp);
|
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*STANDALONE_FTP*/
|