summaryrefslogtreecommitdiffstats
path: root/release/sysinstall/ftp.c
blob: 99e064fe50b86c8448113b220cab8f90835def89 (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * ----------------------------------------------------------------------------
 * "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
 * ----------------------------------------------------------------------------
 *
 * $Id: ftp.c,v 1.12 1995/05/29 11:01:16 jkh Exp $
 *
 */

#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>
#include <ctype.h>
#include "ftp.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifndef STANDALONE_FTP
#include "sysinstall.h"
#endif /*STANDALONE_FTP*/

static void
debug(FTP_t ftp, const char *fmt, ...)
{
    char p[BUFSIZ];
    va_list ap;
    va_start(ap, fmt);
#ifdef STANDALONE_FTP
    strcpy(p,"LIBFTP: ");
#else
    *p = '\0';
#endif
    (void) vsnprintf(p+strlen(p), sizeof p - strlen(p), fmt, ap);
    va_end(ap);

#ifdef STANDALONE_FTP
    write(ftp->fd_debug,p,strlen(p));
#else
    if (isDebug())
	msgDebug(p);
#endif
}

static int
writes(int fd, char *s)
{
    int i = strlen(s);
    if (i != write(fd,s,i))
	return errno ? errno : -1;
    return 0;
}

static char*
get_a_line(FTP_t ftp)
{
    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';
	    debug(ftp, "received <%s>\n",buf);
	    return buf;
	}
	i++;
    }
    return buf;
}

static int
get_a_number(FTP_t ftp, char **q)
{
    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;
	}
    }
}

static int
botch(FTP_t ftp, char *func, char *state)
{
    debug(ftp, "Botch: %s called outside state %s\n",func,state);
    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;
}

static int
cmd(FTP_t ftp, const char *fmt, ...)
{
    char p[BUFSIZ];
    int i;

    va_list ap;
    va_start(ap, fmt);
    (void) vsnprintf(p, sizeof p, fmt, ap);
    va_end(ap);

    debug(ftp, "send <%s>\n",p);
    strcat(p,"\r\n");
    if (writes(ftp->fd_ctrl,p))
	return -1;
    i = get_a_number(ftp,0);
    return i;
}

FTP_t
FtpInit()
{
    FTP_t ftp;

    ftp = malloc(sizeof *ftp);
    if (!ftp)
	return ftp;
    memset(ftp, 0, sizeof *ftp);
    ftp->fd_ctrl = -1;
    ftp->fd_xfer = -1;
    ftp->fd_debug = -1;
    ftp->state = init;
    return ftp;
}

#ifdef STANDALONE_FTP
void
FtpDebug(FTP_t ftp, int i)
{
    ftp->fd_debug = i;
}
#endif

int
FtpOpen(FTP_t ftp, char *host, char *user, char *passwd)
{
    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 */

    debug(ftp, "FtpOpen(ftp, %s, %s, %s)\n", host, user, passwd);

    temp = inet_addr(host);
    if (temp != INADDR_NONE)
    {
	debug(ftp, "Using dotted IP address `%s'\n", host);
	ftp->addrtype = sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = temp;
    } else {
	debug(ftp, "Trying to resolve `%s'\n", host);
	he = gethostbyname(host);
	if (!he)
	{
	    debug(ftp, "Lookup of `%s' failed!\n", host);
	    return ENOENT;
	}
	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)
    {
	debug(ftp, "Socket open failed: %s (%i)\n", strerror(errno), errno);
	return s;
    }

    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
	debug(ftp,"Connection failed: %s (%i)\n", strerror(errno), errno);
	(void)close(s);
	return -1;
    }

    ftp->fd_ctrl = s;

    debug(ftp, "open (%d)\n",get_a_number(ftp,0));

    i = cmd(ftp,"USER %s",user);
    if (i >= 300 && i < 400)
	i = cmd(ftp,"PASS %s",passwd);
    if (i >= 299)
	return -1;
    ftp->state = isopen;
    return 0;

#if 0
fail:
    close(ftp->fd_ctrl);
    ftp->fd_ctrl = -1;
    return -1;
#endif
}

void
FtpClose(FTP_t ftp)
{
    if (ftp->state != isopen)
	botch(ftp,"FtpClose","open");

    debug(ftp, "FtpClose(ftp)\n");
    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;
}

int
FtpChdir(FTP_t ftp, char *dir)
{
    int i;
    if (ftp->state != isopen)
	return botch(ftp,"FtpChdir","open");
    i = cmd(ftp,"CWD %s",dir);
    return 0;
}

int
FtpGet(FTP_t ftp, char *file)
{
    int i,s;
    char *q;
    unsigned char addr[6];
    struct sockaddr_in sin;

    debug(ftp, "FtpGet(ftp,%s)\n",file);
    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;
    }
    if (ftp->passive) {
	debug(ftp, "send <%s>\n","PASV");
	if (writes(ftp->fd_ctrl,"PASV\r\n"))
	    return -1;
	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);
	}

	sin.sin_family = ftp->addrtype;
	bcopy(addr, (char *)&sin.sin_addr, 4);
	bcopy(addr+4, (char *)&sin.sin_port, 2);
	debug(ftp, "Opening active socket to %s : %u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));

	if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0)
	    return -1;

	debug(ftp, "Connecting to %s:%u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
	    (void)close(s);
	    debug(ftp, "connect: %s (%d)\n", strerror(errno), errno);
	    return -1;
	}

	i = cmd(ftp,"RETR %s",file);
	if (i > 299)
	    return -1;
	ftp->state = xfer;
	ftp->fd_xfer = s;
	return s;
    } else {
	return -1;
    }
}

int
FtpEOF(FTP_t ftp)
{
    if (ftp->state != xfer)
	return botch(ftp,"FtpEOF","xfer");
    debug(ftp, "FtpEOF(ftp)\n");
    close(ftp->fd_xfer); ftp->fd_xfer = -1;
    ftp->state = isopen;
    return get_a_number(ftp,0);
}

#ifdef STANDALONE_FTP

/* main.c */

int
main(int argc, char **argv)
{
    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);
    FtpChdir(ftp,"/");
    FtpChdir(ftp,"CTM");
    i = FtpGet(ftp,"README");
    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;
}

#endif /*STANDALONE_FTP*/
OpenPOWER on IntegriCloud