ftp.c revision 1.1.2.2 1 1.1.2.2 matt /*-
2 1.1.2.2 matt * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
3 1.1.2.2 matt * All rights reserved.
4 1.1.2.2 matt *
5 1.1.2.2 matt * Redistribution and use in source and binary forms, with or without
6 1.1.2.2 matt * modification, are permitted provided that the following conditions
7 1.1.2.2 matt * are met:
8 1.1.2.2 matt * 1. Redistributions of source code must retain the above copyright
9 1.1.2.2 matt * notice, this list of conditions and the following disclaimer
10 1.1.2.2 matt * in this position and unchanged.
11 1.1.2.2 matt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 matt * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 matt * documentation and/or other materials provided with the distribution.
14 1.1.2.2 matt * 3. The name of the author may not be used to endorse or promote products
15 1.1.2.2 matt * derived from this software without specific prior written permission
16 1.1.2.2 matt *
17 1.1.2.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1.2.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1.2.2 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1.2.2 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1.2.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1.2.2 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1.2.2 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1.2.2 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1.2.2 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1.2.2 matt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 matt */
28 1.1.2.2 matt
29 1.1.2.2 matt #include "free2net.h"
30 1.1.2.2 matt
31 1.1.2.2 matt #include <sys/cdefs.h>
32 1.1.2.2 matt __FBSDID("$FreeBSD: src/lib/libfetch/ftp.c,v 1.91.2.2 2007/05/16 07:04:02 njl Exp $");
33 1.1.2.2 matt
34 1.1.2.2 matt /*
35 1.1.2.2 matt * Portions of this code were taken from or based on ftpio.c:
36 1.1.2.2 matt *
37 1.1.2.2 matt * ----------------------------------------------------------------------------
38 1.1.2.2 matt * "THE BEER-WARE LICENSE" (Revision 42):
39 1.1.2.2 matt * <phk (at) FreeBSD.org> wrote this file. As long as you retain this notice you
40 1.1.2.2 matt * can do whatever you want with this stuff. If we meet some day, and you think
41 1.1.2.2 matt * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
42 1.1.2.2 matt * ----------------------------------------------------------------------------
43 1.1.2.2 matt *
44 1.1.2.2 matt * Major Changelog:
45 1.1.2.2 matt *
46 1.1.2.2 matt * Dag-Erling Codan Smrgrav
47 1.1.2.2 matt * 9 Jun 1998
48 1.1.2.2 matt *
49 1.1.2.2 matt * Incorporated into libfetch
50 1.1.2.2 matt *
51 1.1.2.2 matt * Jordan K. Hubbard
52 1.1.2.2 matt * 17 Jan 1996
53 1.1.2.2 matt *
54 1.1.2.2 matt * Turned inside out. Now returns xfers as new file ids, not as a special
55 1.1.2.2 matt * `state' of FTP_t
56 1.1.2.2 matt *
57 1.1.2.2 matt * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
58 1.1.2.2 matt *
59 1.1.2.2 matt */
60 1.1.2.2 matt
61 1.1.2.2 matt #include <sys/param.h>
62 1.1.2.2 matt #include <sys/socket.h>
63 1.1.2.2 matt #include <netinet/in.h>
64 1.1.2.2 matt
65 1.1.2.2 matt #include <ctype.h>
66 1.1.2.2 matt #include <err.h>
67 1.1.2.2 matt #include <errno.h>
68 1.1.2.2 matt #include <fcntl.h>
69 1.1.2.2 matt #include <netdb.h>
70 1.1.2.2 matt #include <stdarg.h>
71 1.1.2.2 matt #include <stdint.h>
72 1.1.2.2 matt #include <stdio.h>
73 1.1.2.2 matt #include <stdlib.h>
74 1.1.2.2 matt #include <string.h>
75 1.1.2.2 matt #include <time.h>
76 1.1.2.2 matt #include <unistd.h>
77 1.1.2.2 matt
78 1.1.2.2 matt #include "fetch.h"
79 1.1.2.2 matt #include "common.h"
80 1.1.2.2 matt #include "ftperr.h"
81 1.1.2.2 matt
82 1.1.2.2 matt #define FTP_ANONYMOUS_USER "anonymous"
83 1.1.2.2 matt
84 1.1.2.2 matt #define FTP_CONNECTION_ALREADY_OPEN 125
85 1.1.2.2 matt #define FTP_OPEN_DATA_CONNECTION 150
86 1.1.2.2 matt #define FTP_OK 200
87 1.1.2.2 matt #define FTP_FILE_STATUS 213
88 1.1.2.2 matt #define FTP_SERVICE_READY 220
89 1.1.2.2 matt #define FTP_TRANSFER_COMPLETE 226
90 1.1.2.2 matt #define FTP_PASSIVE_MODE 227
91 1.1.2.2 matt #define FTP_LPASSIVE_MODE 228
92 1.1.2.2 matt #define FTP_EPASSIVE_MODE 229
93 1.1.2.2 matt #define FTP_LOGGED_IN 230
94 1.1.2.2 matt #define FTP_FILE_ACTION_OK 250
95 1.1.2.2 matt #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */
96 1.1.2.2 matt #define FTP_FILE_CREATED 257 /* multiple meanings */
97 1.1.2.2 matt #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */
98 1.1.2.2 matt #define FTP_NEED_PASSWORD 331
99 1.1.2.2 matt #define FTP_NEED_ACCOUNT 332
100 1.1.2.2 matt #define FTP_FILE_OK 350
101 1.1.2.2 matt #define FTP_SYNTAX_ERROR 500
102 1.1.2.2 matt #define FTP_PROTOCOL_ERROR 999
103 1.1.2.2 matt
104 1.1.2.2 matt static struct url cached_host;
105 1.1.2.2 matt static conn_t *cached_connection;
106 1.1.2.2 matt
107 1.1.2.2 matt #define isftpreply(foo) (isdigit((unsigned)foo[0]) && \
108 1.1.2.2 matt isdigit((unsigned)foo[1]) && \
109 1.1.2.2 matt isdigit((unsigned)foo[2]) && \
110 1.1.2.2 matt (foo[3] == ' ' || foo[3] == '\0'))
111 1.1.2.2 matt #define isftpinfo(foo) (isdigit((unsigned)foo[0]) && \
112 1.1.2.2 matt isdigit((unsigned)foo[1]) && \
113 1.1.2.2 matt isdigit((unsigned)foo[2]) && foo[3] == '-')
114 1.1.2.2 matt
115 1.1.2.2 matt /*
116 1.1.2.2 matt * Translate IPv4 mapped IPv6 address to IPv4 address
117 1.1.2.2 matt */
118 1.1.2.2 matt static void
119 1.1.2.2 matt unmappedaddr(struct sockaddr_in6 *sin6)
120 1.1.2.2 matt {
121 1.1.2.2 matt struct sockaddr_in *sin4;
122 1.1.2.2 matt u_int32_t addr;
123 1.1.2.2 matt int port;
124 1.1.2.2 matt
125 1.1.2.2 matt if (sin6->sin6_family != AF_INET6 ||
126 1.1.2.2 matt !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
127 1.1.2.2 matt return;
128 1.1.2.2 matt sin4 = (struct sockaddr_in *)(void *)sin6;
129 1.1.2.2 matt addr = *(u_int32_t *)(void *)&sin6->sin6_addr.s6_addr[12];
130 1.1.2.2 matt port = sin6->sin6_port;
131 1.1.2.2 matt memset(sin4, 0, sizeof(struct sockaddr_in));
132 1.1.2.2 matt sin4->sin_addr.s_addr = addr;
133 1.1.2.2 matt sin4->sin_port = port;
134 1.1.2.2 matt sin4->sin_family = AF_INET;
135 1.1.2.2 matt sin4->sin_len = sizeof(struct sockaddr_in);
136 1.1.2.2 matt }
137 1.1.2.2 matt
138 1.1.2.2 matt /*
139 1.1.2.2 matt * Get server response
140 1.1.2.2 matt */
141 1.1.2.2 matt static int
142 1.1.2.2 matt _ftp_chkerr(conn_t *conn)
143 1.1.2.2 matt {
144 1.1.2.2 matt if (_fetch_getln(conn) == -1) {
145 1.1.2.2 matt _fetch_syserr();
146 1.1.2.2 matt return (-1);
147 1.1.2.2 matt }
148 1.1.2.2 matt if (isftpinfo(conn->buf)) {
149 1.1.2.2 matt while (conn->buflen && !isftpreply(conn->buf)) {
150 1.1.2.2 matt if (_fetch_getln(conn) == -1) {
151 1.1.2.2 matt _fetch_syserr();
152 1.1.2.2 matt return (-1);
153 1.1.2.2 matt }
154 1.1.2.2 matt }
155 1.1.2.2 matt }
156 1.1.2.2 matt
157 1.1.2.2 matt while (conn->buflen && isspace((unsigned)conn->buf[conn->buflen - 1]))
158 1.1.2.2 matt conn->buflen--;
159 1.1.2.2 matt conn->buf[conn->buflen] = '\0';
160 1.1.2.2 matt
161 1.1.2.2 matt if (!isftpreply(conn->buf)) {
162 1.1.2.2 matt _ftp_seterr(FTP_PROTOCOL_ERROR);
163 1.1.2.2 matt return (-1);
164 1.1.2.2 matt }
165 1.1.2.2 matt
166 1.1.2.2 matt conn->err = (conn->buf[0] - '0') * 100
167 1.1.2.2 matt + (conn->buf[1] - '0') * 10
168 1.1.2.2 matt + (conn->buf[2] - '0');
169 1.1.2.2 matt
170 1.1.2.2 matt return (conn->err);
171 1.1.2.2 matt }
172 1.1.2.2 matt
173 1.1.2.2 matt /*
174 1.1.2.2 matt * Send a command and check reply
175 1.1.2.2 matt */
176 1.1.2.2 matt static int
177 1.1.2.2 matt _ftp_cmd(conn_t *conn, const char *fmt, ...)
178 1.1.2.2 matt {
179 1.1.2.2 matt va_list ap;
180 1.1.2.2 matt size_t len;
181 1.1.2.2 matt char *msg;
182 1.1.2.2 matt int r;
183 1.1.2.2 matt
184 1.1.2.2 matt va_start(ap, fmt);
185 1.1.2.2 matt len = vasprintf(&msg, fmt, ap);
186 1.1.2.2 matt va_end(ap);
187 1.1.2.2 matt
188 1.1.2.2 matt if (msg == NULL) {
189 1.1.2.2 matt errno = ENOMEM;
190 1.1.2.2 matt _fetch_syserr();
191 1.1.2.2 matt return (-1);
192 1.1.2.2 matt }
193 1.1.2.2 matt
194 1.1.2.2 matt r = _fetch_putln(conn, msg, len);
195 1.1.2.2 matt free(msg);
196 1.1.2.2 matt
197 1.1.2.2 matt if (r == -1) {
198 1.1.2.2 matt _fetch_syserr();
199 1.1.2.2 matt return (-1);
200 1.1.2.2 matt }
201 1.1.2.2 matt
202 1.1.2.2 matt return (_ftp_chkerr(conn));
203 1.1.2.2 matt }
204 1.1.2.2 matt
205 1.1.2.2 matt /*
206 1.1.2.2 matt * Return a pointer to the filename part of a path
207 1.1.2.2 matt */
208 1.1.2.2 matt static const char *
209 1.1.2.2 matt _ftp_filename(const char *file, int *len, int *type)
210 1.1.2.2 matt {
211 1.1.2.2 matt const char *s;
212 1.1.2.2 matt
213 1.1.2.2 matt if ((s = strrchr(file, '/')) == NULL)
214 1.1.2.2 matt s = file;
215 1.1.2.2 matt else
216 1.1.2.2 matt s = s + 1;
217 1.1.2.2 matt *len = strlen(s);
218 1.1.2.2 matt if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
219 1.1.2.2 matt *type = s[*len - 1];
220 1.1.2.2 matt *len -= 7;
221 1.1.2.2 matt } else {
222 1.1.2.2 matt *type = '\0';
223 1.1.2.2 matt }
224 1.1.2.2 matt return (s);
225 1.1.2.2 matt }
226 1.1.2.2 matt
227 1.1.2.2 matt /*
228 1.1.2.2 matt * Get current working directory from the reply to a CWD, PWD or CDUP
229 1.1.2.2 matt * command.
230 1.1.2.2 matt */
231 1.1.2.2 matt static int
232 1.1.2.2 matt _ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
233 1.1.2.2 matt {
234 1.1.2.2 matt char *src, *dst, *end;
235 1.1.2.2 matt int q;
236 1.1.2.2 matt
237 1.1.2.2 matt if (conn->err != FTP_WORKING_DIRECTORY &&
238 1.1.2.2 matt conn->err != FTP_FILE_ACTION_OK)
239 1.1.2.2 matt return (FTP_PROTOCOL_ERROR);
240 1.1.2.2 matt end = conn->buf + conn->buflen;
241 1.1.2.2 matt src = conn->buf + 4;
242 1.1.2.2 matt if (src >= end || *src++ != '"')
243 1.1.2.2 matt return (FTP_PROTOCOL_ERROR);
244 1.1.2.2 matt for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
245 1.1.2.2 matt if (!q && *src == '"')
246 1.1.2.2 matt q = 1;
247 1.1.2.2 matt else if (q && *src != '"')
248 1.1.2.2 matt break;
249 1.1.2.2 matt else if (q)
250 1.1.2.2 matt *dst++ = '"', q = 0;
251 1.1.2.2 matt else
252 1.1.2.2 matt *dst++ = *src;
253 1.1.2.2 matt }
254 1.1.2.2 matt if (!pwdlen)
255 1.1.2.2 matt return (FTP_PROTOCOL_ERROR);
256 1.1.2.2 matt *dst = '\0';
257 1.1.2.2 matt #if 0
258 1.1.2.2 matt DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
259 1.1.2.2 matt #endif
260 1.1.2.2 matt return (FTP_OK);
261 1.1.2.2 matt }
262 1.1.2.2 matt
263 1.1.2.2 matt /*
264 1.1.2.2 matt * Change working directory to the directory that contains the specified
265 1.1.2.2 matt * file.
266 1.1.2.2 matt */
267 1.1.2.2 matt static int
268 1.1.2.2 matt _ftp_cwd(conn_t *conn, const char *file)
269 1.1.2.2 matt {
270 1.1.2.2 matt const char *beg, *end;
271 1.1.2.2 matt char pwd[PATH_MAX];
272 1.1.2.2 matt int e, i, len;
273 1.1.2.2 matt
274 1.1.2.2 matt /* If no slashes in name, no need to change dirs. */
275 1.1.2.2 matt if ((end = strrchr(file, '/')) == NULL)
276 1.1.2.2 matt return (0);
277 1.1.2.2 matt if ((e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
278 1.1.2.2 matt (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
279 1.1.2.2 matt _ftp_seterr(e);
280 1.1.2.2 matt return (-1);
281 1.1.2.2 matt }
282 1.1.2.2 matt for (;;) {
283 1.1.2.2 matt len = strlen(pwd);
284 1.1.2.2 matt
285 1.1.2.2 matt /* Look for a common prefix between PWD and dir to fetch. */
286 1.1.2.2 matt for (i = 0; i <= len && i <= end - file; ++i)
287 1.1.2.2 matt if (pwd[i] != file[i])
288 1.1.2.2 matt break;
289 1.1.2.2 matt #if 0
290 1.1.2.2 matt DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i));
291 1.1.2.2 matt DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i));
292 1.1.2.2 matt #endif
293 1.1.2.2 matt /* Keep going up a dir until we have a matching prefix. */
294 1.1.2.2 matt if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
295 1.1.2.2 matt break;
296 1.1.2.2 matt if ((e = _ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
297 1.1.2.2 matt (e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
298 1.1.2.2 matt (e = _ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
299 1.1.2.2 matt _ftp_seterr(e);
300 1.1.2.2 matt return (-1);
301 1.1.2.2 matt }
302 1.1.2.2 matt }
303 1.1.2.2 matt
304 1.1.2.2 matt #ifdef FTP_COMBINE_CWDS
305 1.1.2.2 matt /* Skip leading slashes, even "////". */
306 1.1.2.2 matt for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i)
307 1.1.2.2 matt /* nothing */ ;
308 1.1.2.2 matt
309 1.1.2.2 matt /* If there is no trailing dir, we're already there. */
310 1.1.2.2 matt if (beg >= end)
311 1.1.2.2 matt return (0);
312 1.1.2.2 matt
313 1.1.2.2 matt /* Change to the directory all in one chunk (e.g., foo/bar/baz). */
314 1.1.2.2 matt e = _ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg);
315 1.1.2.2 matt if (e == FTP_FILE_ACTION_OK)
316 1.1.2.2 matt return (0);
317 1.1.2.2 matt #endif /* FTP_COMBINE_CWDS */
318 1.1.2.2 matt
319 1.1.2.2 matt /* That didn't work so go back to legacy behavior (multiple CWDs). */
320 1.1.2.2 matt for (beg = file + i; beg < end; beg = file + i + 1) {
321 1.1.2.2 matt while (*beg == '/')
322 1.1.2.2 matt ++beg, ++i;
323 1.1.2.2 matt for (++i; file + i < end && file[i] != '/'; ++i)
324 1.1.2.2 matt /* nothing */ ;
325 1.1.2.2 matt e = _ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
326 1.1.2.2 matt if (e != FTP_FILE_ACTION_OK) {
327 1.1.2.2 matt _ftp_seterr(e);
328 1.1.2.2 matt return (-1);
329 1.1.2.2 matt }
330 1.1.2.2 matt }
331 1.1.2.2 matt return (0);
332 1.1.2.2 matt }
333 1.1.2.2 matt
334 1.1.2.2 matt /*
335 1.1.2.2 matt * Set transfer mode and data type
336 1.1.2.2 matt */
337 1.1.2.2 matt static int
338 1.1.2.2 matt _ftp_mode_type(conn_t *conn, int mode, int type)
339 1.1.2.2 matt {
340 1.1.2.2 matt int e;
341 1.1.2.2 matt
342 1.1.2.2 matt switch (mode) {
343 1.1.2.2 matt case 0:
344 1.1.2.2 matt case 's':
345 1.1.2.2 matt mode = 'S';
346 1.1.2.2 matt /* FALLTHROUGH */
347 1.1.2.2 matt case 'S':
348 1.1.2.2 matt break;
349 1.1.2.2 matt default:
350 1.1.2.2 matt return (FTP_PROTOCOL_ERROR);
351 1.1.2.2 matt }
352 1.1.2.2 matt if ((e = _ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
353 1.1.2.2 matt if (mode == 'S') {
354 1.1.2.2 matt /*
355 1.1.2.2 matt * Stream mode is supposed to be the default - so
356 1.1.2.2 matt * much so that some servers not only do not
357 1.1.2.2 matt * support any other mode, but do not support the
358 1.1.2.2 matt * MODE command at all.
359 1.1.2.2 matt *
360 1.1.2.2 matt * If "MODE S" fails, it is unlikely that we
361 1.1.2.2 matt * previously succeeded in setting a different
362 1.1.2.2 matt * mode. Therefore, we simply hope that the
363 1.1.2.2 matt * server is already in the correct mode, and
364 1.1.2.2 matt * silently ignore the failure.
365 1.1.2.2 matt */
366 1.1.2.2 matt } else {
367 1.1.2.2 matt return (e);
368 1.1.2.2 matt }
369 1.1.2.2 matt }
370 1.1.2.2 matt
371 1.1.2.2 matt switch (type) {
372 1.1.2.2 matt case 0:
373 1.1.2.2 matt case 'i':
374 1.1.2.2 matt type = 'I';
375 1.1.2.2 matt /* FALLTHROUGH */
376 1.1.2.2 matt case 'I':
377 1.1.2.2 matt break;
378 1.1.2.2 matt case 'a':
379 1.1.2.2 matt type = 'A';
380 1.1.2.2 matt /* FALLTHROUGH */
381 1.1.2.2 matt case 'A':
382 1.1.2.2 matt break;
383 1.1.2.2 matt case 'd':
384 1.1.2.2 matt type = 'D';
385 1.1.2.2 matt /* FALLTHROUGH */
386 1.1.2.2 matt case 'D':
387 1.1.2.2 matt /* can't handle yet */
388 1.1.2.2 matt default:
389 1.1.2.2 matt return (FTP_PROTOCOL_ERROR);
390 1.1.2.2 matt }
391 1.1.2.2 matt if ((e = _ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
392 1.1.2.2 matt return (e);
393 1.1.2.2 matt
394 1.1.2.2 matt return (FTP_OK);
395 1.1.2.2 matt }
396 1.1.2.2 matt
397 1.1.2.2 matt /*
398 1.1.2.2 matt * Request and parse file stats
399 1.1.2.2 matt */
400 1.1.2.2 matt static int
401 1.1.2.2 matt _ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
402 1.1.2.2 matt {
403 1.1.2.2 matt char *ln;
404 1.1.2.2 matt const char *filename;
405 1.1.2.2 matt int filenamelen, type;
406 1.1.2.2 matt struct tm tm;
407 1.1.2.2 matt time_t t;
408 1.1.2.2 matt int e;
409 1.1.2.2 matt
410 1.1.2.2 matt us->size = -1;
411 1.1.2.2 matt us->atime = us->mtime = 0;
412 1.1.2.2 matt
413 1.1.2.2 matt filename = _ftp_filename(file, &filenamelen, &type);
414 1.1.2.2 matt
415 1.1.2.2 matt if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK) {
416 1.1.2.2 matt _ftp_seterr(e);
417 1.1.2.2 matt return (-1);
418 1.1.2.2 matt }
419 1.1.2.2 matt
420 1.1.2.2 matt e = _ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
421 1.1.2.2 matt if (e != FTP_FILE_STATUS) {
422 1.1.2.2 matt _ftp_seterr(e);
423 1.1.2.2 matt return (-1);
424 1.1.2.2 matt }
425 1.1.2.2 matt for (ln = conn->buf + 4; *ln && isspace((unsigned)*ln); ln++)
426 1.1.2.2 matt /* nothing */ ;
427 1.1.2.2 matt for (us->size = 0; *ln && isdigit((unsigned)*ln); ln++)
428 1.1.2.2 matt us->size = us->size * 10 + *ln - '0';
429 1.1.2.2 matt if (*ln && !isspace((unsigned)*ln)) {
430 1.1.2.2 matt _ftp_seterr(FTP_PROTOCOL_ERROR);
431 1.1.2.2 matt us->size = -1;
432 1.1.2.2 matt return (-1);
433 1.1.2.2 matt }
434 1.1.2.2 matt if (us->size == 0)
435 1.1.2.2 matt us->size = -1;
436 1.1.2.2 matt DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size));
437 1.1.2.2 matt
438 1.1.2.2 matt e = _ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
439 1.1.2.2 matt if (e != FTP_FILE_STATUS) {
440 1.1.2.2 matt _ftp_seterr(e);
441 1.1.2.2 matt return (-1);
442 1.1.2.2 matt }
443 1.1.2.2 matt for (ln = conn->buf + 4; *ln && isspace((unsigned)*ln); ln++)
444 1.1.2.2 matt /* nothing */ ;
445 1.1.2.2 matt switch (strspn(ln, "0123456789")) {
446 1.1.2.2 matt case 14:
447 1.1.2.2 matt break;
448 1.1.2.2 matt case 15:
449 1.1.2.2 matt ln++;
450 1.1.2.2 matt ln[0] = '2';
451 1.1.2.2 matt ln[1] = '0';
452 1.1.2.2 matt break;
453 1.1.2.2 matt default:
454 1.1.2.2 matt _ftp_seterr(FTP_PROTOCOL_ERROR);
455 1.1.2.2 matt return (-1);
456 1.1.2.2 matt }
457 1.1.2.2 matt if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
458 1.1.2.2 matt &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
459 1.1.2.2 matt &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
460 1.1.2.2 matt _ftp_seterr(FTP_PROTOCOL_ERROR);
461 1.1.2.2 matt return (-1);
462 1.1.2.2 matt }
463 1.1.2.2 matt tm.tm_mon--;
464 1.1.2.2 matt tm.tm_year -= 1900;
465 1.1.2.2 matt tm.tm_isdst = -1;
466 1.1.2.2 matt t = timegm(&tm);
467 1.1.2.2 matt if (t == (time_t)-1)
468 1.1.2.2 matt t = time(NULL);
469 1.1.2.2 matt us->mtime = t;
470 1.1.2.2 matt us->atime = t;
471 1.1.2.2 matt DEBUG(fprintf(stderr,
472 1.1.2.2 matt "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
473 1.1.2.2 matt tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
474 1.1.2.2 matt tm.tm_hour, tm.tm_min, tm.tm_sec));
475 1.1.2.2 matt return (0);
476 1.1.2.2 matt }
477 1.1.2.2 matt
478 1.1.2.2 matt /*
479 1.1.2.2 matt * I/O functions for FTP
480 1.1.2.2 matt */
481 1.1.2.2 matt struct ftpio {
482 1.1.2.2 matt conn_t *cconn; /* Control connection */
483 1.1.2.2 matt conn_t *dconn; /* Data connection */
484 1.1.2.2 matt int dir; /* Direction */
485 1.1.2.2 matt int eof; /* EOF reached */
486 1.1.2.2 matt int err; /* Error code */
487 1.1.2.2 matt };
488 1.1.2.2 matt
489 1.1.2.2 matt static int _ftp_readfn(void *, char *, int);
490 1.1.2.2 matt static int _ftp_writefn(void *, const char *, int);
491 1.1.2.2 matt static fpos_t _ftp_seekfn(void *, fpos_t, int);
492 1.1.2.2 matt static int _ftp_closefn(void *);
493 1.1.2.2 matt
494 1.1.2.2 matt static int
495 1.1.2.2 matt _ftp_readfn(void *v, char *buf, int len)
496 1.1.2.2 matt {
497 1.1.2.2 matt struct ftpio *io;
498 1.1.2.2 matt int r;
499 1.1.2.2 matt
500 1.1.2.2 matt io = (struct ftpio *)v;
501 1.1.2.2 matt if (io == NULL) {
502 1.1.2.2 matt errno = EBADF;
503 1.1.2.2 matt return (-1);
504 1.1.2.2 matt }
505 1.1.2.2 matt if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
506 1.1.2.2 matt errno = EBADF;
507 1.1.2.2 matt return (-1);
508 1.1.2.2 matt }
509 1.1.2.2 matt if (io->err) {
510 1.1.2.2 matt errno = io->err;
511 1.1.2.2 matt return (-1);
512 1.1.2.2 matt }
513 1.1.2.2 matt if (io->eof)
514 1.1.2.2 matt return (0);
515 1.1.2.2 matt r = _fetch_read(io->dconn, buf, (unsigned)len);
516 1.1.2.2 matt if (r > 0)
517 1.1.2.2 matt return (r);
518 1.1.2.2 matt if (r == 0) {
519 1.1.2.2 matt io->eof = 1;
520 1.1.2.2 matt return (0);
521 1.1.2.2 matt }
522 1.1.2.2 matt if (errno != EINTR)
523 1.1.2.2 matt io->err = errno;
524 1.1.2.2 matt return (-1);
525 1.1.2.2 matt }
526 1.1.2.2 matt
527 1.1.2.2 matt static int
528 1.1.2.2 matt _ftp_writefn(void *v, const char *buf, int len)
529 1.1.2.2 matt {
530 1.1.2.2 matt struct ftpio *io;
531 1.1.2.2 matt int w;
532 1.1.2.2 matt
533 1.1.2.2 matt io = (struct ftpio *)v;
534 1.1.2.2 matt if (io == NULL) {
535 1.1.2.2 matt errno = EBADF;
536 1.1.2.2 matt return (-1);
537 1.1.2.2 matt }
538 1.1.2.2 matt if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
539 1.1.2.2 matt errno = EBADF;
540 1.1.2.2 matt return (-1);
541 1.1.2.2 matt }
542 1.1.2.2 matt if (io->err) {
543 1.1.2.2 matt errno = io->err;
544 1.1.2.2 matt return (-1);
545 1.1.2.2 matt }
546 1.1.2.2 matt w = _fetch_write(io->dconn, buf, (unsigned)len);
547 1.1.2.2 matt if (w >= 0)
548 1.1.2.2 matt return (w);
549 1.1.2.2 matt if (errno != EINTR)
550 1.1.2.2 matt io->err = errno;
551 1.1.2.2 matt return (-1);
552 1.1.2.2 matt }
553 1.1.2.2 matt
554 1.1.2.2 matt /* ARGSUSED1 */
555 1.1.2.2 matt static fpos_t
556 1.1.2.2 matt _ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
557 1.1.2.2 matt {
558 1.1.2.2 matt struct ftpio *io;
559 1.1.2.2 matt
560 1.1.2.2 matt io = (struct ftpio *)v;
561 1.1.2.2 matt if (io == NULL) {
562 1.1.2.2 matt errno = EBADF;
563 1.1.2.2 matt return (-1);
564 1.1.2.2 matt }
565 1.1.2.2 matt errno = ESPIPE;
566 1.1.2.2 matt return (-1);
567 1.1.2.2 matt }
568 1.1.2.2 matt
569 1.1.2.2 matt static int
570 1.1.2.2 matt _ftp_closefn(void *v)
571 1.1.2.2 matt {
572 1.1.2.2 matt struct ftpio *io;
573 1.1.2.2 matt int r;
574 1.1.2.2 matt
575 1.1.2.2 matt io = (struct ftpio *)v;
576 1.1.2.2 matt if (io == NULL) {
577 1.1.2.2 matt errno = EBADF;
578 1.1.2.2 matt return (-1);
579 1.1.2.2 matt }
580 1.1.2.2 matt if (io->dir == -1)
581 1.1.2.2 matt return (0);
582 1.1.2.2 matt if (io->cconn == NULL || io->dconn == NULL) {
583 1.1.2.2 matt errno = EBADF;
584 1.1.2.2 matt return (-1);
585 1.1.2.2 matt }
586 1.1.2.2 matt _fetch_close(io->dconn);
587 1.1.2.2 matt io->dir = -1;
588 1.1.2.2 matt io->dconn = NULL;
589 1.1.2.2 matt DEBUG(fprintf(stderr, "Waiting for final status\n"));
590 1.1.2.2 matt r = _ftp_chkerr(io->cconn);
591 1.1.2.2 matt if (io->cconn == cached_connection && io->cconn->ref == 1)
592 1.1.2.2 matt cached_connection = NULL;
593 1.1.2.2 matt _fetch_close(io->cconn);
594 1.1.2.2 matt free(io);
595 1.1.2.2 matt return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
596 1.1.2.2 matt }
597 1.1.2.2 matt
598 1.1.2.2 matt static FILE *
599 1.1.2.2 matt _ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
600 1.1.2.2 matt {
601 1.1.2.2 matt struct ftpio *io;
602 1.1.2.2 matt FILE *f;
603 1.1.2.2 matt
604 1.1.2.2 matt if (cconn == NULL || dconn == NULL)
605 1.1.2.2 matt return (NULL);
606 1.1.2.2 matt if ((io = malloc(sizeof(*io))) == NULL)
607 1.1.2.2 matt return (NULL);
608 1.1.2.2 matt io->cconn = cconn;
609 1.1.2.2 matt io->dconn = dconn;
610 1.1.2.2 matt io->dir = mode;
611 1.1.2.2 matt io->eof = io->err = 0;
612 1.1.2.2 matt f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn);
613 1.1.2.2 matt if (f == NULL)
614 1.1.2.2 matt free(io);
615 1.1.2.2 matt return (f);
616 1.1.2.2 matt }
617 1.1.2.2 matt
618 1.1.2.2 matt /*
619 1.1.2.2 matt * Transfer file
620 1.1.2.2 matt */
621 1.1.2.2 matt static FILE *
622 1.1.2.2 matt _ftp_transfer(conn_t *conn, const char *oper, const char *file,
623 1.1.2.2 matt int mode, off_t offset, const char *flags)
624 1.1.2.2 matt {
625 1.1.2.2 matt struct sockaddr_storage sa;
626 1.1.2.2 matt struct sockaddr_in6 *sin6;
627 1.1.2.2 matt struct sockaddr_in *sin4;
628 1.1.2.2 matt const char *bindaddr;
629 1.1.2.2 matt const char *filename;
630 1.1.2.2 matt int filenamelen, type;
631 1.1.2.2 matt int low, pasv, verbose;
632 1.1.2.2 matt int e, sd = -1;
633 1.1.2.2 matt socklen_t l;
634 1.1.2.2 matt char *s;
635 1.1.2.2 matt FILE *df;
636 1.1.2.2 matt
637 1.1.2.2 matt /* check flags */
638 1.1.2.2 matt low = CHECK_FLAG('l');
639 1.1.2.2 matt pasv = CHECK_FLAG('p');
640 1.1.2.2 matt verbose = CHECK_FLAG('v');
641 1.1.2.2 matt
642 1.1.2.2 matt /* passive mode */
643 1.1.2.2 matt if (!pasv)
644 1.1.2.2 matt pasv = ((s = getenv("FTP_PASSIVE_MODE")) != NULL &&
645 1.1.2.2 matt strncasecmp(s, "no", 2) != 0);
646 1.1.2.2 matt
647 1.1.2.2 matt /* isolate filename */
648 1.1.2.2 matt filename = _ftp_filename(file, &filenamelen, &type);
649 1.1.2.2 matt
650 1.1.2.2 matt /* set transfer mode and data type */
651 1.1.2.2 matt if ((e = _ftp_mode_type(conn, 0, type)) != FTP_OK)
652 1.1.2.2 matt goto ouch;
653 1.1.2.2 matt
654 1.1.2.2 matt /* find our own address, bind, and listen */
655 1.1.2.2 matt l = sizeof(sa);
656 1.1.2.2 matt if (getsockname(conn->sd, (struct sockaddr *)(void *)&sa, &l) == -1)
657 1.1.2.2 matt goto sysouch;
658 1.1.2.2 matt if (sa.ss_family == AF_INET6)
659 1.1.2.2 matt unmappedaddr((struct sockaddr_in6 *)(void *)&sa);
660 1.1.2.2 matt
661 1.1.2.2 matt /* open data socket */
662 1.1.2.2 matt if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
663 1.1.2.2 matt _fetch_syserr();
664 1.1.2.2 matt return (NULL);
665 1.1.2.2 matt }
666 1.1.2.2 matt
667 1.1.2.2 matt if (pasv) {
668 1.1.2.2 matt u_char addr[64];
669 1.1.2.2 matt char *ln, *p;
670 1.1.2.2 matt unsigned int i;
671 1.1.2.2 matt int port;
672 1.1.2.2 matt
673 1.1.2.2 matt /* send PASV command */
674 1.1.2.2 matt if (verbose)
675 1.1.2.2 matt _fetch_info("setting passive mode");
676 1.1.2.2 matt switch (sa.ss_family) {
677 1.1.2.2 matt case AF_INET:
678 1.1.2.2 matt if ((e = _ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
679 1.1.2.2 matt goto ouch;
680 1.1.2.2 matt break;
681 1.1.2.2 matt case AF_INET6:
682 1.1.2.2 matt if ((e = _ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
683 1.1.2.2 matt if (e == -1)
684 1.1.2.2 matt goto ouch;
685 1.1.2.2 matt if ((e = _ftp_cmd(conn, "LPSV")) !=
686 1.1.2.2 matt FTP_LPASSIVE_MODE)
687 1.1.2.2 matt goto ouch;
688 1.1.2.2 matt }
689 1.1.2.2 matt break;
690 1.1.2.2 matt default:
691 1.1.2.2 matt e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
692 1.1.2.2 matt goto ouch;
693 1.1.2.2 matt }
694 1.1.2.2 matt
695 1.1.2.2 matt /*
696 1.1.2.2 matt * Find address and port number. The reply to the PASV command
697 1.1.2.2 matt * is IMHO the one and only weak point in the FTP protocol.
698 1.1.2.2 matt */
699 1.1.2.2 matt ln = conn->buf;
700 1.1.2.2 matt switch (e) {
701 1.1.2.2 matt case FTP_PASSIVE_MODE:
702 1.1.2.2 matt case FTP_LPASSIVE_MODE:
703 1.1.2.2 matt for (p = ln + 3; *p && !isdigit((unsigned)*p); p++)
704 1.1.2.2 matt /* nothing */ ;
705 1.1.2.2 matt if (!*p) {
706 1.1.2.2 matt e = FTP_PROTOCOL_ERROR;
707 1.1.2.2 matt goto ouch;
708 1.1.2.2 matt }
709 1.1.2.2 matt l = (e == FTP_PASSIVE_MODE ? 6 : 21);
710 1.1.2.2 matt for (i = 0; *p && i < l; i++, p++)
711 1.1.2.2 matt addr[i] = (unsigned char) strtol(p, &p, 10);
712 1.1.2.2 matt if (i < l) {
713 1.1.2.2 matt e = FTP_PROTOCOL_ERROR;
714 1.1.2.2 matt goto ouch;
715 1.1.2.2 matt }
716 1.1.2.2 matt break;
717 1.1.2.2 matt case FTP_EPASSIVE_MODE:
718 1.1.2.2 matt for (p = ln + 3; *p && *p != '('; p++)
719 1.1.2.2 matt /* nothing */ ;
720 1.1.2.2 matt if (!*p) {
721 1.1.2.2 matt e = FTP_PROTOCOL_ERROR;
722 1.1.2.2 matt goto ouch;
723 1.1.2.2 matt }
724 1.1.2.2 matt ++p;
725 1.1.2.2 matt if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
726 1.1.2.2 matt &port, &addr[3]) != 5 ||
727 1.1.2.2 matt addr[0] != addr[1] ||
728 1.1.2.2 matt addr[0] != addr[2] || addr[0] != addr[3]) {
729 1.1.2.2 matt e = FTP_PROTOCOL_ERROR;
730 1.1.2.2 matt goto ouch;
731 1.1.2.2 matt }
732 1.1.2.2 matt break;
733 1.1.2.2 matt }
734 1.1.2.2 matt
735 1.1.2.2 matt /* seek to required offset */
736 1.1.2.2 matt if (offset)
737 1.1.2.2 matt if (_ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
738 1.1.2.2 matt goto sysouch;
739 1.1.2.2 matt
740 1.1.2.2 matt /* construct sockaddr for data socket */
741 1.1.2.2 matt l = sizeof(sa);
742 1.1.2.2 matt if (getpeername(conn->sd, (struct sockaddr *)(void *)&sa, &l) == -1)
743 1.1.2.2 matt goto sysouch;
744 1.1.2.2 matt if (sa.ss_family == AF_INET6)
745 1.1.2.2 matt unmappedaddr((struct sockaddr_in6 *)(void *)&sa);
746 1.1.2.2 matt switch (sa.ss_family) {
747 1.1.2.2 matt case AF_INET6:
748 1.1.2.2 matt sin6 = (struct sockaddr_in6 *)(void *)&sa;
749 1.1.2.2 matt if (e == FTP_EPASSIVE_MODE)
750 1.1.2.2 matt sin6->sin6_port = htons(port);
751 1.1.2.2 matt else {
752 1.1.2.2 matt bcopy(addr + 2, (char *)(void *)&sin6->sin6_addr, 16);
753 1.1.2.2 matt bcopy(addr + 19, (char *)(void *)&sin6->sin6_port, 2);
754 1.1.2.2 matt }
755 1.1.2.2 matt break;
756 1.1.2.2 matt case AF_INET:
757 1.1.2.2 matt sin4 = (struct sockaddr_in *)(void *)&sa;
758 1.1.2.2 matt if (e == FTP_EPASSIVE_MODE)
759 1.1.2.2 matt sin4->sin_port = htons(port);
760 1.1.2.2 matt else {
761 1.1.2.2 matt bcopy(addr, (char *)(void *)&sin4->sin_addr, 4);
762 1.1.2.2 matt bcopy(addr + 4, (char *)(void *)&sin4->sin_port, 2);
763 1.1.2.2 matt }
764 1.1.2.2 matt break;
765 1.1.2.2 matt default:
766 1.1.2.2 matt e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
767 1.1.2.2 matt break;
768 1.1.2.2 matt }
769 1.1.2.2 matt
770 1.1.2.2 matt /* connect to data port */
771 1.1.2.2 matt if (verbose)
772 1.1.2.2 matt _fetch_info("opening data connection");
773 1.1.2.2 matt bindaddr = getenv("FETCH_BIND_ADDRESS");
774 1.1.2.2 matt if (bindaddr != NULL && *bindaddr != '\0' &&
775 1.1.2.2 matt _fetch_bind(sd, sa.ss_family, bindaddr) != 0)
776 1.1.2.2 matt goto sysouch;
777 1.1.2.2 matt if (connect(sd, (struct sockaddr *)(void *)&sa, (unsigned)sa.ss_len) == -1)
778 1.1.2.2 matt goto sysouch;
779 1.1.2.2 matt
780 1.1.2.2 matt /* make the server initiate the transfer */
781 1.1.2.2 matt if (verbose)
782 1.1.2.2 matt _fetch_info("initiating transfer");
783 1.1.2.2 matt e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
784 1.1.2.2 matt if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
785 1.1.2.2 matt goto ouch;
786 1.1.2.2 matt
787 1.1.2.2 matt } else {
788 1.1.2.2 matt u_int32_t a;
789 1.1.2.2 matt u_short p;
790 1.1.2.2 matt int arg, d;
791 1.1.2.2 matt char *ap;
792 1.1.2.2 matt char hname[INET6_ADDRSTRLEN];
793 1.1.2.2 matt
794 1.1.2.2 matt switch (sa.ss_family) {
795 1.1.2.2 matt case AF_INET6:
796 1.1.2.2 matt ((struct sockaddr_in6 *)(void *)&sa)->sin6_port = 0;
797 1.1.2.2 matt #ifdef IPV6_PORTRANGE
798 1.1.2.2 matt arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
799 1.1.2.2 matt if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
800 1.1.2.2 matt (char *)(void *)&arg, sizeof(arg)) == -1)
801 1.1.2.2 matt goto sysouch;
802 1.1.2.2 matt #endif
803 1.1.2.2 matt break;
804 1.1.2.2 matt case AF_INET:
805 1.1.2.2 matt ((struct sockaddr_in *)(void *)&sa)->sin_port = 0;
806 1.1.2.2 matt arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
807 1.1.2.2 matt if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
808 1.1.2.2 matt (char *)(void *)&arg, sizeof(arg)) == -1)
809 1.1.2.2 matt goto sysouch;
810 1.1.2.2 matt break;
811 1.1.2.2 matt }
812 1.1.2.2 matt if (verbose)
813 1.1.2.2 matt _fetch_info("binding data socket");
814 1.1.2.2 matt if (bind(sd, (struct sockaddr *)(void *)&sa, (unsigned) sa.ss_len) == -1)
815 1.1.2.2 matt goto sysouch;
816 1.1.2.2 matt if (listen(sd, 1) == -1)
817 1.1.2.2 matt goto sysouch;
818 1.1.2.2 matt
819 1.1.2.2 matt /* find what port we're on and tell the server */
820 1.1.2.2 matt if (getsockname(sd, (struct sockaddr *)(void *)&sa, &l) == -1)
821 1.1.2.2 matt goto sysouch;
822 1.1.2.2 matt switch (sa.ss_family) {
823 1.1.2.2 matt case AF_INET:
824 1.1.2.2 matt sin4 = (struct sockaddr_in *)(void *)&sa;
825 1.1.2.2 matt a = ntohl(sin4->sin_addr.s_addr);
826 1.1.2.2 matt p = ntohs(sin4->sin_port);
827 1.1.2.2 matt e = _ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
828 1.1.2.2 matt (a >> 24) & 0xff, (a >> 16) & 0xff,
829 1.1.2.2 matt (a >> 8) & 0xff, a & 0xff,
830 1.1.2.2 matt ((unsigned)p >> 8) & 0xff, p & 0xff);
831 1.1.2.2 matt break;
832 1.1.2.2 matt case AF_INET6:
833 1.1.2.2 matt #define UC(b) (((int)b)&0xff)
834 1.1.2.2 matt e = -1;
835 1.1.2.2 matt sin6 = (struct sockaddr_in6 *)(void *)&sa;
836 1.1.2.2 matt sin6->sin6_scope_id = 0;
837 1.1.2.2 matt if (getnameinfo((struct sockaddr *)(void *)&sa,
838 1.1.2.2 matt (unsigned)sa.ss_len,
839 1.1.2.2 matt hname, sizeof(hname),
840 1.1.2.2 matt NULL, 0, NI_NUMERICHOST) == 0) {
841 1.1.2.2 matt e = _ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
842 1.1.2.2 matt htons(sin6->sin6_port));
843 1.1.2.2 matt if (e == -1)
844 1.1.2.2 matt goto ouch;
845 1.1.2.2 matt }
846 1.1.2.2 matt if (e != FTP_OK) {
847 1.1.2.2 matt ap = (char *)(void *)&sin6->sin6_addr;
848 1.1.2.2 matt e = _ftp_cmd(conn,
849 1.1.2.2 matt "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
850 1.1.2.2 matt 6, 16,
851 1.1.2.2 matt UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
852 1.1.2.2 matt UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
853 1.1.2.2 matt UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
854 1.1.2.2 matt UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
855 1.1.2.2 matt 2,
856 1.1.2.2 matt ((unsigned)ntohs(sin6->sin6_port) >> 8) & 0xff,
857 1.1.2.2 matt ntohs(sin6->sin6_port) & 0xff);
858 1.1.2.2 matt }
859 1.1.2.2 matt break;
860 1.1.2.2 matt default:
861 1.1.2.2 matt e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
862 1.1.2.2 matt goto ouch;
863 1.1.2.2 matt }
864 1.1.2.2 matt if (e != FTP_OK)
865 1.1.2.2 matt goto ouch;
866 1.1.2.2 matt
867 1.1.2.2 matt /* seek to required offset */
868 1.1.2.2 matt if (offset)
869 1.1.2.2 matt if (_ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
870 1.1.2.2 matt goto sysouch;
871 1.1.2.2 matt
872 1.1.2.2 matt /* make the server initiate the transfer */
873 1.1.2.2 matt if (verbose)
874 1.1.2.2 matt _fetch_info("initiating transfer");
875 1.1.2.2 matt e = _ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
876 1.1.2.2 matt if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
877 1.1.2.2 matt goto ouch;
878 1.1.2.2 matt
879 1.1.2.2 matt /* accept the incoming connection and go to town */
880 1.1.2.2 matt if ((d = accept(sd, NULL, NULL)) == -1)
881 1.1.2.2 matt goto sysouch;
882 1.1.2.2 matt close(sd);
883 1.1.2.2 matt sd = d;
884 1.1.2.2 matt }
885 1.1.2.2 matt
886 1.1.2.2 matt if ((df = _ftp_setup(conn, _fetch_reopen(sd), mode)) == NULL)
887 1.1.2.2 matt goto sysouch;
888 1.1.2.2 matt return (df);
889 1.1.2.2 matt
890 1.1.2.2 matt sysouch:
891 1.1.2.2 matt _fetch_syserr();
892 1.1.2.2 matt if (sd >= 0)
893 1.1.2.2 matt close(sd);
894 1.1.2.2 matt return (NULL);
895 1.1.2.2 matt
896 1.1.2.2 matt ouch:
897 1.1.2.2 matt if (e != -1)
898 1.1.2.2 matt _ftp_seterr(e);
899 1.1.2.2 matt if (sd >= 0)
900 1.1.2.2 matt close(sd);
901 1.1.2.2 matt return (NULL);
902 1.1.2.2 matt }
903 1.1.2.2 matt
904 1.1.2.2 matt /*
905 1.1.2.2 matt * Authenticate
906 1.1.2.2 matt */
907 1.1.2.2 matt static int
908 1.1.2.2 matt _ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
909 1.1.2.2 matt {
910 1.1.2.2 matt const char *user, *pwd, *logname;
911 1.1.2.2 matt char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
912 1.1.2.2 matt int e, len;
913 1.1.2.2 matt
914 1.1.2.2 matt /* XXX FTP_AUTH, and maybe .netrc */
915 1.1.2.2 matt
916 1.1.2.2 matt /* send user name and password */
917 1.1.2.2 matt if (url->user[0] == '\0')
918 1.1.2.2 matt _fetch_netrc_auth(url);
919 1.1.2.2 matt user = url->user;
920 1.1.2.2 matt if (*user == '\0')
921 1.1.2.2 matt user = getenv("FTP_LOGIN");
922 1.1.2.2 matt if (user == NULL || *user == '\0')
923 1.1.2.2 matt user = FTP_ANONYMOUS_USER;
924 1.1.2.2 matt if (purl && url->port == _fetch_default_port(url->scheme))
925 1.1.2.2 matt e = _ftp_cmd(conn, "USER %s@%s", user, url->host);
926 1.1.2.2 matt else if (purl)
927 1.1.2.2 matt e = _ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
928 1.1.2.2 matt else
929 1.1.2.2 matt e = _ftp_cmd(conn, "USER %s", user);
930 1.1.2.2 matt
931 1.1.2.2 matt /* did the server request a password? */
932 1.1.2.2 matt if (e == FTP_NEED_PASSWORD) {
933 1.1.2.2 matt pwd = url->pwd;
934 1.1.2.2 matt if (*pwd == '\0')
935 1.1.2.2 matt pwd = getenv("FTP_PASSWORD");
936 1.1.2.2 matt if (pwd == NULL || *pwd == '\0') {
937 1.1.2.2 matt if ((logname = getlogin()) == 0)
938 1.1.2.2 matt logname = FTP_ANONYMOUS_USER;
939 1.1.2.2 matt if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
940 1.1.2.2 matt len = 0;
941 1.1.2.2 matt else if (len > MAXLOGNAME)
942 1.1.2.2 matt len = MAXLOGNAME;
943 1.1.2.2 matt gethostname(pbuf + len, sizeof(pbuf) - len);
944 1.1.2.2 matt pwd = pbuf;
945 1.1.2.2 matt }
946 1.1.2.2 matt e = _ftp_cmd(conn, "PASS %s", pwd);
947 1.1.2.2 matt }
948 1.1.2.2 matt
949 1.1.2.2 matt return (e);
950 1.1.2.2 matt }
951 1.1.2.2 matt
952 1.1.2.2 matt /*
953 1.1.2.2 matt * Log on to FTP server
954 1.1.2.2 matt */
955 1.1.2.2 matt static conn_t *
956 1.1.2.2 matt _ftp_connect(struct url *url, struct url *purl, const char *flags)
957 1.1.2.2 matt {
958 1.1.2.2 matt conn_t *conn;
959 1.1.2.2 matt int e, direct, verbose;
960 1.1.2.2 matt #ifdef INET6
961 1.1.2.2 matt int af = AF_UNSPEC;
962 1.1.2.2 matt #else
963 1.1.2.2 matt int af = AF_INET;
964 1.1.2.2 matt #endif
965 1.1.2.2 matt
966 1.1.2.2 matt direct = CHECK_FLAG('d');
967 1.1.2.2 matt verbose = CHECK_FLAG('v');
968 1.1.2.2 matt if (CHECK_FLAG('4'))
969 1.1.2.2 matt af = AF_INET;
970 1.1.2.2 matt else if (CHECK_FLAG('6'))
971 1.1.2.2 matt af = AF_INET6;
972 1.1.2.2 matt
973 1.1.2.2 matt if (direct)
974 1.1.2.2 matt purl = NULL;
975 1.1.2.2 matt
976 1.1.2.2 matt /* check for proxy */
977 1.1.2.2 matt if (purl) {
978 1.1.2.2 matt /* XXX proxy authentication! */
979 1.1.2.2 matt conn = _fetch_connect(purl->host, purl->port, af, verbose);
980 1.1.2.2 matt } else {
981 1.1.2.2 matt /* no proxy, go straight to target */
982 1.1.2.2 matt conn = _fetch_connect(url->host, url->port, af, verbose);
983 1.1.2.2 matt purl = NULL;
984 1.1.2.2 matt }
985 1.1.2.2 matt
986 1.1.2.2 matt /* check connection */
987 1.1.2.2 matt if (conn == NULL)
988 1.1.2.2 matt /* _fetch_connect() has already set an error code */
989 1.1.2.2 matt return (NULL);
990 1.1.2.2 matt
991 1.1.2.2 matt /* expect welcome message */
992 1.1.2.2 matt if ((e = _ftp_chkerr(conn)) != FTP_SERVICE_READY)
993 1.1.2.2 matt goto fouch;
994 1.1.2.2 matt
995 1.1.2.2 matt /* authenticate */
996 1.1.2.2 matt if ((e = _ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
997 1.1.2.2 matt goto fouch;
998 1.1.2.2 matt
999 1.1.2.2 matt /* TODO: Request extended features supported, if any (RFC 3659). */
1000 1.1.2.2 matt
1001 1.1.2.2 matt /* done */
1002 1.1.2.2 matt return (conn);
1003 1.1.2.2 matt
1004 1.1.2.2 matt fouch:
1005 1.1.2.2 matt if (e != -1)
1006 1.1.2.2 matt _ftp_seterr(e);
1007 1.1.2.2 matt _fetch_close(conn);
1008 1.1.2.2 matt return (NULL);
1009 1.1.2.2 matt }
1010 1.1.2.2 matt
1011 1.1.2.2 matt /*
1012 1.1.2.2 matt * Disconnect from server
1013 1.1.2.2 matt */
1014 1.1.2.2 matt static void
1015 1.1.2.2 matt _ftp_disconnect(conn_t *conn)
1016 1.1.2.2 matt {
1017 1.1.2.2 matt (void)_ftp_cmd(conn, "QUIT");
1018 1.1.2.2 matt if (conn == cached_connection && conn->ref == 1)
1019 1.1.2.2 matt cached_connection = NULL;
1020 1.1.2.2 matt _fetch_close(conn);
1021 1.1.2.2 matt }
1022 1.1.2.2 matt
1023 1.1.2.2 matt /*
1024 1.1.2.2 matt * Check if we're already connected
1025 1.1.2.2 matt */
1026 1.1.2.2 matt static int
1027 1.1.2.2 matt _ftp_isconnected(struct url *url)
1028 1.1.2.2 matt {
1029 1.1.2.2 matt return (cached_connection
1030 1.1.2.2 matt && (strcmp(url->host, cached_host.host) == 0)
1031 1.1.2.2 matt && (strcmp(url->user, cached_host.user) == 0)
1032 1.1.2.2 matt && (strcmp(url->pwd, cached_host.pwd) == 0)
1033 1.1.2.2 matt && (url->port == cached_host.port));
1034 1.1.2.2 matt }
1035 1.1.2.2 matt
1036 1.1.2.2 matt /*
1037 1.1.2.2 matt * Check the cache, reconnect if no luck
1038 1.1.2.2 matt */
1039 1.1.2.2 matt static conn_t *
1040 1.1.2.2 matt _ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
1041 1.1.2.2 matt {
1042 1.1.2.2 matt conn_t *conn;
1043 1.1.2.2 matt int e;
1044 1.1.2.2 matt
1045 1.1.2.2 matt /* set default port */
1046 1.1.2.2 matt if (!url->port)
1047 1.1.2.2 matt url->port = _fetch_default_port(url->scheme);
1048 1.1.2.2 matt
1049 1.1.2.2 matt /* try to use previously cached connection */
1050 1.1.2.2 matt if (_ftp_isconnected(url)) {
1051 1.1.2.2 matt e = _ftp_cmd(cached_connection, "NOOP");
1052 1.1.2.2 matt if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
1053 1.1.2.2 matt return (_fetch_ref(cached_connection));
1054 1.1.2.2 matt }
1055 1.1.2.2 matt
1056 1.1.2.2 matt /* connect to server */
1057 1.1.2.2 matt if ((conn = _ftp_connect(url, purl, flags)) == NULL)
1058 1.1.2.2 matt return (NULL);
1059 1.1.2.2 matt if (cached_connection)
1060 1.1.2.2 matt _ftp_disconnect(cached_connection);
1061 1.1.2.2 matt cached_connection = _fetch_ref(conn);
1062 1.1.2.2 matt memcpy(&cached_host, url, sizeof(*url));
1063 1.1.2.2 matt return (conn);
1064 1.1.2.2 matt }
1065 1.1.2.2 matt
1066 1.1.2.2 matt /*
1067 1.1.2.2 matt * Check the proxy settings
1068 1.1.2.2 matt */
1069 1.1.2.2 matt static struct url *
1070 1.1.2.2 matt _ftp_get_proxy(const char *flags)
1071 1.1.2.2 matt {
1072 1.1.2.2 matt struct url *purl;
1073 1.1.2.2 matt char *p;
1074 1.1.2.2 matt
1075 1.1.2.2 matt if (flags != NULL && strchr(flags, 'd') != NULL)
1076 1.1.2.2 matt return (NULL);
1077 1.1.2.2 matt if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1078 1.1.2.2 matt (p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1079 1.1.2.2 matt *p && (purl = fetchParseURL(p)) != NULL) {
1080 1.1.2.2 matt if (!*purl->scheme) {
1081 1.1.2.2 matt if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1082 1.1.2.2 matt strcpy(purl->scheme, SCHEME_FTP);
1083 1.1.2.2 matt else
1084 1.1.2.2 matt strcpy(purl->scheme, SCHEME_HTTP);
1085 1.1.2.2 matt }
1086 1.1.2.2 matt if (!purl->port)
1087 1.1.2.2 matt purl->port = _fetch_default_proxy_port(purl->scheme);
1088 1.1.2.2 matt if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
1089 1.1.2.2 matt strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
1090 1.1.2.2 matt return (purl);
1091 1.1.2.2 matt fetchFreeURL(purl);
1092 1.1.2.2 matt }
1093 1.1.2.2 matt return (NULL);
1094 1.1.2.2 matt }
1095 1.1.2.2 matt
1096 1.1.2.2 matt /*
1097 1.1.2.2 matt * Process an FTP request
1098 1.1.2.2 matt */
1099 1.1.2.2 matt FILE *
1100 1.1.2.2 matt _ftp_request(struct url *url, const char *op, struct url_stat *us,
1101 1.1.2.2 matt struct url *purl, const char *flags)
1102 1.1.2.2 matt {
1103 1.1.2.2 matt conn_t *conn;
1104 1.1.2.2 matt int oflag;
1105 1.1.2.2 matt
1106 1.1.2.2 matt /* check if we should use HTTP instead */
1107 1.1.2.2 matt if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
1108 1.1.2.2 matt if (strcmp(op, "STAT") == 0)
1109 1.1.2.2 matt return (_http_request(url, "HEAD", us, purl, flags));
1110 1.1.2.2 matt else if (strcmp(op, "RETR") == 0)
1111 1.1.2.2 matt return (_http_request(url, "GET", us, purl, flags));
1112 1.1.2.2 matt /*
1113 1.1.2.2 matt * Our HTTP code doesn't support PUT requests yet, so try
1114 1.1.2.2 matt * a direct connection.
1115 1.1.2.2 matt */
1116 1.1.2.2 matt }
1117 1.1.2.2 matt
1118 1.1.2.2 matt /* connect to server */
1119 1.1.2.2 matt conn = _ftp_cached_connect(url, purl, flags);
1120 1.1.2.2 matt if (purl)
1121 1.1.2.2 matt fetchFreeURL(purl);
1122 1.1.2.2 matt if (conn == NULL)
1123 1.1.2.2 matt return (NULL);
1124 1.1.2.2 matt
1125 1.1.2.2 matt /* change directory */
1126 1.1.2.2 matt if (_ftp_cwd(conn, url->doc) == -1)
1127 1.1.2.2 matt return (NULL);
1128 1.1.2.2 matt
1129 1.1.2.2 matt /* stat file */
1130 1.1.2.2 matt if (us && _ftp_stat(conn, url->doc, us) == -1
1131 1.1.2.2 matt && fetchLastErrCode != FETCH_PROTO
1132 1.1.2.2 matt && fetchLastErrCode != FETCH_UNAVAIL)
1133 1.1.2.2 matt return (NULL);
1134 1.1.2.2 matt
1135 1.1.2.2 matt /* just a stat */
1136 1.1.2.2 matt if (strcmp(op, "STAT") == 0)
1137 1.1.2.2 matt return (FILE *)1; /* bogus return value */
1138 1.1.2.2 matt if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1139 1.1.2.2 matt oflag = O_WRONLY;
1140 1.1.2.2 matt else
1141 1.1.2.2 matt oflag = O_RDONLY;
1142 1.1.2.2 matt
1143 1.1.2.2 matt /* initiate the transfer */
1144 1.1.2.2 matt return (_ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1145 1.1.2.2 matt }
1146 1.1.2.2 matt
1147 1.1.2.2 matt /*
1148 1.1.2.2 matt * Get and stat file
1149 1.1.2.2 matt */
1150 1.1.2.2 matt FILE *
1151 1.1.2.2 matt fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1152 1.1.2.2 matt {
1153 1.1.2.2 matt return (_ftp_request(url, "RETR", us, _ftp_get_proxy(flags), flags));
1154 1.1.2.2 matt }
1155 1.1.2.2 matt
1156 1.1.2.2 matt /*
1157 1.1.2.2 matt * Get file
1158 1.1.2.2 matt */
1159 1.1.2.2 matt FILE *
1160 1.1.2.2 matt fetchGetFTP(struct url *url, const char *flags)
1161 1.1.2.2 matt {
1162 1.1.2.2 matt return (fetchXGetFTP(url, NULL, flags));
1163 1.1.2.2 matt }
1164 1.1.2.2 matt
1165 1.1.2.2 matt /*
1166 1.1.2.2 matt * Put file
1167 1.1.2.2 matt */
1168 1.1.2.2 matt FILE *
1169 1.1.2.2 matt fetchPutFTP(struct url *url, const char *flags)
1170 1.1.2.2 matt {
1171 1.1.2.2 matt
1172 1.1.2.2 matt return (_ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1173 1.1.2.2 matt _ftp_get_proxy(flags), flags));
1174 1.1.2.2 matt }
1175 1.1.2.2 matt
1176 1.1.2.2 matt /*
1177 1.1.2.2 matt * Get file stats
1178 1.1.2.2 matt */
1179 1.1.2.2 matt int
1180 1.1.2.2 matt fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1181 1.1.2.2 matt {
1182 1.1.2.2 matt FILE *f;
1183 1.1.2.2 matt
1184 1.1.2.2 matt f = _ftp_request(url, "STAT", us, _ftp_get_proxy(flags), flags);
1185 1.1.2.2 matt if (f == NULL)
1186 1.1.2.2 matt return (-1);
1187 1.1.2.2 matt return (0);
1188 1.1.2.2 matt }
1189 1.1.2.2 matt
1190 1.1.2.2 matt /*
1191 1.1.2.2 matt * List a directory
1192 1.1.2.2 matt */
1193 1.1.2.2 matt /* ARGSUSED0 */
1194 1.1.2.2 matt struct url_ent *
1195 1.1.2.2 matt fetchListFTP(struct url *url __unused, const char *flags __unused)
1196 1.1.2.2 matt {
1197 1.1.2.2 matt warnx("fetchListFTP(): not implemented");
1198 1.1.2.2 matt return (NULL);
1199 1.1.2.2 matt }
1200