Package: netkit-ftp / 0.17-34.1

040_ipv6_ftp_c.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
Description: Impose IPv6-capacities on ftp.c.
 Super structure 'struct sockaddr_storage' is replacing 'struct sockaddr_in'
 .
 For peer address strings, a first try aims at IPv4. That failing, next
 try is for IPv6, and ultimatively a host lookup follows as last resort.
 .
 Use a SIGALRM mechanism to decrease the default TCP handshake timeout
 to a value better suited for interactive use. The macro can be set
 externally to change the suggested FTP_CONNECT_TIMEOUT=10
 .
 Use a command EPSV with explicit request for IPv6 address family as soon
 as parsing concluded that the control socket is using IPv6.
 .
 Make IP_TOS conditioned on AF_INET.
Author: Mats Erik Andersson <debian@gisladisker.se>
Forwarded: no
Last-Update: 2011-06-17
--- netkit-ftp-0.17.debian/ftp/ftp.c
+++ netkit-ftp-0.17/ftp/ftp.c
@@ -71,9 +71,11 @@
 int data = -1;
 off_t restart_point = 0;
 
-static struct sockaddr_in hisctladdr;
-static struct sockaddr_in data_addr;
-static struct sockaddr_in myctladdr;
+static char ipstring[INET6_ADDRSTRLEN]; /* Scribble area for resolver. */
+
+static struct sockaddr_storage hisctladdr;
+static struct sockaddr_storage data_addr;
+static struct sockaddr_storage myctladdr;
 static int ptflag = 0;
 static int ptabflg = 0;
 
@@ -95,40 +97,56 @@
 static FILE *dataconn(const char *);
 static void printbytes(off_t);
 
+#if ! defined(FTP_CONNECT_TIMEOUT) || FTP_CONNECT_TIMEOUT < 1
+# define FTP_CONNECT_TIMEOUT 10
+#endif
+
+static void
+trivial_alarm(int sig)
+{
+	/* Only used to generate an EINTR error. */
+	return;
+}
+
 char *
 hookup(char *host, int port)
 {
-	register struct hostent *hp = 0;
+	struct addrinfo hints, *ai = NULL, *aiptr = NULL;
+	struct sigaction sigact, oldsigact;
+	int status;
 	volatile int s = -1;
-	int tos;
+	int tos, af_in_use;
 	socklen_t len;
 	static char hostnamebuf[256];
 	sigjmp_buf jmploc;
 	sigjmp_buf *volatile oldtoplevel;
 	int dupfd;
+	struct sockaddr_in *hisctl_sa4 = (struct sockaddr_in *) &hisctladdr;
+	struct sockaddr_in6 *hisctl_sa6 = (struct sockaddr_in6 *) &hisctladdr;
 
 	memset(&hisctladdr, 0, sizeof(hisctladdr));
-	if (inet_aton(host, &hisctladdr.sin_addr)) {
-		hisctladdr.sin_family = AF_INET;
-		strncpy(hostnamebuf, host, sizeof(hostnamebuf));
-		hostnamebuf[sizeof(hostnamebuf)-1]=0;
-	} 
-	else {
-		hp = gethostbyname(host);
-		if (hp == NULL) {
-			fprintf(stderr, "ftp: %s: ", host);
-			herror((char *)NULL);
-			code = -1;
-			return((char *) 0);
-		}
-		hisctladdr.sin_family = hp->h_addrtype;
-		if (hp->h_length > (int)sizeof(hisctladdr.sin_addr)) {
-			hp->h_length = sizeof(hisctladdr.sin_addr);
-		}
-		memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0], hp->h_length);
-		(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
-		hostnamebuf[sizeof(hostnamebuf)-1] = 0;
+
+	sigact.sa_handler = trivial_alarm;
+	sigemptyset(&sigact.sa_mask);
+	sigact.sa_flags = 0;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+
+	if ( (status = getaddrinfo(host, NULL, &hints, &ai)) ) {
+		fprintf(stderr, "ftp: %s: %s\n", host,
+				gai_strerror(status));
+		code = -1;
+		return((char *) 0);
 	}
+
+	aiptr = ai;
+	memcpy(&hisctladdr, aiptr->ai_addr, aiptr->ai_addrlen);
+	(void) strncpy(hostnamebuf, aiptr->ai_canonname,
+			sizeof(hostnamebuf));
+	hostnamebuf[sizeof(hostnamebuf)-1] = 0;
 	hostname = hostnamebuf;
 
 	oldtoplevel = toplevel;
@@ -141,53 +159,127 @@
 	toplevel = &jmploc;
 
 	INTOFF;
-	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
+	s = socket(hisctladdr.ss_family, SOCK_STREAM, 0);
+	af_in_use = hisctladdr.ss_family;
 	INTON;
 	if (s < 0) {
 		perror("ftp: socket");
+		freeaddrinfo(ai);
 		code = -1;
 		goto out;
 	}
-	hisctladdr.sin_port = port;
-	while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
-		if (hp && hp->h_addr_list[1]) {
+	switch (hisctladdr.ss_family) {
+		case AF_INET:
+			hisctl_sa4->sin_port = port;
+			break;
+		case AF_INET6:
+			hisctl_sa6->sin6_port = port;
+	}
+
+	sigaction(SIGALRM, &sigact, &oldsigact);
+	alarm(FTP_CONNECT_TIMEOUT);
+
+	while (connect(s, (struct sockaddr *)&hisctladdr,
+				(hisctladdr.ss_family == AF_INET)
+				? sizeof(struct sockaddr_in)
+				: sizeof(struct sockaddr_in6))
+			< 0)
+	{
+		alarm(0);
+		sigaction(SIGALRM, &oldsigact, NULL);
+		if (errno == EINTR)
+			errno = ETIMEDOUT;
+
+		if (aiptr && aiptr->ai_next)
+		{
 			int oerrno = errno;
+			struct in_addr *ctladdr4 = &hisctl_sa4->sin_addr;
+			struct in6_addr *ctladdr6 = &hisctl_sa6->sin6_addr;
 
-			fprintf(stderr, "ftp: connect to address %s: ",
-				inet_ntoa(hisctladdr.sin_addr));
+			switch (aiptr->ai_family) {
+			    case AF_INET:
+				fprintf(stderr, "ftp: connect to address %s: ",
+					inet_ntop(aiptr->ai_family,
+							ctladdr4,
+							ipstring,
+							sizeof(ipstring)));
+				break;
+			    case AF_INET6:
+				fprintf(stderr, "ftp: connect to address %s: ",
+					inet_ntop(aiptr->ai_family,
+							ctladdr6,
+							ipstring,
+							sizeof(ipstring)));
+			}
 			errno = oerrno;
 			perror((char *) 0);
-			hp->h_addr_list++;
-			memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0], 
-			       hp->h_length);
-			fprintf(stdout, "Trying %s...\n",
-				inet_ntoa(hisctladdr.sin_addr));
+
+			aiptr = aiptr->ai_next;
+			memcpy(&hisctladdr, aiptr->ai_addr,
+					aiptr->ai_addrlen);
+			switch (hisctladdr.ss_family) {
+				case AF_INET:
+					hisctl_sa4->sin_port = port;
+					break;
+				case AF_INET6:
+					hisctl_sa6->sin6_port = port;
+			}
+
+			switch (aiptr->ai_family) {
+			    case AF_INET:
+				fprintf(stdout, "Trying %s...\n",
+					inet_ntop(aiptr->ai_family,
+							ctladdr4,
+							ipstring,
+							sizeof(ipstring)));
+				break;
+			    case AF_INET6:
+				fprintf(stdout, "Trying %s...\n",
+					inet_ntop(aiptr->ai_family,
+							ctladdr6,
+							ipstring,
+							sizeof(ipstring)));
+			}
 			INTOFF;
 			(void) close(s);
-			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
+			s = socket(aiptr->ai_family, SOCK_STREAM, 0);
+			af_in_use = aiptr->ai_family;
 			INTON;
 			if (s < 0) {
 				perror("ftp: socket");
+				freeaddrinfo(ai);
 				code = -1;
 				goto out;
 			}
+			/* Try next server candidate. */
 			continue;
 		}
+		/* No answer to any call. */
 		perror("ftp: connect");
+		freeaddrinfo(ai);
 		code = -1;
 		goto bad;
 	}
+	alarm(0);
+	sigaction(SIGALRM, &oldsigact, NULL);
+
 	len = sizeof (myctladdr);
 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
 		perror("ftp: getsockname");
+		if (ai)
+			freeaddrinfo(ai);
 		code = -1;
 		goto bad;
 	}
+	if (ai)
+		freeaddrinfo(ai);
 #ifdef IP_TOS
 	tos = IPTOS_LOWDELAY;
-	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
+	if ( (af_in_use == AF_INET) &&
+		(setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
+	  		  sizeof(tos)) < 0) )
 		perror("ftp: setsockopt TOS (ignored)");
-#endif
+#endif /* IP_TOS */
 	INTOFF;
 	if (cin)
 		fclose(cin);
@@ -458,7 +550,7 @@
 			}
 			if (dig < 4 && isdigit(c))
 				code = code * 10 + (c - '0');
-			if (!pflag && code == 227)
+			if (!pflag && (code == 227 || code == 229))
 				pflag = 1;
 			if (dig > 4 && pflag == 1 && isdigit(c))
 				pflag = 2;
@@ -1198,18 +1290,21 @@
 static int
 initconn(void)
 {
-	register char *p, *a;
+	register char *p = NULL, *a = NULL;
 	int result, tmpno = 0;
 	socklen_t len;
 	int on = 1;
 	int tos;
 	u_long a1,a2,a3,a4,p1,p2;
+	unsigned short int port;
+	struct sockaddr_in *data_addr_sa4 = (struct sockaddr_in *) &data_addr;
+	struct sockaddr_in6 *data_addr_sa6 = (struct sockaddr_in6 *) &data_addr;
 
 	if (passivemode) {
 		INTOFF;
 		if (data >= 0)
 			close(data);
-		data = socket(AF_INET, SOCK_STREAM, 0);
+		data = socket(hisctladdr.ss_family, SOCK_STREAM, 0);
 		INTON;
 		if (data < 0) {
 			perror("ftp: socket");
@@ -1219,32 +1314,67 @@
 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
 			       sizeof (on)) < 0)
 			perror("ftp: setsockopt (ignored)");
-		if (command("PASV") != COMPLETE) {
-			printf("Passive mode refused.\n");
-			return(1);
-		}
-
-		/*
-		 * What we've got at this point is a string of comma separated
-		 * one-byte unsigned integer values, separated by commas.
-		 * The first four are the an IP address. The fifth is the MSB
-		 * of the port number, the sixth is the LSB. From that we'll
-		 * prepare a sockaddr_in.
-		 */
-
-		if (sscanf(pasv,"%ld,%ld,%ld,%ld,%ld,%ld",
-			   &a1,&a2,&a3,&a4,&p1,&p2)
-		    != 6) 
-		{
-			printf("Passive mode address scan failure. Shouldn't happen!\n");
-			return(1);
+		switch (hisctladdr.ss_family) {
+			case AF_INET:
+				if (command("PASV") != COMPLETE) {
+					printf("Passive mode refused.\n");
+					return(1);
+				}
+				break;
+			case AF_INET6:
+				if (command("EPSV 2") != COMPLETE) {
+					printf("Passive mode refused.\n");
+					return(1);
+				}
 		}
 
-		data_addr.sin_family = AF_INET;
-		data_addr.sin_addr.s_addr = htonl((a1 << 24) | (a2 << 16) |
-						  (a3 << 8) | a4);
-		data_addr.sin_port = htons((p1 << 8) | p2);
-
+		if (hisctladdr.ss_family == AF_INET) {
+			/*
+			 * IPv4
+			 *
+			 * What we've got at this point is a string of
+			 * comma separated one-byte unsigned integer
+			 * values, separated by commas. The first four
+			 * are the an IP address. The fifth is the MSB
+			 * of the port number, the sixth is the LSB.
+			 * From that we will prepare a sockaddr_in.
+			 */
+
+			if (sscanf(pasv,"%ld,%ld,%ld,%ld,%ld,%ld",
+				   &a1,&a2,&a3,&a4,&p1,&p2)
+			    != 6) 
+			{
+				printf("Passive mode address scan failure."
+					"Shouldn't happen!\n");
+				return(1);
+			}
+
+			data_addr.ss_family = AF_INET;
+			data_addr_sa4->sin_addr.s_addr =
+				htonl((a1 << 24) | (a2 << 16) |
+						(a3 << 8) | a4);
+			data_addr_sa4->sin_port = htons((p1 << 8) | p2);
+		} /* Old IPv4 command PASV */
+		else {
+			/* EPSV for IPv6
+			 *
+			 * Expected: pasv =~ "%u|"
+			 *
+			 * This is a shortcut based on the old code
+			 * for getreply(), only altered to accept
+			 * return code "229" for ESPV, in addition
+			 * to "227" which goes with PASV.
+			 */
+			if (sscanf(pasv, "%hu", &port) != 1) {
+				printf("Extended passive mode address "
+					"scan failure. Unfortunate!\n");
+				return(1);
+			}
+			data_addr = hisctladdr;
+			data_addr.ss_family = AF_INET6;
+			data_addr_sa6->sin6_port = htons(port);
+		} /* EPSV for IPv6 */
+	
 		if (connect(data, (struct sockaddr *) &data_addr,
 		    sizeof(data_addr))<0) {
 			perror("ftp: connect");
@@ -1252,20 +1382,28 @@
 		}
 #ifdef IP_TOS
 		tos = IPTOS_THROUGHPUT;
-		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos,
-		    sizeof(tos)) < 0)
+		if ( (hisctladdr.ss_family == AF_INET) &&
+			(setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos,
+		  		  sizeof(tos)) < 0) )
 			perror("ftp: setsockopt TOS (ignored)");
-#endif
+#endif /* IP_TOS */
 		return(0);
 	}
 noport:
 	data_addr = myctladdr;
 	if (sendport)
-		data_addr.sin_port = 0;	/* let system pick one */ 
+		/* let the system pick a port */ 
+		switch (data_addr.ss_family) {
+			case AF_INET:
+				data_addr_sa4->sin_port = 0;
+				break;
+			case AF_INET6:
+				data_addr_sa6->sin6_port = 0;
+		}
 	INTOFF;
 	if (data != -1)
 		(void) close(data);
-	data = socket(AF_INET, SOCK_STREAM, 0);
+	data = socket(data_addr.ss_family, SOCK_STREAM, 0);
 	INTON;
 	if (data < 0) {
 		perror("ftp: socket");
@@ -1293,13 +1431,23 @@
 	if (listen(data, 1) < 0)
 		perror("ftp: listen");
 	if (sendport) {
-		a = (char *)&data_addr.sin_addr;
-		p = (char *)&data_addr.sin_port;
 #define	UC(b)	(((int)b)&0xff)
-		result =
-		    command("PORT %d,%d,%d,%d,%d,%d",
-		      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
-		      UC(p[0]), UC(p[1]));
+		switch (data_addr.ss_family) {
+			case AF_INET:
+				a = (char *)&data_addr_sa4->sin_addr;
+				p = (char *)&data_addr_sa4->sin_port;
+				result = command("PORT %d,%d,%d,%d,%d,%d",
+					    UC(a[0]), UC(a[1]), UC(a[2]),
+					    UC(a[3]), UC(p[0]), UC(p[1]));
+				break;
+			case AF_INET6:
+				result = command("EPRT |2|%s|%d|",
+						inet_ntop(data_addr.ss_family,
+							&data_addr_sa6->sin6_addr,
+							ipstring,
+							sizeof(ipstring)),
+						ntohs(data_addr_sa6->sin6_port));
+		}
 		if (result == ERROR && sendport == -1) {
 			sendport = 0;
 			tmpno = 1;
@@ -1311,9 +1459,11 @@
 		sendport = 1;
 #ifdef IP_TOS
 	on = IPTOS_THROUGHPUT;
-	if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
+	if ( (data_addr.ss_family == AF_INET) &&
+		(setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos,
+	  		  sizeof(tos)) < 0) )
 		perror("ftp: setsockopt TOS (ignored)");
-#endif
+#endif /* IP_TOS */
 	return (0);
 bad:
 	INTOFF;
@@ -1327,7 +1477,7 @@
 static FILE *
 dataconn(const char *lmode)
 {
-	struct sockaddr_in from;
+	struct sockaddr_storage from;
 	int s, tos;
 	socklen_t fromlen = sizeof(from);
 
@@ -1344,9 +1494,11 @@
 	data = s;
 #ifdef IP_TOS
 	tos = IPTOS_THROUGHPUT;
-	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
+	if ( (from.ss_family == AF_INET) &&
+		(setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
+	  		  sizeof(tos)) < 0) )
 		perror("ftp: setsockopt TOS (ignored)");
-#endif
+#endif /* IP_TOS */
 	return (fdopen(data, lmode));
 }
 
@@ -1398,8 +1550,8 @@
 	static struct comvars {
 		int connect;
 		char name[MAXHOSTNAMELEN];
-		struct sockaddr_in mctl;
-		struct sockaddr_in hctl;
+		struct sockaddr_storage mctl;
+		struct sockaddr_storage hctl;
 		FILE *in;
 		FILE *out;
 		int tpe;