Tue Nov 8 18:02:27 2011 UTC ()
More help for stupid glibc headers.


(joerg)
diff -r1.39 -r1.40 pkgsrc/net/libfetch/files/ftp.c

cvs diff -r1.39 -r1.40 pkgsrc/net/libfetch/files/ftp.c (switch to unified diff)

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